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