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("normalizes slash-prefixed windows drive paths before resolving", () => {
258 expect(
259 resolveMarkdownFileLinkTarget(
260 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
261 ),
262 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
263 });
264
265 it("resolves angle-bracketed windows drive paths", () => {
266 expect(
267 resolveMarkdownFileLinkTarget(
268 "</D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1>",
269 ),
270 ).toBe("D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1");
271 });
272
273 it("does not treat app routes as file links", () => {
274 expect(resolveMarkdownFileLinkTarget("/chat/settings")).toBeNull();
275 });
276});
277
278describe("relative links inside a rendered host file", () => {
279 it("anchor to the file's directory while workspace membership follows cwd", () => {
280 const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
281 expect(meta).toMatchObject({
282 filePath: "/tmp/report/appendix.md",
283 workspaceRelativePath: null,
284 });
285 const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
286 expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
287 expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
288 filePath: "/repo/docs/src/main.ts",
289 workspaceRelativePath: "docs/src/main.ts",
290 });
291 });
292});
293
294describe("resolveInlineCodeFileLinkMeta", () => {
295 it("links relative paths with file extensions", () => {
296 expect(
297 resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md", "/Users/julius/project"),
298 ).toMatchObject({
299 targetPath: "/Users/julius/project/docs/internals/workspace-layout.md",
300 basename: "workspace-layout.md",
301 });
302 });
303
304 it("links absolute posix paths", () => {
305 expect(resolveInlineCodeFileLinkMeta("/Users/julius/project/AGENTS.md")).toMatchObject({
306 targetPath: "/Users/julius/project/AGENTS.md",
307 });
308 expect(resolveInlineCodeFileLinkMeta("/usr/local/bin/tool")).toMatchObject({
309 targetPath: "/usr/local/bin/tool",
310 });
311 expect(resolveInlineCodeFileLinkMeta("/workspace/Makefile")).toMatchObject({
312 basename: "Makefile",
313 });
314 expect(resolveInlineCodeFileLinkMeta("/chat/settings")).toBeNull();
315 });
316
317 it("links windows drive paths", () => {
318 expect(resolveInlineCodeFileLinkMeta("C:\\Users\\mike\\project\\src\\main.ts")).toMatchObject({
319 basename: "main.ts",
320 });
321 });
322
323 it("links relative paths with line positions", () => {
324 expect(
325 resolveInlineCodeFileLinkMeta("src/processRunner.ts:71", "/Users/julius/project"),
326 ).toMatchObject({
327 targetPath: "/Users/julius/project/src/processRunner.ts:71",
328 line: 71,
329 });
330 });
331
332 it("links bare filenames only when a line suffix marks them as file references", () => {
333 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
334 targetPath: "/Users/julius/project/script.ts:10",
335 line: 10,
336 });
337 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
338 });
339
340 it("links extensionless bare filenames with a line suffix", () => {
341 expect(resolveInlineCodeFileLinkMeta("Makefile:12", "/Users/julius/project")).toMatchObject({
342 targetPath: "/Users/julius/project/Makefile:12",
343 basename: "Makefile",
344 line: 12,
345 });
346 expect(resolveInlineCodeFileLinkMeta("Dockerfile:8:2", "/Users/julius/project")).toMatchObject({
347 line: 8,
348 column: 2,
349 });
350 expect(resolveInlineCodeFileLinkMeta("Makefile:12")).toBeNull();
351 });
352
353 it("does not treat arbitrary name:digits shapes as files", () => {
354 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
355 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
356 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
357 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
358 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
359 });
360
361 it("links dot-prefixed relative paths without extensions", () => {
362 expect(
363 resolveInlineCodeFileLinkMeta("./scripts/deploy", "/Users/julius/project"),
364 ).toMatchObject({
365 basename: "deploy",
366 });
367 });
368
369 it("links relative windows-style paths by normalizing backslashes", () => {
370 expect(resolveInlineCodeFileLinkMeta("src\\main.ts", "/Users/julius/project")).toMatchObject({
371 targetPath: "/Users/julius/project/src/main.ts",
372 basename: "main.ts",
373 });
374 expect(
375 resolveInlineCodeFileLinkMeta(".\\scripts\\deploy", "/Users/julius/project"),
376 ).toMatchObject({
377 basename: "deploy",
378 });
379 });
380
381 it("ignores hosts, ports, and versions", () => {
382 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
383 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
384 expect(
385 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
386 ).toBeNull();
387 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
388 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
389 expect(
390 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
391 ).toBeNull();
392 expect(
393 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
394 ).toBeNull();
395 });
396
397 it("still links files whose extension merely resembles a tld", () => {
398 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).not.toBeNull();
399 expect(resolveInlineCodeFileLinkMeta("src/setup.sh:3", "/Users/julius/project")).not.toBeNull();
400 expect(resolveInlineCodeFileLinkMeta("Makefile.in:12", "/Users/julius/project")).not.toBeNull();
401 expect(
402 resolveInlineCodeFileLinkMeta("conf.d/nginx.conf", "/Users/julius/project"),
403 ).not.toBeNull();
404 });
405
406 it("prefers file over country-code host when a line suffix is present", () => {
407 expect(resolveInlineCodeFileLinkMeta("script.pl:10", "/Users/julius/project")).toMatchObject({
408 targetPath: "/Users/julius/project/script.pl:10",
409 line: 10,
410 });
411 expect(resolveInlineCodeFileLinkMeta("model.pt:3", "/Users/julius/project")).not.toBeNull();
412 expect(
413 resolveInlineCodeFileLinkMeta("example.pl/index.html", "/Users/julius/project"),
414 ).toBeNull();
415 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
416 });
417
418 it("ignores commands, flags, and expressions", () => {
419 expect(resolveInlineCodeFileLinkMeta("git worktree list --porcelain")).toBeNull();
420 expect(resolveInlineCodeFileLinkMeta("node.meta", "/Users/julius/project")).toBeNull();
421 expect(resolveInlineCodeFileLinkMeta("pnpm install", "/Users/julius/project")).toBeNull();
422 expect(resolveInlineCodeFileLinkMeta("src/**/*.ts", "/Users/julius/project")).toBeNull();
423 });
424
425 it("ignores extension-less relative segments like git refs and directories", () => {
426 expect(resolveInlineCodeFileLinkMeta("origin/main", "/Users/julius/project")).toBeNull();
427 expect(resolveInlineCodeFileLinkMeta("apps/web", "/Users/julius/project")).toBeNull();
428 });
429
430 it("ignores external urls", () => {
431 expect(resolveInlineCodeFileLinkMeta("https://example.com/docs.html")).toBeNull();
432 });
433
434 it("ignores relative paths without a cwd to resolve against", () => {
435 expect(resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md")).toBeNull();
436 });
437});
438
439describe("directory paths with a trailing separator", () => {
440 it("keeps the final segment for a POSIX directory path", () => {
441 expect(resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project")).toMatchObject({
442 basename: "favicons",
443 });
444 });
445
446 it("keeps the final segment for a Windows directory path", () => {
447 expect(
448 resolveMarkdownFileLinkMeta("C:\\Users\\kelchm\\.claude\\", "/repo/project"),
449 ).toMatchObject({ basename: ".claude" });
450 });
451
452 it("matches the label of the same path without a trailing separator", () => {
453 const withSlash = resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project");
454 const withoutSlash = resolveMarkdownFileLinkMeta("/tmp/favicons", "/repo/project");
455 expect(withSlash?.basename).toBe(withoutSlash?.basename);
456 });
457
458 it("does not produce an empty label for the filesystem root", () => {
459 const meta = resolveMarkdownFileLinkMeta("/tmp/", "/repo/project");
460 expect(meta?.basename).not.toBe("");
461 });
462});