Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/wikitext. Event-stream based wikitext parser
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062/** * Tests for orchestration and tree-building APIs. */// deno-lint-ignore-file no-import-prefix no-unversioned-importimport { describe, it } from 'jsr:@std/testing/bdd';import { expect } from 'jsr:@std/expect';import * as fc from 'npm:fast-check';
import { odd_character_wikitext_string, pathological_wikitext_string, spacing_heavy_wikitext_string, wikiish_string,} from './_test_utils/arbitraries.ts';import { BARE_URI_ACCEPTANCE_FIXTURES, BARE_URI_REJECTION_FIXTURES, EXPLICIT_URI_ACCEPTANCE_FIXTURES,} from './_test_utils/uri_fixtures.ts';import { UNICODE_TEXT_FIXTURES } from './_test_utils/unicode_fixtures.ts';import { buildTree, buildTreeWithDiagnostics, buildTreeStrict, type ParseDiagnosticsResult, type ParseResult,} from './tree_builder.ts';import { DiagnosticCode, enterEvent, errorEvent, exitEvent, textEvent, type WikitextEvent,} from './events.ts';import { blockEvents } from './block_parser.ts';import { analyze, events, materialize, outlineEvents, parse, parseStrictWithDiagnostics, parseWithDiagnostics, parseWithRecovery, tokens, type ParseFindings,} from './parse.ts';import { TreeMaterializationPolicy } from './tree_builder.ts';import { tokenize } from './tokenizer.ts';
const COMPLEX_PARSE_FIXTURES = [ [ '== Lead ==', `A [[Main Page|home]] link with ''italic'' and '''bold'''.`, '', '* Bullet one', '* Bullet two with {{Card|title=Plan|body=value}}', '', '{|', '! Planet !! Notes', '|-', '| Mars || <ref name="m1">Known as the red planet</ref>', '|}', ].join('\n'), [ 'Paragraph with <nowiki>[[literal]]</nowiki> text and & entity.', 'Second line keeps the paragraph open until a blank line.', '', '; Term', ': Description with [https://example.com label]', ].join('\n'),] as const;
const BLOCK_STRUCTURE_FIXTURES = [ ...COMPLEX_PARSE_FIXTURES, 'Paragraph with <ref name="cite-1">note', 'Paragraph with <ref name="cite-1"', '{|\n| Cell', '== Heading ==\nLine one\nLine two\n\n* Item',] as const;
const BLOCK_NODE_TYPE_LOOKUP = new Set([ 'root', 'heading', 'paragraph', 'preformatted', 'list', 'list-item', 'definition-list', 'definition-term', 'definition-description', 'table', 'table-caption', 'table-row', 'table-cell', 'thematic-break', 'redirect',]);
type TreeLikeNode = { readonly type: string; readonly children?: readonly TreeLikeNode[]; readonly url?: string;};
function externalLinkUrlsFromTree(root: TreeLikeNode): string[] { const result: string[] = [];
function walk(node: TreeLikeNode): void { if (node.type === 'external-link' && typeof node.url === 'string') { result.push(node.url); }
for (const child of node.children ?? []) { walk(child); } }
walk(root); return result;}
function blockStructureFromEvents(stream: Iterable<WikitextEvent>): [string, string][] { const result: [string, string][] = [];
for (const event of stream) { if ((event.kind === 'enter' || event.kind === 'exit') && BLOCK_NODE_TYPE_LOOKUP.has(event.node_type)) { result.push([event.kind, event.node_type]); } }
return result;}
function blockStructureFromTree(root: TreeLikeNode): [string, string][] { const result: [string, string][] = [];
function walk(node: TreeLikeNode): void { const is_block = BLOCK_NODE_TYPE_LOOKUP.has(node.type);
if (is_block) { result.push(['enter', node.type]); }
for (const child of node.children ?? []) { walk(child); }
if (is_block) { result.push(['exit', node.type]); } }
walk(root); return result;}
describe('orchestration', () => { it('tokens() aliases the tokenizer output', () => { const input = '== Heading =='; const direct = Array.from(tokenize(input)); const orchestrated = Array.from(tokens(input));
expect(orchestrated).toEqual(direct); });
it('outlineEvents() matches the block parser pipeline', () => { const input = '== Heading ==\n\nParagraph'; const direct = Array.from(blockEvents(input, tokenize(input), { diagnostics: false })); const orchestrated = Array.from(outlineEvents(input));
expect(orchestrated).toEqual(direct); });
it('events() includes inline enrichment', () => { const input = 'A [[Page|home]] link'; const structure = Array.from(events(input)) .filter((event) => event.kind === 'enter' || event.kind === 'exit') .map((event) => event.node_type);
expect(structure).toContain('wikilink'); });
it('keeps block structure consistent across outlineEvents(), events(), and parse()', () => { for (const input of BLOCK_STRUCTURE_FIXTURES) { const outline_structure = blockStructureFromEvents(outlineEvents(input)); const full_structure = blockStructureFromEvents(events(input)); const tree_structure = blockStructureFromTree(parse(input));
expect(full_structure).toEqual(outline_structure); expect(tree_structure).toEqual(outline_structure); } });
it('preserves the shared URI acceptance matrix in final tree output', () => { for (const fixture of [...BARE_URI_ACCEPTANCE_FIXTURES, ...EXPLICIT_URI_ACCEPTANCE_FIXTURES]) { expect(externalLinkUrlsFromTree(parse(fixture.input))).toContain(fixture.url); }
for (const input of BARE_URI_REJECTION_FIXTURES) { expect(externalLinkUrlsFromTree(parse(input))).toEqual([]); } });});
describe('buildTree()', () => { it('treats root enter/exit as document boundaries, not nested nodes', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 12, offset: 11 }, } as const;
const tree = buildTree([ enterEvent('root', {}, pos), enterEvent('paragraph', {}, pos), textEvent(0, 5, { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 6, offset: 5 }, }), exitEvent('paragraph', pos), exitEvent('root', pos), ], { source: 'hello world' });
expect(tree.type).toBe('root'); expect(tree.children).toHaveLength(1); expect(tree.children[0].type).toBe('paragraph'); expect(tree.position).toEqual(pos); });
it('preserves explicit root position even when no child nodes exist', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 1, offset: 0 }, } as const;
const tree = buildTree([ enterEvent('root', {}, pos), exitEvent('root', pos), ], { source: '' });
expect(tree).toEqual({ type: 'root', children: [], position: pos, }); });
it('materializes literal node values from enter-event props', () => { const value_pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 7, offset: 6 }, } as const;
const tree = buildTree([ enterEvent('root', {}, value_pos), enterEvent('paragraph', {}, value_pos), enterEvent('nowiki', { value: '[[raw]]' }, value_pos), exitEvent('nowiki', value_pos), enterEvent('comment', { value: ' hidden ' }, value_pos), exitEvent('comment', value_pos), enterEvent('html-entity', { value: '&' }, value_pos), exitEvent('html-entity', value_pos), exitEvent('paragraph', value_pos), exitEvent('root', value_pos), ], { source: 'unused' });
const paragraph = tree.children[0]; expect(paragraph.type, 'expected paragraph node in literal materialization test').toBe('paragraph'); if (paragraph.type !== 'paragraph') return;
expect(paragraph.children.map((node) => node.type)).toEqual([ 'nowiki', 'comment', 'html-entity', ]);
const nowiki = paragraph.children[0]; const comment = paragraph.children[1]; const entity = paragraph.children[2];
expect(nowiki.type, 'expected nowiki node in literal materialization test').toBe('nowiki'); expect(comment.type, 'expected comment node in literal materialization test').toBe('comment'); expect(entity.type, 'expected html-entity node in literal materialization test').toBe('html-entity'); if (nowiki.type !== 'nowiki' || comment.type !== 'comment' || entity.type !== 'html-entity') return;
expect(nowiki.value).toBe('[[raw]]'); expect(comment.value).toBe(' hidden '); expect(entity.value).toBe('&'); });
it('recovers when exits arrive out of order by closing intermediate frames', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 6, offset: 5 }, } as const;
const tree = buildTree([ enterEvent('root', {}, pos), enterEvent('paragraph', {}, pos), enterEvent('bold', {}, pos), textEvent(0, 5, pos), // Intentionally close paragraph first; recovery should still keep // a usable tree with bold nested under paragraph. exitEvent('paragraph', pos), exitEvent('root', pos), ], { source: 'hello' });
expect(tree.children).toHaveLength(1); const paragraph = tree.children[0]; expect(paragraph.type, 'expected paragraph node after out-of-order exit recovery').toBe('paragraph'); if (paragraph.type !== 'paragraph') return;
expect(paragraph.children).toHaveLength(1); const bold = paragraph.children[0]; expect(bold.type, 'expected bold child after out-of-order exit recovery').toBe('bold'); if (bold.type !== 'bold') return;
expect(bold.children).toHaveLength(1); expect(bold.children[0].type).toBe('text'); });
it('auto-closes open frames at end of stream using default end points', () => { const open_pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 8, offset: 7 }, } as const;
const tree = buildTree([ enterEvent('root', {}, open_pos), enterEvent('paragraph', {}, open_pos), textEvent(0, 5, { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 6, offset: 5 }, }), // No paragraph exit on purpose. exitEvent('root', open_pos), ], { source: 'hello??' });
expect(tree.children).toHaveLength(1); const paragraph = tree.children[0]; expect(paragraph.type, 'expected paragraph node after EOF auto-close').toBe('paragraph'); if (paragraph.type !== 'paragraph') return;
expect(paragraph.position).toEqual(open_pos); expect(paragraph.children).toHaveLength(1); expect(paragraph.children[0].type).toBe('text'); });
it('materializes text values from the original source', () => { const input = '== Title ==\n\nA [[Page|home]] link'; const tree = buildTree(events(input), { source: input });
expect(tree.children[0].type).toBe('heading'); expect(tree.children[1].type).toBe('paragraph');
const heading = tree.children[0]; const paragraph = tree.children[1];
expect(heading.type, 'expected heading node in materialized tree').toBe('heading'); expect(paragraph.type, 'expected paragraph node in materialized tree').toBe('paragraph'); if (heading.type !== 'heading' || paragraph.type !== 'paragraph') return;
expect(heading.children[0]).toEqual({ type: 'text', value: 'Title', position: heading.children[0].position, });
expect(paragraph.children[1].type).toBe('wikilink'); expect(paragraph.children[1].position).toBeDefined(); });
it('builds a block-only tree from outlineEvents(source)', () => { const input = '== Title ==\n\nA [[Page|home]] link'; const tree = buildTree(outlineEvents(input), { source: input });
expect(tree.children.map((node) => node.type)).toEqual([ 'heading', 'paragraph', ]);
const heading = tree.children[0]; const paragraph = tree.children[1];
expect(heading.type, 'expected heading node in outline tree test').toBe('heading'); expect(paragraph.type, 'expected paragraph node in outline tree test').toBe('paragraph'); if (heading.type !== 'heading' || paragraph.type !== 'paragraph') return;
expect(heading.children.map((child) => child.type)).toEqual(['text']); expect(paragraph.children.map((child) => child.type)).toEqual(['text']); expect(paragraph.children[0]).toEqual({ type: 'text', value: 'A [[Page|home]] link', position: paragraph.children[0].position, }); });
it('ignores token and error events while preserving structural nodes', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 6, offset: 5 }, } as const;
const tree = buildTree([ enterEvent('paragraph', {}, pos), textEvent(0, 5, pos), errorEvent('recoverable', pos), exitEvent('paragraph', pos), ], { source: 'hello' });
expect(tree.children).toHaveLength(1); expect(tree.children[0].type).toBe('paragraph');
const paragraph = tree.children[0]; expect(paragraph.type, 'expected paragraph node when ignoring token/error events').toBe('paragraph'); if (paragraph.type !== 'paragraph') return;
expect(paragraph.children[0].type).toBe('text'); if (paragraph.children[0].type !== 'text') { expect(paragraph.children[0].type, 'expected text child when ignoring token/error events').toBe('text'); return; } expect(paragraph.children[0].value).toBe('hello'); });
it('buildTreeWithDiagnostics() preserves event diagnostics with a diagnostic anchor', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 6, offset: 5 }, } as const;
const result = buildTreeWithDiagnostics([ enterEvent('root', {}, pos), enterEvent('paragraph', {}, pos), errorEvent('recoverable', pos, { severity: 'warning', code: 'INLINE_RECOVERY', recoverable: true, source: 'inline', }), textEvent(0, 5, pos), exitEvent('paragraph', pos), exitEvent('root', pos), ], { source: 'hello' });
expect(result.tree.children).toHaveLength(1); expect(result.diagnostics).toEqual([ { message: 'recoverable', severity: 'warning', code: 'INLINE_RECOVERY', recoverable: true, source: 'inline', details: undefined, position: pos, anchor: { kind: 'tree-path', path: [0], node_type: 'paragraph', }, }, ]); });
it('buildTreeWithDiagnostics() reports tree-stage recovery locations', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 6, offset: 5 }, } as const;
const result = buildTreeWithDiagnostics([ enterEvent('root', {}, pos), enterEvent('paragraph', {}, pos), enterEvent('bold', {}, pos), textEvent(0, 5, pos), exitEvent('paragraph', pos), exitEvent('root', pos), ], { source: 'hello' });
expect(result.tree.children).toHaveLength(1); expect(result.diagnostics).toHaveLength(1); expect(result.diagnostics[0].code).toBe(DiagnosticCode.TREE_MISMATCHED_EXIT); expect(result.diagnostics[0].anchor).toEqual({ kind: 'tree-path', path: [0, 0], node_type: 'bold', }); expect(result.diagnostics[0].recoverable).toBe(true); expect(result.diagnostics[0].source).toBe('tree'); expect(result.diagnostics[0].severity).toBe('warning'); });
it('buildTreeWithDiagnostics() reports orphan exits at the root boundary', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 1, offset: 0 }, } as const;
const result = buildTreeWithDiagnostics([ enterEvent('root', {}, pos), exitEvent('italic', pos), exitEvent('root', pos), ], { source: '' });
expect(result.diagnostics).toHaveLength(1); expect(result.diagnostics[0].code).toBe(DiagnosticCode.TREE_ORPHAN_EXIT); expect(result.diagnostics[0].anchor).toEqual({ kind: 'tree-path', path: [], node_type: 'root', }); });
it('buildTreeWithDiagnostics() reports EOF auto-close recovery for open frames', () => { const pos = { start: { line: 1, column: 1, offset: 0 }, end: { line: 1, column: 6, offset: 5 }, } as const;
const result = buildTreeWithDiagnostics([ enterEvent('root', {}, pos), enterEvent('paragraph', {}, pos), textEvent(0, 5, pos), ], { source: 'hello' });
expect(result.diagnostics).toHaveLength(1); expect(result.diagnostics[0].code).toBe(DiagnosticCode.TREE_EOF_AUTOCLOSE); expect(result.diagnostics[0].anchor).toEqual({ kind: 'tree-path', path: [0], node_type: 'paragraph', }); });
it('buildTreeStrict() keeps diagnostics while collapsing recovery-heavy wrappers', () => { const input = '{|\n| Cell'; const strict_result = buildTreeStrict(events(input, { diagnostics: true }), { source: input });
expect(strict_result.tree.children[0]?.type).toBe('text'); expect(strict_result.diagnostics[0]?.anchor.node_type).toBe('text'); });});
describe('parse()', () => { it('builds a usable wikist root', () => { const tree = parse("A '''bold''' word");
expect(tree.type).toBe('root');
const paragraph = tree.children.find((node) => node.type === 'paragraph'); expect(paragraph).toBeDefined();
expect(paragraph?.type, 'expected paragraph node in parsed tree').toBe('paragraph'); if (paragraph === undefined || paragraph.type !== 'paragraph') return;
expect(paragraph.children.map((child) => child.type)).toEqual([ 'text', 'bold', 'text', ]); });
it('matches buildTree(events(source), { source }) on representative mixed inputs', () => { for (const input of COMPLEX_PARSE_FIXTURES) { expect(parse(input)).toEqual(buildTree(events(input), { source: input })); } });
it('builds a full tree with block and inline nodes from a mixed document', () => { const input = COMPLEX_PARSE_FIXTURES[0]; const tree = parse(input);
expect(tree.children.map((node) => node.type)).toEqual([ 'heading', 'paragraph', 'list', 'table', ]);
const paragraph = tree.children[1]; const list = tree.children[2]; const table = tree.children[3];
expect(paragraph.type, 'expected paragraph node in mixed parse test').toBe('paragraph'); expect(list.type, 'expected list node in mixed parse test').toBe('list'); expect(table.type, 'expected table node in mixed parse test').toBe('table'); if (paragraph.type !== 'paragraph' || list.type !== 'list' || table.type !== 'table') return;
expect(paragraph.children.map((child) => child.type)).toEqual([ 'text', 'wikilink', 'text', 'italic', 'text', 'bold', 'text', ]); expect(list.children).toHaveLength(2); expect(list.children[1].type).toBe('list-item'); expect(table.children.some((child) => child.type === 'table-row')).toBe(true); });
it('preserves representative Unicode text classes through the full pipeline', () => { for (const fixture of UNICODE_TEXT_FIXTURES) { const input = `== ${fixture.sample} ==\n\n${fixture.sample}`; const result = parseWithDiagnostics(input); const heading = result.tree.children[0]; const paragraph = result.tree.children[1];
expect(result.diagnostics).toEqual([]); expect(heading?.type).toBe('heading'); expect(paragraph?.type).toBe('paragraph');
if (heading?.type !== 'heading' || paragraph?.type !== 'paragraph') { expect(heading?.type, `expected heading node for ${fixture.key}`).toBe('heading'); expect(paragraph?.type, `expected paragraph node for ${fixture.key}`).toBe('paragraph'); return; }
expect(heading.children[0]?.type).toBe('text'); expect(paragraph.children[0]?.type).toBe('text');
if (heading.children[0]?.type !== 'text' || paragraph.children[0]?.type !== 'text') { expect(heading.children[0]?.type, `expected heading text node for ${fixture.key}`).toBe('text'); expect(paragraph.children[0]?.type, `expected paragraph text node for ${fixture.key}`).toBe('text'); return; }
expect(heading.children[0].value).toBe(fixture.sample); expect(paragraph.children[0].value).toBe(fixture.sample); } });
it('preserves literal inline nodes and block structure through the full pipeline', () => { const input = COMPLEX_PARSE_FIXTURES[1]; const tree = parse(input);
expect(tree.children.map((node) => node.type)).toEqual([ 'paragraph', 'definition-list', ]);
const paragraph = tree.children[0]; const definition_list = tree.children[1];
expect(paragraph.type, 'expected paragraph node in literal parse test').toBe('paragraph'); expect(definition_list.type, 'expected definition-list node in literal parse test').toBe('definition-list'); if (paragraph.type !== 'paragraph' || definition_list.type !== 'definition-list') return;
expect(paragraph.children.map((child) => child.type)).toEqual([ 'text', 'nowiki', 'text', 'html-entity', 'text', 'text', ]);
const trailing_text = paragraph.children[5]; expect(trailing_text?.type, 'expected the second paragraph line to remain a separate text node').toBe('text'); if (trailing_text?.type !== 'text') return;
expect(trailing_text.value).toBe('Second line keeps the paragraph open until a blank line.'); expect(definition_list.children.map((child) => child.type)).toEqual([ 'definition-term', 'definition-description', ]); });
it('keeps a closed reference opener structurally real when the close tag is missing', () => { const input = `Paragraph with <ref name="cite-1">''quoted'' and text`; const tree = parse(input); const paragraph = tree.children[0];
expect(tree.children.map((node) => node.type)).toEqual(['paragraph']);
expect(paragraph?.type, 'expected paragraph node in missing-close reference test').toBe('paragraph'); if (paragraph?.type !== 'paragraph') return;
expect(paragraph.children.map((child) => child.type)).toEqual([ 'text', 'reference', ]);
const reference = paragraph.children[1]; expect(reference?.type, 'expected reference node in missing-close reference test').toBe('reference'); if (reference?.type !== 'reference') return;
expect(reference.name).toBe('cite-1'); expect(JSON.stringify(reference)).toContain('quoted'); });
it('keeps an unterminated opener as literal text when `>` never appears', () => { const input = 'Paragraph with <ref name="cite-1"'; const tree = parse(input); const paragraph = tree.children[0];
expect(tree.children.map((node) => node.type)).toEqual(['paragraph']);
expect(paragraph?.type, 'expected paragraph node in unterminated opener test').toBe('paragraph'); if (paragraph?.type !== 'paragraph') return;
expect(paragraph.children.every((child) => child.type === 'text')).toBe(true); let combined_text = ''; for (const child of paragraph.children) { if (child.type === 'text') { combined_text += child.value; } } expect(combined_text).toBe(input); });
it('never throws and stays equivalent to buildTree(events(source)) on syntax-heavy fuzz input', () => { fc.assert( fc.property( wikiish_string(), spacing_heavy_wikitext_string(), pathological_wikitext_string(), (a: string, b: string, c: string) => { const input = `${a}\n${b}\n${c}`; const direct = parse(input); const layered = buildTree(events(input), { source: input });
expect(direct.type).toBe('root'); expect(layered.type).toBe('root'); expect(direct).toEqual(layered); }, ), ); });
it('never throws on mixed Unicode and recovery-heavy full-pipeline inputs', () => { fc.assert( fc.property( odd_character_wikitext_string(), pathological_wikitext_string(), (odd: string, hostile: string) => { const input = `${odd}\n${hostile}`; const tree = parse(input);
expect(tree.type).toBe('root'); }, ), ); });
it('keeps Unicode payloads stable inside a representative mixed document', () => { for (const fixture of UNICODE_TEXT_FIXTURES) { const input = [ `== ${fixture.sample} ==`, `${fixture.sample}`, `[[Main Page|${fixture.sample}]]`, `{{Card|name=${fixture.sample}}}`, '', ].join('\n'); const tree = parse(input);
expect(tree.type).toBe('root'); expect(JSON.stringify(tree)).toContain(fixture.sample); } });});
describe('parseWithDiagnostics()', () => { it('does not expose the recovery summary field', () => { const result = parseWithDiagnostics(COMPLEX_PARSE_FIXTURES[0]);
expect(Object.hasOwn(result, 'recovered')).toBe(false); });
it('preserves the same diagnostics as parseWithRecovery()', () => { for (const input of COMPLEX_PARSE_FIXTURES) { const diagnostics_result = parseWithDiagnostics(input); const recovery_result = parseWithRecovery(input);
expect(diagnostics_result.diagnostics).toEqual(recovery_result.diagnostics); } });
it('returns the same tree as parse() on representative mixed inputs', () => { for (const input of COMPLEX_PARSE_FIXTURES) { const result = parseWithDiagnostics(input); expect(result.tree).toEqual(parse(input)); } });
it('keeps diagnostics empty when no recovery was needed', () => { const result = parseWithDiagnostics(COMPLEX_PARSE_FIXTURES[0]);
expect(result.diagnostics).toEqual([]); });
it('preserves real parser diagnostics from recovery-heavy input', () => { const input = '{|\n| Cell'; const result = parseWithDiagnostics(input);
expect(result.tree.type).toBe('root'); expect(result.diagnostics.length).toBeGreaterThan(0); expect(result.diagnostics[0].code).toBe(DiagnosticCode.UNCLOSED_TABLE); expect(result.diagnostics[0].source).toBe('block'); expect(result.diagnostics[0].anchor).toEqual({ kind: 'tree-path', path: [0], node_type: 'table', }); expect(result.diagnostics[0].recoverable).toBe(true); expect(result.diagnostics[0].severity).toBe('warning'); expect(result.diagnostics[0].position.start.offset).toBe(input.length); expect(result.diagnostics[0].position.end.offset).toBe(input.length); });
it('reports a diagnostic when a tag opener never reaches `>`', () => { const input = 'Paragraph with <ref name="cite-1"'; const result = parseWithDiagnostics(input);
expect(result.tree.type).toBe('root'); expect(result.diagnostics).toHaveLength(1); expect(result.diagnostics[0].code).toBe(DiagnosticCode.INLINE_TAG_UNTERMINATED_OPENER); expect(result.diagnostics[0].source).toBe('inline'); expect(result.diagnostics[0].recoverable).toBe(true); expect(result.diagnostics[0].severity).toBe('warning'); });
it('reports a diagnostic when a closed opener never finds its matching close tag', () => { const input = 'Paragraph with <ref name="cite-1">note'; const result = parseWithDiagnostics(input);
expect(result.tree.type).toBe('root'); expect(result.diagnostics).toHaveLength(1); expect(result.diagnostics[0].code).toBe(DiagnosticCode.INLINE_TAG_MISSING_CLOSE); expect(result.diagnostics[0].source).toBe('inline'); expect(result.diagnostics[0].recoverable).toBe(true); expect(result.diagnostics[0].severity).toBe('warning'); expect(result.diagnostics[0].anchor.node_type).toBe('reference'); });
it('keeps the same tree as parse() when no diagnostics were emitted', () => { fc.assert( fc.property( wikiish_string(), spacing_heavy_wikitext_string(), pathological_wikitext_string(), (a: string, b: string, c: string) => { const input = `${a}\n${b}\n${c}`; const result: ParseDiagnosticsResult = parseWithDiagnostics(input);
if (result.diagnostics.length === 0) { expect(result.tree).toEqual(parse(input)); } }, ), ); });
it('keeps missing-close tags as structurally real nodes in the default diagnostics lane', () => { const input = 'Paragraph with <ref name="cite-1">note'; const result = parseWithDiagnostics(input);
expect(JSON.stringify(result.tree)).toContain('"type":"reference"'); });
it('keeps unclosed tables as recovered table nodes in the default diagnostics lane', () => { const input = '{|\n| Cell'; const result = parseWithDiagnostics(input);
expect(result.tree.children[0]?.type).toBe('table'); });
it('keeps the default block structure stable when inline recovery happens', () => { const input = 'Paragraph with <ref name="cite-1">note'; const outline_structure = blockStructureFromEvents(outlineEvents(input)); const diagnostics_result = parseWithDiagnostics(input); const recovery_result = parseWithRecovery(input);
expect(blockStructureFromTree(diagnostics_result.tree)).toEqual(outline_structure); expect(blockStructureFromTree(recovery_result.tree)).toEqual(outline_structure); });});
describe('parseStrictWithDiagnostics()', () => { it('preserves the same diagnostics as parseWithDiagnostics()', () => { for (const input of COMPLEX_PARSE_FIXTURES) { const diagnostics_result = parseWithDiagnostics(input); const strict_result = parseStrictWithDiagnostics(input);
expect(strict_result.diagnostics).toEqual(diagnostics_result.diagnostics); } });
it('keeps missing-close tags as plain text instead of recovered nodes', () => { const input = 'Paragraph with <ref name="cite-1">note'; const result = parseStrictWithDiagnostics(input);
expect(JSON.stringify(result.tree)).not.toContain('"type":"reference"'); expect(result.tree.children[0]?.type).toBe('paragraph'); if (result.tree.children[0]?.type !== 'paragraph') return; expect(result.tree.children[0].children).toHaveLength(2); expect(result.tree.children[0].children[0]?.type).toBe('text'); if (result.tree.children[0].children[0]?.type !== 'text') return; expect(result.tree.children[0].children[1]?.type).toBe('text'); if (result.tree.children[0].children[1]?.type !== 'text') return; const text_values = result.tree.children[0].children.map((child) => child.type === 'text' ? child.value : '' ); expect(text_values.join('')).toBe(input); });
it('keeps unclosed tables as plain text instead of recovered table nodes', () => { const input = '{|\n| Cell'; const result = parseStrictWithDiagnostics(input);
expect(result.tree.children[0]?.type).toBe('text'); if (result.tree.children[0]?.type !== 'text') return; expect(result.tree.children[0].value).toBe(input); });
it('keeps block structure aligned when only an inline recovered region collapses to text', () => { const input = 'Paragraph with <ref name="cite-1">note'; const outline_structure = blockStructureFromEvents(outlineEvents(input)); const strict_result = parseStrictWithDiagnostics(input);
expect(blockStructureFromTree(strict_result.tree)).toEqual(outline_structure); expect(strict_result.tree.children[0]?.type).toBe('paragraph'); });
it('matches the default tree when the source never reaches the commitment point', () => { const input = 'Paragraph with <ref name="cite-1"'; const default_tree = parse(input); const diagnostics_tree = parseWithDiagnostics(input).tree; const strict_tree = parseStrictWithDiagnostics(input).tree;
expect(diagnostics_tree).toEqual(default_tree); expect(strict_tree).toEqual(default_tree); });});
describe('parseWithRecovery()', () => { it('adds the recovery summary on top of parseWithDiagnostics()', () => { for (const input of COMPLEX_PARSE_FIXTURES) { const diagnostics_result = parseWithDiagnostics(input); const recovery_result = parseWithRecovery(input);
expect(recovery_result.diagnostics).toEqual(diagnostics_result.diagnostics); expect(recovery_result.tree).toEqual(diagnostics_result.tree); expect(recovery_result.recovered).toBe(diagnostics_result.diagnostics.length > 0); } });
it('sets recovered=false when no recovery was needed', () => { const result: ParseResult = parseWithRecovery(COMPLEX_PARSE_FIXTURES[0]);
expect(result.recovered).toBe(false); expect(result.diagnostics).toEqual([]); });
it('sets recovered=true when diagnostics were recorded', () => { const result: ParseResult = parseWithRecovery('{|\n| Cell');
expect(result.recovered).toBe(true); expect(result.diagnostics.length).toBeGreaterThan(0); });
it('keeps the same default tree shape as parseWithDiagnostics()', () => { const input = 'Paragraph with <ref name="cite-1">note'; const result = parseWithRecovery(input);
expect(JSON.stringify(result.tree)).toContain('"type":"reference"'); expect(result.tree).toEqual(parseWithDiagnostics(input).tree); });});describe('analyze()', () => { it('returns replayable events, diagnostics, and a recovery list for malformed input', () => { const input = 'Paragraph with <ref name="cite-1">note'; const findings: ParseFindings = analyze(input);
expect(findings.source).toBe(input); expect(findings.events.length).toBeGreaterThan(0); expect(findings.diagnostics.length).toBeGreaterThan(0); expect(findings.recovery).toBeDefined(); expect(findings.recovery?.some((entry) => entry.kind === 'missing-close')).toBe(true); });
it('produces the same diagnostics as parseWithDiagnostics()', () => { for (const input of COMPLEX_PARSE_FIXTURES) { const findings = analyze(input); const diagnostics_result = parseWithDiagnostics(input); expect(findings.diagnostics).toEqual(diagnostics_result.diagnostics); } });
it('omits the recovery list when options.recovery is false', () => { const findings = analyze('{|\n| Cell', { recovery: false });
expect(findings.diagnostics.length).toBeGreaterThan(0); expect(findings.recovery).toBeUndefined(); });
it('records unclosed-table recovery with both policies listed', () => { const findings = analyze('{|\n| Cell'); const entry = findings.recovery?.find((item) => item.kind === 'unclosed-table');
expect(entry).toBeDefined(); expect(entry?.policies).toEqual([ TreeMaterializationPolicy.DEFAULT_HTML_LIKE, TreeMaterializationPolicy.SOURCE_STRICT, ]); });
it('records unterminated-opener recovery with a single-policy list', () => { const findings = analyze('Paragraph with <ref name="cite-1"'); const entry = findings.recovery?.find((item) => item.kind === 'unterminated-opener');
expect(entry).toBeDefined(); expect(entry?.policies).toEqual([TreeMaterializationPolicy.DEFAULT_HTML_LIKE]); });});
describe('materialize()', () => { it('defaults to the default-html-like policy', () => { const input = 'Paragraph with <ref name="cite-1">note'; const findings = analyze(input); const output = materialize(findings); const diagnostics_result = parseWithDiagnostics(input);
expect(output.tree).toEqual(diagnostics_result.tree); expect(output.diagnostics).toEqual(diagnostics_result.diagnostics); });
it('honors the source-strict policy', () => { const input = 'Paragraph with <ref name="cite-1">note'; const findings = analyze(input); const output = materialize(findings, { policy: TreeMaterializationPolicy.SOURCE_STRICT, }); const strict_result = parseStrictWithDiagnostics(input);
expect(output.tree).toEqual(strict_result.tree); expect(output.diagnostics).toEqual(strict_result.diagnostics); });
it('produces distinct trees for the two public policies when recovery is heavy', () => { const findings = analyze('Paragraph with <ref name="cite-1">note'); const tolerant = materialize(findings); const strict = materialize(findings, { policy: TreeMaterializationPolicy.SOURCE_STRICT, });
expect(JSON.stringify(tolerant.tree)).toContain('"type":"reference"'); expect(JSON.stringify(strict.tree)).not.toContain('"type":"reference"'); });
it('can be called more than once on the same findings without reparsing', () => { const findings = analyze('Paragraph with <ref name="cite-1">note'); const first = materialize(findings); const second = materialize(findings);
expect(first.tree).toEqual(second.tree); expect(first.diagnostics).toEqual(second.diagnostics); });});