Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdown-clipboard.test.ts

426 lines15.3 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 serializeCodeBlockToMarkdown,
5 serializeRenderedMarkdownFragment,
6 serializeTableElementToMarkdown,
7} from "./markdown-clipboard";
8import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
9import {
10 collectAssistantCitations,
11 serializeAssistantCitation,
12} from "@t3tools/shared/assistantCitations";
13import { markdownMathRanges, parseMarkdownWithMath } from "@t3tools/shared/markdownMath";
14​
15const TEXT_NODE = 3;
16const ELEMENT_NODE = 1;
17​
18class FakeText {
19 readonly nodeType = TEXT_NODE;
20 readonly childNodes: ReadonlyArray<never> = [];
21​
22 constructor(readonly textContent: string) {}
24​
25class FakeElement {
26 readonly nodeType = ELEMENT_NODE;
27 checked = false;
28 readonly childNodes: Array<FakeElement | FakeText> = [];
29 readonly classList = {
30 contains: (name: string) => this.classNames.includes(name),
31 };
32​
33 constructor(
34 readonly tagName: string,
35 private readonly classNames: ReadonlyArray<string> = [],
36 private readonly attributes: Readonly<Record<string, string>> = {},
37 ) {}
38​
39 get localName(): string {
40 return this.tagName.toLowerCase();
41 }
42​
43 get textContent(): string {
44 return this.childNodes.map((child) => child.textContent).join("");
45 }
46​
47 get children(): ReadonlyArray<FakeElement> {
48 return this.childNodes.filter((child): child is FakeElement => child instanceof FakeElement);
49 }
50​
51 append(...children: Array<FakeElement | FakeText>): this {
52 this.childNodes.push(...children);
53 return this;
54 }
55​
56 getAttribute(name: string): string | null {
57 return this.attributes[name] ?? null;
58 }
59​
60 hasAttribute(name: string): boolean {
61 return Object.hasOwn(this.attributes, name);
62 }
63​
64 closest(): FakeElement | null {
65 return null;
66 }
67​
68 querySelector(selector: string): FakeElement | null {
69 if (selector.includes(", ")) {
70 for (const part of selector.split(", ")) {
71 const match = this.querySelector(part);
72 if (match) return match;
73 }
74 return null;
75 }
76 const childOnly = selector.startsWith(":scope > ");
77 const [target, ...rest] = (childOnly ? selector.slice(":scope > ".length) : selector).split(
78 " > ",
79 );
80 const matches = (element: FakeElement): boolean => {
81 if (target === 'input[type="checkbox"]') {
82 return element.tagName === "INPUT" && element.getAttribute("type") === "checkbox";
83 }
84 return element.tagName === target?.toUpperCase();
85 };
86 const search = (parent: FakeElement): FakeElement | null => {
87 for (const child of parent.childNodes) {
88 if (!(child instanceof FakeElement)) continue;
89 if (matches(child)) {
90 if (rest.length === 0) return child;
91 const nested = child.querySelector(`:scope > ${rest.join(" > ")}`);
92 if (nested) return nested;
93 }
94 if (!childOnly) {
95 const nested = search(child);
96 if (nested) return nested;
97 }
98 }
99 return null;
100 };
101 return search(this);
102 }
103​
104 /** Supports the `:scope > a > b` child paths the table serializer queries. */
105 querySelectorAll(selector: string): FakeElement[] {
106 return selector.split(", ").flatMap((part) =>
107 part
108 .replace(/^:scope > /, "")
109 .split(" > ")
110 .reduce<FakeElement[]>(
111 (level, tag) =>
112 level.flatMap((element) =>
113 element.children.filter((child) => child.tagName === tag.toUpperCase()),
114 ),
115 [this],
116 ),
117 );
118 }
120​
121function asNode(element: FakeElement): Node {
122 return element as unknown as Node;
124​
125function shikiCodeLine(text: string): FakeElement {
126 const token = new FakeElement("SPAN").append(new FakeText(text));
127 return new FakeElement("SPAN", ["line"]).append(token);
129​
130function renderedCodeBlock(lines: ReadonlyArray<string>): FakeElement {
131 const code = new FakeElement("CODE");
132 lines.forEach((line, index) => {
133 if (index > 0) code.append(new FakeText("\n"));
134 code.append(shikiCodeLine(line));
135 });
136 return new FakeElement("DIV", ["chat-markdown-codeblock"]).append(
137 new FakeElement("DIV", ["chat-markdown-codeblock-header", "select-none"]).append(
138 new FakeText("sh"),
139 ),
140 new FakeElement("DIV", ["chat-markdown-shiki"]).append(new FakeElement("PRE").append(code)),
141 );
143​
144describe("serializeRenderedMarkdownFragment", () => {
145 it("copies a popover context reference once, without its details or nested label", () => {
146 const reference = "[Review comment](t3-context://v1/review-comment/review-1)";
147 const container = new FakeElement("DIV").append(
148 new FakeText("Fix "),
149 new FakeElement("BUTTON", [], { "data-markdown-copy": reference }).append(
150 new FakeElement("SPAN", [], { "data-markdown-copy": reference }).append(
151 new FakeText("Review comment"),
152 ),
153 ),
154 new FakeText(" please"),
155 );
156​
157 expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(`Fix ${reference} please`);
158 });
159​
160 beforeEach(() => {
161 vi.stubGlobal("Node", { TEXT_NODE, ELEMENT_NODE });
162 });
163​
164 afterEach(() => {
165 vi.unstubAllGlobals();
166 });
167​
168 it("copies the complete quote, source, and comment instead of the comment-only chip label", () => {
169 const citation = {
170 version: 1 as const,
171 environmentId: EnvironmentId.make("environment-one"),
172 threadId: ThreadId.make("thread-one"),
173 messageId: MessageId.make("assistant-one"),
174 text: "A complete quote, including the part hidden by the chip preview.",
175 comment: "What does this mean?\nPlease expand on the hidden part.",
176 start: 0,
177 end: 66,
178 prefix: "",
179 suffix: "",
180 };
181 const anchor = new FakeElement("A", [], {
182 "data-markdown-copy": serializeAssistantCitation(citation),
183 href: "/environment-one/thread-one#citation",
184 }).append(new FakeText("What does this mean?…"));
185 const chip = new FakeElement("SPAN", [], {
186 "data-markdown-copy": serializeAssistantCitation(citation),
187 }).append(anchor, new FakeElement("BUTTON").append(new FakeText("Edit comment")));
188 const container = new FakeElement("DIV").append(new FakeText("Explain "), chip);
189 const copied = serializeRenderedMarkdownFragment(asNode(container));
190 expect(copied).toBe(`Explain ${serializeAssistantCitation(citation)}`);
191 expect(collectAssistantCitations(copied).map((entry) => entry.citation)).toEqual([citation]);
192 });
193​
194 it("keeps a multi-line code selection plain instead of inline-wrapping it", () => {
195 const code = new FakeElement("CODE").append(new FakeText("first line\nsecond line"));
196 const container = new FakeElement("DIV").append(code);
197​
198 expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("first line\nsecond line");
199 });
200​
201 it.each([
202 ["backtick edges", "`value`"],
203 ["space edges", " value "],
204 ["only spaces", " "],
205 ])("copies inline code that parses back unchanged: %s", (_, value) => {
206 const container = new FakeElement("DIV").append(
207 new FakeElement("P").append(
208 new FakeText("Use "),
209 new FakeElement("CODE").append(new FakeText(value)),
210 new FakeText(" here."),
211 ),
212 );
213 const paragraph = parseMarkdownWithMath(serializeRenderedMarkdownFragment(asNode(container)))
214 .children[0];
215 if (paragraph?.type !== "paragraph") throw new Error("Expected a paragraph");
216 expect(paragraph.children.find((child) => child.type === "inlineCode")?.value).toBe(value);
217 });
218​
219 it("copies alert and insight callouts as their Markdown instead of the rendered label", () => {
220 // The shape ChatMarkdown renders for `> [!WARNING]\n> Back up first.`.
221 const callout = new FakeElement("DIV", [], {
222 role: "note",
223 "data-markdown-alert": "warning",
224 }).append(
225 new FakeElement("P", [], {
226 "data-thread-find-ignore": "true",
227 "data-markdown-alert-label": "",
228 }).append(new FakeElement("SVG", [], { "aria-hidden": "true" }), new FakeText("Warning")),
229 new FakeElement("P").append(new FakeText("Back up first.")),
230 );
231 const container = new FakeElement("DIV").append(callout);
232​
233 expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(
234 "> [!WARNING]\n> Back up first.",
235 );
236 const insight = new FakeElement("DIV", [], {
237 role: "note",
238 "data-markdown-alert": "insight",
239 }).append(
240 new FakeElement("P", [], {
241 "data-thread-find-ignore": "true",
242 "data-markdown-alert-label": "",
243 }).append(new FakeText("Insight")),
244 new FakeElement("P").append(new FakeText("Caches are warm.")),
245 );
246 expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(insight)))).toBe(
247 `★ Insight ${"─".repeat(37)}\nCaches are warm.\n${"─".repeat(49)}`,
248 );
249 });
250​
251 it("preserves explicit ordered-list values and continues from resets when copying", () => {
252 const list = new FakeElement("OL", [], { start: "0" }).append(
253 new FakeElement("LI").append(new FakeText("A")),
254 new FakeElement("LI", [], { value: "5" }).append(new FakeText("B")),
255 new FakeElement("LI").append(new FakeText("C")),
256 new FakeElement("LI", [], { value: "0" }).append(new FakeText("D")),
257 );
258 expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(list)))).toBe(
259 "0. A\n5. B\n6. C\n0. D",
260 );
261 });
262​
263 it("keeps a bare list item that sits alongside the code block", () => {
264 const container = new FakeElement("DIV").append(
265 new FakeElement("UL").append(new FakeElement("LI")),
266 renderedCodeBlock(["pnpm test"]),
267 );
268​
269 expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("-\n\npnpm test");
270 });
271​
272 it("copies a command and the prose after it as shown, without code fences", () => {
273 const container = new FakeElement("DIV").append(
274 renderedCodeBlock(["printf 'hello\\n'"]),
275 new FakeElement("P").append(new FakeText("T")),
276 );
277​
278 expect(serializeRenderedMarkdownFragment(asNode(container))).toBe("printf 'hello\\n'\n\nT");
279 });
280​
281 it("keeps a diagram's source exactly as written when copied with prose", () => {
282 const source = 'flowchart TD\n A["first \n\n\nlast"]';
283 const container = new FakeElement("DIV").append(
284 new FakeElement("DIV", [], {
285 "data-markdown-mermaid": "",
286 "data-markdown-copy": serializeCodeBlockToMarkdown(source, "mermaid"),
287 }),
288 new FakeElement("P").append(new FakeText("After")),
289 );
290​
291 expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(
292 `\`\`\`mermaid\n${source}\n\`\`\`\n\nAfter`,
293 );
294 });
295​
296 it("keeps blank lines inside a code block in a list item, quote, or longer fence", () => {
297 const code = ["import os", "", "", "def main():"];
298 const list = new FakeElement("OL").append(
299 new FakeElement("LI").append(new FakeText("Create it:\n"), renderedCodeBlock(code)),
300 );
301 const quote = new FakeElement("BLOCKQUOTE").append(
302 new FakeElement("P").append(new FakeText("Note:")),
303 renderedCodeBlock(code),
304 );
305 const nestedFence = new FakeElement("DIV").append(
306 new FakeElement("P").append(new FakeText("Example:")),
307 renderedCodeBlock(["```js", "a()", "", "", "b() ", "```"]),
308 );
309​
310 expect(serializeRenderedMarkdownFragment(asNode(nestedFence))).toBe(
311 "Example:\n\n```js\na()\n\n\nb() \n```",
312 );
313​
314 expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(list)))).toBe(
315 "1. Create it:\n import os\n\n\n def main():",
316 );
317 expect(serializeRenderedMarkdownFragment(asNode(new FakeElement("DIV").append(quote)))).toBe(
318 "> Note:\n>\n> import os\n>\n>\n> def main():",
319 );
320 });
321​
322 it("omits fences when a selection past the last line drags in the whole code block", () => {
323 const container = new FakeElement("DIV").append(
324 renderedCodeBlock(["printf '%s' 'TOKEN' | gh secret set CLOUDFLARE_API_TOKEN"]),
325 new FakeText("\n"),
326 new FakeElement("P").append(new FakeText("")),
327 );
328​
329 expect(serializeRenderedMarkdownFragment(asNode(container))).toBe(
330 "printf '%s' 'TOKEN' | gh secret set CLOUDFLARE_API_TOKEN",
331 );
332 });
333});
334​
335function renderedFormula(tex: string): FakeElement {
336 // MarkdownMath's wrapper; its children stand in for KaTeX's glyphs.
337 return new FakeElement("SPAN", [], {
338 "data-markdown-math": "inline",
339 "data-markdown-copy": `\\(${tex}\\)`,
340 }).append(new FakeText(tex));
342​
343function renderedTable(...rows: Array<Array<FakeElement | FakeText>>): FakeElement {
344 const row = (tag: string, cells: Array<FakeElement | FakeText>) =>
345 new FakeElement("TR").append(...cells.map((cell) => new FakeElement(tag).append(cell)));
346 const [head = [], ...body] = rows;
347 return new FakeElement("TABLE").append(
348 new FakeElement("THEAD").append(row("TH", head)),
349 new FakeElement("TBODY").append(...body.map((cells) => row("TD", cells))),
350 );
352​
353describe("copying rendered Markdown tables", () => {
354 beforeEach(() => {
355 vi.stubGlobal("Node", { TEXT_NODE, ELEMENT_NODE });
356 });
357​
358 afterEach(() => {
359 vi.unstubAllGlobals();
360 });
361​
362 it("keeps formula pipes as authored while escaping prose pipes", () => {
363 const table = renderedTable(
364 [new FakeText("Formula"), new FakeText("Note")],
365 [renderedFormula("|x|"), new FakeText("a | b")],
366 [renderedFormula("\\|x\\|"), new FakeElement("CODE").append(new FakeText("\\(c|d\\)"))],
367 [
368 new FakeElement("A", [], { href: "https://example.com/?q=a|b" }).append(
369 new FakeText("norm "),
370 renderedFormula("|y|"),
371 ),
372 new FakeText("e"),
373 ],
374 [
375 new FakeElement("UL").append(
376 new FakeElement("LI").append(renderedFormula("|z|"), new FakeText(" a | b")),
377 ),
378 new FakeElement("BLOCKQUOTE").append(new FakeElement("P").append(new FakeText("c | d"))),
379 ],
380 );
381 const markdown = serializeTableElementToMarkdown(table as unknown as Element);
382​
383 const tree = parseMarkdownWithMath(markdown);
384 const parsed = tree.children[0];
385 if (parsed?.type !== "table") throw new Error("Expected a table");
386 expect(parsed.children.map((row) => row.children.length)).toEqual([2, 2, 2, 2, 2]);
387 expect(markdownMathRanges(markdown, tree).map((formula) => formula.tex)).toEqual([
388 "|x|",
389 "\\|x\\|",
390 "|y|",
391 "|z|",
392 ]);
393 expect(parsed.children[1]?.children[1]?.children).toMatchObject([
394 { type: "text", value: "a | b" },
395 ]);
396 expect(parsed.children[4]?.children[1]?.children).toMatchObject([
397 { type: "text", value: "> c | d" },
398 ]);
399 const [, , codeRow, linkRow] = parsed.children;
400 expect(codeRow?.children[1]?.children[0]).toMatchObject({
401 type: "inlineCode",
402 value: "\\(c|d\\)",
403 });
404 expect(linkRow?.children[0]?.children[0]).toMatchObject({
405 type: "link",
406 url: "https://example.com/?q=a|b",
407 });
408 });
409​
410 it("copies code and diagrams in cells without markers or unescaped pipes", () => {
411 const code = new FakeElement("PRE").append(
412 new FakeElement("CODE").append(new FakeText("a b")),
413 );
414 const diagram = new FakeElement("DIV", [], {
415 "data-markdown-mermaid": "",
416 "data-markdown-copy": serializeCodeBlockToMarkdown("A -->|hi| B", "mermaid"),
417 });
418 expect(
419 serializeTableElementToMarkdown(
420 asNode(
421 renderedTable([new FakeText("Command"), new FakeText("Diagram")], [code, diagram]),
422 ) as Element,
423 ),
424 ).toBe("| Command | Diagram |\n| --- | --- |\n| a b | ```mermaid A -->\\|hi\\| B ``` |");
425 });
426});