import test from 'node:test'; import assert from 'node:assert/strict'; import { renderItems, UNREADABLE } from '../src/index.mjs'; const A = 'a'.repeat(40), B = 'b'.repeat(40); const cite = (path, lines, quote, commit = A) => ({ path, commit, lines, quote }); const options = { scopeOf: path => path.includes('/Playground/') ? 'playground' : path.endsWith('.md') ? 'canon' : undefined, changed: ({ path, commit }) => path === 'Book One/Characters/Mara Vell.md' && commit === A, }; const render = items => renderItems(JSON.stringify({ items }), options); // One snapshot per item kind: the plain text both channels show. const SNAPSHOTS = { fact: [{ kind: 'fact', text: 'Tomas is the ferryman.', cites: [cite('Book One/Characters/Tomas.md', [3, 3], 'the ferryman', B)] }], finding: [{ kind: 'finding', text: 'Eye colour differs.', cites: [cite('Book One/Chapters/Chapter 01.md', [41, 41], 'her grey eyes'), cite('Book One/Characters/Mara Vell.md', [9, 10], 'green eyes')] }], correction: [{ kind: 'correction', text: 'Chapter 1 says grey.', from: 'green eyes', to: 'grey eyes', cites: [cite('Book One/Characters/Mara Vell.md', [9, 9], 'green eyes', B)] }], question: [{ kind: 'question', text: 'Is the harbor north of the town?', cites: [cite('Book One/Places/Harbor Town.md', [2, 4], 'the harbor', B)] }], idea: [{ kind: 'idea', text: 'A storm could close the harbor.', cites: [cite('Book One/Playground/What If.md', [5, 5], 'a storm', B), { url: 'https://example.invalid/tides', quote: 'spring tides' }] }], }; const EXPECTED = { fact: '1. Fact: Tomas is the ferryman.\n [canon] Book One/Characters/Tomas.md:3 @bbbbbbb "the ferryman"', finding: '1. Finding: Eye colour differs.\n [canon] Book One/Chapters/Chapter 01.md:41 @aaaaaaa "her grey eyes"\n' + ' [canon] Book One/Characters/Mara Vell.md:9-10 @aaaaaaa "green eyes" (changed since)', correction: '1. Correction: Chapter 1 says grey.\n From: "green eyes"\n To: "grey eyes"\n' + ' [canon] Book One/Characters/Mara Vell.md:9 @bbbbbbb "green eyes"', question: '1. Question: Is the harbor north of the town?\n [canon] Book One/Places/Harbor Town.md:2-4 @bbbbbbb "the harbor"', idea: '1. Idea: A storm could close the harbor.\n [playground] Book One/Playground/What If.md:5 @bbbbbbb "a storm"\n' + ' [web] https://example.invalid/tides "spring tides"', }; for (const kind of Object.keys(SNAPSHOTS)) { test(`renderItems snapshot: ${kind}`, () => { const out = render(SNAPSHOTS[kind]); assert.equal(out.kind, 'items'); assert.equal(out.text, EXPECTED[kind]); assert.equal(out.items[0].kind, kind); }); } test('renderItems numbers several items and takes a parsed body as well as a string', () => { const items = [...SNAPSHOTS.fact, ...SNAPSHOTS.question]; const out = renderItems({ items }, options); assert.equal(out.text, `${EXPECTED.fact}\n${EXPECTED.question.replace(/^1\./, '2.')}`); assert.deepEqual(out.items[1].cites[0], { badge: 'canon', path: 'Book One/Places/Harbor Town.md', commit: B, lines: [2, 4], ref: 'Book One/Places/Harbor Town.md:2-4 @bbbbbbb', quote: 'the harbor', changed: false }); }); test('without options a path cite has no badge and is never marked changed', () => { assert.equal(renderItems({ items: SNAPSHOTS.finding }).text, '1. Finding: Eye colour differs.\n Book One/Chapters/Chapter 01.md:41 @aaaaaaa "her grey eyes"\n' + ' Book One/Characters/Mara Vell.md:9-10 @aaaaaaa "green eyes"'); }); const NOTICE_SNAPSHOTS = { 'no-cited-answer': 'No cited answer', 'no-answer': 'No answer', 'turn-failed': 'The turn failed' }; for (const [notice, text] of Object.entries(NOTICE_SNAPSHOTS)) { test(`renderItems snapshot: notice ${notice}`, () => { assert.deepEqual(renderItems(JSON.stringify({ items: [], notice }), options), { kind: 'notice', notice, text }); }); } test('a body without the reply shape shows a fixed line and none of its text', () => { const secret = 'free prose the guard would refuse'; const bodies = [secret, JSON.stringify(secret), '{"items": [], "notice": "other"}', JSON.stringify({ items: [], notice: 'no-answer', extra: secret }), JSON.stringify({ items: [] }), JSON.stringify({ items: [{ kind: 'story', text: secret, cites: [] }] }), JSON.stringify({ items: [{ kind: 'fact', text: secret }] }), JSON.stringify({ items: SNAPSHOTS.fact, prose: secret }), JSON.stringify({ items: [{ kind: 'correction', text: secret, cites: [] }] }), JSON.stringify({ items: [{ kind: 'fact', text: secret, cites: [cite('a.md', [2, 1], 'q')] }] }), JSON.stringify({ items: [{ kind: 'fact', text: secret, cites: [{ path: 'a.md', commit: 'abc', lines: [1, 1], quote: 'q' }] }] }), JSON.stringify({ items: [{ kind: 'fact', text: secret, cites: [{ url: 'https://x.invalid', path: 'a.md', quote: 'q' }] }] }), null, 42, '[]']; for (const body of bodies) { const out = renderItems(body, options); assert.deepEqual(out, { kind: 'unreadable', text: UNREADABLE }, String(body)); assert.ok(!JSON.stringify(out).includes(secret)); } });