|
| 1 | +import {equal, LogicalClock, tick, ts} from '../clock'; |
| 2 | +import {combine, compact} from '../compaction'; |
| 3 | +import {InsStrOp, NopOp} from '../operations'; |
| 4 | +import {Patch} from '../Patch'; |
| 5 | +import {PatchBuilder} from '../PatchBuilder'; |
| 6 | + |
| 7 | +describe('.combine()', () => { |
| 8 | + test('can combine two adjacent patches', () => { |
| 9 | + const builder = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 10 | + const objId = builder.json({ |
| 11 | + str: 'hello', |
| 12 | + num: 123, |
| 13 | + tags: ['a', 'b', 'c'], |
| 14 | + }); |
| 15 | + builder.root(objId); |
| 16 | + const patch = builder.flush(); |
| 17 | + const str1 = patch + ''; |
| 18 | + for (let i = 1; i < patch.ops.length - 2; i++) { |
| 19 | + const ops1 = patch.ops.slice(0, i); |
| 20 | + const ops2 = patch.ops.slice(i); |
| 21 | + const patch1 = new Patch(); |
| 22 | + patch1.ops = patch1.ops.concat(ops1); |
| 23 | + const patch2 = new Patch(); |
| 24 | + patch2.ops = patch2.ops.concat(ops2); |
| 25 | + combine([patch1, patch2]); |
| 26 | + const str2 = patch1 + ''; |
| 27 | + expect(str2).toBe(str1); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + test('can combine three adjacent patches', () => { |
| 32 | + const builder = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 33 | + const objId = builder.json({ |
| 34 | + str: 'hello', |
| 35 | + num: 123, |
| 36 | + tags: ['a', 'b', 'c'], |
| 37 | + }); |
| 38 | + builder.root(objId); |
| 39 | + const patch = builder.flush(); |
| 40 | + const str1 = patch + ''; |
| 41 | + const ops1 = patch.ops.slice(0, 2); |
| 42 | + const ops2 = patch.ops.slice(2, 4); |
| 43 | + const ops3 = patch.ops.slice(4); |
| 44 | + const patch1 = new Patch(); |
| 45 | + patch1.ops = patch1.ops.concat(ops1); |
| 46 | + const patch2 = new Patch(); |
| 47 | + patch2.ops = patch2.ops.concat(ops2); |
| 48 | + const patch3 = new Patch(); |
| 49 | + patch3.ops = patch3.ops.concat(ops3); |
| 50 | + combine([patch1, patch2, patch3]); |
| 51 | + const str2 = patch1 + ''; |
| 52 | + expect(str2).toBe(str1); |
| 53 | + }); |
| 54 | + |
| 55 | + test('can combine two patches with gap', () => { |
| 56 | + const builder1 = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 57 | + const builder2 = new PatchBuilder(new LogicalClock(123456789, 100)); |
| 58 | + builder1.str(); |
| 59 | + builder2.const(123); |
| 60 | + const patch1 = builder1.flush(); |
| 61 | + const patch2 = builder2.flush(); |
| 62 | + combine([patch1, patch2]); |
| 63 | + expect(patch1.ops.length).toBe(3); |
| 64 | + expect(patch1.ops[1]).toBeInstanceOf(NopOp); |
| 65 | + const nop = patch1.ops[1] as NopOp; |
| 66 | + expect(nop.id.sid).toBe(123456789); |
| 67 | + expect(nop.id.time).toBe(2); |
| 68 | + expect(nop.len).toBe(98); |
| 69 | + }); |
| 70 | + |
| 71 | + test('can combine four patches with gap', () => { |
| 72 | + const builder1 = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 73 | + const builder2 = new PatchBuilder(new LogicalClock(123456789, 100)); |
| 74 | + const builder3 = new PatchBuilder(new LogicalClock(123456789, 110)); |
| 75 | + const builder4 = new PatchBuilder(new LogicalClock(123456789, 220)); |
| 76 | + builder1.str(); |
| 77 | + builder2.const(123); |
| 78 | + builder3.obj(); |
| 79 | + builder4.bin(); |
| 80 | + const patch1 = builder1.flush(); |
| 81 | + const patch2 = builder2.flush(); |
| 82 | + const patch3 = builder3.flush(); |
| 83 | + const patch4 = builder4.flush(); |
| 84 | + combine([patch1, patch2, patch3, patch4]); |
| 85 | + expect(patch1.ops.length).toBe(7); |
| 86 | + expect(patch1.ops[1]).toBeInstanceOf(NopOp); |
| 87 | + expect(patch1.ops[3]).toBeInstanceOf(NopOp); |
| 88 | + expect(patch1.ops[5]).toBeInstanceOf(NopOp); |
| 89 | + }); |
| 90 | + |
| 91 | + test('throws on mismatching sessions', () => { |
| 92 | + const builder1 = new PatchBuilder(new LogicalClock(1111111, 1)); |
| 93 | + const builder2 = new PatchBuilder(new LogicalClock(2222222, 100)); |
| 94 | + builder1.str(); |
| 95 | + builder2.const(123); |
| 96 | + const patch1 = builder1.flush(); |
| 97 | + const patch2 = builder2.flush(); |
| 98 | + expect(() => combine([patch1, patch2])).toThrow(new Error('SID_MISMATCH')); |
| 99 | + }); |
| 100 | + |
| 101 | + test('first patch can be empty', () => { |
| 102 | + const builder2 = new PatchBuilder(new LogicalClock(2222222, 100)); |
| 103 | + builder2.const(123); |
| 104 | + const patch1 = new Patch(); |
| 105 | + const patch2 = builder2.flush(); |
| 106 | + combine([patch1, patch2]); |
| 107 | + expect(patch1 + '').toBe(patch2 + ''); |
| 108 | + }); |
| 109 | + |
| 110 | + test('second patch can be empty', () => { |
| 111 | + const builder2 = new PatchBuilder(new LogicalClock(2222222, 100)); |
| 112 | + builder2.const(123); |
| 113 | + const patch1 = new Patch(); |
| 114 | + const patch2 = builder2.flush(); |
| 115 | + const str1 = patch2 + ''; |
| 116 | + combine([patch2, patch1]); |
| 117 | + const str2 = patch2 + ''; |
| 118 | + expect(str2).toBe(str1); |
| 119 | + expect(patch1.getId()).toBe(undefined); |
| 120 | + }); |
| 121 | + |
| 122 | + test('throws if first patch has higher logical time', () => { |
| 123 | + const builder1 = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 124 | + const builder2 = new PatchBuilder(new LogicalClock(123456789, 100)); |
| 125 | + builder1.str(); |
| 126 | + builder2.const(123); |
| 127 | + const patch1 = builder1.flush(); |
| 128 | + const patch2 = builder2.flush(); |
| 129 | + expect(() => combine([patch2, patch1])).toThrow(new Error('TIMESTAMP_CONFLICT')); |
| 130 | + combine([patch1, patch2]); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('.compact()', () => { |
| 135 | + test('can combine two consecutive string inserts', () => { |
| 136 | + const builder = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 137 | + const strId = builder.str(); |
| 138 | + const ins1Id = builder.insStr(strId, strId, 'hello'); |
| 139 | + builder.insStr(strId, tick(ins1Id, 'hello'.length - 1), ' world'); |
| 140 | + builder.root(strId); |
| 141 | + const patch = builder.flush(); |
| 142 | + const patch2 = patch.clone(); |
| 143 | + compact(patch); |
| 144 | + expect(equal(patch.ops[0].id, patch2.ops[0].id)).toBe(true); |
| 145 | + expect(equal(patch.ops[1].id, patch2.ops[1].id)).toBe(true); |
| 146 | + expect(equal(patch.ops[2].id, patch2.ops[3].id)).toBe(true); |
| 147 | + expect((patch.ops[1] as any).data).toBe('hello world'); |
| 148 | + }); |
| 149 | + |
| 150 | + test('can combine two consecutive string inserts - 2', () => { |
| 151 | + const builder = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 152 | + const strId = builder.str(); |
| 153 | + const ins1Id = builder.insStr(strId, strId, 'a'); |
| 154 | + builder.insStr(strId, tick(ins1Id, 'a'.length - 1), 'b'); |
| 155 | + builder.root(strId); |
| 156 | + const patch = builder.flush(); |
| 157 | + const patch2 = patch.clone(); |
| 158 | + compact(patch); |
| 159 | + expect(equal(patch.ops[0].id, patch2.ops[0].id)).toBe(true); |
| 160 | + expect(equal(patch.ops[1].id, patch2.ops[1].id)).toBe(true); |
| 161 | + expect(equal(patch.ops[2].id, patch2.ops[3].id)).toBe(true); |
| 162 | + expect((patch.ops[1] as any).data).toBe('ab'); |
| 163 | + }); |
| 164 | + |
| 165 | + test('can combine two consecutive string inserts - 3', () => { |
| 166 | + const patch = new Patch(); |
| 167 | + patch.ops.push(new InsStrOp(ts(123, 30), ts(123, 10), ts(123, 20), 'a')); |
| 168 | + patch.ops.push(new InsStrOp(ts(123, 31), ts(123, 10), ts(123, 30), 'b')); |
| 169 | + compact(patch); |
| 170 | + expect(patch.ops.length).toBe(1); |
| 171 | + expect((patch.ops[0] as InsStrOp).data).toBe('ab'); |
| 172 | + }); |
| 173 | + |
| 174 | + test('does not combine inserts, if they happen into different strings', () => { |
| 175 | + const patch = new Patch(); |
| 176 | + patch.ops.push(new InsStrOp(ts(123, 30), ts(123, 10), ts(123, 20), 'a')); |
| 177 | + patch.ops.push(new InsStrOp(ts(123, 31), ts(123, 99), ts(123, 30), 'b')); |
| 178 | + compact(patch); |
| 179 | + expect(patch.ops.length).toBe(2); |
| 180 | + }); |
| 181 | + |
| 182 | + test('does not combine inserts, if time is not consecutive', () => { |
| 183 | + const patch = new Patch(); |
| 184 | + patch.ops.push(new InsStrOp(ts(123, 30), ts(123, 10), ts(123, 20), 'a')); |
| 185 | + patch.ops.push(new InsStrOp(ts(123, 99), ts(123, 10), ts(123, 30), 'b')); |
| 186 | + compact(patch); |
| 187 | + expect(patch.ops.length).toBe(2); |
| 188 | + }); |
| 189 | + |
| 190 | + test('does not combine inserts, if the second operation is not an append', () => { |
| 191 | + const patch = new Patch(); |
| 192 | + patch.ops.push(new InsStrOp(ts(123, 30), ts(123, 10), ts(123, 20), 'a')); |
| 193 | + patch.ops.push(new InsStrOp(ts(123, 31), ts(123, 10), ts(123, 22), 'b')); |
| 194 | + compact(patch); |
| 195 | + expect(patch.ops.length).toBe(2); |
| 196 | + }); |
| 197 | + |
| 198 | + test('does not combine inserts, if the second operation is not an append - 2', () => { |
| 199 | + const patch = new Patch(); |
| 200 | + patch.ops.push(new InsStrOp(ts(123, 30), ts(123, 10), ts(123, 20), 'a')); |
| 201 | + patch.ops.push(new InsStrOp(ts(123, 31), ts(123, 10), ts(999, 30), 'b')); |
| 202 | + compact(patch); |
| 203 | + expect(patch.ops.length).toBe(2); |
| 204 | + }); |
| 205 | + |
| 206 | + test('returns a patch as-is', () => { |
| 207 | + const builder = new PatchBuilder(new LogicalClock(123456789, 1)); |
| 208 | + builder.root(builder.json({str: 'hello'})); |
| 209 | + const patch = builder.flush(); |
| 210 | + const str1 = patch + ''; |
| 211 | + compact(patch); |
| 212 | + const str2 = patch + ''; |
| 213 | + expect(str2).toBe(str1); |
| 214 | + }); |
| 215 | +}); |
0 commit comments