-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindext.test.ts
More file actions
706 lines (543 loc) · 17.7 KB
/
indext.test.ts
File metadata and controls
706 lines (543 loc) · 17.7 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
import { describe, expect, test } from 'vitest';
import * as Y from 'yjs';
import { rawReturn } from 'mutative';
import { bind, createBinder } from '../src';
import { createSampleObject, id1, id2, id3 } from './sample-data';
test('bind usage demo', () => {
const doc = new Y.Doc();
const initialObj = createSampleObject(); // plain object
const topLevelMap = 'map';
// get reference of CRDT type for binding
const map = doc.getMap(topLevelMap);
// bind the top-level CRDT type, works for Y.Array as well
const binder = bind<typeof initialObj>(map);
// initialize document with sample data
binder.update(() => {
return rawReturn(initialObj);
});
// snapshot reference should not change if no update
expect(binder.get()).toBe(binder.get());
// get current state as snapshot
const snapshot1 = binder.get();
// should equal to initial structurally
expect(snapshot1).toStrictEqual(initialObj);
// should equal to yjs structurally
expect(snapshot1).toStrictEqual(map.toJSON());
// get the reference to be compared after changes are made
const yd1 = map.get(id1) as any;
// nested objects / arrays are properly converted to Y.Maps / Y.Arrays
expect(yd1).toBeInstanceOf(Y.Map);
expect(yd1.get('batters')).toBeInstanceOf(Y.Map);
expect(yd1.get('topping')).toBeInstanceOf(Y.Array);
expect(yd1.get('batters').get('batter')).toBeInstanceOf(Y.Array);
expect(yd1.get('batters').get('batter').get(0).get('id')).toBeTypeOf(
'string'
);
// update the state with immer
binder.update((state) => {
state[id1].ppu += 0.1;
const d1 = state[id1];
d1.topping.splice(
2,
2,
{ id: '7777', type: 'test1' },
{ id: '8888', type: 'test2' }
);
d1.topping.push({ id: '9999', type: 'test3' });
delete state[id3];
});
// get snapshot after modified
const snapshot2 = binder.get();
// snapshot1 unchanged
expect(snapshot1).toStrictEqual(initialObj);
// snapshot2 changed
expect(snapshot1).not.equal(snapshot2);
// changed properties should reflect what we did in update(...)
expect(snapshot2[id1].ppu).toStrictEqual(0.65);
expect(snapshot2[id1].topping.find((x) => x.id === '9999')).toStrictEqual({
id: '9999',
type: 'test3',
});
expect(snapshot2[id3]).toBeUndefined();
// reference changed as well
expect(snapshot2[id1]).not.toBe(snapshot1[id1]);
// unchanged properties should keep referential equality with previous snapshot
expect(snapshot2[id2]).toBe(snapshot1[id2]);
expect(snapshot2[id1].batters).toBe(snapshot1[id1].batters);
expect(snapshot2[id1].topping[0]).toBe(snapshot1[id1].topping[0]);
// the underlying yjs data type reflect changes as well
expect(map.toJSON()).toStrictEqual(snapshot2);
// but yjs data type should not change reference (they are mutated in-place whenever possible)
expect(map).toBe(doc.getMap(topLevelMap));
expect(map.get(id1)).toBe(yd1);
expect((map.get(id1) as any).get('topping')).toBe(yd1.get('topping'));
// save the length for later comparison
const expectLength = binder.get()[id1].batters.batter.length;
// change from y.js
yd1
.get('batters')
.get('batter')
.push([{ id: '1005', type: 'test' }]);
// change reflected in snapshot
expect(binder.get()[id1].batters.batter.at(-1)).toStrictEqual({
id: '1005',
type: 'test',
});
// now the length + 1
expect(binder.get()[id1].batters.batter.length).toBe(expectLength + 1);
// delete something from yjs
yd1.delete('topping');
// deletion reflected in snapshot
expect(binder.get()[id1].topping).toBeUndefined();
// release the observer, so the CRDT type can be bind again
binder.unbind();
});
test('boolean in array', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<any>(map);
binder.update((state) => {
state.k1 = true;
state.k2 = false;
state.k3 = [true, false, true];
});
expect(map.toJSON()).toStrictEqual({
k1: true,
k2: false,
k3: [true, false, true],
});
});
test('customize applyPatch', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const initialObj = createSampleObject(); // plain object
const binder = bind<typeof initialObj>(map, {
applyPatch: (target, patch, applyPatch) => {
// you can inspect the patch.path and decide what to do with target
// optionally delegate to the default patch handler
// (modify target/patch before delegating as you want)
applyPatch(target, patch);
// can also postprocessing after the default behavior is applied
},
});
binder.update(() => rawReturn(initialObj));
expect(binder.get()).toStrictEqual(initialObj);
expect(binder.get()).toStrictEqual(map.toJSON());
expect(binder.get()).toBe(binder.get());
});
describe('array splice', () => {
function prepareArrayDoc(...items: number[]) {
const doc = new Y.Doc();
const binder = bind<{ array: number[] }>(doc.getMap('data'), {
applyPatch: (target, patch, apply) => {
apply(target, patch);
},
});
binder.update((data) => {
data.array = items;
});
return { doc, binder };
}
test('remove nonexistent item', () => {
const { binder } = prepareArrayDoc();
binder.update((data) => {
data.array.splice(0, 1);
});
expect(binder.get().array.length).toBe(0);
});
test('remove single item', () => {
const { binder } = prepareArrayDoc(1);
binder.update((data) => {
data.array.splice(0, 1);
});
expect(binder.get().array.length).toBe(0);
});
test('remove first item of many', () => {
const { binder } = prepareArrayDoc(1, 2, 3);
// results in ops
// replace array[0] value 2
// replace array[1] value 3
// replace array.length value 2
binder.update((data) => {
data.array.splice(0, 1);
});
expect(binder.get().array.length).toBe(2);
});
test('remove last multiple items', () => {
const { binder } = prepareArrayDoc(1, 2, 3, 4);
binder.update((data) => {
data.array.splice(2, 2);
});
const result = binder.get().array;
expect(result.length).toBe(2);
expect(result[0]).toBe(1);
expect(result[1]).toBe(2);
});
test('replace last multiple items', () => {
const { binder } = prepareArrayDoc(1, 2, 3, 4);
binder.update((data) => {
data.array.splice(2, 2, 5, 6);
});
const result = binder.get().array;
expect(result.length).toBe(4);
expect(result[0]).toBe(1);
expect(result[1]).toBe(2);
expect(result[2]).toBe(5);
expect(result[3]).toBe(6);
});
test('remove first multiple items', () => {
const { binder } = prepareArrayDoc(1, 2, 3, 4);
binder.update((data) => {
data.array.splice(0, 2);
});
const result = binder.get().array;
expect(result.length).toBe(2);
expect(result[0]).toBe(3);
expect(result[1]).toBe(4);
});
test('replace first multiple items', () => {
const { binder } = prepareArrayDoc(1, 2, 3, 4);
binder.update((data) => {
data.array.splice(0, 2, 5, 6);
});
const result = binder.get().array;
expect(result.length).toBe(4);
expect(result[0]).toBe(5);
expect(result[1]).toBe(6);
expect(result[2]).toBe(3);
expect(result[3]).toBe(4);
});
});
describe('subscription', () => {
test('should trigger subscription on update', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map);
let callCount = 0;
let receivedSnapshot: any = null;
const unsubscribe = binder.subscribe((snapshot) => {
callCount++;
receivedSnapshot = snapshot;
});
binder.update((state) => {
state.count = 10;
});
expect(callCount).toBe(1);
expect(receivedSnapshot).toEqual({ count: 10 });
binder.update((state) => {
state.count = 20;
});
expect(callCount).toBe(2);
expect(receivedSnapshot).toEqual({ count: 20 });
unsubscribe();
binder.update((state) => {
state.count = 30;
});
// Should not trigger after unsubscribe
expect(callCount).toBe(2);
});
test('should trigger subscription on Yjs changes', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map);
let callCount = 0;
binder.subscribe(() => {
callCount++;
});
// Direct Yjs mutation
map.set('count', 42);
expect(callCount).toBe(1);
expect(binder.get()).toEqual({ count: 42 });
});
test('should support multiple subscribers', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map);
let count1 = 0;
let count2 = 0;
const unsub1 = binder.subscribe(() => { count1++; });
const unsub2 = binder.subscribe(() => { count2++; });
binder.update((state) => {
state.count = 1;
});
expect(count1).toBe(1);
expect(count2).toBe(1);
unsub1();
binder.update((state) => {
state.count = 2;
});
expect(count1).toBe(1); // No longer called
expect(count2).toBe(2);
unsub2();
});
});
describe('collaborative editing', () => {
test('should reflect direct Yjs mutations across binders', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder1 = bind<{ count: number }>(map);
const binder2 = bind<{ count: number }>(map);
// Direct Yjs mutation (simulating remote update)
map.set('count', 42);
// Both binders should see the change
expect(binder1.get().count).toBe(42);
expect(binder2.get().count).toBe(42);
// Another direct mutation
map.set('count', 100);
expect(binder1.get().count).toBe(100);
expect(binder2.get().count).toBe(100);
});
test('should not cause circular updates', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map);
let updateCount = 0;
binder.subscribe((snapshot) => {
updateCount++;
// This should not trigger infinite loop
if (snapshot.count < 5) {
binder.update((state) => {
state.count = snapshot.count + 1;
});
}
});
binder.update((state) => {
state.count = 0;
});
// Should update: 0 -> 1 -> 2 -> 3 -> 4 -> 5
expect(updateCount).toBe(6);
expect(binder.get().count).toBe(5);
});
});
describe('edge cases', () => {
test('should handle null values', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ value: null | string }>(map);
binder.update((state) => {
state.value = null;
});
expect(binder.get().value).toBe(null);
});
test('should handle array element updated to null', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ items: Array<{ id: number } | null> }>(map);
binder.update((state) => {
state.items = [{ id: 1 }, { id: 2 }, { id: 3 }];
});
expect(binder.get().items).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }]);
// Update array element to null - this should not crash
binder.update((state) => {
state.items[1] = null;
});
expect(binder.get().items).toEqual([{ id: 1 }, null, { id: 3 }]);
expect(binder.get().items[1]).toBe(null);
});
test('should handle empty objects and arrays', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ obj: {}; arr: [] }>(map);
binder.update((state) => {
state.obj = {};
state.arr = [];
});
expect(binder.get()).toEqual({ obj: {}, arr: [] });
});
test('should handle deeply nested structures', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
type DeepType = { a: { b: { c: { d: number } } } };
const binder = bind<DeepType>(map);
binder.update((state) => {
state.a = { b: { c: { d: 42 } } };
});
expect(binder.get().a.b.c.d).toBe(42);
binder.update((state) => {
state.a.b.c.d = 100;
});
expect(binder.get().a.b.c.d).toBe(100);
});
test('should handle array with mixed types', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ mixed: any[] }>(map);
binder.update((state) => {
state.mixed = [1, 'two', true, null, { nested: 'object' }, [1, 2, 3]];
});
const result = binder.get().mixed;
expect(result[0]).toBe(1);
expect(result[1]).toBe('two');
expect(result[2]).toBe(true);
expect(result[3]).toBe(null);
expect(result[4]).toEqual({ nested: 'object' });
expect(result[5]).toEqual([1, 2, 3]);
});
test('should detect circular references', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<any>(map);
const circular: any = { a: 1 };
circular.self = circular;
expect(() => {
binder.update((state) => {
state.data = circular;
});
}).toThrow('Circular reference detected');
});
test('should detect circular references in arrays', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<any>(map);
const circularArray: any[] = [1, 2];
circularArray.push(circularArray);
expect(() => {
binder.update((state) => {
state.arr = circularArray;
});
}).toThrow('Circular reference detected');
});
});
describe('createBinder helper', () => {
test('should create binder with initial state', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const initialState = { count: 42, name: 'test' };
const binder = createBinder(map, initialState);
expect(binder.get()).toEqual(initialState);
expect(map.toJSON()).toEqual(initialState);
});
test('should work with array data', () => {
const doc = new Y.Doc();
const arr = doc.getArray('items');
const initialState = [1, 2, 3];
const binder = createBinder(arr, initialState);
expect(binder.get()).toEqual(initialState);
expect(arr.toJSON()).toEqual(initialState);
});
test('should respect options', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
let patchApplied = false;
const binder = createBinder(
map,
{ value: 1 },
{
applyPatch: (target, patch, defaultApply) => {
patchApplied = true;
defaultApply(target, patch);
},
}
);
expect(patchApplied).toBe(true);
expect(binder.get()).toEqual({ value: 1 });
});
});
describe('options and configuration', () => {
test('should accept valid patchesOptions as boolean', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map, {
patchesOptions: true,
});
binder.update((state) => {
state.count = 10;
});
expect(binder.get().count).toBe(10);
});
test('should accept valid patchesOptions as object', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map, {
patchesOptions: {
pathAsArray: true,
arrayLengthAssignment: false,
},
});
binder.update((state) => {
state.count = 20;
});
expect(binder.get().count).toBe(20);
});
test('should work with detached Y.Map (no document initially)', () => {
// Create Y.Map, then attach to document
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map);
// Initial state should be empty
expect(binder.get()).toEqual({});
binder.update((state) => {
state.count = 100;
});
// After update, should have the value
expect(binder.get().count).toBe(100);
expect(map.toJSON()).toEqual({ count: 100 });
});
test('should notify subscribers on all updates', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map);
let notificationCount = 0;
binder.subscribe(() => {
notificationCount++;
});
binder.update((state) => {
state.count = 50;
});
expect(notificationCount).toBe(1);
binder.update((state) => {
state.count = 51;
});
expect(notificationCount).toBe(2);
});
test('should support immediate subscription', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<{ count: number }>(map);
binder.update((state) => {
state.count = 99;
});
let receivedSnapshot: any = null;
let callCount = 0;
binder.subscribe(
(snapshot) => {
receivedSnapshot = snapshot;
callCount++;
},
{ immediate: true }
);
// Should be called immediately
expect(callCount).toBe(1);
expect(receivedSnapshot).toEqual({ count: 99 });
});
});
describe('error handling', () => {
test('should throw descriptive error for unsupported operations', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
const binder = bind<any>(map);
// This will try to apply an unsupported patch operation
expect(() => {
binder.update((state) => {
state.value = { a: 1 };
// Force a scenario that hits the "not implemented" path
// by trying to apply operations that aren't supported
});
// Note: This test may need adjustment based on actual edge cases
}).not.toThrow(); // Normal operations should work
});
test('should reject invalid patchesOptions', () => {
const doc = new Y.Doc();
const map = doc.getMap('data');
// Create binder with invalid options
const binder = bind<{ count: number }>(map, {
patchesOptions: 'invalid' as any, // Invalid type
});
// Should throw when trying to update
expect(() => {
binder.update((state) => {
state.count = 1;
});
}).toThrow('patchesOptions must be a boolean or an object');
});
});