Open History to see the last commit
1import { fc } from '@fast-check/vitest'
2import { describe, expect, it } from 'vitest'
3import { serialize } from '../src/pg-serializer'
4import {
5 parseFiniteArray,
6 parseFiniteNumber,
7} from './pg-serializer-finite-decoder'
8import { serializerProp } from './pg-serializer-property-support'
9
10describe(`pg-serializer`, () => {
11 describe(`serialize`, () => {
12 it(`should serialize null as empty string`, () => {
13 expect(serialize(null)).toBe(``)
14 })
15
16 it(`should serialize undefined as empty string`, () => {
17 expect(serialize(undefined)).toBe(``)
18 })
19
20 it(`should serialize strings as-is`, () => {
21 expect(serialize(`hello`)).toBe(`hello`)
22 expect(serialize(``)).toBe(``)
23 expect(serialize(`with spaces`)).toBe(`with spaces`)
24 })
25
26 it(`should serialize numbers as strings`, () => {
27 expect(serialize(42)).toBe(`42`)
28 expect(serialize(0)).toBe(`0`)
29 expect(serialize(-123)).toBe(`-123`)
30 expect(serialize(3.14)).toBe(`3.14`)
31 expect(serialize(Infinity)).toBe(`Infinity`)
32 expect(serialize(-Infinity)).toBe(`-Infinity`)
33 })
34
35 it(`should serialize bigints as strings`, () => {
36 expect(serialize(BigInt(42))).toBe(`42`)
37 expect(serialize(BigInt(0))).toBe(`0`)
38 expect(serialize(BigInt(-123))).toBe(`-123`)
39 expect(serialize(BigInt(`9007199254740993`))).toBe(`9007199254740993`)
40 expect(serialize(BigInt(`-9007199254740993`))).toBe(`-9007199254740993`)
41 })
42
43 it(`should serialize booleans as lowercase strings`, () => {
44 expect(serialize(true)).toBe(`true`)
45 expect(serialize(false)).toBe(`false`)
46 })
47
48 it(`should serialize dates as ISO strings`, () => {
49 const date = new Date(`2024-01-15T10:30:45.000Z`)
50 expect(serialize(date)).toBe(`2024-01-15T10:30:45.000Z`)
51 })
52
53 describe(`arrays`, () => {
54 it(`should serialize empty arrays`, () => {
55 expect(serialize([])).toBe(`{}`)
56 })
57
58 it(`should serialize arrays of numbers`, () => {
59 expect(serialize([1, 2, 3])).toBe(`{1,2,3}`)
60 })
61
62 it(`should serialize arrays of bigints`, () => {
63 expect(serialize([BigInt(1), BigInt(2), BigInt(3)])).toBe(`{1,2,3}`)
64 expect(serialize([BigInt(`9007199254740993`)])).toBe(
65 `{9007199254740993}`,
66 )
67 })
68
69 it(`should serialize arrays of strings with proper escaping`, () => {
70 expect(serialize([`a`, `b`, `c`])).toBe(`{"a","b","c"}`)
71 expect(serialize([`hello world`])).toBe(`{"hello world"}`)
72 })
73
74 it(`should escape quotes and backslashes in string array elements`, () => {
75 expect(serialize([`with"quote`])).toBe(`{"with\\"quote"}`)
76 expect(serialize([`with\\backslash`])).toBe(`{"with\\\\backslash"}`)
77 expect(serialize([`both"and\\`])).toBe(`{"both\\"and\\\\"}`)
78 })
79
80 it(`should serialize arrays with null/undefined as NULL`, () => {
81 expect(serialize([1, null, 3])).toBe(`{1,NULL,3}`)
82 expect(serialize([`a`, undefined, `c`])).toBe(`{"a",NULL,"c"}`)
83 })
84
85 it(`should serialize arrays of booleans`, () => {
86 expect(serialize([true, false, true])).toBe(`{true,false,true}`)
87 })
88
89 it(`should serialize arrays of dates`, () => {
90 const date = new Date(`2024-01-15T10:30:45.000Z`)
91 expect(serialize([date])).toBe(`{2024-01-15T10:30:45.000Z}`)
92 })
93
94 it(`should serialize mixed type arrays`, () => {
95 expect(serialize([1, `two`, true, null])).toBe(`{1,"two",true,NULL}`)
96 })
97
98 it(`should serialize nested arrays`, () => {
99 expect(
100 serialize([
101 [1, 2],
102 [3, 4],
103 ]),
104 ).toBe(`{{1,2},{3,4}}`)
105 })
106 })
107
108 describe(`error handling`, () => {
109 it(`should throw for unsupported types`, () => {
110 expect(() => serialize({ key: `value` })).toThrow(
111 `Cannot serialize value:`,
112 )
113 expect(() => serialize(Symbol(`test`))).toThrow(
114 `Cannot serialize value:`,
115 )
116 })
117
118 it(`should handle error message for non-JSON-serializable values`, () => {
119 // This tests that the error message doesn't throw when trying to stringify
120 // values that JSON.stringify can't handle
121 const circularObj: any = {}
122 circularObj.self = circularObj
123
124 expect(() => serialize(circularObj)).toThrow(`Cannot serialize value:`)
125 })
126
127 it(`should throw for functions`, () => {
128 expect(() => serialize(() => {})).toThrow(`Cannot serialize value:`)
129 })
130 })
131 })
132})
133
134describe(`pg-serializer property-based tests`, () => {
135 describe(`null/undefined serialization`, () => {
136 serializerProp(`pg-serializer.nullish-empty`, [
137 fc.constantFrom(null, undefined),
138 ])(`null and undefined both serialize to empty string`, (val) => {
139 expect(serialize(val)).toBe(``)
140 })
141 })
142
143 describe(`date serialization`, () => {
144 it.each([
145 [new Date(`0000-01-01T00:00:00.000Z`), `0000-01-01T00:00:00.000Z`],
146 [new Date(`9999-12-31T23:59:59.999Z`), `9999-12-31T23:59:59.999Z`],
147 ] as const)(`serializes the bounded Date endpoint %s`, (date, text) => {
148 expect(serialize(date)).toBe(text)
149 })
150 })
151
152 describe(`finite scalar and array checker calibration`, () => {
153 it.each([
154 [`0`, 0],
155 [`-0`, -0],
156 [`1.25`, 1.25],
157 [`5e-324`, Number.MIN_VALUE],
158 [`1.7976931348623157e+308`, Number.MAX_VALUE],
159 [`-2.5E-3`, -0.0025],
160 ] as const)(`reads the complete finite token %s`, (text, value) => {
161 expect(parseFiniteNumber(text)).toBe(value)
162 })
163
164 it.each([
165 ``,
166 ` `,
167 `1.25junk`,
168 `1.25\n`,
169 `1.25 2`,
170 `0x10`,
171 `NaN`,
172 `Infinity`,
173 `1e999`,
174 `1e`,
175 `1.2.3`,
176 ])(`rejects the invalid finite numeric token %j`, (text) => {
177 expect(() => parseFiniteNumber(text)).toThrow(
178 `invalid finite numeric output`,
179 )
180 })
181
182 it.each([
183 ``,
184 `[]`,
185 `{`,
186 `}`,
187 `{1`,
188 `{1,}`,
189 `{,1}`,
190 `{true,,false}`,
191 `{1}tail`,
192 `{"unterminated}`,
193 `{"a" "b"}`,
194 `{"a"b"}`,
195 `{"trailing\\`,
196 ])(`rejects malformed array output %j`, (output) => {
197 expect(() => parseFiniteArray(output)).toThrow()
198 })
199 })
200})