|
2 | 2 |
|
3 | 3 | import { EnvironmentId, type AuthEnvironmentScope } from "@t3tools/contracts"; |
4 | 4 | import { act, type ComponentProps, type ReactNode } from "react"; |
| 5 | +import { createRoot } from "react-dom/client"; |
5 | 6 | import { renderToStaticMarkup } from "react-dom/server"; |
6 | 7 | import { create, type ReactTestRenderer } from "react-test-renderer"; |
7 | 8 | import { describe, expect, it, vi } from "vite-plus/test"; |
8 | 9 |
|
9 | 10 | import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting"; |
| 11 | +import { |
| 12 | + chatMarkdownClipboardPayload, |
| 13 | + serializeTableElementToCsv, |
| 14 | + serializeTableElementToMarkdown, |
| 15 | +} from "../markdown-clipboard"; |
10 | 16 | import { GitHubIcon } from "./Icons"; |
11 | 17 | import { Button } from "./ui/button"; |
12 | 18 | import { setMarkdownTaskChecked } from "./files/filePreviewMode"; |
13 | 19 |
|
14 | 20 | vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null })); |
15 | 21 | vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) })); |
| 22 | +const settingsOverrides = vi.hoisted(() => ({}) as Record<string, unknown>); |
16 | 23 | vi.mock("../hooks/useSettings", async (importOriginal) => { |
17 | 24 | const actual = await importOriginal<typeof import("../hooks/useSettings")>(); |
18 | | - const settings = actual.getClientSettings(); |
| 25 | + const defaults = actual.getClientSettings(); |
19 | 26 | return { |
20 | 27 | ...actual, |
21 | | - useClientSettings: (select?: (value: typeof settings) => unknown) => |
22 | | - select ? select(settings) : settings, |
| 28 | + useClientSettings: (select?: (value: typeof defaults) => unknown) => { |
| 29 | + const settings = { ...defaults, ...settingsOverrides }; |
| 30 | + return select ? select(settings) : settings; |
| 31 | + }, |
23 | 32 | }; |
24 | 33 | }); |
25 | 34 | vi.mock("./ui/tooltip", async () => { |
@@ -1034,3 +1043,268 @@ describe("ChatMarkdown Windows file links", () => { |
1034 | 1043 | expect(html).not.toContain("chat-markdown-file-link"); |
1035 | 1044 | }); |
1036 | 1045 | }); |
| 1046 | + |
| 1047 | +describe("ChatMarkdown math", () => { |
| 1048 | + // A real assistant reply: `\(…\)` inline, including table cells, and `\[` / `\]` on their own lines. |
| 1049 | + const reply = [ |
| 1050 | + "**Your revised \\(G\\) value is reproducible.** Starting from the printed mean free path:", |
| 1051 | + "", |
| 1052 | + "\\[", |
| 1053 | + "r_1=3.88781\\times10^{-5}\\frac{5772}{6000}", |
| 1054 | + "=3.74007322\\times10^{-5}\\ \\mathrm m.", |
| 1055 | + "\\]", |
| 1056 | + "", |
| 1057 | + "| Quantity | Calculated | Accepted |", |
| 1058 | + "|---|---:|---:|", |
| 1059 | + "| \\(G\\) | \\(6.74731\\times10^{-11}\\) | \\(6.67430\\times10^{-11}\\) |", |
| 1060 | + "", |
| 1061 | + "Your equations imply", |
| 1062 | + "", |
| 1063 | + "\\[", |
| 1064 | + "\\sigma\\propto\\frac{1}{r_1T^4},\\qquad r_1\\propto T", |
| 1065 | + "\\quad\\Rightarrow\\quad \\sigma\\propto T^{-5}.", |
| 1066 | + "\\]", |
| 1067 | + ].join("\n"); |
| 1068 | + |
| 1069 | + async function renderMath( |
| 1070 | + text: string, |
| 1071 | + props: Partial<ComponentProps<typeof ChatMarkdown>> = {}, |
| 1072 | + ) { |
| 1073 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 1074 | + // KaTeX loads lazily; having the module already resolved lets its Suspense boundaries settle. |
| 1075 | + await import("./chat/KatexMath"); |
| 1076 | + const container = document.createElement("div"); |
| 1077 | + const root = createRoot(container); |
| 1078 | + await act(async () => { |
| 1079 | + root.render(<ChatMarkdown cwd="/tmp/project" text={text} {...props} />); |
| 1080 | + }); |
| 1081 | + const tex = (selector: string) => |
| 1082 | + [...container.querySelectorAll(selector)].map( |
| 1083 | + (node) => node.querySelector('annotation[encoding="application/x-tex"]')?.textContent, |
| 1084 | + ); |
| 1085 | + return { container, tex, unmount: () => act(async () => root.unmount()) }; |
| 1086 | + } |
| 1087 | + |
| 1088 | + it("renders \\(…\\) and \\[…\\] as math when math rendering is enabled", async () => { |
| 1089 | + settingsOverrides.mathRenderingEnabled = true; |
| 1090 | + const view = await renderMath(reply); |
| 1091 | + try { |
| 1092 | + expect(view.tex(".katex-display")).toEqual([ |
| 1093 | + "r_1=3.88781\\times10^{-5}\\frac{5772}{6000}\n=3.74007322\\times10^{-5}\\ \\mathrm m.", |
| 1094 | + "\\sigma\\propto\\frac{1}{r_1T^4},\\qquad r_1\\propto T\n\\quad\\Rightarrow\\quad \\sigma\\propto T^{-5}.", |
| 1095 | + ]); |
| 1096 | + expect(view.tex("td .katex")).toEqual([ |
| 1097 | + "G", |
| 1098 | + "6.74731\\times10^{-11}", |
| 1099 | + "6.67430\\times10^{-11}", |
| 1100 | + ]); |
| 1101 | + expect(view.container.querySelector("strong .katex")).not.toBeNull(); |
| 1102 | + } finally { |
| 1103 | + await view.unmount(); |
| 1104 | + delete settingsOverrides.mathRenderingEnabled; |
| 1105 | + } |
| 1106 | + }); |
| 1107 | + |
| 1108 | + it.each([true, false])("renders math with parseRawHtml=%s", async (parseRawHtml) => { |
| 1109 | + settingsOverrides.mathRenderingEnabled = true; |
| 1110 | + const view = await renderMath("Inline \\(a_1\\) here.\n\n\\[\nb^2\n\\]", { |
| 1111 | + parseRawHtml, |
| 1112 | + lineBreaks: !parseRawHtml, |
| 1113 | + }); |
| 1114 | + try { |
| 1115 | + expect(view.tex(".katex")).toEqual(["a_1", "b^2"]); |
| 1116 | + } finally { |
| 1117 | + await view.unmount(); |
| 1118 | + delete settingsOverrides.mathRenderingEnabled; |
| 1119 | + } |
| 1120 | + }); |
| 1121 | + |
| 1122 | + it("keeps code, escapes, currency, and unfinished formulas as written", async () => { |
| 1123 | + settingsOverrides.mathRenderingEnabled = true; |
| 1124 | + const view = await renderMath( |
| 1125 | + [ |
| 1126 | + "Costs $5 and $10, and `\\(x\\)` stays code, as does \\\\(y\\\\).", |
| 1127 | + "", |
| 1128 | + "```tex", |
| 1129 | + "\\[", |
| 1130 | + "z", |
| 1131 | + "\\]", |
| 1132 | + "```", |
| 1133 | + "", |
| 1134 | + "Still streaming \\(w and a [link](https://x.test/a_\\(b\\))", |
| 1135 | + ].join("\n"), |
| 1136 | + ); |
| 1137 | + try { |
| 1138 | + expect(view.container.querySelector(".katex")).toBeNull(); |
| 1139 | + expect(view.container.querySelector("code")?.textContent).toBe("\\(x\\)"); |
| 1140 | + expect(view.container.querySelector("pre code")?.textContent).toBe("\\[\nz\n\\]\n"); |
| 1141 | + expect(view.container.textContent).toContain("Costs $5 and $10"); |
| 1142 | + expect(view.container.querySelector("a")?.getAttribute("href")).toBe("https://x.test/a_(b)"); |
| 1143 | + } finally { |
| 1144 | + await view.unmount(); |
| 1145 | + delete settingsOverrides.mathRenderingEnabled; |
| 1146 | + } |
| 1147 | + }); |
| 1148 | + |
| 1149 | + it("shows the source of TeX that KaTeX rejects and copies formulas as TeX", async () => { |
| 1150 | + settingsOverrides.mathRenderingEnabled = true; |
| 1151 | + const view = await renderMath("Good \\(x^2\\), bad \\(\\frac{1\\)."); |
| 1152 | + try { |
| 1153 | + expect(view.tex(".katex")).toEqual(["x^2"]); |
| 1154 | + const copies = [...view.container.querySelectorAll("[data-markdown-copy]")].map((node) => |
| 1155 | + node.getAttribute("data-markdown-copy"), |
| 1156 | + ); |
| 1157 | + expect(copies).toEqual(["\\(x^2\\)", "\\(\\frac{1\\)"]); |
| 1158 | + expect(view.container.querySelector("code")?.textContent).toBe("\\frac{1"); |
| 1159 | + } finally { |
| 1160 | + await view.unmount(); |
| 1161 | + delete settingsOverrides.mathRenderingEnabled; |
| 1162 | + } |
| 1163 | + }); |
| 1164 | + |
| 1165 | + describe("clipboard", () => { |
| 1166 | + const displayTex = "r_1=3.88781\\times10^{-5}\\frac{5772}{6000}\n=3.74\\ \\mathrm m."; |
| 1167 | + const source = `Intro \\(a_1\\) text.\n\n\\[\n${displayTex}\n\\]\n\nOutro.`; |
| 1168 | + |
| 1169 | + async function copyFrom(select: (container: HTMLElement, range: Range) => void) { |
| 1170 | + settingsOverrides.mathRenderingEnabled = true; |
| 1171 | + const view = await renderMath(source); |
| 1172 | + const range = document.createRange(); |
| 1173 | + select(view.container, range); |
| 1174 | + const selection = { rangeCount: 1, getRangeAt: () => range } as unknown as Selection; |
| 1175 | + const payload = chatMarkdownClipboardPayload(selection); |
| 1176 | + await view.unmount(); |
| 1177 | + delete settingsOverrides.mathRenderingEnabled; |
| 1178 | + return payload; |
| 1179 | + } |
| 1180 | + |
| 1181 | + it.each([ |
| 1182 | + [ |
| 1183 | + "the whole formula", |
| 1184 | + (c: HTMLElement, r: Range) => |
| 1185 | + r.selectNode(c.querySelector(".katex-display")!.parentElement!), |
| 1186 | + ], |
| 1187 | + [ |
| 1188 | + "the formula's contents", |
| 1189 | + (c: HTMLElement, r: Range) => |
| 1190 | + r.selectNodeContents(c.querySelector(".katex-display")!.parentElement!), |
| 1191 | + ], |
| 1192 | + [ |
| 1193 | + "its visible .katex-html", |
| 1194 | + (c: HTMLElement, r: Range) => |
| 1195 | + r.selectNodeContents(c.querySelector(".katex-display .katex-html")!), |
| 1196 | + ], |
| 1197 | + [ |
| 1198 | + "one glyph run inside it", |
| 1199 | + (c: HTMLElement, r: Range) => |
| 1200 | + r.selectNodeContents(c.querySelector(".katex-display .katex-html .base")!), |
| 1201 | + ], |
| 1202 | + [ |
| 1203 | + "an inline formula's .katex-html", |
| 1204 | + (c: HTMLElement, r: Range) => r.selectNodeContents(c.querySelector("p .katex-html")!), |
| 1205 | + ], |
| 1206 | + ])("copies the TeX when %s is selected", async (name, select) => { |
| 1207 | + const payload = await copyFrom(select); |
| 1208 | + expect(payload?.text).toBe( |
| 1209 | + name === "an inline formula's .katex-html" ? "\\(a_1\\)" : `\\[\n${displayTex}\n\\]`, |
| 1210 | + ); |
| 1211 | + // The rich flavor is the same source, never KaTeX's hidden MathML or its glyph soup. |
| 1212 | + expect(payload?.html).not.toMatch(/katex|annotation|<math/); |
| 1213 | + expect(payload?.html).toContain("<code>"); |
| 1214 | + }); |
| 1215 | + |
| 1216 | + it("keeps one coherent copy when a selection spans prose and formulas", async () => { |
| 1217 | + const payload = await copyFrom((container, range) => { |
| 1218 | + range.setStartBefore(container.querySelector("p")!); |
| 1219 | + range.setEndAfter(container.lastElementChild!); |
| 1220 | + }); |
| 1221 | + expect(payload?.text).toBe(`Intro \\(a_1\\) text.\n\n\\[\n${displayTex}\n\\]\n\nOutro.`); |
| 1222 | + expect(payload?.html).not.toMatch(/katex|annotation|<math/); |
| 1223 | + expect(payload?.html).toContain("Outro."); |
| 1224 | + }); |
| 1225 | + }); |
| 1226 | + |
| 1227 | + it("copies table math as TeX in CSV and Markdown while ordinary cells stay as before", async () => { |
| 1228 | + settingsOverrides.mathRenderingEnabled = true; |
| 1229 | + const view = await renderMath( |
| 1230 | + "| Quantity | Value |\n|---|---|\n| \\(G\\) | \\(6.7\\times10^{-11}\\) |\n| **bold**, plain | so \\(\\sigma\\) here |", |
| 1231 | + ); |
| 1232 | + try { |
| 1233 | + const table = view.container.querySelector("table")!; |
| 1234 | + expect(serializeTableElementToCsv(table)).toBe( |
| 1235 | + 'Quantity,Value\n\\(G\\),\\(6.7\\times10^{-11}\\)\n"bold, plain",so \\(\\sigma\\) here', |
| 1236 | + ); |
| 1237 | + expect(serializeTableElementToMarkdown(table)).toContain( |
| 1238 | + "| \\(G\\) | \\(6.7\\times10^{-11}\\) |", |
| 1239 | + ); |
| 1240 | + } finally { |
| 1241 | + await view.unmount(); |
| 1242 | + delete settingsOverrides.mathRenderingEnabled; |
| 1243 | + } |
| 1244 | + }); |
| 1245 | + |
| 1246 | + it("keeps code that only looks like math as code while math rendering is on", async () => { |
| 1247 | + settingsOverrides.mathRenderingEnabled = true; |
| 1248 | + const view = await renderMath( |
| 1249 | + [ |
| 1250 | + "```math-inline", |
| 1251 | + "a_1", |
| 1252 | + "```", |
| 1253 | + "", |
| 1254 | + "```math-display", |
| 1255 | + "b^2", |
| 1256 | + "```", |
| 1257 | + "", |
| 1258 | + '<code class="language-math-inline">c^3</code> and <span><code class="language-math-inline">d^4</code></span>', |
| 1259 | + "", |
| 1260 | + '<pre><code class="language-math-display">e^5</code></pre>', |
| 1261 | + "", |
| 1262 | + "Real \\(f\\).", |
| 1263 | + ].join("\n"), |
| 1264 | + ); |
| 1265 | + try { |
| 1266 | + expect(view.tex(".katex")).toEqual(["f"]); |
| 1267 | + const code = view.container.textContent ?? ""; |
| 1268 | + for (const source of ["a_1", "b^2", "c^3", "d^4", "e^5"]) expect(code).toContain(source); |
| 1269 | + } finally { |
| 1270 | + await view.unmount(); |
| 1271 | + delete settingsOverrides.mathRenderingEnabled; |
| 1272 | + } |
| 1273 | + }); |
| 1274 | + |
| 1275 | + it("points task checkboxes at their own source after math", async () => { |
| 1276 | + settingsOverrides.mathRenderingEnabled = true; |
| 1277 | + const text = "Do \\(a\\) first:\n\n\\[\nb\n\\]\n\n- [ ] one \\(c\\)\n- [x] two"; |
| 1278 | + const view = await renderMath(text); |
| 1279 | + try { |
| 1280 | + const markers = [...view.container.querySelectorAll("li")].map((item) => { |
| 1281 | + const offset = Number(item.getAttribute("data-task-marker-offset")); |
| 1282 | + return text.slice(offset, offset + 3); |
| 1283 | + }); |
| 1284 | + expect(markers).toEqual(["[ ]", "[x]"]); |
| 1285 | + } finally { |
| 1286 | + await view.unmount(); |
| 1287 | + delete settingsOverrides.mathRenderingEnabled; |
| 1288 | + } |
| 1289 | + }); |
| 1290 | + |
| 1291 | + it("does not typeset math-classed raw HTML while math rendering is off", async () => { |
| 1292 | + const view = await renderMath('<code class="language-math-inline">x^2</code>'); |
| 1293 | + try { |
| 1294 | + expect(view.container.querySelector(".katex")).toBeNull(); |
| 1295 | + expect(view.container.querySelector("code")?.textContent).toBe("x^2"); |
| 1296 | + } finally { |
| 1297 | + await view.unmount(); |
| 1298 | + } |
| 1299 | + }); |
| 1300 | + |
| 1301 | + it("leaves the source as ordinary Markdown while math rendering is off", async () => { |
| 1302 | + const view = await renderMath(reply); |
| 1303 | + try { |
| 1304 | + expect(view.container.querySelector(".katex")).toBeNull(); |
| 1305 | + expect(view.container.textContent).toContain("(G)"); |
| 1306 | + } finally { |
| 1307 | + await view.unmount(); |
| 1308 | + } |
| 1309 | + }); |
| 1310 | +}); |
0 commit comments