-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(json-crdt-extensions): 🎸 improve Inline.key() implementation
- Loading branch information
Showing
5 changed files
with
142 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/json-crdt-extensions/peritext/block/__tests__/Inline.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import {Timestamp} from '../../../../json-crdt-patch'; | ||
import {updateId} from '../../../../json-crdt/hash'; | ||
import {updateNum} from '../../../../json-hash'; | ||
import {Kit, setupKit, setupNumbersKit, setupNumbersWithTombstonesKit} from '../../__tests__/setup'; | ||
import {Point} from '../../rga/Point'; | ||
import {Inline} from '../Inline'; | ||
|
||
describe('range hash', () => { | ||
test('computes unique hash - 1', () => { | ||
const {peritext} = setupKit(); | ||
const p1 = new Point(peritext.str, new Timestamp(12313123, 41), 0); | ||
const p2 = new Point(peritext.str, new Timestamp(12313123, 41), 1); | ||
const p3 = new Point(peritext.str, new Timestamp(12313123, 43), 0); | ||
const p4 = new Point(peritext.str, new Timestamp(12313123, 43), 1); | ||
const hash1 = updateNum(p1.refresh(), p2.refresh()); | ||
const hash2 = updateNum(p3.refresh(), p4.refresh()); | ||
expect(hash1).not.toBe(hash2); | ||
}); | ||
|
||
test('computes unique hash - 2', () => { | ||
const {peritext} = setupKit(); | ||
const p1 = new Point(peritext.str, new Timestamp(12313123, 61), 0); | ||
const p2 = new Point(peritext.str, new Timestamp(12313123, 23), 1); | ||
const p3 = new Point(peritext.str, new Timestamp(12313123, 60), 0); | ||
const p4 = new Point(peritext.str, new Timestamp(12313123, 56), 1); | ||
const hash1 = updateNum(p1.refresh(), p2.refresh()); | ||
const hash2 = updateNum(p3.refresh(), p4.refresh()); | ||
expect(hash1).not.toBe(hash2); | ||
}); | ||
|
||
test('computes unique hash - 3', () => { | ||
const {peritext} = setupKit(); | ||
const p1 = new Point(peritext.str, new Timestamp(12313123, 43), 0); | ||
const p2 = new Point(peritext.str, new Timestamp(12313123, 61), 1); | ||
const p3 = new Point(peritext.str, new Timestamp(12313123, 43), 0); | ||
const p4 = new Point(peritext.str, new Timestamp(12313123, 60), 1); | ||
const hash1 = updateNum(p1.refresh(), p2.refresh()); | ||
const hash2 = updateNum(p3.refresh(), p4.refresh()); | ||
expect(hash1).not.toBe(hash2); | ||
}); | ||
|
||
test('computes unique hash - 4', () => { | ||
const {peritext} = setupKit(); | ||
const hash1 = updateNum( | ||
updateId(0, new Timestamp(2, 7)), | ||
updateId(1, new Timestamp(2, 7)), | ||
); | ||
const hash2 = updateNum( | ||
updateId(0, new Timestamp(2, 6)), | ||
updateId(1, new Timestamp(2, 40)), | ||
); | ||
expect(hash1).not.toBe(hash2); | ||
}); | ||
}); | ||
|
||
const runPairsTests = (setup: () => Kit) => { | ||
describe('.key()', () => { | ||
test('construct unique keys for all ranges', () => { | ||
const {peritext} = setup(); | ||
const overlay = peritext.overlay; | ||
const length = peritext.strApi().length(); | ||
const keys = new Map<number | string, Inline>(); | ||
let cnt = 0; | ||
for (let i = 0; i < length; i++) { | ||
for (let j = 1; j <= length - i; j++) { | ||
peritext.editor.cursor.setAt(i, j); | ||
overlay.refresh(); | ||
const [start, end] = [...overlay.points()]; | ||
const inline = Inline.create(peritext, start, end); | ||
if (keys.has(inline.key())) { | ||
const inline2 = keys.get(inline.key())!; | ||
// tslint:disable-next-line:no-console | ||
console.error('DUPLICATE HASH:', inline.key()); | ||
// tslint:disable-next-line:no-console | ||
console.log('INLINE 1:', inline.start.id, inline.start.anchor, inline.end.id, inline.end.anchor); | ||
// tslint:disable-next-line:no-console | ||
console.log('INLINE 2:', inline2.start.id, inline2.start.anchor, inline2.end.id, inline2.end.anchor); | ||
throw new Error('Duplicate key'); | ||
} | ||
keys.set(inline.key(), inline); | ||
cnt++; | ||
} | ||
} | ||
expect(keys.size).toBe(cnt); | ||
}); | ||
}); | ||
}; | ||
|
||
describe('Inline', () => { | ||
describe('lorem ipsum', () => { | ||
runPairsTests(() => setupKit('lorem ipsum dolor sit amet consectetur adipiscing elit')); | ||
}); | ||
|
||
describe('numbers "0123456789", no edits', () => { | ||
runPairsTests(setupNumbersKit); | ||
}); | ||
|
||
describe('numbers "0123456789", with default schema and tombstones', () => { | ||
runPairsTests(setupNumbersWithTombstonesKit); | ||
}); | ||
|
||
describe('numbers "0123456789", with default schema and tombstones and constant sid', () => { | ||
runPairsTests(() => setupNumbersWithTombstonesKit(12313123)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters