Files
substrata/test/attach-citations.test.ts
CatoandClaude 94527fb45d fix(chat): a source link is not attached inside another link's title (#371)
A lead whose title holds a "?" ("Is There a Helium Shortage in 2026?
Why It Happened…") was split there as a sentence end, and the row's
link was attached inside the title: the reader got a link nested in a
link (seen on production).

The citation pass now splits through sentencesIn, like every other pass
that rewrites an answer, so a link stays whole; and a sentence already
linked to the row's page is not linked to it again.

Claude-Session: https://claude.ai/code/session_019MDtmuvTGrpo3VChP3skH8

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 05:55:48 +01:00

62 lines
2.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Code-attached citations (lib/chat-agent/attach-citations.ts): a sentence one
* row vouches for — every name and figure in it is in that row — gets the
* row's link, and so survives default deny. Anything else gets nothing.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import recorded from './fixtures/ru-rare-earth-nickel-producers-in-2026-10-24.json';
import { attachCitations, rowFor, rowsIn } from '../lib/chat-agent/attach-citations';
const rows = rowsIn(recorded.results);
const USGS = 'https://pubs.usgs.gov/myb/vol3/2022/myb3-2022-russia.pdf';
test('a sentence every name and figure of which is in one row gets that row’s source', () => {
for (const s of [
'Nornickel runs mines in Murmansk Oblast and in Norilsk region.',
'The capacity printed for the Murmansk Oblast mines is 300000 metric tons.',
'OAO Apatit mines the Lovozerskoye deposit on the Kola Peninsula.',
])
assert.equal(rowFor(s, rows)?.href, USGS, s);
const out = attachCitations(
'OAO Apatit mines the Lovozerskoye deposit on the Kola Peninsula.',
recorded.results,
);
assert.match(out, /Kola Peninsula \(\[[^\]]+\]\(https:\/\/pubs\.usgs\.gov\/[^)]+\)\)\.$/);
});
test('nothing is attached where no single row vouches for it', () => {
for (const s of [
'OAO Apatit sells its concentrate to China.', // China is in no row
'Nornickel is the largest nickel producer in the world.', // generalises
'Nornickel mines 450000 metric tons a year in Murmansk Oblast.', // a figure from nowhere
'Capacity is capacity, not production.', // names nothing, gives no figure
'Rare earths', // a title
])
assert.equal(rowFor(s, rows), null, s);
assert.equal(
attachCitations('Nornickel is the largest nickel producer in the world.', recorded.results),
'Nornickel is the largest nickel producer in the world.',
);
// A computed sentence is the code's own: never re-cited.
const summary = 'Operators, as printed: OAO Apatit.';
assert.equal(attachCitations(summary, recorded.results, [summary]), summary);
});
test('a "?" or "." inside a link title is not a sentence end: nothing is attached inside the title', () => {
// Seen live 2026-11-12: "[Is There a Helium Shortage in 2026 ([…](…))? Why…](…)".
const line =
'A post covers it: [Is there a shortage? OAO Apatit mines the Lovozerskoye deposit on the Kola Peninsula. Why it matters](https://example.org/post).';
const out = attachCitations(line, recorded.results);
assert.ok(
out.startsWith(
'A post covers it: [Is there a shortage? OAO Apatit mines the Lovozerskoye deposit on the Kola Peninsula. Why it matters](https://example.org/post)',
),
out,
);
// A sentence already linked to the row's own page is not linked to it again.
const linked = `OAO Apatit mines the Lovozerskoye deposit on the Kola Peninsula ([USGS](${USGS})).`;
assert.equal(attachCitations(linked, recorded.results), linked);
});