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

markdown-links.test.ts

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