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 });
167
168 it("does not double-decode file URLs", () => {
169 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
170 "/Users/julius/project/file%20name.md",
171 );
172 });
173
174 it("resolves file uri authorities as windows UNC paths", () => {
175 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg")).toBe(
176 "\\\\server\\share\\workspace-image.svg",
177 );
178 });
179
180 it("resolves a localhost file uri as a local path", () => {
181 expect(resolveMarkdownFileLinkTarget("file://localhost/home/me/notes.md")).toBe(
182 "/home/me/notes.md",
183 );
184 });
185
186 it("formats tooltip display paths relative to the cwd when possible", () => {
187 expect(
188 resolveMarkdownFileLinkMeta(
189 "file:///C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts#L501",
190 "C:/Users/mike/dev-stuff/t3code",
191 ),
192 ).toMatchObject({
193 displayPath: "t3code/apps/web/src/session-logic.ts:501",
194 workspaceRelativePath: "apps/web/src/session-logic.ts",
195 });
196 });
197
198 it("resolves the encoded spaces emitted by the markdown renderer", () => {
199 expect(
200 resolveMarkdownFileLinkMeta(
201 "/Users/dara/Downloads/Lime%20Ride%20Artifacts/Bike%20Receipts",
202 "/Users/dara/Downloads/Lime Ride Artifacts",
203 ),
204 ).toMatchObject({
205 targetPath: "/Users/dara/Downloads/Lime Ride Artifacts/Bike Receipts",
206 workspaceRelativePath: "Bike Receipts",
207 basename: "Bike Receipts",
208 });
209 });
210
211 it("resolves relative spaced folders from the markdown renderer", () => {
212 const href = renderMarkdownLinkHref("[folder](<docs/My Folder>)");
213
214 expect(href).toBe("docs/My%20Folder");
215 expect(resolveMarkdownFileLinkMeta(href, "/repo/project")).toMatchObject({
216 targetPath: "/repo/project/docs/My Folder",
217 workspaceRelativePath: "docs/My Folder",
218 basename: "My Folder",
219 });
220 });
221
222 it.each(["md", "html", "xml"])(
223 "resolves a bare spaced .%s filename from the markdown renderer",
224 (extension) => {
225 const href = renderMarkdownLinkHref(`[checklist](<Updated cutover checklist.${extension}>)`);
226
227 expect(href).toBe(`Updated%20cutover%20checklist.${extension}`);
228 expect(resolveMarkdownFileLinkMeta(href, "/repo/project")).toMatchObject({
229 targetPath: `/repo/project/Updated cutover checklist.${extension}`,
230 workspaceRelativePath: `Updated cutover checklist.${extension}`,
231 basename: `Updated cutover checklist.${extension}`,
232 });
233 },
234 );
235
236 it("formats tooltip display paths relative to the cwd for slash-prefixed windows paths", () => {
237 expect(
238 resolveMarkdownFileLinkMeta(
239 "/C:/Users/mike/dev-stuff/t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
240 "C:/Users/mike/dev-stuff/t3code",
241 ),
242 ).toMatchObject({
243 displayPath:
244 "t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
245 workspaceRelativePath:
246 "apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
247 });
248 });
249
250 it("does not create a preview path for files outside the workspace", () => {
251 expect(resolveMarkdownFileLinkMeta("/tmp/report.ts", "/repo/project")).toMatchObject({
252 workspaceRelativePath: null,
253 });
254 });
255
256 it("normalizes slash-prefixed windows drive paths before resolving", () => {
257 expect(
258 resolveMarkdownFileLinkTarget(
259 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
260 ),
261 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
262 });
263
264 it("resolves angle-bracketed windows drive paths", () => {
265 expect(
266 resolveMarkdownFileLinkTarget(
267 "</D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1>",
268 ),
269 ).toBe("D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1");
270 });
271
272 it("does not treat app routes as file links", () => {
273 expect(resolveMarkdownFileLinkTarget("/chat/settings")).toBeNull();
274 });
275});
276
277describe("resolveInlineCodeFileLinkMeta", () => {
278 it("links relative paths with file extensions", () => {
279 expect(
280 resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md", "/Users/julius/project"),
281 ).toMatchObject({
282 targetPath: "/Users/julius/project/docs/internals/workspace-layout.md",
283 basename: "workspace-layout.md",
284 });
285 });
286
287 it("links absolute posix paths", () => {
288 expect(resolveInlineCodeFileLinkMeta("/Users/julius/project/AGENTS.md")).toMatchObject({
289 targetPath: "/Users/julius/project/AGENTS.md",
290 });
291 expect(resolveInlineCodeFileLinkMeta("/usr/local/bin/tool")).toMatchObject({
292 targetPath: "/usr/local/bin/tool",
293 });
294 expect(resolveInlineCodeFileLinkMeta("/workspace/Makefile")).toMatchObject({
295 basename: "Makefile",
296 });
297 expect(resolveInlineCodeFileLinkMeta("/chat/settings")).toBeNull();
298 });
299
300 it("links windows drive paths", () => {
301 expect(resolveInlineCodeFileLinkMeta("C:\\Users\\mike\\project\\src\\main.ts")).toMatchObject({
302 basename: "main.ts",
303 });
304 });
305
306 it("links relative paths with line positions", () => {
307 expect(
308 resolveInlineCodeFileLinkMeta("src/processRunner.ts:71", "/Users/julius/project"),
309 ).toMatchObject({
310 targetPath: "/Users/julius/project/src/processRunner.ts:71",
311 line: 71,
312 });
313 });
314
315 it("links bare filenames only when a line suffix marks them as file references", () => {
316 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
317 targetPath: "/Users/julius/project/script.ts:10",
318 line: 10,
319 });
320 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
321 });
322
323 it("links extensionless bare filenames with a line suffix", () => {
324 expect(resolveInlineCodeFileLinkMeta("Makefile:12", "/Users/julius/project")).toMatchObject({
325 targetPath: "/Users/julius/project/Makefile:12",
326 basename: "Makefile",
327 line: 12,
328 });
329 expect(resolveInlineCodeFileLinkMeta("Dockerfile:8:2", "/Users/julius/project")).toMatchObject({
330 line: 8,
331 column: 2,
332 });
333 expect(resolveInlineCodeFileLinkMeta("Makefile:12")).toBeNull();
334 });
335
336 it("does not treat arbitrary name:digits shapes as files", () => {
337 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
338 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
339 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
340 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
341 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
342 });
343
344 it("links dot-prefixed relative paths without extensions", () => {
345 expect(
346 resolveInlineCodeFileLinkMeta("./scripts/deploy", "/Users/julius/project"),
347 ).toMatchObject({
348 basename: "deploy",
349 });
350 });
351
352 it("links relative windows-style paths by normalizing backslashes", () => {
353 expect(resolveInlineCodeFileLinkMeta("src\\main.ts", "/Users/julius/project")).toMatchObject({
354 targetPath: "/Users/julius/project/src/main.ts",
355 basename: "main.ts",
356 });
357 expect(
358 resolveInlineCodeFileLinkMeta(".\\scripts\\deploy", "/Users/julius/project"),
359 ).toMatchObject({
360 basename: "deploy",
361 });
362 });
363
364 it("ignores hosts, ports, and versions", () => {
365 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
366 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
367 expect(
368 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
369 ).toBeNull();
370 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
371 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
372 expect(
373 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
374 ).toBeNull();
375 expect(
376 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
377 ).toBeNull();
378 });
379
380 it("still links files whose extension merely resembles a tld", () => {
381 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).not.toBeNull();
382 expect(resolveInlineCodeFileLinkMeta("src/setup.sh:3", "/Users/julius/project")).not.toBeNull();
383 expect(resolveInlineCodeFileLinkMeta("Makefile.in:12", "/Users/julius/project")).not.toBeNull();
384 expect(
385 resolveInlineCodeFileLinkMeta("conf.d/nginx.conf", "/Users/julius/project"),
386 ).not.toBeNull();
387 });
388
389 it("prefers file over country-code host when a line suffix is present", () => {
390 expect(resolveInlineCodeFileLinkMeta("script.pl:10", "/Users/julius/project")).toMatchObject({
391 targetPath: "/Users/julius/project/script.pl:10",
392 line: 10,
393 });
394 expect(resolveInlineCodeFileLinkMeta("model.pt:3", "/Users/julius/project")).not.toBeNull();
395 expect(
396 resolveInlineCodeFileLinkMeta("example.pl/index.html", "/Users/julius/project"),
397 ).toBeNull();
398 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
399 });
400
401 it("ignores commands, flags, and expressions", () => {
402 expect(resolveInlineCodeFileLinkMeta("git worktree list --porcelain")).toBeNull();
403 expect(resolveInlineCodeFileLinkMeta("node.meta", "/Users/julius/project")).toBeNull();
404 expect(resolveInlineCodeFileLinkMeta("pnpm install", "/Users/julius/project")).toBeNull();
405 expect(resolveInlineCodeFileLinkMeta("src/**/*.ts", "/Users/julius/project")).toBeNull();
406 });
407
408 it("ignores extension-less relative segments like git refs and directories", () => {
409 expect(resolveInlineCodeFileLinkMeta("origin/main", "/Users/julius/project")).toBeNull();
410 expect(resolveInlineCodeFileLinkMeta("apps/web", "/Users/julius/project")).toBeNull();
411 });
412
413 it("ignores external urls", () => {
414 expect(resolveInlineCodeFileLinkMeta("https://example.com/docs.html")).toBeNull();
415 });
416
417 it("ignores relative paths without a cwd to resolve against", () => {
418 expect(resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md")).toBeNull();
419 });
420});
421
422describe("directory paths with a trailing separator", () => {
423 it("keeps the final segment for a POSIX directory path", () => {
424 expect(resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project")).toMatchObject({
425 basename: "favicons",
426 });
427 });
428
429 it("keeps the final segment for a Windows directory path", () => {
430 expect(
431 resolveMarkdownFileLinkMeta("C:\\Users\\kelchm\\.claude\\", "/repo/project"),
432 ).toMatchObject({ basename: ".claude" });
433 });
434
435 it("matches the label of the same path without a trailing separator", () => {
436 const withSlash = resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project");
437 const withoutSlash = resolveMarkdownFileLinkMeta("/tmp/favicons", "/repo/project");
438 expect(withSlash?.basename).toBe(withoutSlash?.basename);
439 });
440
441 it("does not produce an empty label for the filesystem root", () => {
442 const meta = resolveMarkdownFileLinkMeta("/tmp/", "/repo/project");
443 expect(meta?.basename).not.toBe("");
444 });
445});