import { describe, expect, it } from "vitest"; import { renderInlineContent, renderModelOutput } from "../lib/render"; import type { Source } from "../lib/types"; /** * The drawn text, for the many cases here that are about what the pane shows * rather than about which citations were linked. */ function rendered(markdown: string, sources?: Source[]): string { return renderModelOutput(markdown, sources ? { sources } : {}).text; } describe("renderModelOutput", () => { it("renders inline maths in a sentence", () => { expect(rendered("The answer is $x^2 + 1$, obviously.")).toBe("The answer is x² + 1, obviously."); expect(rendered("So \\(x^2\\) holds.")).toBe("So x² holds."); }); it("leaves money and shell variables alone", () => { expect(rendered("Costs $5 and $10 today.")).toBe("Costs $5 and $10 today."); expect(rendered("cd $HOME && echo $PATH")).toBe("cd $HOME && echo $PATH"); }); it("puts a display formula on a paragraph of its own", () => { expect(rendered("$$\\frac{a}{b} = c$$")).toBe("\na/b = c\n"); expect(rendered("$$\nx = 1\n$$")).toBe("\nx = 1\n"); expect(rendered("\\[\\sum_{i=1}^{n} i\\]")).toBe("\n∑ᵢ₌₁ⁿ i\n"); }); it("does not let a stray marker swallow the rest of the answer", () => { // An opening `$$` with no closer only counts when what follows reads as a // formula, so a price is not a formula and the paragraphs after it survive. expect(rendered("The price is\n$$5 per unit.\n\nThat is cheap.")).toBe( "The price is\n$$5 per unit.\n\nThat is cheap.", ); }); it("renders a maths fence as a formula rather than as code", () => { expect(rendered("```math\n\\sum_{i=1}^{n} i\n```")).toBe("\n∑ᵢ₌₁ⁿ i\n"); expect(rendered("```latex\nx^2\n```")).toBe("\nx²\n"); }); it("leaves maths inside a fence alone", () => { const markdown = "```ts\nconst x = $a$;\n```"; expect(rendered(markdown)).toBe(markdown); }); it("leaves a dollar inside a code span alone", () => { expect(rendered("`$x^2$` stays.")).toBe("`$x^2$` stays."); }); it("renames a fence language the renderer would print but not colour", () => { expect(rendered("```console\nls\n```")).toBe("```bash\nls\n```"); expect(rendered("```terminal\nls\n```")).toBe("```bash\nls\n```"); }); it("drops a fence label that carries no information", () => { expect(rendered("```text\nplain\n```")).toBe("```\nplain\n```"); }); it("leaves a label that does resolve exactly as written", () => { const markdown = "```python\nprint(1)\n```"; expect(rendered(markdown)).toBe(markdown); }); it("fences a diagram so its alignment survives", () => { expect(rendered("src/\n├── a.ts\n└── b.ts")).toBe("```\nsrc/\n├── a.ts\n└── b.ts\n```"); }); it("leaves a real table to the table renderer", () => { const table = "| a | b |\n| --- | --- |\n| 1 | 2 |"; expect(rendered(table)).toBe(table); }); it("leaves a diagram inside a fence alone", () => { const markdown = "```text\n├── a.ts\n└── b.ts\n```"; expect(rendered(markdown)).toBe("```\n├── a.ts\n└── b.ts\n```"); }); it("filters HTML in the flow of an answer", () => { expect(rendered("still here")).toBe("still here"); expect(rendered('x')).toBe( 'x', ); expect(rendered('Read the docs.')).toBe( "Read [the docs](https://example.com).", ); }); it("leaves the markup inside a fence as the code it is", () => { const markdown = "```html\nx\n```"; expect(rendered(markdown)).toBe(markdown); }); it("hands a block of HTML to the engine as a paragraph", () => { // A line that starts with a block tag is an HTML *block*, and Vicinae's block // path keeps only the text of one. Wrapped in a span it is a paragraph, which // is the path that keeps raw HTML. expect(rendered("
ab
")).toBe( "
ab
", ); }); it("joins a block of HTML onto one line", () => { // `class` is dropped, because there is no stylesheet for it to name, and the // style attribute is the only thing the renderer reads. expect(rendered('
\nbold\n
')).toBe("
bold
"); }); it("leaves markup that does not balance where the text it carries still shows", () => { expect(rendered("
hello")).toBe("
hello"); }); it("leaves a block holding an image alone, because that path loads it", () => { expect(rendered('
')).toBe('
'); }); it("leaves a lone inline tag alone, because it is not a block", () => { const markdown = "Try bold for that."; expect(rendered(markdown)).toBe(markdown); }); it("keeps an answer that has nothing to render exactly as written", () => { const plain = "Just a sentence, with a [link](https://example.com) and `code`."; expect(rendered(plain)).toBe(plain); }); it("repairs fences in model output but not in what a person typed", () => { // A lone command is a fence when a model writes it, and a question about it // is left as the person typed it. expect(rendered("`pwd`")).toBe("```bash\npwd\n```"); expect(renderInlineContent("`pwd`")).toBe("`pwd`"); }); it("still renders maths and markup in what a person typed", () => { expect(renderInlineContent("what is $x^2$?")).toBe("what is x²?"); expect(renderInlineContent("hi")).toBe("hi"); expect(renderInlineContent("```ts\nconst x = $a$;\n```")).toBe("```ts\nconst x = $a$;\n```"); }); }); describe("citations in the pipeline", () => { const sources = [{ index: 1, title: "First", url: "https://first.example" }]; it("reports which citations it linked", () => { const result = renderModelOutput("Rain [1].", { sources }); expect(result.text).toBe("Rain [\\[1\\]](https://first.example)."); expect([...result.cited]).toEqual([1]); }); it("links a marker that has to survive the maths and markup passes", () => { expect(rendered("Let $x^2$ be [1], and nothing else.", sources)).toContain( "x² be [\\[1\\]](https://first.example)", ); }); it("leaves a marker inside a fence for the reader to copy", () => { const text = rendered("```bash\ncurl [1]\n```\n\nSee [1].", sources); expect(text).toContain("curl [1]"); expect(text).toContain("[\\[1\\]](https://first.example)."); }); it("changes nothing when no sources are given", () => { expect(rendered("Rain [1].")).toBe("Rain [1]."); }); });