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

ChatMarkdown.test.tsx

499 lines17.4 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { renderToStaticMarkup } from "react-dom/server";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
6vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));
7vi.mock("../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => vi.fn() }));
8vi.mock("../state/use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
9vi.mock("../state/session", async (importOriginal) => ({
10 ...(await importOriginal<typeof import("../state/session")>()),
11 usePreparedConnection: () => ({ _tag: "Loading" }),
12}));
13vi.mock("../state/entities", () => ({
14 readThreadShell: () => null,
15 useProjects: () => [],
16}));
17vi.mock("../remoteOpen", () => ({
18 useRemoteOpenResolution: () => ({ state: { mode: "local-exec" }, isResolved: true }),
19}));
20vi.mock("../editorPreferences", () => ({
21 useOpenInPreferredEditor: () => vi.fn(),
22 usePreferredEditor: () => [null, vi.fn()],
23}));
24vi.mock("~/lib/openPullRequestLink", () => ({
25 findProjectForChangeRequest: () => undefined,
26 matchesLinkedPullRequestUrl: () => false,
27 parseChangeRequestUrl: () => null,
28 useOpenChangeRequestLink: () => vi.fn(),
29}));
30​
31import ChatMarkdown, {
32 canUseMarkdownFileShellActions,
33 hasMarkdownFilePrimaryAction,
34 orderedListGutterStyle,
35 shouldUseMarkdownFileBrowserPrimaryAction,
36} from "./ChatMarkdown";
37​
38describe("canUseMarkdownFileShellActions", () => {
39 const environmentId = EnvironmentId.make("environment-1");
40​
41 it("allows editor and file manager actions for local environments", () => {
42 expect(canUseMarkdownFileShellActions(environmentId, "local-exec", true)).toBe(true);
43 });
44​
45 it("hides shell actions until the environment mode is resolved", () => {
46 expect(canUseMarkdownFileShellActions(environmentId, "local-exec", false)).toBe(false);
47 });
48​
49 it("hides editor and file manager actions for remote environments", () => {
50 expect(canUseMarkdownFileShellActions(environmentId, "remote-links", true)).toBe(false);
51 expect(canUseMarkdownFileShellActions(environmentId, "remote-unavailable", true)).toBe(false);
52 });
53​
54 it("hides shell actions when no environment owns the markdown", () => {
55 expect(canUseMarkdownFileShellActions(null, "local-exec", true)).toBe(false);
56 });
57});
58​
59describe("hasMarkdownFilePrimaryAction", () => {
60 it("keeps the chip interactive when an editor, browser, or panel can open it", () => {
61 expect(
62 hasMarkdownFilePrimaryAction({
63 canOpenInEditor: true,
64 canOpenInBrowser: false,
65 canOpenInPanel: false,
66 }),
67 ).toBe(true);
68 expect(
69 hasMarkdownFilePrimaryAction({
70 canOpenInEditor: false,
71 canOpenInBrowser: true,
72 canOpenInPanel: false,
73 }),
74 ).toBe(true);
75 expect(
76 hasMarkdownFilePrimaryAction({
77 canOpenInEditor: false,
78 canOpenInBrowser: false,
79 canOpenInPanel: true,
80 }),
81 ).toBe(true);
82 });
83​
84 it("removes the link affordance when no primary action can open the file", () => {
85 expect(
86 hasMarkdownFilePrimaryAction({
87 canOpenInEditor: false,
88 canOpenInBrowser: false,
89 canOpenInPanel: false,
90 }),
91 ).toBe(false);
92 });
93});
94​
95describe("ChatMarkdown file option chips", () => {
96 it("keeps the fallback button text selectable", () => {
97 const html = renderToStaticMarkup(
98 <ChatMarkdown cwd="/tmp/project" text="[Source](/tmp/project/src/main.ts)" />,
99 );
100​
101 expect(html).toContain("<button");
102 expect(html).toContain('aria-haspopup="menu"');
103 expect(html).toContain("select-text");
104 });
105​
106 it.each([true, false])(
107 "renders Codex file citations as file chips with parseRawHtml=%s",
108 (parseRawHtml) => {
109 const html = renderToStaticMarkup(
110 <ChatMarkdown
111 cwd="/tmp/project"
112 text={
113 'Created :codex-file-citation{path="/tmp/project/outputs/report.xlsx" purpose="output"}.'
114 }
115 lineBreaks={!parseRawHtml}
116 parseRawHtml={parseRawHtml}
117 />,
118 );
119​
120 expect(html).not.toContain("codex-file-citation");
121 expect(html).toContain("chat-markdown-file-link");
122 expect(html).toContain(
123 'data-markdown-copy="[report.xlsx](/tmp/project/outputs/report.xlsx)"',
124 );
125 expect(html).toContain("report.xlsx");
126 },
127 );
128​
129 it("leaves an unfinished streaming citation visible until it is complete", () => {
130 const html = renderToStaticMarkup(
131 <ChatMarkdown
132 cwd="/tmp/project"
133 text={'Created :codex-file-citation{path="/tmp/project/outputs/report.xlsx"'}
134 isStreaming
135 />,
136 );
137​
138 expect(html).toContain(":codex-file-citation");
139 expect(html).not.toContain("chat-markdown-file-link");
140 });
141​
142 it("leaves malformed and similarly named file directives literal", () => {
143 for (const text of [
144 ':codex-file-citation{purpose="output"}',
145 ':codex-file-citation-extra{path="/tmp/project/outputs/report.xlsx"}',
146 ]) {
147 const html = renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />);
148​
149 expect(html).toContain(text.replaceAll('"', "&quot;"));
150 expect(html).not.toContain("chat-markdown-file-link");
151 }
152 });
153​
154 it("preserves Codex file citation examples inside code", () => {
155 const directive = ':codex-file-citation{path="/tmp/project/outputs/report.xlsx"}';
156 const html = renderToStaticMarkup(
157 <ChatMarkdown
158 cwd="/tmp/project"
159 text={`Example: \`${directive}\`\n\n\`\`\`text\n${directive}\n\`\`\``}
160 />,
161 );
162​
163 expect(html.match(/:codex-file-citation/g)).toHaveLength(2);
164 expect(html).not.toContain("chat-markdown-file-link");
165 });
166​
167 it("preserves escaped Codex file citations as literal text", () => {
168 const html = renderToStaticMarkup(
169 <ChatMarkdown
170 cwd="/tmp/project"
171 text={'Example: \\:codex-file-citation{path="/tmp/project/outputs/report.xlsx"}'}
172 />,
173 );
174​
175 expect(html).toContain(":codex-file-citation");
176 expect(html).not.toContain("chat-markdown-file-link");
177 });
178​
179 it("does not create a nested link for citations inside link text", () => {
180 const directive = ':codex-file-citation{path="/tmp/project/outputs/report.xlsx"}';
181 const html = renderToStaticMarkup(
182 <ChatMarkdown cwd="/tmp/project" text={`[See ${directive}](https://example.com)`} />,
183 );
184 const renderedText = html.replace(/<[^>]+>/g, "");
185​
186 expect(renderedText).toContain("codex-file-citation");
187 expect(html).not.toContain("chat-markdown-file-link");
188 });
189​
190 it("renders file citations created by over-indented list recovery", () => {
191 const html = renderToStaticMarkup(
192 <ChatMarkdown
193 cwd="/tmp/project"
194 text={'- Created :codex-file-citation{path="/tmp/project/outputs/report.xlsx"}'}
195 />,
196 );
197​
198 expect(html).not.toContain("<pre>");
199 expect(html).toContain("Created ");
200 expect(html).toContain("chat-markdown-file-link");
201 expect(html).toContain("report.xlsx");
202 });
203​
204 it("disambiguates Codex citations with the same basename", () => {
205 const html = renderToStaticMarkup(
206 <ChatMarkdown
207 cwd="/tmp/project"
208 text={
209 'Changed :codex-file-citation{path="/tmp/project/src/index.ts"} and :codex-file-citation{path="/tmp/project/test/index.ts"}.'
210 }
211 />,
212 );
213​
214 expect(html).toContain("index.ts · project/src");
215 expect(html).toContain("index.ts · project/test");
216 });
217​
218 it("preserves rejected citations created by over-indented list recovery", () => {
219 const malformedHtml = renderToStaticMarkup(
220 <ChatMarkdown
221 cwd="/tmp/project"
222 text={'Leading text before list.\n\n- Bad :codex-file-citation{purpose="output"}'}
223 />,
224 );
225 const nestedLinkHtml = renderToStaticMarkup(
226 <ChatMarkdown
227 cwd="/tmp/project"
228 text={
229 'Leading text before list.\n\n- [Bad :codex-file-citation{path="/tmp/project/report.xlsx"}](https://example.com)'
230 }
231 />,
232 );
233 const nestedLinkText = nestedLinkHtml.replace(/<[^>]+>/g, "");
234​
235 expect(malformedHtml).toContain(
236 "<li>Bad :codex-file-citation{purpose=&quot;output&quot;}</li>",
237 );
238 expect(nestedLinkText).toContain(
239 "Bad :codex-file-citation{path=&quot;/tmp/project/report.xlsx&quot;}",
240 );
241 });
242});
243​
244const ARTIFACT_TEMPLATE_DIRECTIVE =
245 '::artifact-template{skill_name="artifact-template-hello-world" skill_directory="/Users/test/.codex/skills/artifact-template-hello-world" display_name="Hello World" artifact_kind="document"}';
246​
247describe("ChatMarkdown artifact-template cards", () => {
248 it.each([true, false])("renders the Codex result card with parseRawHtml=%s", (parseRawHtml) => {
249 const html = renderToStaticMarkup(
250 <ChatMarkdown
251 cwd="/tmp/project"
252 text={ARTIFACT_TEMPLATE_DIRECTIVE}
253 parseRawHtml={parseRawHtml}
254 onUseArtifactTemplate={() => undefined}
255 />,
256 );
257​
258 expect(html).not.toContain("::artifact-template");
259 expect(html).toContain("chat-markdown-artifact-template");
260 expect(html).toContain('data-artifact-kind="document"');
261 expect(html).toContain('data-markdown-copy="Hello World (Document template)\n\n"');
262 expect(html).toContain('data-skill-name="artifact-template-hello-world"');
263 expect(html).toContain("Hello World");
264 expect(html).toContain("Document template");
265 expect(html).toContain("Use template");
266 expect(html).not.toContain("<p><div");
267 });
268​
269 it("renders a passive card outside a composer-backed timeline", () => {
270 const html = renderToStaticMarkup(
271 <ChatMarkdown cwd="/tmp/project" text={ARTIFACT_TEMPLATE_DIRECTIVE} />,
272 );
273​
274 expect(html).toContain("chat-markdown-artifact-template");
275 expect(html).not.toContain("Use template");
276 });
277​
278 it("leaves malformed and unfinished artifact-template directives literal", () => {
279 const malformed =
280 '::artifact-template{skill_name="artifact-template-hello-world" display_name="Hello World" artifact_kind="document"}';
281 const unfinished = ARTIFACT_TEMPLATE_DIRECTIVE.slice(0, -1);
282​
283 for (const text of [malformed, unfinished]) {
284 const html = renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />);
285 expect(html).toContain("::artifact-template");
286 expect(html).not.toContain("chat-markdown-artifact-template");
287 }
288 });
289​
290 it("leaves escaped and similarly named artifact-template directives literal", () => {
291 for (const text of [
292 `\\${ARTIFACT_TEMPLATE_DIRECTIVE}`,
293 ARTIFACT_TEMPLATE_DIRECTIVE.replace("::artifact-template", "::artifact-template-extra"),
294 ]) {
295 const html = renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />);
296​
297 expect(html).toContain("::artifact-template");
298 expect(html).not.toContain("chat-markdown-artifact-template");
299 }
300 });
301​
302 it("preserves artifact-template examples inside code", () => {
303 const html = renderToStaticMarkup(
304 <ChatMarkdown
305 cwd="/tmp/project"
306 text={`\`${ARTIFACT_TEMPLATE_DIRECTIVE}\`\n\n\`\`\`text\n${ARTIFACT_TEMPLATE_DIRECTIVE}\n\`\`\``}
307 />,
308 );
309​
310 expect(html.match(/::artifact-template/g)).toHaveLength(2);
311 expect(html).not.toContain("chat-markdown-artifact-template");
312 });
313});
314​
315describe("shouldUseMarkdownFileBrowserPrimaryAction", () => {
316 it("uses the browser when it is the only available primary action", () => {
317 expect(
318 shouldUseMarkdownFileBrowserPrimaryAction({
319 iconPath: "/tmp/report.html",
320 canOpenInEditor: false,
321 canOpenInBrowser: true,
322 canOpenInPanel: false,
323 }),
324 ).toBe(true);
325 });
326​
327 it("preserves the normal editor and panel defaults for HTML files", () => {
328 expect(
329 shouldUseMarkdownFileBrowserPrimaryAction({
330 iconPath: "/tmp/report.html",
331 canOpenInEditor: true,
332 canOpenInBrowser: true,
333 canOpenInPanel: false,
334 }),
335 ).toBe(false);
336 expect(
337 shouldUseMarkdownFileBrowserPrimaryAction({
338 iconPath: "/tmp/report.html",
339 canOpenInEditor: false,
340 canOpenInBrowser: true,
341 canOpenInPanel: true,
342 }),
343 ).toBe(false);
344 });
345​
346 it("continues to open PDF files in the browser by default", () => {
347 expect(
348 shouldUseMarkdownFileBrowserPrimaryAction({
349 iconPath: "/tmp/report.pdf",
350 canOpenInEditor: true,
351 canOpenInBrowser: true,
352 canOpenInPanel: true,
353 }),
354 ).toBe(true);
355 });
356});
357​
358describe("orderedListGutterStyle", () => {
359 it("leaves the default gutter alone for single-digit lists", () => {
360 expect(orderedListGutterStyle(9, undefined)).toBeUndefined();
361 });
362​
363 it("leaves the default gutter alone for two-digit lists", () => {
364 expect(orderedListGutterStyle(99, undefined)).toBeUndefined();
365 });
366​
367 it("leaves the default gutter alone for a two-digit list that starts above 1", () => {
368 // start=50 + 49 items => last marker is "98", still two digits.
369 expect(orderedListGutterStyle(49, 50)).toBeUndefined();
370 });
371​
372 it("widens the gutter once the last marker reaches three digits", () => {
373 // item 100 is the bug from #6512: a 100-item list starting at 1.
374 expect(orderedListGutterStyle(100, undefined)).toEqual({ "--list-gutter": "4ch" });
375 });
376​
377 it("accounts for a non-default start attribute", () => {
378 // start=95 + 9 items => last marker is "103", three digits.
379 expect(orderedListGutterStyle(9, 95)).toEqual({ "--list-gutter": "4ch" });
380 expect(orderedListGutterStyle(5, "999995")).toEqual({ "--list-gutter": "7ch" });
381 });
382​
383 it("scales further for four-digit markers", () => {
384 expect(orderedListGutterStyle(1000, undefined)).toEqual({ "--list-gutter": "5ch" });
385 });
386​
387 it("uses the widest marker and includes a negative start's minus sign", () => {
388 expect(orderedListGutterStyle(1001, -1000)).toEqual({ "--list-gutter": "6ch" });
389 expect(orderedListGutterStyle(3, -15)).toEqual({ "--list-gutter": "4ch" });
390 expect(orderedListGutterStyle(3, -5)).toBeUndefined();
391 });
392​
393 it("treats a missing/zero item count as a single item", () => {
394 expect(orderedListGutterStyle(0, undefined)).toBeUndefined();
395 expect(orderedListGutterStyle(0, 100)).toEqual({ "--list-gutter": "4ch" });
396 });
397});
398​
399describe("ChatMarkdown Windows file links", () => {
400 const environmentId = EnvironmentId.make("env-windows");
401​
402 it.each([true, false])("preserves drive paths with parseRawHtml=%s", (parseRawHtml) => {
403 const html = renderToStaticMarkup(
404 <ChatMarkdown
405 cwd="C:/Users/shawn/project"
406 environmentId={environmentId}
407 text="[Open](C:/Users/shawn/project/src/main.ts)"
408 lineBreaks={!parseRawHtml}
409 parseRawHtml={parseRawHtml}
410 />,
411 );
412​
413 expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"');
414 expect(html).toContain("chat-markdown-file-link");
415 });
416​
417 it.each([true, false])("normalizes backslashes with parseRawHtml=%s", (parseRawHtml) => {
418 const html = renderToStaticMarkup(
419 <ChatMarkdown
420 cwd="C:/Users/shawn/project"
421 environmentId={environmentId}
422 text={String.raw`[Open](C:\Users\shawn\project\src\main.ts)`}
423 lineBreaks={!parseRawHtml}
424 parseRawHtml={parseRawHtml}
425 />,
426 );
427​
428 expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"');
429 expect(html).toContain("chat-markdown-file-link");
430 });
431​
432 it.each([true, false])(
433 "distinguishes same-named backslash paths with parseRawHtml=%s",
434 (parseRawHtml) => {
435 const html = renderToStaticMarkup(
436 <ChatMarkdown
437 cwd="C:/Users/shawn/project"
438 environmentId={environmentId}
439 text={String.raw`[Source](C:\Users\shawn\project\src\index.ts) and [Test](C:\Users\shawn\project\test\index.ts)`}
440 lineBreaks={!parseRawHtml}
441 parseRawHtml={parseRawHtml}
442 />,
443 );
444​
445 expect(html).toContain("index.ts · project/src");
446 expect(html).toContain("index.ts · project/test");
447 },
448 );
449​
450 it.each([true, false])(
451 "does not disambiguate the same file in links and inline code with parseRawHtml=%s",
452 (parseRawHtml) => {
453 const path = String.raw`C:\Users\shawn\project\src\main.ts`;
454 const html = renderToStaticMarkup(
455 <ChatMarkdown
456 cwd="C:/Users/shawn/project"
457 environmentId={environmentId}
458 text={`[Source](${path}) and \`${path}\``}
459 lineBreaks={!parseRawHtml}
460 parseRawHtml={parseRawHtml}
461 />,
462 );
463​
464 expect(html.match(/chat-markdown-file-link/g)).toHaveLength(2);
465 expect(html).not.toContain("main.ts ·");
466 },
467 );
468​
469 it.each([true, false])("preserves reference links with parseRawHtml=%s", (parseRawHtml) => {
470 const html = renderToStaticMarkup(
471 <ChatMarkdown
472 cwd="C:/Users/shawn/project"
473 environmentId={environmentId}
474 text={"[Open][source]\n\n[source]: C:/Users/shawn/project/src/main.ts"}
475 lineBreaks={!parseRawHtml}
476 parseRawHtml={parseRawHtml}
477 />,
478 );
479​
480 expect(html).toContain('href="C:/Users/shawn/project/src/main.ts"');
481 expect(html).toContain("chat-markdown-file-link");
482 });
483​
484 it.each([true, false])("still rejects unsafe schemes with parseRawHtml=%s", (parseRawHtml) => {
485 const html = renderToStaticMarkup(
486 <ChatMarkdown
487 cwd="C:/Users/shawn/project"
488 environmentId={environmentId}
489 text="[unsafe](javascript:alert(1)) and [unknown](d:alert(1))"
490 lineBreaks={!parseRawHtml}
491 parseRawHtml={parseRawHtml}
492 />,
493 );
494​
495 expect(html).not.toContain("javascript:");
496 expect(html).not.toContain("d:alert");
497 expect(html).not.toContain("chat-markdown-file-link");
498 });
499});