-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncDelta.test.ts
More file actions
410 lines (344 loc) · 13.1 KB
/
Copy pathsyncDelta.test.ts
File metadata and controls
410 lines (344 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import { FileSyncState } from '../src/services/syncState'
import { DriveFile } from '../src/services/googleDriveService'
/**
* These tests verify the three-way comparison logic used in calculateDelta
* Testing the decision matrix for:
* - Local unchanged, Remote unchanged -> In sync
* - Local changed, Remote unchanged -> Upload
* - Local unchanged, Remote changed -> Download
* - Local changed, Remote changed -> Conflict
*/
describe('Sync Delta Calculation Logic', () => {
describe('Three-Way Comparison Decision Matrix', () => {
it('should detect files in sync (no changes)', () => {
const localFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: 'hash123',
lastSyncedTime: 1000,
lastSyncedSize: 100,
lastSyncRevisionId: 'rev-123',
remoteFileId: 'file-id-123'
}
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date(1000).toISOString(),
headRevisionId: 'rev-123' // Same as lastSyncRevisionId
}
const currentLocalHash = 'hash123' // Same as lastSyncedHash
// Decision logic
const localChanged = currentLocalHash !== localFile.lastSyncedHash
const remoteChanged = remoteFile.headRevisionId !== localFile.lastSyncRevisionId
expect(localChanged).toBe(false)
expect(remoteChanged).toBe(false)
// Result: IN SYNC
})
it('should detect local-only changes (upload needed)', () => {
const localFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: 'hash123',
lastSyncedTime: 1000,
lastSyncedSize: 100,
lastSyncRevisionId: 'rev-123',
remoteFileId: 'file-id-123'
}
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date(1000).toISOString(),
headRevisionId: 'rev-123' // Same as lastSyncRevisionId
}
const currentLocalHash = 'hash456' // CHANGED from lastSyncedHash
// Decision logic
const localChanged = currentLocalHash !== localFile.lastSyncedHash
const remoteChanged = remoteFile.headRevisionId !== localFile.lastSyncRevisionId
expect(localChanged).toBe(true)
expect(remoteChanged).toBe(false)
// Result: UPLOAD
})
it('should detect remote-only changes (download needed)', () => {
const localFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: 'hash123',
lastSyncedTime: 1000,
lastSyncedSize: 100,
lastSyncRevisionId: 'rev-123',
remoteFileId: 'file-id-123'
}
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 150,
modifiedTime: new Date(2000).toISOString(),
headRevisionId: 'rev-456' // CHANGED from lastSyncRevisionId
}
const currentLocalHash = 'hash123' // Same as lastSyncedHash
// Decision logic
const localChanged = currentLocalHash !== localFile.lastSyncedHash
const remoteChanged = remoteFile.headRevisionId !== localFile.lastSyncRevisionId
expect(localChanged).toBe(false)
expect(remoteChanged).toBe(true)
// Result: DOWNLOAD
})
it('should detect conflicts (both changed)', () => {
const localFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: 'hash123',
lastSyncedTime: 1000,
lastSyncedSize: 100,
lastSyncRevisionId: 'rev-123',
remoteFileId: 'file-id-123'
}
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 150,
modifiedTime: new Date(2000).toISOString(),
headRevisionId: 'rev-456' // CHANGED from lastSyncRevisionId
}
const currentLocalHash = 'hash789' // CHANGED from lastSyncedHash
// Decision logic
const localChanged = currentLocalHash !== localFile.lastSyncedHash
const remoteChanged = remoteFile.headRevisionId !== localFile.lastSyncRevisionId
expect(localChanged).toBe(true)
expect(remoteChanged).toBe(true)
// Result: CONFLICT
})
})
describe('Echo Detection', () => {
it('should detect own changes via syncAgentId', () => {
const ownSyncAgentId = 'device-abc-123'
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date().toISOString(),
headRevisionId: 'rev-123',
appProperties: {
lastModifiedByAgent: 'device-abc-123' // Same as ownSyncAgentId
}
}
const isOwnChange = remoteFile.appProperties?.lastModifiedByAgent === ownSyncAgentId
expect(isOwnChange).toBe(true)
// Result: SKIP (echo detected)
})
it('should detect changes from other devices', () => {
const ownSyncAgentId = 'device-abc-123'
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date().toISOString(),
headRevisionId: 'rev-123',
appProperties: {
lastModifiedByAgent: 'device-xyz-789' // Different from ownSyncAgentId
}
}
const isOwnChange = remoteFile.appProperties?.lastModifiedByAgent === ownSyncAgentId
expect(isOwnChange).toBe(false)
// Result: Process normally
})
it('should handle files without appProperties', () => {
const ownSyncAgentId = 'device-abc-123'
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date().toISOString(),
headRevisionId: 'rev-123'
// No appProperties
}
const isOwnChange = remoteFile.appProperties?.lastModifiedByAgent === ownSyncAgentId
expect(isOwnChange).toBe(false)
// Result: Process normally (not an echo)
})
})
describe('New File Detection', () => {
it('should detect new remote files (not in local index)', () => {
const localFilesMap = new Map<string, FileSyncState>()
// Empty - no files in index
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'new-file.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date().toISOString(),
headRevisionId: 'rev-123'
}
const localFile = localFilesMap.get(remoteFile.name)
expect(localFile).toBeUndefined()
// Result: DOWNLOAD (new remote file)
})
it('should detect new local files (not in remote)', () => {
const localFilesMap = new Map<string, FileSyncState>()
localFilesMap.set('new-local.md', {
path: 'new-local.md',
lastSyncedHash: '',
lastSyncedTime: 0,
lastSyncedSize: 0
// No remoteFileId
})
const remoteFilesMap = new Map<string, DriveFile>()
// Empty - no files on remote
const localFile = localFilesMap.get('new-local.md')
const remoteFile = remoteFilesMap.get('new-local.md')
expect(localFile).toBeDefined()
expect(remoteFile).toBeUndefined()
expect(localFile?.remoteFileId).toBeUndefined()
// Result: UPLOAD (new local file)
})
})
describe('Edge Cases', () => {
it('should handle missing lastSyncRevisionId (assume changed)', () => {
const localFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: 'hash123',
lastSyncedTime: 1000,
lastSyncedSize: 100,
// No lastSyncRevisionId
remoteFileId: 'file-id-123'
}
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date().toISOString(),
headRevisionId: 'rev-123'
}
// When lastSyncRevisionId is missing, assume remote changed
const remoteChanged = localFile.lastSyncRevisionId
? (remoteFile.headRevisionId !== localFile.lastSyncRevisionId)
: true
expect(remoteChanged).toBe(true)
// Result: If local also unchanged, DOWNLOAD
})
it('should handle files synced but deleted remotely', () => {
const localFile: FileSyncState = {
path: 'deleted-remotely.md',
lastSyncedHash: 'hash123',
lastSyncedTime: 1000,
lastSyncedSize: 100,
lastSyncRevisionId: 'rev-123',
remoteFileId: 'file-id-123'
}
const remoteFilesMap = new Map<string, DriveFile>()
// File not in remote anymore
const remoteFile = remoteFilesMap.get(localFile.path)
expect(remoteFile).toBeUndefined()
expect(localFile.remoteFileId).toBeDefined() // Was synced before
// Result: UPLOAD (missing remote, was synced) or check tombstones
})
it('should skip remote-only tracking entries', () => {
const localFile: FileSyncState = {
path: 'remote-only.md',
lastSyncedHash: '', // Empty
lastSyncedTime: 0, // Never synced
lastSyncedSize: 0,
remoteFileId: 'file-id-123'
}
const isRemoteOnlyEntry = localFile.lastSyncedTime === 0 && localFile.lastSyncedHash === ''
expect(isRemoteOnlyEntry).toBe(true)
// Result: SKIP (remote-only tracking entry)
})
})
describe('Revision ID Comparison Reliability', () => {
it('should use headRevisionId over timestamps for reliability', () => {
// Scenario: Clocks are out of sync, but revisionIds are correct
const localFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: 'hash123',
lastSyncedTime: 5000, // Local clock ahead
lastSyncedSize: 100,
lastSyncRevisionId: 'rev-123',
remoteFileId: 'file-id-123'
}
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date(3000).toISOString(), // Remote clock behind
headRevisionId: 'rev-123' // Same revision!
}
// Using timestamps would suggest local is newer
const timestampComparison = localFile.lastSyncedTime > new Date(remoteFile.modifiedTime).getTime()
expect(timestampComparison).toBe(true) // Would incorrectly suggest upload
// Using revisionId correctly identifies they're in sync
const revisionComparison = remoteFile.headRevisionId === localFile.lastSyncRevisionId
expect(revisionComparison).toBe(true) // Correct: IN SYNC
// Result: headRevisionId is more reliable than timestamps
})
})
describe('Multiple Simultaneous Changes Scenarios', () => {
it('should handle rapid local edits (same headRevisionId)', () => {
const localFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: 'hash1',
lastSyncedTime: 1000,
lastSyncedSize: 100,
lastSyncRevisionId: 'rev-123'
}
// User makes multiple rapid local edits
const currentLocalHash1 = 'hash2' // First edit
const currentLocalHash2 = 'hash3' // Second edit
const currentLocalHash3 = 'hash4' // Third edit
const remoteFile: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date(1000).toISOString(),
headRevisionId: 'rev-123' // Remote unchanged
}
// All three local edits should be detected as needing upload
expect(currentLocalHash1).not.toBe(localFile.lastSyncedHash)
expect(currentLocalHash2).not.toBe(localFile.lastSyncedHash)
expect(currentLocalHash3).not.toBe(localFile.lastSyncedHash)
expect(remoteFile.headRevisionId).toBe(localFile.lastSyncRevisionId)
// Result: UPLOAD (local changed, remote unchanged)
})
it('should handle offline edits on multiple devices', () => {
// Initial synced state
const initialRevisionId = 'rev-100'
const initialHash = 'hash100'
// Device A makes offline edits
const deviceAHash = 'hash-device-A'
const deviceAChanged = deviceAHash !== initialHash
// Device B makes offline edits (different)
const deviceBHash = 'hash-device-B'
// Device A syncs first -> creates rev-101
const remoteAfterDeviceA: DriveFile = {
id: 'file-id-123',
name: 'test.md',
mimeType: 'text/markdown',
size: 100,
modifiedTime: new Date().toISOString(),
headRevisionId: 'rev-101',
appProperties: { lastModifiedByAgent: 'device-A' }
}
// Device B comes online
const deviceBLocalFile: FileSyncState = {
path: 'test.md',
lastSyncedHash: initialHash,
lastSyncedTime: 1000,
lastSyncedSize: 100,
lastSyncRevisionId: initialRevisionId // Still at rev-100!
}
const deviceBLocalChanged = deviceBHash !== deviceBLocalFile.lastSyncedHash
const remoteChanged = remoteAfterDeviceA.headRevisionId !== deviceBLocalFile.lastSyncRevisionId
expect(deviceBLocalChanged).toBe(true)
expect(remoteChanged).toBe(true)
// Result: CONFLICT (both devices edited offline)
})
})
})