@verdant-web/common 2.7.2 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/esm/diffing.d.ts +39 -0
  2. package/dist/esm/diffing.js +257 -0
  3. package/dist/esm/diffing.js.map +1 -0
  4. package/dist/esm/diffing.test.d.ts +1 -0
  5. package/dist/esm/diffing.test.js +896 -0
  6. package/dist/esm/diffing.test.js.map +1 -0
  7. package/dist/esm/index.d.ts +20 -19
  8. package/dist/esm/index.js +17 -16
  9. package/dist/esm/index.js.map +1 -1
  10. package/dist/esm/migration.js +1 -1
  11. package/dist/esm/migration.js.map +1 -1
  12. package/dist/esm/migration.test.d.ts +1 -0
  13. package/dist/esm/migration.test.js +98 -0
  14. package/dist/esm/migration.test.js.map +1 -0
  15. package/dist/esm/operation.d.ts +7 -21
  16. package/dist/esm/operation.js +28 -266
  17. package/dist/esm/operation.js.map +1 -1
  18. package/dist/esm/operation.test.js +122 -592
  19. package/dist/esm/operation.test.js.map +1 -1
  20. package/dist/esm/patch.d.ts +13 -13
  21. package/dist/esm/patch.js +41 -16
  22. package/dist/esm/patch.js.map +1 -1
  23. package/dist/esm/timestamp.d.ts +1 -0
  24. package/dist/esm/timestamp.js +3 -0
  25. package/dist/esm/timestamp.js.map +1 -1
  26. package/dist/esm/undo.js +15 -2
  27. package/dist/esm/undo.js.map +1 -1
  28. package/dist/esm/undo.test.d.ts +1 -0
  29. package/dist/esm/undo.test.js +20 -0
  30. package/dist/esm/undo.test.js.map +1 -0
  31. package/dist/esm/utils.js +13 -3
  32. package/dist/esm/utils.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/diffing.test.ts +939 -0
  35. package/src/diffing.ts +325 -0
  36. package/src/index.ts +21 -20
  37. package/src/migration.test.ts +112 -0
  38. package/src/migration.ts +2 -1
  39. package/src/operation.test.ts +148 -623
  40. package/src/operation.ts +37 -371
  41. package/src/patch.ts +68 -11
  42. package/src/timestamp.ts +4 -1
  43. package/src/undo.test.ts +21 -0
  44. package/src/undo.ts +15 -2
  45. package/src/utils.ts +13 -3
@@ -0,0 +1,939 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { diffToPatches } from './diffing.js';
3
+ import { createFileRef } from './files.js';
4
+ import {
5
+ assignOid,
6
+ assignOidsToAllSubObjects,
7
+ getOid,
8
+ ObjectIdentifier,
9
+ } from './oids.js';
10
+ import {
11
+ applyOperations,
12
+ deconstructSnapshotToRefs,
13
+ groupPatchesByOid,
14
+ Operation,
15
+ substituteRefsWithObjects,
16
+ } from './operation.js';
17
+ import { cloneDeep } from './utils.js';
18
+
19
+ function createClock(init = 0) {
20
+ let i = init;
21
+ const clock = () => (i++).toString();
22
+ Object.defineProperty(clock, 'current', {
23
+ get() {
24
+ return i;
25
+ },
26
+ });
27
+ return clock;
28
+ }
29
+
30
+ function expectDiffProducesResult(from: any, patches: Operation[], to: any) {
31
+ // all required sub-objects should either be present on the from object,
32
+ // or represented by initialize operations in the patches. using these we can
33
+ // reconstruct the final representation.
34
+ const refTargets = new Map<ObjectIdentifier, any>();
35
+ const fromCopy = cloneDeep(from); // retains OIDs
36
+ deconstructSnapshotToRefs(fromCopy, refTargets);
37
+ for (const patch of patches) {
38
+ if (patch.data.op === 'initialize') {
39
+ refTargets.set(patch.oid, patch.data.value);
40
+ }
41
+ }
42
+ const fromOid = getOid(from);
43
+ const fromRoot = refTargets.get(fromOid);
44
+ expect(fromRoot).toBeDefined();
45
+ const groupedPatches = groupPatchesByOid(patches);
46
+
47
+ for (const [oid, ops] of Object.entries(groupedPatches)) {
48
+ const target = refTargets.get(oid);
49
+ expect(target).toBeDefined();
50
+ refTargets.set(oid, applyOperations(target, ops));
51
+ }
52
+
53
+ // reconstruct the applied final object
54
+ substituteRefsWithObjects(fromRoot, refTargets);
55
+ expect(fromRoot).toEqual(to);
56
+ }
57
+
58
+ describe('generating diff operations', () => {
59
+ describe('on flat objects', () => {
60
+ it('generates and applies set and remove operations', () => {
61
+ const from = { foo: 'bar', baz: 'qux', zing: 1 };
62
+ assignOid(from, 'test/a');
63
+ const to = { foo: 'bar', baz: 'pop' };
64
+ assignOid(to, 'test/a');
65
+ const ops = diffToPatches(from, to, createClock());
66
+ expect(ops).toMatchInlineSnapshot(`
67
+ [
68
+ {
69
+ "authz": undefined,
70
+ "data": {
71
+ "name": "baz",
72
+ "op": "set",
73
+ "value": "pop",
74
+ },
75
+ "oid": "test/a",
76
+ "timestamp": "0",
77
+ },
78
+ {
79
+ "authz": undefined,
80
+ "data": {
81
+ "name": "zing",
82
+ "op": "remove",
83
+ },
84
+ "oid": "test/a",
85
+ "timestamp": "1",
86
+ },
87
+ ]
88
+ `);
89
+ expectDiffProducesResult(from, ops, to);
90
+ });
91
+ });
92
+ describe('on nested objects', () => {
93
+ it('replaces whole nested objects', () => {
94
+ const from = assignOid(
95
+ {
96
+ foo: 'bar',
97
+ baz: assignOid({ qux: 'corge' }, 'test/a:0'),
98
+ },
99
+ 'test/a',
100
+ );
101
+ const to = assignOid(
102
+ {
103
+ foo: 'bar',
104
+ // NOTE: this object is assigned an OID. it's even one related to the
105
+ // from object. but we still discard it. adding references to other
106
+ // already-known objects to a different entity is not supported due
107
+ // to the risk of having the same identity in multiple places
108
+ baz: assignOid({ qux: 'grault' }, 'test/a:1'),
109
+ },
110
+ 'test/a',
111
+ );
112
+ const patches = diffToPatches(from, to, createClock(), createClock(2));
113
+ expect(patches).toMatchInlineSnapshot(`
114
+ [
115
+ {
116
+ "data": {
117
+ "op": "initialize",
118
+ "value": {
119
+ "qux": "grault",
120
+ },
121
+ },
122
+ "oid": "test/a:2",
123
+ "timestamp": "0",
124
+ },
125
+ {
126
+ "authz": undefined,
127
+ "data": {
128
+ "name": "baz",
129
+ "op": "set",
130
+ "value": {
131
+ "@@type": "ref",
132
+ "id": "test/a:2",
133
+ },
134
+ },
135
+ "oid": "test/a",
136
+ "timestamp": "1",
137
+ },
138
+ {
139
+ "authz": undefined,
140
+ "data": {
141
+ "op": "delete",
142
+ },
143
+ "oid": "test/a:0",
144
+ "timestamp": "2",
145
+ },
146
+ ]
147
+ `);
148
+ expectDiffProducesResult(from, patches, to);
149
+ });
150
+ it('replaces whole nested objects with different ids even if fields are the same', () => {
151
+ const from = {
152
+ foo: 'bar',
153
+ baz: { qux: { cat: 'corge' } },
154
+ };
155
+ assignOid(from, 'test/a');
156
+ assignOid(from.baz, 'test/a:0');
157
+ assignOid(from.baz.qux, 'test/a:1');
158
+ const to = {
159
+ foo: 'bar',
160
+ baz: { qux: 'corge' },
161
+ };
162
+ assignOid(to, 'test/a');
163
+ assignOid(to.baz, 'test/a:2');
164
+ const patches = diffToPatches(from, to, createClock(), createClock(3));
165
+ expect(patches).toMatchInlineSnapshot(`
166
+ [
167
+ {
168
+ "data": {
169
+ "op": "initialize",
170
+ "value": {
171
+ "qux": "corge",
172
+ },
173
+ },
174
+ "oid": "test/a:3",
175
+ "timestamp": "0",
176
+ },
177
+ {
178
+ "authz": undefined,
179
+ "data": {
180
+ "name": "baz",
181
+ "op": "set",
182
+ "value": {
183
+ "@@type": "ref",
184
+ "id": "test/a:3",
185
+ },
186
+ },
187
+ "oid": "test/a",
188
+ "timestamp": "1",
189
+ },
190
+ {
191
+ "authz": undefined,
192
+ "data": {
193
+ "op": "delete",
194
+ },
195
+ "oid": "test/a:0",
196
+ "timestamp": "2",
197
+ },
198
+ {
199
+ "authz": undefined,
200
+ "data": {
201
+ "op": "delete",
202
+ },
203
+ "oid": "test/a:1",
204
+ "timestamp": "3",
205
+ },
206
+ ]
207
+ `);
208
+ expectDiffProducesResult(from, patches, to);
209
+ });
210
+ it('does not replace objects with the same identity and same fields', () => {
211
+ const from = {
212
+ foo: 'bar',
213
+ baz: { qux: 'corge' },
214
+ };
215
+ assignOid(from, 'test/a');
216
+ assignOid(from.baz, 'test/a:0');
217
+ const to = {
218
+ foo: 'bar',
219
+ baz: { qux: 'corge' },
220
+ };
221
+ assignOid(to, 'test/a');
222
+ assignOid(to.baz, 'test/a:0');
223
+ const patches = diffToPatches(from, to, createClock());
224
+ expect(patches).toEqual([]);
225
+ });
226
+ it('patches sub-objects with same identity', () => {
227
+ const from = {
228
+ foo: 'bar',
229
+ baz: { qux: 'corge' },
230
+ };
231
+ assignOid(from, 'test/a');
232
+ assignOid(from.baz, 'test/a:0');
233
+ const to = {
234
+ foo: 'bar',
235
+ baz: { qux: 'fig' },
236
+ };
237
+ assignOid(to, 'test/a');
238
+ assignOid(to.baz, 'test/a:0');
239
+ const patches = diffToPatches(from, to, createClock());
240
+ expect(patches).toMatchInlineSnapshot(`
241
+ [
242
+ {
243
+ "authz": undefined,
244
+ "data": {
245
+ "name": "qux",
246
+ "op": "set",
247
+ "value": "fig",
248
+ },
249
+ "oid": "test/a:0",
250
+ "timestamp": "0",
251
+ },
252
+ ]
253
+ `);
254
+ expectDiffProducesResult(from, patches, to);
255
+ });
256
+ it('retains keys which are undefined in new object if merge is set', () => {
257
+ const from = {
258
+ foo: 'bar',
259
+ baz: { qux: 'corge' },
260
+ };
261
+ assignOid(from, 'test/a');
262
+ assignOid(from.baz, 'test/a:0');
263
+ const to = {
264
+ foo: 'bip',
265
+ };
266
+ assignOid(to, 'test/a');
267
+ const patches = diffToPatches(from, to, createClock(), undefined, [], {
268
+ merge: true,
269
+ mergeUnknownObjects: true,
270
+ });
271
+ expect(patches).toMatchInlineSnapshot(`
272
+ [
273
+ {
274
+ "authz": undefined,
275
+ "data": {
276
+ "name": "foo",
277
+ "op": "set",
278
+ "value": "bip",
279
+ },
280
+ "oid": "test/a",
281
+ "timestamp": "0",
282
+ },
283
+ ]
284
+ `);
285
+ expectDiffProducesResult(from, patches, { ...from, ...to });
286
+ });
287
+ it('deletes old complex objects when theyre replaced by primitives', () => {
288
+ const from = assignOid(
289
+ {
290
+ thing: { qux: { foo: 'bar' } },
291
+ },
292
+ 'test/a',
293
+ );
294
+ assignOidsToAllSubObjects(from, createClock());
295
+ const to = assignOid(
296
+ {
297
+ thing: 'foo',
298
+ },
299
+ 'test/a',
300
+ );
301
+ const patches = diffToPatches(from, to, createClock());
302
+ expect(patches).toMatchInlineSnapshot(`
303
+ [
304
+ {
305
+ "authz": undefined,
306
+ "data": {
307
+ "name": "thing",
308
+ "op": "set",
309
+ "value": "foo",
310
+ },
311
+ "oid": "test/a",
312
+ "timestamp": "0",
313
+ },
314
+ {
315
+ "authz": undefined,
316
+ "data": {
317
+ "op": "delete",
318
+ },
319
+ "oid": "test/a:0",
320
+ "timestamp": "1",
321
+ },
322
+ {
323
+ "authz": undefined,
324
+ "data": {
325
+ "op": "delete",
326
+ },
327
+ "oid": "test/a:1",
328
+ "timestamp": "2",
329
+ },
330
+ ]
331
+ `);
332
+ expectDiffProducesResult(from, patches, to);
333
+ });
334
+ });
335
+ describe('on lists of primitives', () => {
336
+ it('pushes new items', () => {
337
+ const from = assignOid([1, 2], 'test/a');
338
+ const to = assignOid([1, 2, 3], 'test/a');
339
+ const patches = diffToPatches(from, to, createClock());
340
+ expect(patches).toEqual([
341
+ {
342
+ data: {
343
+ op: 'list-push',
344
+ value: 3,
345
+ },
346
+ oid: 'test/a',
347
+ timestamp: '0',
348
+ },
349
+ ] satisfies Operation[]);
350
+ expectDiffProducesResult(from, patches, to);
351
+ });
352
+
353
+ it('inserts items when remaining list is equal', () => {
354
+ const from = assignOid([1, 2, 4, 5], 'test/a');
355
+ const to = assignOid([1, 2, 3, 4, 5], 'test/a');
356
+ const patches = diffToPatches(from, to, createClock());
357
+ expect(patches).toEqual([
358
+ {
359
+ authz: undefined,
360
+ data: {
361
+ index: 2,
362
+ op: 'list-insert',
363
+ value: 3,
364
+ },
365
+ oid: 'test/a',
366
+ timestamp: '0',
367
+ },
368
+ ] satisfies Operation[]);
369
+ expectDiffProducesResult(from, patches, to);
370
+ });
371
+
372
+ it('does not insert items when remaining list is unequal', () => {
373
+ const from = assignOid([1, 2, 4, 5, 6, 7], 'test/a');
374
+ const to = assignOid([1, 2, 3, 8, 9, 10], 'test/a');
375
+ const patches = diffToPatches(from, to, createClock());
376
+ expect(patches).toEqual([
377
+ {
378
+ authz: undefined,
379
+ data: {
380
+ op: 'list-set',
381
+ value: 3,
382
+ index: 2,
383
+ },
384
+ oid: 'test/a',
385
+ timestamp: '0',
386
+ },
387
+ {
388
+ authz: undefined,
389
+ data: {
390
+ op: 'list-set',
391
+ value: 8,
392
+ index: 3,
393
+ },
394
+ oid: 'test/a',
395
+ timestamp: '1',
396
+ },
397
+ {
398
+ authz: undefined,
399
+ data: {
400
+ op: 'list-set',
401
+ index: 4,
402
+ value: 9,
403
+ },
404
+ oid: 'test/a',
405
+ timestamp: '2',
406
+ },
407
+ {
408
+ authz: undefined,
409
+ data: {
410
+ op: 'list-set',
411
+ index: 5,
412
+ value: 10,
413
+ },
414
+ oid: 'test/a',
415
+ timestamp: '3',
416
+ },
417
+ ] satisfies Operation[]);
418
+ expectDiffProducesResult(from, patches, to);
419
+ });
420
+
421
+ it('inserts items at the end of the new list if old list had more unmatched items at the end', () => {
422
+ const from = assignOid([1, 2, 4, 5, 6, 7], 'test/a');
423
+ const to = assignOid([1, 2, 3, 5, 8], 'test/a');
424
+ const patches = diffToPatches(from, to, createClock());
425
+ expect(patches).toEqual([
426
+ {
427
+ authz: undefined,
428
+ data: {
429
+ op: 'list-set',
430
+ value: 3,
431
+ index: 2,
432
+ },
433
+ oid: 'test/a',
434
+ timestamp: '0',
435
+ },
436
+ {
437
+ authz: undefined,
438
+ data: {
439
+ op: 'list-insert',
440
+ value: 8,
441
+ index: 4,
442
+ },
443
+ oid: 'test/a',
444
+ timestamp: '1',
445
+ },
446
+ {
447
+ authz: undefined,
448
+ data: {
449
+ op: 'list-delete',
450
+ index: 5,
451
+ count: 2,
452
+ },
453
+ oid: 'test/a',
454
+ timestamp: '2',
455
+ },
456
+ ] satisfies Operation[]);
457
+ expectDiffProducesResult(from, patches, to);
458
+ });
459
+ });
460
+ describe('on lists of objects', () => {
461
+ it('pushes items', () => {
462
+ const from = assignOid(
463
+ [
464
+ { id: '1', value: 'Item one' },
465
+ { id: '2', value: 'Item two' },
466
+ ],
467
+ 'test/a',
468
+ );
469
+ assignOidsToAllSubObjects(from, createClock());
470
+ const to = assignOid(
471
+ [
472
+ { id: '1', value: 'Item one' },
473
+ { id: '2', value: 'Item two' },
474
+ { id: '3', value: 'Item three' },
475
+ ],
476
+ 'test/a',
477
+ );
478
+ assignOidsToAllSubObjects(to, createClock());
479
+ const patches = diffToPatches(from, to, createClock(), createClock(2));
480
+ expect(patches).toMatchInlineSnapshot(`
481
+ [
482
+ {
483
+ "data": {
484
+ "op": "initialize",
485
+ "value": {
486
+ "id": "3",
487
+ "value": "Item three",
488
+ },
489
+ },
490
+ "oid": "test/a:2",
491
+ "timestamp": "0",
492
+ },
493
+ {
494
+ "authz": undefined,
495
+ "data": {
496
+ "op": "list-push",
497
+ "value": {
498
+ "@@type": "ref",
499
+ "id": "test/a:2",
500
+ },
501
+ },
502
+ "oid": "test/a",
503
+ "timestamp": "1",
504
+ },
505
+ ]
506
+ `);
507
+ expectDiffProducesResult(from, patches, to);
508
+ });
509
+ it.todo('moves objects by identity');
510
+ it.todo('removes objects by identity');
511
+ });
512
+ describe.todo('on lists of lists', () => {
513
+ it.todo('rejects operations by value or identity');
514
+ });
515
+ it('should work for complex nested arrays and objects', () => {
516
+ const from = {
517
+ foo: {
518
+ bar: [1, 2, 3],
519
+ },
520
+ baz: [
521
+ {
522
+ corge: true,
523
+ },
524
+ ],
525
+ };
526
+ assignOid(from, 'test/a');
527
+ assignOid(from.foo, 'test/a:1');
528
+ assignOid(from.foo.bar, 'test/a:2');
529
+ assignOid(from.baz, 'test/a:3');
530
+ assignOid(from.baz[0], 'test/a:4');
531
+
532
+ const to = {
533
+ foo: {
534
+ bar: [1, 2],
535
+ bop: [0],
536
+ },
537
+ baz: [
538
+ {
539
+ corge: false,
540
+ },
541
+ {
542
+ corge: false,
543
+ },
544
+ ],
545
+ };
546
+ assignOid(to, 'test/a');
547
+ assignOid(to.foo, 'test/a:1');
548
+ assignOid(to.foo.bar, 'test/a:2');
549
+ assignOid(to.foo.bop, 'test/a:6');
550
+ assignOid(to.baz, 'test/a:3');
551
+ assignOid(to.baz[0], 'test/a:4');
552
+ assignOid(to.baz[1], 'test/a:5');
553
+ const patches = diffToPatches(from, to, createClock(), createClock(7));
554
+ expect(patches).toMatchInlineSnapshot(`
555
+ [
556
+ {
557
+ "authz": undefined,
558
+ "data": {
559
+ "count": 1,
560
+ "index": 2,
561
+ "op": "list-delete",
562
+ },
563
+ "oid": "test/a:2",
564
+ "timestamp": "0",
565
+ },
566
+ {
567
+ "data": {
568
+ "op": "initialize",
569
+ "value": [
570
+ 0,
571
+ ],
572
+ },
573
+ "oid": "test/a:7",
574
+ "timestamp": "1",
575
+ },
576
+ {
577
+ "authz": undefined,
578
+ "data": {
579
+ "name": "bop",
580
+ "op": "set",
581
+ "value": {
582
+ "@@type": "ref",
583
+ "id": "test/a:7",
584
+ },
585
+ },
586
+ "oid": "test/a:1",
587
+ "timestamp": "2",
588
+ },
589
+ {
590
+ "authz": undefined,
591
+ "data": {
592
+ "name": "corge",
593
+ "op": "set",
594
+ "value": false,
595
+ },
596
+ "oid": "test/a:4",
597
+ "timestamp": "3",
598
+ },
599
+ {
600
+ "data": {
601
+ "op": "initialize",
602
+ "value": {
603
+ "corge": false,
604
+ },
605
+ },
606
+ "oid": "test/a:8",
607
+ "timestamp": "4",
608
+ },
609
+ {
610
+ "authz": undefined,
611
+ "data": {
612
+ "op": "list-push",
613
+ "value": {
614
+ "@@type": "ref",
615
+ "id": "test/a:8",
616
+ },
617
+ },
618
+ "oid": "test/a:3",
619
+ "timestamp": "5",
620
+ },
621
+ ]
622
+ `);
623
+ expectDiffProducesResult(from, patches, to);
624
+ });
625
+
626
+ it('should try to merge unknown objects if specified to do so', () => {
627
+ const from = {
628
+ foo: {
629
+ bar: [1, 2, 3],
630
+ },
631
+ baz: [
632
+ {
633
+ corge: true,
634
+ },
635
+ ],
636
+ };
637
+ assignOid(from, 'test/a');
638
+ assignOidsToAllSubObjects(from, createClock());
639
+
640
+ const to = {
641
+ foo: {
642
+ bar: [1, 2],
643
+ bop: [0],
644
+ },
645
+ baz: [
646
+ {
647
+ corge: false,
648
+ },
649
+ {
650
+ corge: false,
651
+ },
652
+ ],
653
+ };
654
+ const patches = diffToPatches(from, to, createClock(), createClock(5), [], {
655
+ mergeUnknownObjects: true,
656
+ });
657
+ expect(patches).toMatchInlineSnapshot(`
658
+ [
659
+ {
660
+ "authz": undefined,
661
+ "data": {
662
+ "count": 1,
663
+ "index": 2,
664
+ "op": "list-delete",
665
+ },
666
+ "oid": "test/a:1",
667
+ "timestamp": "0",
668
+ },
669
+ {
670
+ "data": {
671
+ "op": "initialize",
672
+ "value": [
673
+ 0,
674
+ ],
675
+ },
676
+ "oid": "test/a:5",
677
+ "timestamp": "1",
678
+ },
679
+ {
680
+ "authz": undefined,
681
+ "data": {
682
+ "name": "bop",
683
+ "op": "set",
684
+ "value": {
685
+ "@@type": "ref",
686
+ "id": "test/a:5",
687
+ },
688
+ },
689
+ "oid": "test/a:0",
690
+ "timestamp": "2",
691
+ },
692
+ {
693
+ "authz": undefined,
694
+ "data": {
695
+ "name": "corge",
696
+ "op": "set",
697
+ "value": false,
698
+ },
699
+ "oid": "test/a:3",
700
+ "timestamp": "3",
701
+ },
702
+ {
703
+ "data": {
704
+ "op": "initialize",
705
+ "value": {
706
+ "corge": false,
707
+ },
708
+ },
709
+ "oid": "test/a:6",
710
+ "timestamp": "4",
711
+ },
712
+ {
713
+ "authz": undefined,
714
+ "data": {
715
+ "op": "list-push",
716
+ "value": {
717
+ "@@type": "ref",
718
+ "id": "test/a:6",
719
+ },
720
+ },
721
+ "oid": "test/a:2",
722
+ "timestamp": "5",
723
+ },
724
+ ]
725
+ `);
726
+ expectDiffProducesResult(from, patches, to);
727
+ });
728
+
729
+ it('should not diff file refs', () => {
730
+ const from = {
731
+ foo: {
732
+ file: createFileRef('abc123'),
733
+ },
734
+ bar: [createFileRef('def456'), createFileRef('ghi789')],
735
+ };
736
+ assignOid(from, 'test/a');
737
+ assignOidsToAllSubObjects(from, createClock());
738
+
739
+ const to = {
740
+ foo: {
741
+ file: createFileRef('abc456'),
742
+ },
743
+ bar: [createFileRef('def456')],
744
+ };
745
+ const patches = diffToPatches(from, to, createClock(), createClock(5), [], {
746
+ mergeUnknownObjects: true,
747
+ });
748
+ expect(patches).toMatchInlineSnapshot(`
749
+ [
750
+ {
751
+ "authz": undefined,
752
+ "data": {
753
+ "name": "file",
754
+ "op": "set",
755
+ "value": {
756
+ "@@type": "file",
757
+ "id": "abc456",
758
+ },
759
+ },
760
+ "oid": "test/a:0",
761
+ "timestamp": "0",
762
+ },
763
+ {
764
+ "authz": undefined,
765
+ "data": {
766
+ "count": 1,
767
+ "index": 1,
768
+ "op": "list-delete",
769
+ },
770
+ "oid": "test/a:1",
771
+ "timestamp": "1",
772
+ },
773
+ ]
774
+ `);
775
+ expectDiffProducesResult(from, patches, to);
776
+ });
777
+
778
+ it('should assign a new OID to objects which had an incompatible existing OID', () => {
779
+ const from = {
780
+ foo: {
781
+ bar: [1, 2, 3],
782
+ },
783
+ };
784
+ assignOid(from, 'test/a');
785
+ assignOidsToAllSubObjects(from, createClock());
786
+ const to = {
787
+ foo: {
788
+ bar: [1, 2],
789
+ },
790
+ };
791
+ assignOid(to, 'test/a');
792
+ assignOid(to.foo, 'uff/b');
793
+ assignOidsToAllSubObjects(to.foo, createClock());
794
+ const patches = diffToPatches(from, to, createClock(), createClock(5), [], {
795
+ mergeUnknownObjects: true,
796
+ });
797
+ expect(patches).toMatchInlineSnapshot(`
798
+ [
799
+ {
800
+ "data": {
801
+ "op": "initialize",
802
+ "value": [
803
+ 1,
804
+ 2,
805
+ ],
806
+ },
807
+ "oid": "test/a:6",
808
+ "timestamp": "0",
809
+ },
810
+ {
811
+ "data": {
812
+ "op": "initialize",
813
+ "value": {
814
+ "bar": {
815
+ "@@type": "ref",
816
+ "id": "test/a:6",
817
+ },
818
+ },
819
+ },
820
+ "oid": "test/a:5",
821
+ "timestamp": "1",
822
+ },
823
+ {
824
+ "authz": undefined,
825
+ "data": {
826
+ "name": "foo",
827
+ "op": "set",
828
+ "value": {
829
+ "@@type": "ref",
830
+ "id": "test/a:5",
831
+ },
832
+ },
833
+ "oid": "test/a",
834
+ "timestamp": "2",
835
+ },
836
+ {
837
+ "authz": undefined,
838
+ "data": {
839
+ "op": "delete",
840
+ },
841
+ "oid": "test/a:0",
842
+ "timestamp": "3",
843
+ },
844
+ {
845
+ "authz": undefined,
846
+ "data": {
847
+ "op": "delete",
848
+ },
849
+ "oid": "test/a:1",
850
+ "timestamp": "4",
851
+ },
852
+ ]
853
+ `);
854
+ expectDiffProducesResult(from, patches, to);
855
+ });
856
+
857
+ // an edge case - if a subobject which contains nested objects changes identity,
858
+ // this gets written as a new init patch tree - we want to ensure all init subobjects
859
+ // in that tree have compatible OIDs.
860
+ it('should assign a new OID to objects which have an incompatible OID when parent identity changes', () => {
861
+ const from = {
862
+ bar: [],
863
+ };
864
+ assignOid(from, 'test/a');
865
+ assignOid(from.bar, 'test/random');
866
+ const to = {
867
+ bar: [{ baz: 1 }, { baz: 2 }],
868
+ };
869
+ assignOid(to, 'test/a');
870
+ assignOidsToAllSubObjects(to, createClock());
871
+ assignOid(to.bar[0], 'uff/b');
872
+ const patches = diffToPatches(from, to, createClock(), createClock(5), [], {
873
+ mergeUnknownObjects: true,
874
+ });
875
+ expect(patches).toMatchInlineSnapshot(`
876
+ [
877
+ {
878
+ "data": {
879
+ "op": "initialize",
880
+ "value": {
881
+ "baz": 1,
882
+ },
883
+ },
884
+ "oid": "test/a:6",
885
+ "timestamp": "0",
886
+ },
887
+ {
888
+ "data": {
889
+ "op": "initialize",
890
+ "value": {
891
+ "baz": 2,
892
+ },
893
+ },
894
+ "oid": "test/a:2",
895
+ "timestamp": "1",
896
+ },
897
+ {
898
+ "data": {
899
+ "op": "initialize",
900
+ "value": [
901
+ {
902
+ "@@type": "ref",
903
+ "id": "test/a:6",
904
+ },
905
+ {
906
+ "@@type": "ref",
907
+ "id": "test/a:2",
908
+ },
909
+ ],
910
+ },
911
+ "oid": "test/a:5",
912
+ "timestamp": "2",
913
+ },
914
+ {
915
+ "authz": undefined,
916
+ "data": {
917
+ "name": "bar",
918
+ "op": "set",
919
+ "value": {
920
+ "@@type": "ref",
921
+ "id": "test/a:5",
922
+ },
923
+ },
924
+ "oid": "test/a",
925
+ "timestamp": "3",
926
+ },
927
+ {
928
+ "authz": undefined,
929
+ "data": {
930
+ "op": "delete",
931
+ },
932
+ "oid": "test/random",
933
+ "timestamp": "4",
934
+ },
935
+ ]
936
+ `);
937
+ expectDiffProducesResult(from, patches, to);
938
+ });
939
+ });