import { describe, expect, test } from "vitest"; import { Cursor } from "./cursor"; describe("Cursor Update After Insertion", () => { test("Insertion before cursor in same strand", () => { const loc = new Cursor([], 5); loc.updateAfterInsertion(new Cursor([], 4), 2); expect(loc).toEqual(new Cursor([], 7)); }); test("Insertion at cursor in same strand", () => { const loc = new Cursor([], 5); loc.updateAfterInsertion(new Cursor([], 5), 3); expect(loc).toEqual(new Cursor([], 8)); }); test("Insertion after cursor in same strand", () => { const loc = new Cursor([], 5); loc.updateAfterInsertion(new Cursor([], 6), 2); expect(loc).toEqual(new Cursor([], 5)); }); test("Insertion in parent strand before cursor", () => { const loc = new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ); loc.updateAfterInsertion( new Cursor([{ tokenIndex: 1, childIndex: 0 }], 2), 2 ); expect(loc).toEqual( new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 4, childIndex: 1 }, ], 4 ) ); }); test("Insertion in grandparent strand before cursor", () => { const loc = new Cursor( [ { tokenIndex: 2, childIndex: 0 }, { tokenIndex: 0, childIndex: 2 }, ], 3 ); loc.updateAfterInsertion(new Cursor([], 2), 1); expect(loc).toEqual( new Cursor( [ { tokenIndex: 3, childIndex: 0 }, { tokenIndex: 0, childIndex: 2 }, ], 3 ) ); }); test("Insertion in parent strand after cursor", () => { const loc = new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ); loc.updateAfterInsertion( new Cursor([{ tokenIndex: 1, childIndex: 0 }], 3), 2 ); expect(loc).toEqual( new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ) ); }); test("Insertion in grandparent strand after cursor", () => { const loc = new Cursor( [ { tokenIndex: 2, childIndex: 0 }, { tokenIndex: 0, childIndex: 2 }, ], 3 ); loc.updateAfterInsertion(new Cursor([], 3), 1); expect(loc).toEqual( new Cursor( [ { tokenIndex: 2, childIndex: 0 }, { tokenIndex: 0, childIndex: 2 }, ], 3 ) ); }); }); describe("Cursor Update After Token Deletion", () => { test("Same-strand deletion before token", () => { const loc = new Cursor([], 2); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 1 }); expect(loc).toEqual(new Cursor([], 1)); }); test("Same-strand deletion after token", () => { const loc = new Cursor([], 2); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 2 }); expect(loc).toEqual(new Cursor([], 2)); }); test("Parent-strand deletion before token", () => { const loc = new Cursor([{ tokenIndex: 2, childIndex: 0 }], 3); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 1 }); expect(loc).toEqual(new Cursor([{ tokenIndex: 1, childIndex: 0 }], 3)); }); test("Parent-strand deletion after token", () => { const loc = new Cursor([{ tokenIndex: 2, childIndex: 0 }], 3); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 3 }); expect(loc).toEqual(new Cursor([{ tokenIndex: 2, childIndex: 0 }], 3)); }); test("Parent-strand deletion of current token's parent", () => { const loc = new Cursor([{ tokenIndex: 2, childIndex: 0 }], 3); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 2 }); expect(loc).toEqual(new Cursor([], 2)); }); test("Grandparent-strand deletion before token", () => { const loc = new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 0 }); expect(loc).toEqual( new Cursor( [ { tokenIndex: 0, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ) ); }); test("Grandparent-strand deletion after token", () => { const loc = new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 2 }); expect(loc).toEqual( new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ) ); }); test("Grandparent-strand deletion of current token's grandparent", () => { const loc = new Cursor( [ { tokenIndex: 1, childIndex: 0 }, { tokenIndex: 2, childIndex: 1 }, ], 4 ); loc.updateAfterTokenDeletion({ strandPath: [], tokenIndex: 1 }); expect(loc).toEqual(new Cursor([], 1)); }); });