import { describe, expect, it } from 'vitest'; import type { OneNoteContentBlock } from '../onenote/one/content-model.js'; import { prepareBlocksForRendering } from './page-render-budget.js'; describe('page render budget', () => { it('stops reading blocks at the configured limit', () => { const blocks: OneNoteContentBlock[] = Array.from( { length: 200_000 }, (_, index) => ({ kind: 'unsupported', id: String(index), sourceJcid: index, description: 'unsupported', }) ); const plan = prepareBlocksForRendering(blocks, { maxBlocks: 3 }); expect(plan.blocks).toHaveLength(3); expect(plan.truncated).toBe(true); }); it('caps rich-text runs and characters before React sees them', () => { const block: OneNoteContentBlock = { kind: 'text', id: 'text', sourceText: 'x'.repeat(1_000), text: 'x'.repeat(1_000), runs: Array.from({ length: 100 }, (_, index) => ({ start: index * 10, end: index * 10 + 10, text: 'x'.repeat(10), style: {}, })), paragraphStyle: {}, layout: {}, }; const plan = prepareBlocksForRendering([block], { maxTextRuns: 3, maxTextCharacters: 25, }); const projected = plan.blocks[0]; expect(projected?.kind).toBe('text'); if (projected?.kind !== 'text') throw new Error('Expected text'); expect(projected.runs).toHaveLength(3); expect(projected.runs.map((run) => run.text).join('')).toHaveLength(25); expect(plan.truncated).toBe(true); }); it('caps table cells and ink points independently', () => { const table: OneNoteContentBlock = { kind: 'table', id: 'table', rows: [ { id: 'row', cells: Array.from({ length: 100 }, (_, index) => ({ id: String(index), blocks: [], })), }, ], columnWidths: [], lockedColumns: [], bordersVisible: true, layout: {}, }; const ink: OneNoteContentBlock = { kind: 'ink', id: 'ink', strokes: [ { points: Array.from({ length: 100 }, (_, index) => ({ x: index, y: index, })), }, ], children: [], layout: {}, }; const plan = prepareBlocksForRendering([table, ink], { maxTableCells: 4, maxInkPoints: 5, }); const projectedTable = plan.blocks[0]; const projectedInk = plan.blocks[1]; expect(projectedTable?.kind).toBe('table'); expect(projectedInk?.kind).toBe('ink'); if (projectedTable?.kind !== 'table' || projectedInk?.kind !== 'ink') { throw new Error('Expected projected table and ink'); } expect(projectedTable.rows[0]?.cells).toHaveLength(4); expect(projectedInk.strokes[0]?.points).toHaveLength(5); expect(plan.truncated).toBe(true); }); });