Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { TransactionSerializer } from '../src/outbox/TransactionSerializer'
3import type { OfflineTransaction } from '../src/types'
4import type { PendingMutation } from '@tanstack/db'
5
6describe(`TransactionSerializer`, () => {
7 const mockCollection = {
8 id: `test-collection`,
9 getKeyFromItem: (item: any) => item.id,
10 }
11
12 const createSerializer = () => {
13 return new TransactionSerializer({
14 'test-collection': mockCollection as any,
15 })
16 }
17
18 describe(`date handling`, () => {
19 it(`should preserve plain ISO date strings without converting to Date objects`, () => {
20 const serializer = createSerializer()
21
22 // This is the bug: a plain string that looks like an ISO date
23 // should NOT be converted to a Date object after round-trip
24 const isoDateString = `2024-01-15T10:30:00.000Z`
25
26 const transaction: OfflineTransaction = {
27 id: `tx-1`,
28 createdAt: new Date(`2024-01-01T00:00:00.000Z`),
29 status: `pending`,
30 mutationFnName: `syncData`,
31 mutations: [
32 {
33 globalKey: `key-1`,
34 type: `insert`,
35 // This field intentionally stores an ISO date as a STRING
36 // (e.g., a DB value, or a user-provided string)
37 modified: {
38 id: `1`,
39 eventId: isoDateString, // Should remain a string!
40 description: `Some event`,
41 },
42 original: null,
43 collection: mockCollection,
44 mutationId: `mut-1`,
45 key: `1`,
46 changes: {},
47 metadata: undefined,
48 syncMetadata: {},
49 optimistic: true,
50 createdAt: new Date(),
51 updatedAt: new Date(),
52 } as PendingMutation,
53 ],
54 }
55
56 // Serialize and deserialize (simulating app restart)
57 const serialized = serializer.serialize(transaction)
58 const deserialized = serializer.deserialize(serialized)
59
60 // The eventId should still be a string, not a Date object
61 const eventId = deserialized.mutations[0]!.modified.eventId
62 expect(typeof eventId).toBe(`string`)
63 expect(eventId).toBe(isoDateString)
64 })
65
66 it(`should correctly restore actual Date objects using the marker system`, () => {
67 const serializer = createSerializer()
68
69 const actualDate = new Date(`2024-01-15T10:30:00.000Z`)
70
71 const transaction: OfflineTransaction = {
72 id: `tx-1`,
73 createdAt: new Date(`2024-01-01T00:00:00.000Z`),
74 status: `pending`,
75 mutationFnName: `syncData`,
76 mutations: [
77 {
78 globalKey: `key-1`,
79 type: `insert`,
80 modified: {
81 id: `1`,
82 createdAt: actualDate, // This is an actual Date object
83 name: `Test`,
84 },
85 original: null,
86 collection: mockCollection,
87 mutationId: `mut-1`,
88 key: `1`,
89 changes: {},
90 metadata: undefined,
91 syncMetadata: {},
92 optimistic: true,
93 createdAt: new Date(),
94 updatedAt: new Date(),
95 } as PendingMutation,
96 ],
97 }
98
99 const serialized = serializer.serialize(transaction)
100 const deserialized = serializer.deserialize(serialized)
101
102 // The createdAt should be restored as a Date object
103 const restoredDate = deserialized.mutations[0]!.modified.createdAt
104 expect(restoredDate).toBeInstanceOf(Date)
105 expect(restoredDate.toISOString()).toBe(actualDate.toISOString())
106 })
107
108 it(`should handle mixed Date objects and ISO string values correctly`, () => {
109 const serializer = createSerializer()
110
111 const actualDate = new Date(`2024-06-15T14:00:00.000Z`)
112 const isoStringValue = `2024-01-15T10:30:00.000Z` // Plain string, not a Date
113
114 const transaction: OfflineTransaction = {
115 id: `tx-1`,
116 createdAt: new Date(`2024-01-01T00:00:00.000Z`),
117 status: `pending`,
118 mutationFnName: `syncData`,
119 mutations: [
120 {
121 globalKey: `key-1`,
122 type: `insert`,
123 modified: {
124 id: `1`,
125 timestamp: actualDate, // Actual Date object
126 scheduledFor: isoStringValue, // Plain string that looks like ISO date
127 notes: `Meeting scheduled`,
128 },
129 original: null,
130 collection: mockCollection,
131 mutationId: `mut-1`,
132 key: `1`,
133 changes: {},
134 metadata: undefined,
135 syncMetadata: {},
136 optimistic: true,
137 createdAt: new Date(),
138 updatedAt: new Date(),
139 } as PendingMutation,
140 ],
141 }
142
143 const serialized = serializer.serialize(transaction)
144 const deserialized = serializer.deserialize(serialized)
145
146 const modified = deserialized.mutations[0]!.modified
147
148 // The actual Date should be restored as Date
149 expect(modified.timestamp).toBeInstanceOf(Date)
150 expect(modified.timestamp.toISOString()).toBe(actualDate.toISOString())
151
152 // The string should remain a string
153 expect(typeof modified.scheduledFor).toBe(`string`)
154 expect(modified.scheduledFor).toBe(isoStringValue)
155 })
156
157 it(`should not corrupt nested ISO string values`, () => {
158 const serializer = createSerializer()
159
160 const transaction: OfflineTransaction = {
161 id: `tx-1`,
162 createdAt: new Date(`2024-01-01T00:00:00.000Z`),
163 status: `pending`,
164 mutationFnName: `syncData`,
165 mutations: [
166 {
167 globalKey: `key-1`,
168 type: `insert`,
169 modified: {
170 id: `1`,
171 metadata: {
172 // Nested ISO strings should also be preserved
173 lastSync: `2024-03-20T08:00:00.000Z`,
174 importedFrom: `external-system`,
175 },
176 },
177 original: null,
178 collection: mockCollection,
179 mutationId: `mut-1`,
180 key: `1`,
181 changes: {},
182 metadata: undefined,
183 syncMetadata: {},
184 optimistic: true,
185 createdAt: new Date(),
186 updatedAt: new Date(),
187 } as PendingMutation,
188 ],
189 }
190
191 const serialized = serializer.serialize(transaction)
192 const deserialized = serializer.deserialize(serialized)
193
194 const lastSync = deserialized.mutations[0]!.modified.metadata.lastSync
195 expect(typeof lastSync).toBe(`string`)
196 expect(lastSync).toBe(`2024-03-20T08:00:00.000Z`)
197 })
198
199 it(`should correctly restore top-level createdAt as Date`, () => {
200 const serializer = createSerializer()
201
202 const transactionDate = new Date(`2024-05-15T12:30:00.000Z`)
203
204 const transaction: OfflineTransaction = {
205 id: `tx-1`,
206 createdAt: transactionDate,
207 status: `pending`,
208 mutationFnName: `syncData`,
209 mutations: [
210 {
211 globalKey: `key-1`,
212 type: `insert`,
213 modified: { id: `1` },
214 original: null,
215 collection: mockCollection,
216 mutationId: `mut-1`,
217 key: `1`,
218 changes: {},
219 metadata: undefined,
220 syncMetadata: {},
221 optimistic: true,
222 createdAt: new Date(),
223 updatedAt: new Date(),
224 } as PendingMutation,
225 ],
226 }
227
228 const serialized = serializer.serialize(transaction)
229 const deserialized = serializer.deserialize(serialized)
230
231 // Top-level createdAt should be a Date object
232 expect(deserialized.createdAt).toBeInstanceOf(Date)
233 expect(deserialized.createdAt.toISOString()).toBe(
234 transactionDate.toISOString(),
235 )
236 })
237 })
238})