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

markdown-links.test.ts

469 lines17.7 KB

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