// Charts in answers (0.15): chatkit holds the data contract and the fallback; // the drawing is injected. These tests are the contract. 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, parseChartBlock, chartFallback, richAnswerText, plainTextForSpeech, RICH_ANSWERS_INSTRUCTION, } from "../dist/index.js"; import { Markdown, ChatThread, ChatScreen, RichAnswerBlock } from "../dist/react/index.js"; const bars = { type: "chart", chart: "bars", caption: "Gallium production, 2024", unit: "t", highlight: "China", rows: [ { label: "China", value: 750, share: 98, href: "/atlas/china" }, { label: "Japan", value: 10, estimated: true }, { label: "Russia", value: 5, note: "USGS estimate" }, ], }; const share = { type: "chart", chart: "share", caption: "Who refines it", rows: [ { label: "China", share: 98, href: "https://example.com/china" }, { label: "Japan", share: 1 }, ], other: 1, }; const line = { type: "chart", chart: "line", caption: "Production by year", unit: "t", series: [ { id: "cn", label: "China", points: [ { x: 2020, y: 420 }, { x: 2022, y: 600 }, { x: 2024, y: 750, href: "/atlas/china" }, ], }, ], }; const timeline = { type: "chart", chart: "timeline", caption: "Export controls", events: [ { date: "2023-08", label: "China restricts gallium exports", tone: "down" }, { date: "2024-12", label: "Ban on exports to the US", href: "/findings/ga-ban", kind: "policy", }, { date: "2025-11", label: "Ban suspended", tone: "up" }, ], }; const tiles = { type: "chart", chart: "tiles", caption: "Gallium at a glance", tiles: [ { label: "World production", value: "760 t", href: "/resources/gallium" }, { label: "China's share", value: 98, note: "percent" }, ], }; const fence = (b) => "```chat_ui\n" + (typeof b === "string" ? b : JSON.stringify(b)) + "\n```"; test("every chart kind parses to the block it was written as, mirroring chartkit's props", () => { for (const b of [bars, share, line, timeline, tiles]) { assert.deepEqual(parseRichBlock(JSON.stringify(b)), b); assert.deepEqual(parseChartBlock(b), b); } }); test("a malformed chart is rejected by the parser but keeps its caption and safe rows", () => { const bad = [ { ...bars, rows: [] }, { ...bars, rows: [{ label: "China", value: "lots" }] }, { ...bars, rows: [{ label: "China", value: 750, share: 140 }] }, { ...bars, rows: [{ label: "China", value: 750, href: "javascript:alert(1)" }] }, { ...share, rows: [{ label: "China", share: -1 }] }, { ...line, series: [{ id: "a", label: "A", points: [] }] }, { ...line, series: [line.series[0], line.series[0]] }, { ...timeline, events: [{ date: "2023", label: "x", tone: "sideways" }] }, { ...tiles, tiles: Array(7).fill(tiles.tiles[0]) }, { ...bars, chart: "pie" }, { ...bars, caption: "" }, ]; for (const b of bad) assert.equal(parseRichBlock(JSON.stringify(b)), null, JSON.stringify(b)); // The fallback keeps what a reader can use and drops what it cannot trust. const t = chartFallback( JSON.stringify({ ...bars, rows: [ { label: "China", value: 750, href: "javascript:alert(1)" }, { label: "Japan", value: "ten" }, { label: "Russia", value: 5 }, ], }), ); assert.equal(t.caption, bars.caption); assert.deepEqual( t.rows.map((r) => r.cells[0]), ["China", "Russia"], ); assert.equal(t.rows[0].href, undefined); // No caption, not a chart, or partial JSON: nothing to fall back to. assert.equal(chartFallback(JSON.stringify({ ...bars, caption: "" })), null); assert.equal(chartFallback(JSON.stringify({ type: "callout", caption: "x" })), null); assert.equal(chartFallback('{"type":"chart","chart":"bars","caption":"Gal'), null); }); test("without a renderer a chart is its caption and a table, never a blank", () => { const html = renderToStaticMarkup(h(Markdown, { text: fence(bars) })); assert.match(html, /ck-chart ck-chart-table/); assert.match(html, /