Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145import { 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 { readBlocks, writeBlocks, readTree, UnreadableBlock, SceneItemBlock, SceneLineItemBlock, SceneInfo, RootTextBlock } from '../lib/scene-stream.js';import { Pen, PenByValue, PenColorByValue, Line, ParagraphStyle, ColumnWidth } from '../lib/scene-items.js';import { StrokePen } from '../lib/exporters/writing-tools.js';
const __dirname = dirname(fileURLToPath(import.meta.url));const NOTEBOOK_ID = 'f63a4691-25eb-43c5-95b0-a20c2dbeb80b';const NOTEBOOK_PATH = join(__dirname, 'data', 'notebook');
const content = JSON.parse(readFileSync(join(NOTEBOOK_PATH, `${NOTEBOOK_ID}.content`), 'utf8'));const pages = content.cPages.pages.map(p => p.id);
function loadPage (id) { return readFileSync(join(NOTEBOOK_PATH, NOTEBOOK_ID, `${id}.rm`));}
function hexLines (buf, n = 32) { const lines = []; for (let i = 0; i < buf.length; i += n) lines.push(buf.slice(i, i + n).toString('hex')); return lines;}
test('notebook content lists all pages', () => { assert.equal(pages.length, content.pageCount);});
for (const [i, id] of pages.entries()) { test(`page ${i + 1} (${id}) parses fully`, () => { const data = loadPage(id); const blocks = [...readBlocks(data)]; assert.ok(blocks.length > 0, 'No blocks read'); for (const block of blocks) { assert.ok(!(block instanceof UnreadableBlock), `Block was unreadable: ${block.error}`); assert.equal(block.extraData.length, 0, `${block.constructor.name} has extra data: ${block.extraData.toString('hex')}`); if (block instanceof SceneItemBlock) { assert.equal(block.extraValueData.length, 0, `${block.constructor.name} has extra value data: ${block.extraValueData.toString('hex')}`); } } });
test(`page ${i + 1} (${id}) roundtrips byte-for-byte`, () => { const data = loadPage(id); const output = writeBlocks(readBlocks(data), { version: '3.15' }); assert.deepStrictEqual(hexLines(output), hexLines(data)); });
test(`page ${i + 1} (${id}) builds a scene tree`, () => { const tree = readTree(loadPage(id)); assert.ok(tree.root, 'Tree has no root'); });
test(`page ${i + 1} (${id}) uses only known pens and colors`, () => { const data = loadPage(id); const lines = [...readBlocks(data)] .filter(b => b instanceof SceneLineItemBlock && b.item.value instanceof Line) .map(b => b.item.value); for (const line of lines) { assert.ok(PenByValue[line.tool] != null, `Unknown pen tool id: ${line.tool}`); if (line.colorRgba == null) { assert.ok(PenColorByValue[line.color] != null, `Unknown pen color id: ${line.color}`); } assert.doesNotThrow( () => StrokePen.create(line.tool, [0, 0, 0, 255], line.thicknessScale), `StrokePen.create failed for tool id ${line.tool} (${PenByValue[line.tool] ?? 'unknown'})` ); } });}
test('notebook exercises the expected set of pens', () => { const tools = new Set(); for (const id of pages) { for (const b of readBlocks(loadPage(id))) { if (b instanceof SceneLineItemBlock && b.item.value instanceof Line) tools.add(b.item.value.tool); } } assert.deepStrictEqual( [...tools].sort((a, b) => a - b), [ Pen.PAINTBRUSH_2, Pen.MECHANICAL_PENCIL_2, Pen.PENCIL_2, Pen.BALLPOINT_2, Pen.MARKER_2, Pen.FINELINER_2, Pen.HIGHLIGHTER_2, Pen.CALIGRAPHY, Pen.SHADER, ].sort((a, b) => a - b) );});
function sceneInfoOf (id) { return [...readBlocks(loadPage(id))].find(b => b instanceof SceneInfo);}function rootTextOf (id) { return [...readBlocks(loadPage(id))].find(b => b instanceof RootTextBlock);}
test('SceneInfo new fields parse on every page', () => { for (const id of pages) { const info = sceneInfoOf(id); assert.ok(info, `page ${id} has no SceneInfo`); assert.deepStrictEqual(info.paperSize, [1404, 1872]); assert.deepStrictEqual(info.paperSizeFloat, [1404, 1872]); assert.deepStrictEqual(info.paperSizeFloatLww.value, [1404, 1872]); assert.equal(info.customZoom.value.length, 32); assert.ok(info.columnWidth != null, `page ${id} has no columnWidth`); }});
test('column width narrow/default/wide round-trips through SceneInfo and RootTextBlock', () => { const expectations = [ // [page index, column width mode, text width] [9, ColumnWidth.DEFAULT, 936], [10, ColumnWidth.NARROW, 740], [11, ColumnWidth.WIDE, 1152], ]; for (const [idx, mode, width] of expectations) { const id = pages[idx]; assert.equal(sceneInfoOf(id).columnWidth.value, mode, `page ${idx + 1} column width mode`); const text = rootTextOf(id).value; assert.equal(text.width, width, `page ${idx + 1} text width`); assert.equal(text.posX, -width / 2, `page ${idx + 1} text posX`); if (text.widthLww != null) assert.equal(text.widthLww.value, width); }});
test('page 10 uses the numbered-list paragraph style', () => { const text = rootTextOf(pages[9]).value; const styles = [...text.styles.values()].map(s => s.value); assert.equal(styles.filter(s => s === ParagraphStyle.NUMBERED).length, 2); assert.ok(styles.includes(ParagraphStyle.CHECKBOX)); assert.ok(styles.includes(ParagraphStyle.CHECKBOX_CHECKED)); assert.ok(styles.includes(ParagraphStyle.BULLET));});
test('page 10 preserves extended text-format attributes', () => { const text = rootTextOf(pages[9]).value; const withExtra = [...text.styles.values()].filter(s => s.extra != null); assert.equal(withExtra.length, 1); assert.equal(withExtra[0].value, ParagraphStyle.BOLD); assert.deepStrictEqual(withExtra[0].extra, { unknown2: 2, unknown3: 3, raw: null });});