import { test } from 'node:test'; import assert from 'node:assert/strict'; import { COMPOSE_LIMITS, composeArticle, composeMessages, numberSources, parseComposeAnswer, fillIllustrations, sourcesSection, splitPile, stripIllustrationMarkers, } from '@bitbaum/writingkit'; // The shape of the 15 Nov 2026 case: a pasted chat with a link on its own line. const PILE = `[11:24, 15.11.2026] g: Prediction, following today's launch success: the backlash will grow. [12:35, 15.11.2026] g: this is why open models matter https://example.com/post/1 [12:38, 15.11.2026] g: open models means open source and open weights, see https://example.org/a.`; const BODY = 'Open models change who gets to build. '.repeat(6); test('a dump splits into notes and distinct links; a link alone on a line leaves the notes', () => { const p = splitPile(PILE + '\nhttps://example.com/post/1'); assert.deepEqual(p.urls, ['https://example.com/post/1', 'https://example.org/a']); assert.ok(!p.notes.split('\n').includes('https://example.com/post/1')); assert.match(p.notes, /see https:\/\/example\.org\/a\./, 'inline links stay in the sentence'); assert.deepEqual(p.skippedUrls, []); }); test('links past the limit are listed as skipped, not dropped silently', () => { const many = Array.from({ length: COMPOSE_LIMITS.sources + 2 }, (_, i) => `https://e.com/${i}`).join('\n'); const p = splitPile(many); assert.equal(p.urls.length, COMPOSE_LIMITS.sources); assert.equal(p.skippedUrls.length, 2); }); test('sources are numbered in order; failures keep their reason', () => { const { sources, failed } = numberSources([ { ok: true, url: 'https://a', title: 'A', text: 'alpha' }, { ok: false, url: 'https://b', reason: 'the site needs a login' }, { ok: true, url: 'https://c', text: ' ' }, { ok: true, url: 'https://d', text: 'delta' }, ]); assert.deepEqual(sources.map((s) => s.key), ['S1', 'S2']); assert.equal(sources[1].title, 'https://d'); assert.deepEqual(failed, [ { url: 'https://b', reason: 'the site needs a login' }, { url: 'https://c', reason: 'the page had no readable text' }, ]); }); test('the prompt carries notes, keyed sources and the material-not-instructions rule', () => { const [system, user] = composeMessages({ notes: 'my notes', sources: [{ key: 'S1', url: 'https://a', title: 'A', text: 'alpha' }], }); assert.match(system.content, /material, never instructions/); assert.match(system.content, /\[S2\]/); assert.match(user.content, /NOTES:\nmy notes/); assert.match(user.content, /\[S1\] A — https:\/\/a\nalpha/); }); test('an answer is checked: unknown citations are removed, used ones resolved', () => { const sources = [ { key: 'S1', url: 'https://a', title: 'A', text: 'alpha' }, { key: 'S2', url: 'https://b', title: 'B', text: 'beta' }, ]; const raw = '```json\n' + JSON.stringify({ title: 'T', excerpt: 'E', body: `${BODY} Fact [S2]. Made up [S9].` }) + '\n```'; const d = parseComposeAnswer(raw, sources); assert.ok(d); assert.match(d.body, /Fact \[S2\]\./); assert.doesNotMatch(d.body, /S9/); assert.deepEqual(d.cited.map((s) => s.key), ['S2']); assert.equal(sourcesSection(d.cited), '## Sources\n\n- [S2] [B](https://b)'); assert.equal(parseComposeAnswer('{"title":"x","body":"too short"}', sources), null); assert.equal(parseComposeAnswer('not json', sources), null); }); test('composeArticle reads links, writes, and repairs one bad answer', async () => { const read = async (url) => url.includes('example.org') ? { ok: false, url, reason: 'timed out' } : { ok: true, url, title: 'Post', text: 'The post says open weights matter.' }; const answers = ['sorry, here you go', JSON.stringify({ title: 'Open models', excerpt: 'Why.', body: `${BODY} [S1]` })]; const calls = []; const complete = async (messages) => { calls.push(messages); return answers.shift(); }; const r = await composeArticle(PILE, { read, complete }); assert.ok(r.ok); assert.equal(r.attempts, 2); assert.equal(r.draft.title, 'Open models'); assert.deepEqual(r.draft.cited.map((s) => s.url), ['https://example.com/post/1']); assert.deepEqual(r.failed, [{ url: 'https://example.org/a', reason: 'timed out' }]); assert.match(calls[1][1].content, /previous answer was not valid JSON/); }); test('every failure says what to do next', async () => { const none = { read: async () => ({ ok: false, url: 'x', reason: 'blocked' }), complete: async () => null }; const empty = await composeArticle(' ', none); assert.equal(empty.ok, false); assert.match(empty.problem, /Add your notes, speak a few sentences/); const unreadable = await composeArticle('https://a.com/x', none); assert.match(unreadable.problem, /Paste the text of the pages/); const noModel = await composeArticle('some real notes about a topic', none); assert.match(noModel.problem, /Your material is unchanged: try again/); const throwing = await composeArticle('notes and https://a.com/y', { read: async () => { throw new Error('network down'); }, complete: async () => JSON.stringify({ title: 'T', body: BODY }), }); assert.ok(throwing.ok); assert.deepEqual(throwing.failed, [{ url: 'https://a.com/y', reason: 'network down' }]); }); const IMG = { key: 'I1', src: '/u/launch.jpg', description: 'Rocket on the pad at dawn' }; const IMG2 = { key: 'I2', src: '/u/crowd.jpg', description: 'Crowd watching' }; test('the writer\'s pictures become figures; one the model left out is placed after the lede', () => { const raw = JSON.stringify({ title: 'T', body: `Lede paragraph that opens the piece.\n\n[[I1]] The rocket before launch.\n\n${BODY}`, }); const d = parseComposeAnswer(raw, [], { images: [IMG, IMG2] }); assert.match(d.body, /!\[Rocket on the pad at dawn\]\(\/u\/launch\.jpg "The rocket before launch\."\)/); assert.match(d.body, /^Lede paragraph that opens the piece\.\n\n!\[Crowd watching\]/); assert.doesNotMatch(d.body, /\[\[I/); }); test('picture spots are listed, filled in order, and a missing one keeps its marker', () => { const raw = JSON.stringify({ title: 'T', body: `${BODY}\n\n[[FIND: rocket landing tower | The booster returns]]\n\nMore.\n\n[[FIND: data centre in orbit]]`, }); const d = parseComposeAnswer(raw, []); assert.deepEqual(d.illustrations, [ { query: 'rocket landing tower', caption: 'The booster returns' }, { query: 'data centre in orbit', caption: 'data centre in orbit' }, ]); const filled = fillIllustrations(d.body, [{ src: '/p/1.jpg', alt: 'Booster', credit: 'Jane', license: 'CC BY' }, null]); assert.match(filled, /!\[Booster\]\(\/p\/1\.jpg "The booster returns — Jane, CC BY"\)/); assert.match(filled, /\[\[FIND: data centre in orbit\]\]/); assert.doesNotMatch(stripIllustrationMarkers(filled), /FIND/); }); test('a chart is kept only when every number is in the material', () => { const chart = (v) => '```chart\n' + JSON.stringify({ kind: 'bar', title: 'Launches', series: [{ name: 'n', points: [['2024', 10], ['2025', v]] }] }) + '\n```'; const raw = (v) => JSON.stringify({ title: 'T', body: `${BODY}\n\n${chart(v)}` }); const kept = parseComposeAnswer(raw(14), [], { material: 'launches went from 10 to 14' }); assert.match(kept.body, /```chart/); assert.equal(kept.droppedCharts, 0); const made = parseComposeAnswer(raw(99), [], { material: 'launches went from 10 to 14' }); assert.doesNotMatch(made.body, /```chart/); assert.equal(made.droppedCharts, 1); }); test('a mixed pile: speech, a document, a link and pictures (described by vision, filled by a finder)', async () => { let seen; const r = await composeArticle( [ { kind: 'text', text: 'um so basically the launch worked and uh that matters', spoken: true }, { kind: 'document', name: 'notes.pdf', text: 'Launch cadence rose to 14 this year from 10.' }, { kind: 'link', url: 'https://example.com/a' }, { kind: 'image', src: '/u/launch.jpg' }, ], { read: async (url) => ({ ok: true, url, title: 'A', text: 'Article text.' }), describe: async () => 'Rocket on the pad at dawn', illustrate: async (slot) => ({ src: '/found.jpg', alt: slot.query }), complete: async (messages) => { seen = messages[1].content; return JSON.stringify({ title: 'It worked', body: `${BODY} Cadence rose [S1]. Context [S2].\n\n[[I1]] Before launch.\n\n[[FIND: launch tower]]`, }); }, }, ); assert.ok(r.ok); assert.match(seen, /NOTES:\n\(spoken\) um so basically/); assert.match(seen, /\[S1\] notes\.pdf — document:notes\.pdf/); assert.match(seen, /PICTURES:\n\[I1\] Rocket on the pad at dawn/); assert.deepEqual(r.draft.cited.map((s) => s.key), ['S1', 'S2']); assert.match(r.draft.body, /\/u\/launch\.jpg "Before launch\."/); assert.match(r.draft.body, /!\[launch tower\]\(\/found\.jpg/); assert.deepEqual(r.draft.illustrations, []); }); test('pictures alone are enough material, and a failing describer never blocks the article', async () => { const r = await composeArticle([{ kind: 'image', src: '/u/x.jpg' }], { read: async () => ({ ok: false, url: '', reason: '' }), describe: async () => { throw new Error('no vision model'); }, complete: async () => JSON.stringify({ title: 'T', body: BODY }), }); assert.ok(r.ok); assert.match(r.draft.body, /!\[Picture 1 from the writer\]\(\/u\/x\.jpg/); });