Open History to see the last commit
1import { expect, it, vi } from "vite-plus/test";
2import { unified } from "unified";
3import remarkParse from "remark-parse";
4import remarkRehype from "remark-rehype";
5import rehypeRaw from "rehype-raw";
6import {
7 CHAT_MARKDOWN_REMARK_PLUGINS,
8 chatMarkdownRehypePlugins,
9 markdownMayContainRawHtml,
10} from "./markdownPipeline.ts";
11
12function chatMarkdownTree(markdown: string, rawHtml: boolean) {
13 const processor = unified()
14 .use(remarkParse)
15 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
16 .use(remarkRehype, { allowDangerousHtml: true })
17 .use(chatMarkdownRehypePlugins(rawHtml ? rehypeRaw : null));
18 return processor.runSync(processor.parse(markdown));
19}
20
21it("preserves in-app thread links through the shared Markdown sanitizer", () => {
22 const tree = chatMarkdownTree(
23 '[Open thread](t3-thread://v1/environment/thread)\n\n<a href="javascript:alert(1)">Unsafe</a>',
24 true,
25 );
26 expect(tree.children[0]).toMatchObject({
27 tagName: "p",
28 children: [
29 {
30 tagName: "a",
31 properties: { href: "t3-thread://v1/environment/thread" },
32 children: [{ type: "text", value: "Open thread" }],
33 },
34 ],
35 });
36 expect(JSON.stringify(tree)).not.toContain("javascript:");
37});
38
39it("flags every way markdown can start raw HTML", () => {
40 for (const markdown of ["<br>", "a</b>", "<!-- note -->", "<?php ?>", "<![CDATA[x]]>"]) {
41 expect(markdownMayContainRawHtml(markdown)).toBe(true);
42 }
43});
44
45it("renders tag-free markdown without loading raw HTML", () => {
46 const markdown = "# Title\n\n1 < 2 and **bold** with `code`";
47 expect(markdownMayContainRawHtml(markdown)).toBe(false);
48 expect(chatMarkdownTree(markdown, false).children).toEqual(
49 chatMarkdownTree(markdown, true).children,
50 );
51});
52
53it("keeps a failed load so every render waits on the same settled promise", async () => {
54 vi.resetModules();
55 vi.doMock("rehype-raw", () => {
56 throw new Error("chunk failed");
57 });
58 try {
59 const { loadRehypeRaw } = await import("./markdownPipeline.ts");
60 const failed = loadRehypeRaw();
61 await expect(failed).rejects.toThrow();
62 expect(loadRehypeRaw()).toBe(failed);
63 } finally {
64 vi.doUnmock("rehype-raw");
65 }
66});
67
68it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
69 const processor = unified()
70 .use(remarkParse)
71 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
72 .use(remarkRehype, { allowDangerousHtml: true })
73 .use(chatMarkdownRehypePlugins(rehypeRaw));
74 type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
75 const text = (node: Node): string =>
76 node.type === "text"
77 ? (node.value ?? "")
78 : node.tagName === "code"
79 ? `<code>${(node.children ?? []).map(text).join("")}</code>`
80 : (node.children ?? []).map(text).join("");
81 const render = (markdown: string) => text(processor.runSync(processor.parse(markdown)) as Node);
82
83 expect(
84 render(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`),
85 ).toBe(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`);
86 // Paths render as written, so one an agent already escaped keeps its doubled backslashes.
87 expect(render(String.raw`Escaped C:\\Users\\me`)).toBe(String.raw`Escaped C:\\Users\\me`);
88 expect(render(String.raw`See C:\x and \*this\* and Note:\*that\*.`)).toBe(
89 String.raw`See C:\x and *this* and Note:*that*.`,
90 );
91 expect(render(String.raw`${"`"}C:\me\.t3${"`"} and C:\me\.t3`)).toBe(
92 String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,
93 );
94});