Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 resolveInlineCodeFileLinkMeta,
5 resolveMarkdownFileLinkMeta,
6 rewriteMarkdownFileUriHref,
7 shouldOpenMarkdownFileLinkInBrowserByDefault,
8 shouldOpenMarkdownFileLinkInEditor,
9} from "./markdown-links";
10import {
11 markdownFileCopyPaths,
12 markdownFileEditorTarget,
13 resolveMarkdownFileHostPath,
14} from "./markdownFileHostPath";
15
16describe("shouldOpenMarkdownFileLinkInEditor", () => {
17 it("uses command-click on macOS", () => {
18 expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "MacIntel")).toBe(
19 true,
20 );
21 expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "MacIntel")).toBe(
22 false,
23 );
24 });
25
26 it("uses control-click on other platforms", () => {
27 expect(
28 shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "Linux x86_64"),
29 ).toBe(true);
30 expect(
31 shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "Linux x86_64"),
32 ).toBe(false);
33 });
34});
35
36describe("shouldOpenMarkdownFileLinkInBrowserByDefault", () => {
37 it("keeps PDFs browser-first while source files open in the file viewer", () => {
38 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.pdf")).toBe(true);
39 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.PDF?download=1")).toBe(true);
40 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.html")).toBe(false);
41 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.xml")).toBe(false);
42 });
43});
44
45describe("rewriteMarkdownFileUriHref", () => {
46 it("rewrites file uri hrefs into direct path hrefs", () => {
47 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/src/main.ts#L42")).toBe(
48 "/Users/julius/project/src/main.ts#L42",
49 );
50 });
51
52 it("preserves encoded octets so file paths are decoded only once later", () => {
53 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(
54 "/Users/julius/project/file%2520name.md",
55 );
56 });
57
58 it("normalizes file uri hrefs for windows drive paths", () => {
59 expect(
60 rewriteMarkdownFileUriHref(
61 "file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
62 ),
63 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
64 });
65
66 it("preserves file uri authorities as windows UNC paths", () => {
67 expect(rewriteMarkdownFileUriHref("file://server/share/workspace-image.svg")).toBe(
68 "\\\\server\\share\\workspace-image.svg",
69 );
70 });
71
72 it("treats a localhost file uri as a local path", () => {
73 expect(rewriteMarkdownFileUriHref("file://localhost/home/me/notes.md")).toBe(
74 "/home/me/notes.md",
75 );
76 });
77
78 it("unwraps angle-bracketed file uri hrefs", () => {
79 expect(
80 rewriteMarkdownFileUriHref(" <file:///D:/Programme/t3code/apps/web/src/markdown-links.ts> "),
81 ).toBe("D:/Programme/t3code/apps/web/src/markdown-links.ts");
82 });
83});
84
85describe("relative links inside a rendered host file", () => {
86 it("anchor to the file's directory while workspace membership follows cwd", () => {
87 const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
88 expect(meta).toMatchObject({
89 filePath: "/tmp/report/appendix.md",
90 workspaceRelativePath: null,
91 });
92 const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
93 expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
94 expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
95 filePath: "/repo/docs/src/main.ts",
96 workspaceRelativePath: "docs/src/main.ts",
97 });
98 });
99
100 it("resolve multi-segment inline code from the workspace root in a workspace file", () => {
101 expect(
102 resolveInlineCodeFileLinkMeta("docs/ai/design.md", "/repo", "/repo/docs/ai"),
103 ).toMatchObject({ filePath: "/repo/docs/ai/design.md" });
104 expect(
105 resolveInlineCodeFileLinkMeta("src/index.ts:4", "/repo", "/repo/packages/a"),
106 ).toMatchObject({ filePath: "/repo/src/index.ts", line: 4 });
107 });
108
109 it("keep sibling and explicitly relative inline code beside the file", () => {
110 expect(resolveInlineCodeFileLinkMeta("design.md:12", "/repo", "/repo/docs/ai")).toMatchObject({
111 filePath: "/repo/docs/ai/design.md",
112 line: 12,
113 });
114 expect(
115 resolveInlineCodeFileLinkMeta("./src/index.ts", "/repo", "/repo/packages/a"),
116 ).toMatchObject({ filePath: "/repo/packages/a/./src/index.ts" });
117 expect(resolveInlineCodeFileLinkMeta("../b/notes.md", "/repo", "/repo/docs/a")).toMatchObject({
118 filePath: "/repo/docs/a/../b/notes.md",
119 });
120 });
121
122 it("keep multi-segment inline code beside a file outside the workspace", () => {
123 expect(resolveInlineCodeFileLinkMeta("src/main.ts", "/repo", "/tmp/report")).toMatchObject({
124 filePath: "/tmp/report/src/main.ts",
125 workspaceRelativePath: null,
126 });
127 });
128});
129
130describe("resolveInlineCodeFileLinkMeta", () => {
131 it("links relative paths with file extensions", () => {
132 expect(
133 resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md", "/Users/julius/project"),
134 ).toMatchObject({
135 targetPath: "/Users/julius/project/docs/internals/workspace-layout.md",
136 basename: "workspace-layout.md",
137 });
138 });
139
140 it("links absolute posix paths", () => {
141 expect(resolveInlineCodeFileLinkMeta("/Users/julius/project/AGENTS.md")).toMatchObject({
142 targetPath: "/Users/julius/project/AGENTS.md",
143 });
144 expect(resolveInlineCodeFileLinkMeta("/usr/local/bin/tool")).toMatchObject({
145 targetPath: "/usr/local/bin/tool",
146 });
147 expect(resolveInlineCodeFileLinkMeta("/workspace/Makefile")).toMatchObject({
148 basename: "Makefile",
149 });
150 expect(resolveInlineCodeFileLinkMeta("/chat/settings")).toBeNull();
151 });
152
153 it("links windows drive paths", () => {
154 expect(resolveInlineCodeFileLinkMeta("C:\\Users\\mike\\project\\src\\main.ts")).toMatchObject({
155 basename: "main.ts",
156 });
157 });
158
159 it("links relative paths with line positions", () => {
160 expect(
161 resolveInlineCodeFileLinkMeta("src/processRunner.ts:71", "/Users/julius/project"),
162 ).toMatchObject({
163 targetPath: "/Users/julius/project/src/processRunner.ts:71",
164 line: 71,
165 });
166 });
167
168 it("links bare filenames only when a line suffix marks them as file references", () => {
169 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
170 targetPath: "/Users/julius/project/script.ts:10",
171 line: 10,
172 });
173 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
174 });
175
176 it("links extensionless bare filenames with a line suffix", () => {
177 expect(resolveInlineCodeFileLinkMeta("Makefile:12", "/Users/julius/project")).toMatchObject({
178 targetPath: "/Users/julius/project/Makefile:12",
179 basename: "Makefile",
180 line: 12,
181 });
182 expect(resolveInlineCodeFileLinkMeta("Dockerfile:8:2", "/Users/julius/project")).toMatchObject({
183 line: 8,
184 column: 2,
185 });
186 expect(resolveInlineCodeFileLinkMeta("Makefile:12")).toBeNull();
187 });
188
189 it("does not treat arbitrary name:digits shapes as files", () => {
190 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
191 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
192 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
193 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
194 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
195 });
196
197 it("links dot-prefixed relative paths without extensions", () => {
198 expect(
199 resolveInlineCodeFileLinkMeta("./scripts/deploy", "/Users/julius/project"),
200 ).toMatchObject({
201 basename: "deploy",
202 });
203 });
204
205 it("links relative windows-style paths by normalizing backslashes", () => {
206 expect(resolveInlineCodeFileLinkMeta("src\\main.ts", "/Users/julius/project")).toMatchObject({
207 targetPath: "/Users/julius/project/src/main.ts",
208 basename: "main.ts",
209 });
210 expect(
211 resolveInlineCodeFileLinkMeta(".\\scripts\\deploy", "/Users/julius/project"),
212 ).toMatchObject({
213 basename: "deploy",
214 });
215 });
216
217 it("ignores hosts, ports, and versions", () => {
218 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
219 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
220 expect(
221 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
222 ).toBeNull();
223 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
224 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
225 expect(
226 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
227 ).toBeNull();
228 expect(
229 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
230 ).toBeNull();
231 });
232
233 it("still links files whose extension merely resembles a tld", () => {
234 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).not.toBeNull();
235 expect(resolveInlineCodeFileLinkMeta("src/setup.sh:3", "/Users/julius/project")).not.toBeNull();
236 expect(resolveInlineCodeFileLinkMeta("Makefile.in:12", "/Users/julius/project")).not.toBeNull();
237 expect(
238 resolveInlineCodeFileLinkMeta("conf.d/nginx.conf", "/Users/julius/project"),
239 ).not.toBeNull();
240 });
241
242 it("prefers file over country-code host when a line suffix is present", () => {
243 expect(resolveInlineCodeFileLinkMeta("script.pl:10", "/Users/julius/project")).toMatchObject({
244 targetPath: "/Users/julius/project/script.pl:10",
245 line: 10,
246 });
247 expect(resolveInlineCodeFileLinkMeta("model.pt:3", "/Users/julius/project")).not.toBeNull();
248 expect(
249 resolveInlineCodeFileLinkMeta("example.pl/index.html", "/Users/julius/project"),
250 ).toBeNull();
251 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
252 });
253
254 it("ignores commands, flags, and expressions", () => {
255 expect(resolveInlineCodeFileLinkMeta("git worktree list --porcelain")).toBeNull();
256 expect(resolveInlineCodeFileLinkMeta("node.meta", "/Users/julius/project")).toBeNull();
257 expect(resolveInlineCodeFileLinkMeta("pnpm install", "/Users/julius/project")).toBeNull();
258 expect(resolveInlineCodeFileLinkMeta("src/**/*.ts", "/Users/julius/project")).toBeNull();
259 });
260
261 it("ignores extension-less relative segments like git refs and directories", () => {
262 expect(resolveInlineCodeFileLinkMeta("origin/main", "/Users/julius/project")).toBeNull();
263 expect(resolveInlineCodeFileLinkMeta("apps/web", "/Users/julius/project")).toBeNull();
264 });
265
266 it("ignores external urls", () => {
267 expect(resolveInlineCodeFileLinkMeta("https://example.com/docs.html")).toBeNull();
268 });
269
270 it("ignores relative paths without a cwd to resolve against", () => {
271 expect(resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md")).toBeNull();
272 });
273});
274
275describe("directory paths with a trailing separator", () => {
276 it("keeps the final segment for a POSIX directory path", () => {
277 expect(resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project")).toMatchObject({
278 basename: "favicons",
279 });
280 });
281
282 it("keeps the final segment for a Windows directory path", () => {
283 expect(
284 resolveMarkdownFileLinkMeta("C:\\Users\\kelchm\\.claude\\", "/repo/project"),
285 ).toMatchObject({ basename: ".claude" });
286 });
287
288 it("matches the label of the same path without a trailing separator", () => {
289 const withSlash = resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project");
290 const withoutSlash = resolveMarkdownFileLinkMeta("/tmp/favicons", "/repo/project");
291 expect(withSlash?.basename).toBe(withoutSlash?.basename);
292 });
293
294 it("does not produce an empty label for the filesystem root", () => {
295 const meta = resolveMarkdownFileLinkMeta("/tmp/", "/repo/project");
296 expect(meta?.basename).not.toBe("");
297 });
298});
299
300it("routes the project-root code link to the workspace explorer", () => {
301 const cwd = "/Users/saphid/.t3/worktrees/ov2-standalone-20260918";
302 expect(resolveInlineCodeFileLinkMeta(cwd, cwd)).toMatchObject({
303 workspaceRelativePath: ".",
304 filePath: cwd,
305 });
306});
307
308describe("chat file chip paths", () => {
309 const cwd = String.raw`C:\Users\alice\project`;
310
311 it("opens and copies the indexed file for a bare filename, preserving its line and column", async () => {
312 const meta = resolveMarkdownFileLinkMeta("user.ts:261:7", cwd);
313 if (!meta) throw new Error("Expected a file link");
314 const hostPath = await resolveMarkdownFileHostPath({
315 meta,
316 cwd,
317 findWorkspaceMatch: async () => "src/discord/events/user.ts",
318 });
319
320 expect(markdownFileEditorTarget(meta, hostPath)).toBe(
321 String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
322 );
323 expect(markdownFileCopyPaths(meta, hostPath, cwd)).toEqual({
324 fullPath: String.raw`C:\Users\alice\project\src\discord\events\user.ts:261:7`,
325 relativePath: "src/discord/events/user.ts:261:7",
326 });
327 });
328
329 it("keeps the authored path and position when the workspace index has no match", async () => {
330 const meta = resolveMarkdownFileLinkMeta("missing.ts:12:3", cwd);
331 if (!meta) throw new Error("Expected a file link");
332 const hostPath = await resolveMarkdownFileHostPath({
333 meta,
334 cwd,
335 findWorkspaceMatch: async () => null,
336 });
337
338 expect(markdownFileEditorTarget(meta, hostPath)).toBe(
339 String.raw`C:\Users\alice\project\missing.ts:12:3`,
340 );
341 expect(markdownFileCopyPaths(meta, hostPath, cwd)).toEqual({
342 fullPath: String.raw`C:\Users\alice\project\missing.ts:12:3`,
343 relativePath: "missing.ts:12:3",
344 });
345 });
346});