-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdot-kernel.test.js
More file actions
466 lines (403 loc) · 15.2 KB
/
dot-kernel.test.js
File metadata and controls
466 lines (403 loc) · 15.2 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
'use strict';
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const { DotKernel } = require('../dot-kernel.js');
const { DotContext } = require('../dot-context.js');
const { Dot } = require('../dot.js');
// Helper function to create test kernel
function createKernel(data, context = null) {
const kernel = new DotKernel(context);
for (const [dotStr, value] of Object.entries(data)) {
kernel.dataStorage.set(dotStr, value);
kernel.sharedContext.insertDot(dotStr);
}
return kernel;
}
describe('DotKernel', () => {
describe('Constructor & Context Management', () => {
it('should create kernel with own base context', () => {
const kernel = new DotKernel();
assert.ok(kernel.sharedContext === kernel.contextBase);
assert.ok(kernel.sharedContext instanceof DotContext);
});
it('should create kernel with shared context', () => {
const externalCtx = new DotContext();
const kernel = new DotKernel(externalCtx);
assert.ok(kernel.sharedContext === externalCtx);
assert.ok(kernel.contextBase !== externalCtx);
});
it('should allow multiple kernels to share same context', () => {
const sharedCtx = new DotContext();
const kernel1 = new DotKernel(sharedCtx);
const kernel2 = new DotKernel(sharedCtx);
kernel1.add('a', 'value1');
assert.ok(kernel1.sharedContext === kernel2.sharedContext);
assert.equal(sharedCtx.compactCausalContext.get('a'), 1);
});
it('should initialize with empty data storage', () => {
const kernel = new DotKernel();
assert.equal(kernel.dataStorage.size, 0);
});
});
describe('add()', () => {
it('should add value with new dot', () => {
const kernel = new DotKernel();
kernel.add('replicaA', 'value1');
assert.ok(kernel.dataStorage.has('replicaA:1'));
assert.equal(kernel.dataStorage.get('replicaA:1'), 'value1');
});
it('should return delta kernel with just the added dot', () => {
const kernel = new DotKernel();
const delta = kernel.add('a', 'val');
assert.equal(delta.dataStorage.size, 1);
assert.ok(delta.dataStorage.has('a:1'));
assert.equal(delta.dataStorage.get('a:1'), 'val');
assert.equal(delta.sharedContext.dotIn({ id: 'a', counter: 1 }), true);
});
it('should increment counter for same replica', () => {
const kernel = new DotKernel();
kernel.add('a', 'v1');
kernel.add('a', 'v2');
assert.ok(kernel.dataStorage.has('a:1'));
assert.ok(kernel.dataStorage.has('a:2'));
assert.equal(kernel.dataStorage.get('a:1'), 'v1');
assert.equal(kernel.dataStorage.get('a:2'), 'v2');
});
it('should update shared context', () => {
const sharedCtx = new DotContext();
const kernel = new DotKernel(sharedCtx);
kernel.add('a', 'val');
assert.equal(sharedCtx.compactCausalContext.get('a'), 1);
});
it('should handle multiple replicas independently', () => {
const kernel = new DotKernel();
kernel.add('a', 'x');
kernel.add('b', 'y');
kernel.add('a', 'z');
assert.equal(kernel.dataStorage.get('a:1'), 'x');
assert.equal(kernel.dataStorage.get('b:1'), 'y');
assert.equal(kernel.dataStorage.get('a:2'), 'z');
});
it('should add complex values', () => {
const kernel = new DotKernel();
const obj = { key: 'value', nested: { data: 123 } };
kernel.add('a', obj);
assert.deepEqual(kernel.dataStorage.get('a:1'), obj);
});
it('should add null values', () => {
const kernel = new DotKernel();
kernel.add('a', null);
assert.equal(kernel.dataStorage.get('a:1'), null);
});
});
describe('dotAdd()', () => {
it('should add value and return dot', () => {
const kernel = new DotKernel();
const dot = kernel.dotAdd('a', 'val');
assert.ok(dot instanceof Dot);
assert.equal(dot.id, 'a');
assert.equal(dot.counter, 1);
assert.equal(kernel.dataStorage.get('a:1'), 'val');
});
it('should not return delta kernel', () => {
const kernel = new DotKernel();
const result = kernel.dotAdd('a', 'val');
assert.ok(!(result instanceof DotKernel));
assert.ok(result instanceof Dot);
});
it('should increment counter like add()', () => {
const kernel = new DotKernel();
const dot1 = kernel.dotAdd('a', 'v1');
const dot2 = kernel.dotAdd('a', 'v2');
assert.equal(dot1.counter, 1);
assert.equal(dot2.counter, 2);
});
});
describe('rmv()', () => {
describe('Remove by value', () => {
it('should remove all matching dots', () => {
const kernel = createKernel({
'a:1': 'x',
'b:1': 'x',
'c:1': 'y',
});
kernel.rmv('x');
assert.ok(!kernel.dataStorage.has('a:1'));
assert.ok(!kernel.dataStorage.has('b:1'));
assert.ok(kernel.dataStorage.has('c:1'));
});
it('should return delta with removed dots in context', () => {
const kernel = createKernel({ 'a:1': 'x' });
const delta = kernel.rmv('x');
assert.equal(delta.sharedContext.dotIn({ id: 'a', counter: 1 }), true);
assert.equal(delta.dataStorage.size, 0);
});
it('should not remove if value not found', () => {
const kernel = createKernel({ 'a:1': 'x' });
const delta = kernel.rmv('y');
assert.ok(kernel.dataStorage.has('a:1'));
assert.equal(delta.dataStorage.size, 0);
});
it('should handle complex value comparison', () => {
const obj = { key: 'value' };
const kernel = createKernel({ 'a:1': obj });
kernel.rmv({ key: 'value' });
assert.ok(!kernel.dataStorage.has('a:1'));
});
});
describe('Remove by dot', () => {
it('should remove specific dot', () => {
const kernel = createKernel({ 'a:1': 'x', 'a:2': 'y' });
kernel.rmv({ id: 'a', counter: 1 });
assert.ok(!kernel.dataStorage.has('a:1'));
assert.ok(kernel.dataStorage.has('a:2'));
});
it('should handle not found gracefully', () => {
const kernel = createKernel({ 'a:1': 'x' });
const delta = kernel.rmv({ id: 'b', counter: 1 });
assert.ok(kernel.dataStorage.has('a:1'));
assert.equal(delta.dataStorage.size, 0);
});
it('should remove by dot-like object with string keys', () => {
const kernel = createKernel({ 'a:1': 'x', 'a:2': 'y' });
kernel.rmv({ id: 'a', counter: 1 });
assert.ok(!kernel.dataStorage.has('a:1'));
assert.ok(kernel.dataStorage.has('a:2'));
});
it('should accept Dot instance', () => {
const kernel = createKernel({ 'a:1': 'x', 'a:2': 'y' });
const dot = new Dot('a', 1);
kernel.rmv(dot);
assert.ok(!kernel.dataStorage.has('a:1'));
assert.ok(kernel.dataStorage.has('a:2'));
});
});
describe('Remove all', () => {
it('should remove all dots', () => {
const kernel = createKernel({ 'a:1': 'x', 'b:1': 'y', 'a:2': 'z' });
kernel.rmv();
assert.equal(kernel.dataStorage.size, 0);
});
it('should remember all removed dots in delta', () => {
const kernel = createKernel({ 'a:1': 'x', 'b:1': 'y' });
const delta = kernel.rmv();
assert.equal(delta.sharedContext.dotIn({ id: 'a', counter: 1 }), true);
assert.equal(delta.sharedContext.dotIn({ id: 'b', counter: 1 }), true);
});
it('should handle empty kernel', () => {
const kernel = new DotKernel();
const delta = kernel.rmv();
assert.equal(kernel.dataStorage.size, 0);
assert.equal(delta.dataStorage.size, 0);
});
});
});
describe('join()', () => {
it('should merge disjoint dot sets', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = createKernel({ 'b:1': 'y' });
k1.join(k2);
assert.ok(k1.dataStorage.has('a:1'));
assert.ok(k1.dataStorage.has('b:1'));
assert.equal(k1.dataStorage.get('a:1'), 'x');
assert.equal(k1.dataStorage.get('b:1'), 'y');
});
it('should keep dots present in both kernels', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = createKernel({ 'a:1': 'x' });
k1.join(k2);
assert.ok(k1.dataStorage.has('a:1'));
assert.equal(k1.dataStorage.size, 1);
});
it('should remove dots known by other context (observed remove)', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = new DotKernel();
k2.sharedContext.insertDot({ id: 'a', counter: 1 });
k1.join(k2);
assert.ok(!k1.dataStorage.has('a:1'));
});
it('should add dots from other not in this context', () => {
const k1 = new DotKernel();
const k2 = createKernel({ 'b:5': 'new' });
k1.join(k2);
assert.ok(k1.dataStorage.has('b:5'));
assert.equal(k1.dataStorage.get('b:5'), 'new');
});
it('should not import dots this context already knows', () => {
const k1 = new DotKernel();
k1.sharedContext.insertDot({ id: 'a', counter: 3 });
const k2 = createKernel({ 'a:3': 'x' });
k1.join(k2);
assert.ok(!k1.dataStorage.has('a:3'));
});
it('should join contexts', () => {
const k1 = new DotKernel();
k1.sharedContext.compactCausalContext.set('a', 3);
const k2 = new DotKernel();
k2.sharedContext.compactCausalContext.set('a', 5);
k1.join(k2);
assert.equal(k1.sharedContext.compactCausalContext.get('a'), 5);
});
it('should be idempotent', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = createKernel({ 'b:1': 'y' });
k1.join(k2);
const size1 = k1.dataStorage.size;
k1.join(k2);
const size2 = k1.dataStorage.size;
assert.equal(size1, size2);
});
it('should handle self-join gracefully', () => {
const k1 = createKernel({ 'a:1': 'x' });
const before = k1.dataStorage.size;
k1.join(k1);
assert.equal(k1.dataStorage.size, before);
});
it('should use sorted iteration', () => {
const k1 = createKernel({ 'b:1': 'y', 'a:1': 'x' });
const k2 = createKernel({ 'c:1': 'z' });
k1.join(k2);
assert.equal(k1.dataStorage.size, 3);
});
it('should handle complex concurrent scenario', () => {
const k1 = new DotKernel();
k1.add('a', 'v1');
k1.add('a', 'v2');
const k2 = new DotKernel();
k2.add('a', 'v3');
k1.join(k2);
// k1 has (a:1) and (a:2) with CC[a]=2
// k2 has (a:1) with CC[a]=1
// When joining, k2's (a:1) is already known by k1's context
assert.ok(k1.dataStorage.has('a:1'));
assert.ok(k1.dataStorage.has('a:2'));
// k2's (a:1) is not imported because k1 already knows about (a:1)
assert.equal(k1.dataStorage.size, 2);
});
it('should handle complex concurrent scenarios with deltas', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = createKernel({ 'a:1': 'y' });
const delta1 = k1.add('a', 'v1');
const delta2 = k1.add('a', 'v2');
k2.join(delta1);
k2.join(delta2);
assert.equal(k2.dataStorage.size, 3);
assert.equal(k2.dataStorage.get('a:1'), 'y');
assert.equal(k2.dataStorage.get('a:2'), 'v1');
assert.equal(k2.dataStorage.get('a:3'), 'v2');
});
});
describe('deepJoin()', () => {
it('should merge values for dots in both kernels', () => {
const k1 = createKernel({ 'a:1': 5 });
const k2 = createKernel({ 'a:1': 3 });
k1.deepJoin(k2);
assert.ok(k1.dataStorage.has('a:1'));
assert.equal(k1.dataStorage.get('a:1'), 5);
});
it('should only join when values differ', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = createKernel({ 'a:1': 'x' });
k1.deepJoin(k2);
assert.equal(k1.dataStorage.get('a:1'), 'x');
});
it('should behave like join() for non-overlapping dots', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = createKernel({ 'b:1': 'y' });
k1.deepJoin(k2);
assert.ok(k1.dataStorage.has('a:1'));
assert.ok(k1.dataStorage.has('b:1'));
});
it('should handle nested CRDTs with join method', () => {
const crdt1 = {
value: 5,
join(other) {
this.value = Math.max(this.value, other.value);
},
};
const crdt2 = {
value: 8,
join(other) {
this.value = Math.max(this.value, other.value);
},
};
const k1 = createKernel({ 'a:1': crdt1 });
const k2 = createKernel({ 'a:1': crdt2 });
k1.deepJoin(k2);
assert.ok(k1.dataStorage.has('a:1'));
});
// eslint-disable-next-line max-len
it('should remove dots known by other context, and removed in other', () => {
const k1 = createKernel({ 'a:1': 'x' });
const k2 = new DotKernel();
k2.sharedContext.insertDot({ id: 'a', counter: 1 });
k1.deepJoin(k2);
assert.ok(!k1.dataStorage.has('a:1'));
});
it('should not import dots this context already knows', () => {
const k1 = new DotKernel();
k1.sharedContext.insertDot({ id: 'a', counter: 3 });
const k2 = createKernel({ 'a:3': 'x' });
k1.deepJoin(k2);
assert.ok(!k1.dataStorage.has('a:3'));
});
it('should handle self-deepJoin gracefully', () => {
const k1 = createKernel({ 'a:1': 'x' });
const before = k1.dataStorage.size;
k1.deepJoin(k1);
assert.equal(k1.dataStorage.size, before);
});
});
describe('clone()', () => {
it('should create independent copy', () => {
const kernel = createKernel({ 'a:1': 'x' });
const copy = kernel.clone();
copy.dataStorage.set('b:1', 'y');
assert.ok(!kernel.dataStorage.has('b:1'));
assert.ok(copy.dataStorage.has('b:1'));
});
it('should clone with local context', () => {
const kernel = new DotKernel();
kernel.add('a', 'x');
const copy = kernel.clone();
assert.ok(copy.sharedContext !== kernel.sharedContext);
assert.equal(
copy.sharedContext.compactCausalContext.get('a'),
kernel.sharedContext.compactCausalContext.get('a'),
);
});
it('should preserve shared context reference', () => {
const sharedCtx = new DotContext();
const kernel = new DotKernel(sharedCtx);
kernel.add('a', 'x');
const copy = kernel.clone();
assert.ok(copy.sharedContext === kernel.sharedContext);
assert.ok(copy.sharedContext === sharedCtx);
});
it('should clone empty kernel', () => {
const kernel = new DotKernel();
const copy = kernel.clone();
assert.equal(copy.dataStorage.size, 0);
});
});
describe('toString()', () => {
it('should show kernel state', () => {
const kernel = createKernel({ 'a:1': 'x' });
const str = kernel.toString();
assert.ok(str.includes('Kernel'));
assert.ok(str.includes('a:1'));
assert.ok(str.includes('x'));
});
it('should show empty kernel', () => {
const kernel = new DotKernel();
const str = kernel.toString();
assert.ok(str.includes('Kernel'));
});
it('should show context information', () => {
const kernel = new DotKernel();
kernel.add('a', 'x');
const str = kernel.toString();
assert.ok(str.includes('Context'));
});
});
});