|
| 1 | +import {LogicalClock} from '../clock'; |
| 2 | +import {combine} from '../compaction'; |
| 3 | +import {SESSION} from '../constants'; |
| 4 | +import {NopOp} from '../operations'; |
| 5 | +import {Patch} from '../Patch'; |
| 6 | +import {PatchBuilder} from '../PatchBuilder'; |
| 7 | + |
| 8 | +describe('.combine()', () => { |
| 9 | + test('can combine two adjacent patches', () => { |
| 10 | + const builder = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 11 | + const objId = builder.json({ |
| 12 | + str: 'hello', |
| 13 | + num: 123, |
| 14 | + tags: ['a', 'b', 'c'], |
| 15 | + }); |
| 16 | + builder.root(objId); |
| 17 | + const patch = builder.flush(); |
| 18 | + const str1 = patch + ''; |
| 19 | + for (let i = 1; i < patch.ops.length - 2; i++) { |
| 20 | + const ops1 = patch.ops.slice(0, i); |
| 21 | + const ops2 = patch.ops.slice(i); |
| 22 | + const patch1 = new Patch(); |
| 23 | + patch1.ops = patch1.ops.concat(ops1); |
| 24 | + const patch2 = new Patch(); |
| 25 | + patch2.ops = patch2.ops.concat(ops2); |
| 26 | + combine(patch1, patch2); |
| 27 | + const str2 = patch1 + ''; |
| 28 | + expect(str2).toBe(str1); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + test('can combine two patches with gap', () => { |
| 33 | + const builder1 = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 34 | + const builder2 = new PatchBuilder(new LogicalClock(123456789, 100)); |
| 35 | + builder1.str(); |
| 36 | + builder2.const(123); |
| 37 | + const patch1 = builder1.flush(); |
| 38 | + const patch2 = builder2.flush(); |
| 39 | + combine(patch1, patch2); |
| 40 | + expect(patch1.ops.length).toBe(3); |
| 41 | + expect(patch1.ops[1]).toBeInstanceOf(NopOp); |
| 42 | + const nop = patch1.ops[1] as NopOp; |
| 43 | + expect(nop.id.sid).toBe(123456789); |
| 44 | + expect(nop.id.time).toBe(2); |
| 45 | + expect(nop.len).toBe(98); |
| 46 | + }); |
| 47 | + |
| 48 | + test('throws on mismatching sessions', () => { |
| 49 | + const builder1 = new PatchBuilder(new LogicalClock(1111111, 1)); |
| 50 | + const builder2 = new PatchBuilder(new LogicalClock(2222222, 100)); |
| 51 | + builder1.str(); |
| 52 | + builder2.const(123); |
| 53 | + const patch1 = builder1.flush(); |
| 54 | + const patch2 = builder2.flush(); |
| 55 | + expect(() => combine(patch1, patch2)).toThrow(new Error('SID_MISMATCH')); |
| 56 | + }); |
| 57 | + |
| 58 | + test('first patch can be empty', () => { |
| 59 | + const builder2 = new PatchBuilder(new LogicalClock(2222222, 100)); |
| 60 | + builder2.const(123); |
| 61 | + const patch1 = new Patch(); |
| 62 | + const patch2 = builder2.flush(); |
| 63 | + combine(patch1, patch2); |
| 64 | + expect(patch1 + '').toBe(patch2 + ''); |
| 65 | + }); |
| 66 | + |
| 67 | + test('second patch can be empty', () => { |
| 68 | + const builder2 = new PatchBuilder(new LogicalClock(2222222, 100)); |
| 69 | + builder2.const(123); |
| 70 | + const patch1 = new Patch(); |
| 71 | + const patch2 = builder2.flush(); |
| 72 | + const str1 = patch2 + ''; |
| 73 | + combine(patch2, patch1); |
| 74 | + const str2 = patch2 + ''; |
| 75 | + expect(str2).toBe(str1); |
| 76 | + expect(patch1.getId()).toBe(undefined); |
| 77 | + }); |
| 78 | + |
| 79 | + test('throws if first patch has higher logical time', () => { |
| 80 | + const builder1 = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 81 | + const builder2 = new PatchBuilder(new LogicalClock(123456789, 100)); |
| 82 | + builder1.str(); |
| 83 | + builder2.const(123); |
| 84 | + const patch1 = builder1.flush(); |
| 85 | + const patch2 = builder2.flush(); |
| 86 | + expect(() => combine(patch2, patch1)).toThrow(new Error('TIMESTAMP_CONFLICT')); |
| 87 | + combine(patch1, patch2) |
| 88 | + }); |
| 89 | +}); |
0 commit comments