import { test } from "node:test"; import assert from "node:assert/strict"; import { createElement as h } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { parseRichBlock, safeImageSrc, richAnswerText, plainTextForSpeech } from "../dist/index.js"; import { Markdown } from "../dist/react/index.js"; const diagram = { type: "diagram", title: "Dependency", nodes: [ { id: "a", label: "Machine" }, { id: "b", label: "Material" }, ], edges: [{ from: "a", to: "b" }], }; test("diagrams reject dangling edges, duplicate ids and oversized model output", () => { assert.deepEqual(parseRichBlock(JSON.stringify(diagram)), diagram); assert.equal( parseRichBlock(JSON.stringify({ ...diagram, edges: [{ from: "a", to: "missing" }] })), null, ); assert.equal( parseRichBlock(JSON.stringify({ ...diagram, nodes: [diagram.nodes[0], diagram.nodes[0]] })), null, ); assert.equal(parseRichBlock("x".repeat(40001)), null); assert.equal(parseRichBlock('{"type":"diagram"'), null); }); test("images cannot inject protocols, credentials or markup", () => { for (const src of [ "javascript:alert(1)", "data:image/svg+xml,", "//evil.test/img", "/\\evil.test", "https://user:pass@evil.test/x", ]) assert.equal(safeImageSrc(src), null); assert.equal(safeImageSrc("/photos/hinge.jpg"), "/photos/hinge.jpg"); assert.equal(safeImageSrc("https://example.com/img.jpg"), "https://example.com/img.jpg"); }); test("visuals render semantic content and escape labels, without executing model code", () => { const html = renderToStaticMarkup( h(Markdown, { text: "```chat_ui\n" + JSON.stringify(diagram) + "\n```" }), ); assert.match(html, /ck-diagram-nodes/); assert.match(html, /Machine/); assert.doesNotMatch(html, /
alert(1)", body: "Helpful" }) +
"\n```",
}),
);
assert.doesNotMatch(unsafe, /