Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { transformNullsToUndefined, undoNullWidening } from '../src/transforms'
3import type { NullWideningMap } from '../src/transforms'
4
5describe('transformNullsToUndefined', () => {
6 it('should convert null values to undefined', () => {
7 const result = transformNullsToUndefined({ a: null, b: 'hello' })
8 expect(result).toEqual({ b: 'hello' })
9 expect('a' in result).toBe(false)
10 })
11
12 it('should handle nested objects', () => {
13 const result = transformNullsToUndefined({
14 a: { b: null, c: 'value' },
15 d: null,
16 })
17 expect(result).toEqual({ a: { c: 'value' } })
18 })
19
20 it('should handle arrays', () => {
21 const result = transformNullsToUndefined({
22 items: [
23 { a: null, b: 1 },
24 { a: 'x', b: null },
25 ],
26 })
27 expect(result).toEqual({
28 items: [{ b: 1 }, { a: 'x' }],
29 })
30 })
31
32 it('should return non-objects unchanged', () => {
33 expect(transformNullsToUndefined('hello')).toBe('hello')
34 expect(transformNullsToUndefined(42)).toBe(42)
35 expect(transformNullsToUndefined(true)).toBe(true)
36 })
37
38 it('should return null as undefined', () => {
39 expect(transformNullsToUndefined(null)).toBeUndefined()
40 })
41
42 it('should handle empty objects', () => {
43 expect(transformNullsToUndefined({})).toEqual({})
44 })
45
46 it('should handle deeply nested nulls', () => {
47 const result = transformNullsToUndefined({
48 a: { b: { c: { d: null, e: 'keep' } } },
49 })
50 expect(result).toEqual({ a: { b: { c: { e: 'keep' } } } })
51 })
52})
53
54describe('undoNullWidening', () => {
55 // The widening pass records a map of the nulls it synthesized. For an object
56 // with one optional field (`opt`) and one nullable field (`nul`), only `opt`
57 // is widened — so only `opt` is marked:
58 // req: string (required) -> not widened, absent from the map
59 // opt: optional(string) -> widened to `required` + null
60 // nul: nullable(string) -> already allowed null, not widened
61 const map: NullWideningMap = {
62 properties: {
63 opt: { widened: true },
64 },
65 }
66
67 it('drops a synthesized null on a widened field (key becomes absent)', () => {
68 const result = undoNullWidening({ req: 'a', opt: null }, map)
69 expect(result).toEqual({ req: 'a' })
70 expect('opt' in (result as object)).toBe(false)
71 })
72
73 it('keeps a genuine null on a field the widener did not touch', () => {
74 const result = undoNullWidening({ req: 'a', nul: null }, map)
75 expect(result).toEqual({ req: 'a', nul: null })
76 })
77
78 it('handles widened and genuine nulls in the same object', () => {
79 const result = undoNullWidening({ req: 'a', opt: null, nul: null }, map)
80 expect(result).toEqual({ req: 'a', nul: null })
81 })
82
83 it('leaves present values untouched', () => {
84 const result = undoNullWidening({ req: 'a', opt: 'b', nul: 'c' }, map)
85 expect(result).toEqual({ req: 'a', opt: 'b', nul: 'c' })
86 })
87
88 it('descends into a widened object to drop its inner synthesized null', () => {
89 // `obj` is itself optional (so it may come back null) AND has an inner
90 // optional `note`. The map marks both the object and the nested field.
91 const nested: NullWideningMap = {
92 properties: {
93 obj: {
94 widened: true,
95 properties: { note: { widened: true } },
96 },
97 },
98 }
99 // obj is present (kept), but its optional `note` came back null.
100 const result = undoNullWidening({ obj: { inner: 'x', note: null } }, nested)
101 expect(result).toEqual({ obj: { inner: 'x' } })
102
103 // …and when the whole object comes back null, the key drops out.
104 expect(undoNullWidening({ obj: null }, nested)).toEqual({})
105 })
106
107 it('strips synthesized nulls inside array items', () => {
108 const arrMap: NullWideningMap = {
109 properties: {
110 items: {
111 items: { properties: { label: { widened: true } } },
112 },
113 },
114 }
115 const result = undoNullWidening(
116 {
117 items: [
118 { id: '1', label: null },
119 { id: '2', label: 'two' },
120 ],
121 },
122 arrMap,
123 )
124 expect(result).toEqual({ items: [{ id: '1' }, { id: '2', label: 'two' }] })
125 })
126
127 it('applies tuple-style item maps per index', () => {
128 // [ { name }, { note? } ] — only the second position has a widened field.
129 const tupleMap: NullWideningMap = {
130 properties: {
131 pair: {
132 items: [{}, { properties: { note: { widened: true } } }],
133 },
134 },
135 }
136 const result = undoNullWidening(
137 { pair: [{ name: 'Ada' }, { note: null }] },
138 tupleMap,
139 )
140 // The synthesized null in the second tuple position is dropped using that
141 // position's map, not the first's.
142 expect(result).toEqual({ pair: [{ name: 'Ada' }, {}] })
143 })
144
145 it('returns the value untouched when no map is supplied', () => {
146 const value = { a: null, b: 1 }
147 expect(undoNullWidening(value)).toBe(value)
148 })
149
150 it('leaves nulls under positions the map does not describe', () => {
151 // `extra` carries no map entry — the widener never synthesized a null
152 // there, so it is preserved.
153 const result = undoNullWidening({ req: 'a', extra: null }, map)
154 expect(result).toEqual({ req: 'a', extra: null })
155 })
156})