Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { createElement } from "react";
3import { renderToStaticMarkup } from "react-dom/server";
4import ReactMarkdown from "react-markdown";
5
6import {
7 extractMarkdownLinkHrefs,
8 isWindowsDrivePathHref,
9 resolveInlineCodeFileLinkMeta,
10 resolveMarkdownFileLinkMeta,
11 resolveMarkdownFileLinkTarget,
12 rewriteMarkdownFileUriHref,
13 shouldOpenMarkdownFileLinkInBrowserByDefault,
14 shouldOpenMarkdownFileLinkInEditor,
15} from "./markdown-links";
16
17describe("isWindowsDrivePathHref", () => {
18 it.each([
19 ["C:\\repo\\image.png", true],
20 ["C:%5Crepo%5Cimage.png", true],
21 ["https://example.com/image.png", false],
22 ])("classifies %s as %s", (href, expected) => {
23 expect(isWindowsDrivePathHref(href)).toBe(expected);
24 });
25});
26
27function renderMarkdownLinkHref(markdown: string): string | undefined {
28 let renderedHref: string | undefined;
29 renderToStaticMarkup(
30 createElement(
31 ReactMarkdown,
32 {
33 components: {
34 a({ href }) {
35 renderedHref = href;
36 return createElement("a", { href });
37 },
38 },
39 },
40 markdown,
41 ),
42 );
43 return renderedHref;
44}
45
46describe("extractMarkdownLinkHrefs", () => {
47 it("extracts angle-bracketed paths containing spaces", () => {
48 expect(
49 extractMarkdownLinkHrefs(
50 "[Open the Bike Receipts folder](</Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts>)",
51 ),
52 ).toEqual(["/Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts"]);
53 });
54
55 it("preserves ordinary destinations and ignores link titles", () => {
56 expect(
57 extractMarkdownLinkHrefs(
58 '[source](apps/web/src/markdown-links.ts "implementation") and [docs](https://example.com)',
59 ),
60 ).toEqual(["apps/web/src/markdown-links.ts", "https://example.com"]);
61 });
62});
63
64describe("shouldOpenMarkdownFileLinkInEditor", () => {
65 it("uses command-click on macOS", () => {
66 expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "MacIntel")).toBe(
67 true,
68 );
69 expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "MacIntel")).toBe(
70 false,
71 );
72 });
73
74 it("uses control-click on other platforms", () => {
75 expect(
76 shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "Linux x86_64"),
77 ).toBe(true);
78 expect(
79 shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "Linux x86_64"),
80 ).toBe(false);
81 });
82});
83
84describe("shouldOpenMarkdownFileLinkInBrowserByDefault", () => {
85 it("keeps PDFs browser-first while source files open in the file viewer", () => {
86 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.pdf")).toBe(true);
87 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.PDF?download=1")).toBe(true);
88 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.html")).toBe(false);
89 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.xml")).toBe(false);
90 });
91});
92
93describe("rewriteMarkdownFileUriHref", () => {
94 it("rewrites file uri hrefs into direct path hrefs", () => {
95 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/src/main.ts#L42")).toBe(
96 "/Users/julius/project/src/main.ts#L42",
97 );
98 });
99
100 it("preserves encoded octets so file paths are decoded only once later", () => {
101 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(
102 "/Users/julius/project/file%2520name.md",
103 );
104 });
105
106 it("normalizes file uri hrefs for windows drive paths", () => {
107 expect(
108 rewriteMarkdownFileUriHref(
109 "file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
110 ),
111 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
112 });
113
114 it("preserves file uri authorities as windows UNC paths", () => {
115 expect(rewriteMarkdownFileUriHref("file://server/share/workspace-image.svg")).toBe(
116 "\\\\server\\share\\workspace-image.svg",
117 );
118 });
119
120 it("treats a localhost file uri as a local path", () => {
121 expect(rewriteMarkdownFileUriHref("file://localhost/home/me/notes.md")).toBe(
122 "/home/me/notes.md",
123 );
124 });
125
126 it("unwraps angle-bracketed file uri hrefs", () => {
127 expect(
128 rewriteMarkdownFileUriHref(" <file:///D:/Programme/t3code/apps/web/src/markdown-links.ts> "),
129 ).toBe("D:/Programme/t3code/apps/web/src/markdown-links.ts");
130 });
131});
132
133describe("resolveMarkdownFileLinkTarget", () => {
134 it("resolves absolute posix file paths", () => {
135 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/AGENTS.md")).toBe(
136 "/Users/julius/project/AGENTS.md",
137 );
138 });
139
140 it("resolves relative file paths against cwd", () => {
141 expect(resolveMarkdownFileLinkTarget("src/processRunner.ts:71", "/Users/julius/project")).toBe(
142 "/Users/julius/project/src/processRunner.ts:71",
143 );
144 });
145
146 it("does not treat filename line references as external schemes", () => {
147 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
148 "/Users/julius/project/script.ts:10",
149 );
150 });
151
152 it("resolves bare file names against cwd", () => {
153 expect(resolveMarkdownFileLinkTarget("AGENTS.md", "/Users/julius/project")).toBe(
154 "/Users/julius/project/AGENTS.md",
155 );
156 });
157
158 it("maps #L line anchors to editor line suffixes", () => {
159 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
160 "/Users/julius/project/src/main.ts:42:7",
161 );
162 });
163
164 it("ignores external urls", () => {
165 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
166 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
167 });
168
169 it("does not double-decode file URLs", () => {
170 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
171 "/Users/julius/project/file%20name.md",
172 );
173 });
174
175 it("resolves file uri authorities as windows UNC paths", () => {
176 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg")).toBe(
177 "\\\\server\\share\\workspace-image.svg",
178 );
179 });
180
181 it("resolves a localhost file uri as a local path", () => {
182 expect(resolveMarkdownFileLinkTarget("file://localhost/home/me/notes.md")).toBe(
183 "/home/me/notes.md",
184 );
185 });
186
187 it("formats tooltip display paths relative to the cwd when possible", () => {
188 expect(
189 resolveMarkdownFileLinkMeta(
190 "file:///C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts#L501",
191 "C:/Users/mike/dev-stuff/t3code",
192 ),
193 ).toMatchObject({
194 displayPath: "t3code/apps/web/src/session-logic.ts:501",
195 workspaceRelativePath: "apps/web/src/session-logic.ts",
196 });
197 });
198
199 it("resolves the encoded spaces emitted by the markdown renderer", () => {
200 expect(
201 resolveMarkdownFileLinkMeta(
202 "/Users/dara/Downloads/Lime%20Ride%20Artifacts/Bike%20Receipts",
203 "/Users/dara/Downloads/Lime Ride Artifacts",
204 ),
205 ).toMatchObject({
206 targetPath: "/Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts",
207 workspaceRelativePath: "Bike Receipts",
208 basename: "Bike Receipts",
209 });
210 });
211
212 it("resolves relative spaced folders from the markdown renderer", () => {
213 const href = renderMarkdownLinkHref("[folder](<docs/My Folder>)");
214
215 expect(href).toBe("docs/My%20Folder");
216 expect(resolveMarkdownFileLinkMeta(href, "/repo/project")).toMatchObject({
217 targetPath: "/repo/project/docs/My Folder",
218 workspaceRelativePath: "docs/My Folder",
219 basename: "My Folder",
220 });
221 });
222
223 it.each(["md", "html", "xml"])(
224 "resolves a bare spaced .%s filename from the markdown renderer",
225 (extension) => {
226 const href = renderMarkdownLinkHref(`[checklist](<Updated cutover checklist.${extension}>)`);
227
228 expect(href).toBe(`Updated%20cutover%20checklist.${extension}`);
229 expect(resolveMarkdownFileLinkMeta(href, "/repo/project")).toMatchObject({
230 targetPath: `/repo/project/Updated cutover checklist.${extension}`,
231 workspaceRelativePath: `Updated cutover checklist.${extension}`,
232 basename: `Updated cutover checklist.${extension}`,
233 });
234 },
235 );
236
237 it("formats tooltip display paths relative to the cwd for slash-prefixed windows paths", () => {
238 expect(
239 resolveMarkdownFileLinkMeta(
240 "/C:/Users/mike/dev-stuff/t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
241 "C:/Users/mike/dev-stuff/t3code",
242 ),
243 ).toMatchObject({
244 displayPath:
245 "t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
246 workspaceRelativePath:
247 "apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
248 });
249 });
250
251 it("does not create a preview path for files outside the workspace", () => {
252 expect(resolveMarkdownFileLinkMeta("/tmp/report.ts", "/repo/project")).toMatchObject({
253 workspaceRelativePath: null,
254 });
255 });
256
257 it("does not classify a case-distinct POSIX sibling as a workspace file", () => {
258 expect(
259 resolveMarkdownFileLinkMeta(
260 "/tmp/t3code-case-test/project/probe.txt",
261 "/tmp/t3code-case-test/Project",
262 ),
263 ).toMatchObject({
264 displayPath: "/tmp/t3code-case-test/project/probe.txt",
265 workspaceRelativePath: null,
266 });
267 });
268
269 it("keeps Windows workspace comparisons case-insensitive", () => {
270 expect(
271 resolveMarkdownFileLinkMeta("C:/Users/MIKE/Project/src/main.ts", "c:/users/mike/project"),
272 ).toMatchObject({
273 displayPath: "project/src/main.ts",
274 workspaceRelativePath: "src/main.ts",
275 });
276 });
277
278 it("keeps drive-root workspace comparisons case-insensitive", () => {
279 expect(resolveMarkdownFileLinkMeta("C:/Users/MIKE/project.ts", "c:/")).toMatchObject({
280 displayPath: "c:/Users/MIKE/project.ts",
281 workspaceRelativePath: "Users/MIKE/project.ts",
282 });
283 });
284
285 it("keeps backslash UNC workspace comparisons case-insensitive", () => {
286 expect(
287 resolveMarkdownFileLinkMeta(
288 "\\\\server\\share\\PROJECT\\src\\main.ts",
289 "\\\\Server\\Share\\Project",
290 ),
291 ).toMatchObject({
292 displayPath: "Project/src/main.ts",
293 workspaceRelativePath: "src/main.ts",
294 });
295 });
296
297 it.each([
298 ["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
299 ["C:/Users/MIKE/file.ts", "c:/", "Users/MIKE/file.ts"],
300 ["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
301 ["/tmp/repo/file.ts%20", "/tmp/repo", "file.ts "],
302 ])("preserves the preview target for %s in workspace %s", (href, cwd, workspaceRelativePath) => {
303 expect(resolveMarkdownFileLinkMeta(href, cwd)).toMatchObject({ workspaceRelativePath });
304 });
305
306 it("keeps an encoded final space in the absolute target", () => {
307 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
308 "/tmp/repo/file.ts ",
309 );
310 });
311
312 it("normalizes slash-prefixed windows drive paths before resolving", () => {
313 expect(
314 resolveMarkdownFileLinkTarget(
315 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
316 ),
317 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
318 });
319
320 it("resolves angle-bracketed windows drive paths", () => {
321 expect(
322 resolveMarkdownFileLinkTarget(
323 "</D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1>",
324 ),
325 ).toBe("D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1");
326 });
327
328 it("does not treat app routes as file links, even with a line anchor", () => {
329 expect(resolveMarkdownFileLinkTarget("/chat/settings")).toBeNull();
330 expect(resolveMarkdownFileLinkTarget("/chat/settings#L3", "/repo")).toBeNull();
331 });
332
333 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
334 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
335 "c:/Users/x/shot.png",
336 );
337 });
338});
339
340describe("relative links inside a rendered host file", () => {
341 it("anchor to the file's directory while workspace membership follows cwd", () => {
342 const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
343 expect(meta).toMatchObject({
344 filePath: "/tmp/report/appendix.md",
345 workspaceRelativePath: null,
346 });
347 const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
348 expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
349 expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
350 filePath: "/repo/docs/src/main.ts",
351 workspaceRelativePath: "docs/src/main.ts",
352 });
353 });
354});
355
356describe("resolveInlineCodeFileLinkMeta", () => {
357 it("links relative paths with file extensions", () => {
358 expect(
359 resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md", "/Users/julius/project"),
360 ).toMatchObject({
361 targetPath: "/Users/julius/project/docs/internals/workspace-layout.md",
362 basename: "workspace-layout.md",
363 });
364 });
365
366 it("links absolute posix paths", () => {
367 expect(resolveInlineCodeFileLinkMeta("/Users/julius/project/AGENTS.md")).toMatchObject({
368 targetPath: "/Users/julius/project/AGENTS.md",
369 });
370 expect(resolveInlineCodeFileLinkMeta("/usr/local/bin/tool")).toMatchObject({
371 targetPath: "/usr/local/bin/tool",
372 });
373 expect(resolveInlineCodeFileLinkMeta("/workspace/Makefile")).toMatchObject({
374 basename: "Makefile",
375 });
376 expect(resolveInlineCodeFileLinkMeta("/chat/settings")).toBeNull();
377 });
378
379 it("links windows drive paths", () => {
380 expect(resolveInlineCodeFileLinkMeta("C:\\Users\\mike\\project\\src\\main.ts")).toMatchObject({
381 basename: "main.ts",
382 });
383 });
384
385 it("links relative paths with line positions", () => {
386 expect(
387 resolveInlineCodeFileLinkMeta("src/processRunner.ts:71", "/Users/julius/project"),
388 ).toMatchObject({
389 targetPath: "/Users/julius/project/src/processRunner.ts:71",
390 line: 71,
391 });
392 });
393
394 it("links bare filenames only when a line suffix marks them as file references", () => {
395 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
396 targetPath: "/Users/julius/project/script.ts:10",
397 line: 10,
398 });
399 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
400 });
401
402 it("links extensionless bare filenames with a line suffix", () => {
403 expect(resolveInlineCodeFileLinkMeta("Makefile:12", "/Users/julius/project")).toMatchObject({
404 targetPath: "/Users/julius/project/Makefile:12",
405 basename: "Makefile",
406 line: 12,
407 });
408 expect(resolveInlineCodeFileLinkMeta("Dockerfile:8:2", "/Users/julius/project")).toMatchObject({
409 line: 8,
410 column: 2,
411 });
412 expect(resolveInlineCodeFileLinkMeta("Makefile:12")).toBeNull();
413 });
414
415 it("does not treat arbitrary name:digits shapes as files", () => {
416 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
417 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
418 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
419 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
420 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
421 });
422
423 it("links dot-prefixed relative paths without extensions", () => {
424 expect(
425 resolveInlineCodeFileLinkMeta("./scripts/deploy", "/Users/julius/project"),
426 ).toMatchObject({
427 basename: "deploy",
428 });
429 });
430
431 it("links relative windows-style paths by normalizing backslashes", () => {
432 expect(resolveInlineCodeFileLinkMeta("src\\main.ts", "/Users/julius/project")).toMatchObject({
433 targetPath: "/Users/julius/project/src/main.ts",
434 basename: "main.ts",
435 });
436 expect(
437 resolveInlineCodeFileLinkMeta(".\\scripts\\deploy", "/Users/julius/project"),
438 ).toMatchObject({
439 basename: "deploy",
440 });
441 });
442
443 it("ignores hosts, ports, and versions", () => {
444 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
445 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
446 expect(
447 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
448 ).toBeNull();
449 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
450 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
451 expect(
452 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
453 ).toBeNull();
454 expect(
455 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
456 ).toBeNull();
457 });
458
459 it("still links files whose extension merely resembles a tld", () => {
460 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).not.toBeNull();
461 expect(resolveInlineCodeFileLinkMeta("src/setup.sh:3", "/Users/julius/project")).not.toBeNull();
462 expect(resolveInlineCodeFileLinkMeta("Makefile.in:12", "/Users/julius/project")).not.toBeNull();
463 expect(
464 resolveInlineCodeFileLinkMeta("conf.d/nginx.conf", "/Users/julius/project"),
465 ).not.toBeNull();
466 });
467
468 it("prefers file over country-code host when a line suffix is present", () => {
469 expect(resolveInlineCodeFileLinkMeta("script.pl:10", "/Users/julius/project")).toMatchObject({
470 targetPath: "/Users/julius/project/script.pl:10",
471 line: 10,
472 });
473 expect(resolveInlineCodeFileLinkMeta("model.pt:3", "/Users/julius/project")).not.toBeNull();
474 expect(
475 resolveInlineCodeFileLinkMeta("example.pl/index.html", "/Users/julius/project"),
476 ).toBeNull();
477 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
478 });
479
480 it("ignores commands, flags, and expressions", () => {
481 expect(resolveInlineCodeFileLinkMeta("git worktree list --porcelain")).toBeNull();
482 expect(resolveInlineCodeFileLinkMeta("node.meta", "/Users/julius/project")).toBeNull();
483 expect(resolveInlineCodeFileLinkMeta("pnpm install", "/Users/julius/project")).toBeNull();
484 expect(resolveInlineCodeFileLinkMeta("src/**/*.ts", "/Users/julius/project")).toBeNull();
485 });
486
487 it("ignores extension-less relative segments like git refs and directories", () => {
488 expect(resolveInlineCodeFileLinkMeta("origin/main", "/Users/julius/project")).toBeNull();
489 expect(resolveInlineCodeFileLinkMeta("apps/web", "/Users/julius/project")).toBeNull();
490 });
491
492 it("ignores external urls", () => {
493 expect(resolveInlineCodeFileLinkMeta("https://example.com/docs.html")).toBeNull();
494 });
495
496 it("ignores relative paths without a cwd to resolve against", () => {
497 expect(resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md")).toBeNull();
498 });
499});
500
501describe("directory paths with a trailing separator", () => {
502 it("keeps the final segment for a POSIX directory path", () => {
503 expect(resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project")).toMatchObject({
504 basename: "favicons",
505 });
506 });
507
508 it("keeps the final segment for a Windows directory path", () => {
509 expect(
510 resolveMarkdownFileLinkMeta("C:\\Users\\kelchm\\.claude\\", "/repo/project"),
511 ).toMatchObject({ basename: ".claude" });
512 });
513
514 it("matches the label of the same path without a trailing separator", () => {
515 const withSlash = resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project");
516 const withoutSlash = resolveMarkdownFileLinkMeta("/tmp/favicons", "/repo/project");
517 expect(withSlash?.basename).toBe(withoutSlash?.basename);
518 });
519
520 it("does not produce an empty label for the filesystem root", () => {
521 const meta = resolveMarkdownFileLinkMeta("/tmp/", "/repo/project");
522 expect(meta?.basename).not.toBe("");
523 });
524});