|
| 1 | +import {Timestamp} from '../../../../json-crdt-patch'; |
| 2 | +import {updateId} from '../../../../json-crdt/hash'; |
| 3 | +import {updateNum} from '../../../../json-hash'; |
| 4 | +import {Kit, setupKit, setupNumbersKit, setupNumbersWithTombstonesKit} from '../../__tests__/setup'; |
| 5 | +import {Point} from '../../rga/Point'; |
| 6 | +import {Inline} from '../Inline'; |
| 7 | + |
| 8 | +describe('range hash', () => { |
| 9 | + test('computes unique hash - 1', () => { |
| 10 | + const {peritext} = setupKit(); |
| 11 | + const p1 = new Point(peritext.str, new Timestamp(12313123, 41), 0); |
| 12 | + const p2 = new Point(peritext.str, new Timestamp(12313123, 41), 1); |
| 13 | + const p3 = new Point(peritext.str, new Timestamp(12313123, 43), 0); |
| 14 | + const p4 = new Point(peritext.str, new Timestamp(12313123, 43), 1); |
| 15 | + const hash1 = updateNum(p1.refresh(), p2.refresh()); |
| 16 | + const hash2 = updateNum(p3.refresh(), p4.refresh()); |
| 17 | + expect(hash1).not.toBe(hash2); |
| 18 | + }); |
| 19 | + |
| 20 | + test('computes unique hash - 2', () => { |
| 21 | + const {peritext} = setupKit(); |
| 22 | + const p1 = new Point(peritext.str, new Timestamp(12313123, 61), 0); |
| 23 | + const p2 = new Point(peritext.str, new Timestamp(12313123, 23), 1); |
| 24 | + const p3 = new Point(peritext.str, new Timestamp(12313123, 60), 0); |
| 25 | + const p4 = new Point(peritext.str, new Timestamp(12313123, 56), 1); |
| 26 | + const hash1 = updateNum(p1.refresh(), p2.refresh()); |
| 27 | + const hash2 = updateNum(p3.refresh(), p4.refresh()); |
| 28 | + expect(hash1).not.toBe(hash2); |
| 29 | + }); |
| 30 | + |
| 31 | + test('computes unique hash - 3', () => { |
| 32 | + const {peritext} = setupKit(); |
| 33 | + const p1 = new Point(peritext.str, new Timestamp(12313123, 43), 0); |
| 34 | + const p2 = new Point(peritext.str, new Timestamp(12313123, 61), 1); |
| 35 | + const p3 = new Point(peritext.str, new Timestamp(12313123, 43), 0); |
| 36 | + const p4 = new Point(peritext.str, new Timestamp(12313123, 60), 1); |
| 37 | + const hash1 = updateNum(p1.refresh(), p2.refresh()); |
| 38 | + const hash2 = updateNum(p3.refresh(), p4.refresh()); |
| 39 | + expect(hash1).not.toBe(hash2); |
| 40 | + }); |
| 41 | + |
| 42 | + test('computes unique hash - 4', () => { |
| 43 | + const {peritext} = setupKit(); |
| 44 | + const hash1 = updateNum( |
| 45 | + updateId(0, new Timestamp(2, 7)), |
| 46 | + updateId(1, new Timestamp(2, 7)), |
| 47 | + ); |
| 48 | + const hash2 = updateNum( |
| 49 | + updateId(0, new Timestamp(2, 6)), |
| 50 | + updateId(1, new Timestamp(2, 40)), |
| 51 | + ); |
| 52 | + expect(hash1).not.toBe(hash2); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +const runPairsTests = (setup: () => Kit) => { |
| 57 | + describe('.key()', () => { |
| 58 | + test('construct unique keys for all ranges', () => { |
| 59 | + const {peritext} = setup(); |
| 60 | + const overlay = peritext.overlay; |
| 61 | + const length = peritext.strApi().length(); |
| 62 | + const keys = new Map<number | string, Inline>(); |
| 63 | + let cnt = 0; |
| 64 | + for (let i = 0; i < length; i++) { |
| 65 | + for (let j = 1; j <= length - i; j++) { |
| 66 | + peritext.editor.cursor.setAt(i, j); |
| 67 | + overlay.refresh(); |
| 68 | + const [start, end] = [...overlay.points()]; |
| 69 | + const inline = Inline.create(peritext, start, end); |
| 70 | + if (keys.has(inline.key())) { |
| 71 | + const inline2 = keys.get(inline.key())!; |
| 72 | + // tslint:disable-next-line:no-console |
| 73 | + console.error('DUPLICATE HASH:', inline.key()); |
| 74 | + // tslint:disable-next-line:no-console |
| 75 | + console.log('INLINE 1:', inline.start.id, inline.start.anchor, inline.end.id, inline.end.anchor); |
| 76 | + // tslint:disable-next-line:no-console |
| 77 | + console.log('INLINE 2:', inline2.start.id, inline2.start.anchor, inline2.end.id, inline2.end.anchor); |
| 78 | + throw new Error('Duplicate key'); |
| 79 | + } |
| 80 | + keys.set(inline.key(), inline); |
| 81 | + cnt++; |
| 82 | + } |
| 83 | + } |
| 84 | + expect(keys.size).toBe(cnt); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}; |
| 88 | + |
| 89 | +describe('Inline', () => { |
| 90 | + describe('lorem ipsum', () => { |
| 91 | + runPairsTests(() => setupKit('lorem ipsum dolor sit amet consectetur adipiscing elit')); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('numbers "0123456789", no edits', () => { |
| 95 | + runPairsTests(setupNumbersKit); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('numbers "0123456789", with default schema and tombstones', () => { |
| 99 | + runPairsTests(setupNumbersWithTombstonesKit); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('numbers "0123456789", with default schema and tombstones and constant sid', () => { |
| 103 | + runPairsTests(() => setupNumbersWithTombstonesKit(12313123)); |
| 104 | + }); |
| 105 | +}); |
0 commit comments