Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182import { test } from 'node:test';import assert from 'node:assert/strict';import { readFileSync } from 'node:fs';import { join, dirname } from 'node:path';import { fileURLToPath } from 'node:url';import { PageEditor, blankPage, makeLine, LAYER_ID, readBlocks, readTree, writeBlocks, rmToSvg, pagePaint, lineSegments, SceneLineItemBlock, SceneTombstoneItemBlock, AuthorIdsBlock, Pen, PenColor, HEADER_V6,} from '../lib/index.js';import { pageIndexAfter, pageIndexBetween, swapUuidEndianness, registerAuthors, contentAuthors, notebookContent, findPage } from '../lib/cloud/documents.js';
const here = dirname(fileURLToPath(import.meta.url));const FIXTURE = join(here, 'data', 'notebook', 'f63a4691-25eb-43c5-95b0-a20c2dbeb80b', '0fa00eed-eb69-415e-a0dc-ba685f00e566.rm');const AUTHOR = '7a1a9c7e-1c5e-4d8e-9f0a-2b3c4d5e6f70';
const stroke = (dx = 0) => makeLine({ tool: Pen.FINELINER_2, color: PenColor.BLUE, thickness: 2, points: Array.from({ length: 12 }, (_, i) => ({ x: -100 + dx + i * 10, y: 300 + Math.sin(i) * 20, pressure: 120 + i, speed: i, width: 8 })),});
test('a blank page writes, reads back empty and renders', () => { const data = blankPage({ authorUuid: AUTHOR }); assert.ok(data.length > HEADER_V6.length); const editor = PageEditor.parse(data); assert.deepEqual([...editor.authors], [[1, AUTHOR]]); assert.equal(editor.layers.length, 1); assert.equal(editor.layers[0].key, LAYER_ID.toKey()); assert.equal(editor.layers[0].label, 'Layer 1'); assert.equal(editor.strokes.length, 0); assert.ok(editor.isEmpty); const svg = rmToSvg(data); assert.match(svg, /<svg /); assert.doesNotMatch(svg, /polyline/); // bytes are stable: writing the parsed page again gives the same file assert.deepEqual([...editor.write()], [...data]);});
test('strokes are appended to the layer with fresh ids and come back on read', () => { const editor = PageEditor.blank({ authorUuid: AUTHOR }); const k1 = editor.addLine(stroke(), { author: 1 }); const k2 = editor.addLine(stroke(50), { author: 1 }); assert.equal(k1, '1:2'); // 1:1 is the page's migration id assert.equal(k2, '1:3'); assert.equal(editor.strokes.length, 2); assert.equal(editor.strokes[0].layer, LAYER_ID.toKey()); const data = editor.write(); const again = PageEditor.parse(data); assert.deepEqual(again.strokes.map(s => s.key), [k1, k2]); const tree = readTree(data); const { items } = pagePaint(tree); const segs = items.filter(i => i.kind === 'segment'); assert.ok(segs.length >= 2); assert.deepEqual(new Set(segs.map(s => s.item)), new Set([k1, k2])); assert.ok(segs.every(s => s.layer === LAYER_ID.toKey())); // the live renderer paints the same thing as the page renderer const live = lineSegments(again.strokes[0].line); assert.deepEqual(live.map(s => s.points), segs.filter(s => s.item === k1).map(s => s.points)); assert.match(rmToSvg(data), /polyline/); // a third stroke continues the counter after a round trip assert.equal(again.addLine(stroke(100)), '1:4');});
test('dropping the latest stroke (an undo) leaves no trace of it', () => { const editor = PageEditor.blank({ authorUuid: AUTHOR }); const k1 = editor.addLine(stroke()); const k2 = editor.addLine(stroke(50)); assert.ok(editor.dropLine(k2)); assert.ok(!editor.dropLine(k2)); assert.deepEqual(editor.strokes.map(s => s.key), [k1]); const blocks = [...readBlocks(editor.write())]; assert.equal(blocks.filter(b => b instanceof SceneLineItemBlock).length, 1); assert.equal(blocks.filter(b => b instanceof SceneTombstoneItemBlock).length, 0); assert.equal(editor.addLine(stroke(100)), '1:4'); // ids are never reused});
test('ids continue after the ones a real page already holds', () => { const data = readFileSync(FIXTURE); const editor = PageEditor.parse(data); const before = editor.strokes.length; assert.equal(before, 23); const key = editor.addLine(stroke()); assert.equal(key, '1:38'); // 1:37 is the highest id the fixture holds for author 1 const again = PageEditor.parse(editor.write()); assert.equal(again.strokes.length, before + 1); assert.equal(again.strokes.at(-1).key, key);});
test('another author is registered in the page and gets its own counter', () => { const editor = PageEditor.parse(readFileSync(FIXTURE)); const index = editor.authorIndex(AUTHOR, { preferred: 2 }); assert.equal(index, 2); assert.equal(editor.authorIndex(AUTHOR), 2); const k = editor.addLine(stroke(), { author: index }); assert.equal(k, '2:1'); const again = PageEditor.parse(editor.write()); assert.equal(again.authors.get(2), AUTHOR); assert.equal(again.strokes.at(-1).key, '2:1'); // a preferred index that is taken falls through to the next free one const other = PageEditor.parse(readFileSync(FIXTURE)); assert.equal(other.authorIndex(AUTHOR, { preferred: 1 }), 2);});
test('erasing a stroke leaves a tombstone, which an undo turns back', () => { const editor = PageEditor.blank({ authorUuid: AUTHOR }); const k1 = editor.addLine(stroke()); const k2 = editor.addLine(stroke(50)); const line = editor.strokes[0].line; assert.ok(editor.removeStroke(k1)); assert.ok(!editor.removeStroke('9:9')); assert.deepEqual(editor.strokes.map(s => s.key), [k2]); const data = editor.write(); const blocks = [...readBlocks(data)]; const tomb = blocks.find(b => b instanceof SceneTombstoneItemBlock); assert.ok(tomb); assert.equal(tomb.item.itemId.toKey(), k1); assert.equal(tomb.item.deletedLength, 1); assert.equal(tomb.item.value, null); assert.equal(blocks.filter(b => b instanceof SceneLineItemBlock).length, 1); const again = PageEditor.parse(data); assert.deepEqual(again.strokes.map(s => s.key), [k2]); // the second stroke still sits after the (now gone) first one, and a new one after it assert.equal(again.addLine(stroke(100)), '1:4'); assert.deepEqual(again.strokes.map(s => s.key), [k2, '1:4']); assert.ok(again.restoreStroke(k1, line)); assert.deepEqual(PageEditor.parse(again.write()).strokes.map(s => s.key), [k1, k2, '1:4']);});
test('makeLine clamps point fields to what the format holds', () => { const line = makeLine({ tool: Pen.BALLPOINT_2, points: [{ x: 1.5, y: 2.5, pressure: 999, speed: -3, direction: 300, width: 70000 }] }); const [p] = line.points; assert.deepEqual([p.x, p.y, p.pressure, p.speed, p.direction, p.width], [1.5, 2.5, 255, 0, 255, 65535]); assert.equal(line.thicknessScale, 2); const data = writeBlocks([...PageEditor.blank().blocks]); assert.ok(data.length);});
test('page indexes sort between their neighbours', () => { assert.equal(pageIndexAfter(null), 'ba'); assert.equal(pageIndexAfter('ba'), 'bb'); assert.equal(pageIndexAfter('bz'), 'ca'); assert.equal(pageIndexAfter('zz'), 'zza'); const check = (a, b) => { const m = pageIndexBetween(a, b); assert.ok(m, `${a} < ? < ${b}`); if (a) assert.ok(a < m, `${a} < ${m}`); if (b) assert.ok(m < b, `${m} < ${b}`); return m; }; assert.equal(pageIndexBetween(null, null), 'ba'); check('ba', 'bb'); check('ba', 'bc'); check('baz', 'bb'); check(null, 'ba'); check(null, 'bb'); check('bb', null); let a = 'ba'; const b = 'bb'; for (let i = 0; i < 20; i++) a = check(a, b); // repeated inserts before the same page keep finding room assert.equal(pageIndexBetween('b', 'ba'), null); assert.equal(pageIndexBetween('bb', 'ba'), null);});
test('author UUIDs swap spelling between .rm and .content, and register once', () => { assert.equal(swapUuidEndianness('aad2621d-b1af-8457-874e-4ea9e2470491'), '1d62d2aa-afb1-5784-874e-4ea9e2470491'); assert.equal(swapUuidEndianness(swapUuidEndianness(AUTHOR)), AUTHOR); const content = notebookContent({ pageIds: ['p1'], authors: new Map([[1, 'aad2621d-b1af-8457-874e-4ea9e2470491']]) }); assert.deepEqual(content.cPages.uuids, [{ first: '1d62d2aa-afb1-5784-874e-4ea9e2470491', second: 1 }]); registerAuthors(content, new Map([[1, 'aad2621d-b1af-8457-874e-4ea9e2470491'], [2, AUTHOR]])); assert.equal(content.cPages.uuids.length, 2); assert.equal(content.cPages.uuids[1].second, 2); // a clashing index moves to a free one registerAuthors(content, new Map([[2, '00000000-0000-4000-8000-000000000000']])); assert.equal(content.cPages.uuids[2].second, 3); assert.deepEqual([...contentAuthors(content)][0], [1, 'aad2621d-b1af-8457-874e-4ea9e2470491']); assert.equal(findPage(content, 'p1').id, 'p1'); assert.equal(findPage(content, 'nope'), null); // an array of UUIDs still works, numbered from 1 assert.deepEqual(notebookContent({ pageIds: [], authors: [AUTHOR] }).cPages.uuids, [{ first: swapUuidEndianness(AUTHOR), second: 1 }]);});