Open History to see the last commit
1import { workspaceRelativeFilePath } from "./path.ts";
2import { describe, expect, it } from "vite-plus/test";
3import { serializeComposerFileLink } from "./composerTrigger.ts";
4import {
5 extractInlineCodeSpans,
6 extractMarkdownLinkHrefs,
7 resolveMarkdownFileLinkTarget,
8} from "./markdownLinks.ts";
9
10import {
11 inlineCodeFilePathCandidate,
12 isMarkdownFileLinkLabel,
13 parseMarkdownFileLink,
14} from "./markdownLinks.ts";
15
16describe("isMarkdownFileLinkLabel", () => {
17 // regression: #15509 descriptive labels are not mistaken for the linked file name
18 it.each([
19 ["validates the input", "/repo/src/example.ts:12", false],
20 ["example.ts:12", "/repo/src/example.ts:12", true],
21 ["example.ts:99", "/repo/src/example.ts:12", false],
22 ["example.ts:12-20", "/repo/src/example.ts:12-20", true],
23 ["example.ts:12-20", "/repo/src/example.ts:12", false],
24 ["src/main.ts#L10-L20", "/repo/src/main.ts#L10-L20", true],
25 ["main.ts#L42C7", "/repo/src/main.ts:42:7", true],
26 ["src/main.ts#L10-L20", "/repo/src/main.ts#L10", false],
27 ["src/main.ts:10#L20", "/repo/src/main.ts:10", false],
28 ["C#/Program.cs#L10", "./C%23/Program.cs#L10", true],
29 ["src/main.ts#L10", "./src/main.ts%23L10", true],
30 ])("classifies %s for %s", (label, href, expected) => {
31 expect(isMarkdownFileLinkLabel(label, href)).toBe(expected);
32 });
33});
34
35describe("inlineCodeFilePathCandidate", () => {
36 // regression: #13909 model ids in inline code do not become file chips
37 it.each([
38 ["src\\main.ts", "src/main.ts"],
39 ["allocator.cc:217", "allocator.cc:217"],
40 ["src/http.ts:42-64", "src/http.ts:42-64"],
41 ["http.ts:42-64", "http.ts:42-64"],
42 ["origin/main", null],
43 ["z-ai/glm-5.3", null],
44 ["share/man/ls.1", "share/man/ls.1"],
45 ["docker/Dockerfile", "docker/Dockerfile"],
46 ["src/main.ts/", null],
47 ["./patches/", "./patches/"],
48 // regression: #30757 an agent's shortened path is not a file
49 ["/tmp/claude-1000/...", null],
50 ["/tmp/claude-1000/...:12", null],
51 ["/tmp/.../scratchpad/notes.md", null],
52 ["~/projects/…/notes.md", null],
53 ["src/.../main.ts", null],
54 // regression: #22181 a `:line` suffix does not make an extensionless basename a file
55 ["I/W217:220", null],
56 ["bin/run:12", null],
57 ["docs/README:12", "docs/README:12"],
58 ["./bin/run:12", "./bin/run:12"],
59 ["src/資料.備份:12", "src/資料.備份:12"],
60 ["src/foo.c++:12", "src/foo.c++:12"],
61 // regression: #37740 absolute paths with spaces are files, commands are not
62 ["/Users/me/Path Test/notes with spaces.md", "/Users/me/Path Test/notes with spaces.md"],
63 ["~/Library/Mobile Documents/notes.md", "~/Library/Mobile Documents/notes.md"],
64 [
65 "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md",
66 "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md",
67 ],
68 ["cd /tmp && ls", null],
69 ["/usr/bin/python3 -m venv .venv", null],
70 ["/usr/local/bin/node server.js", null],
71 ["/usr/bin/env python3 foo.py", null],
72 ["/opt/homebrew/bin/python3 manage.py", null],
73 ["/bin/echo hello world.txt", null],
74 ["~/bin/run tests.sh", null],
75 ["/Users/me/notes with spaces.md", "/Users/me/notes with spaces.md"],
76 [
77 "\\\\server\\share\\Project\\Guide Notes.md:10-20",
78 "\\\\server\\share\\Project\\Guide Notes.md:10-20",
79 ],
80 ["\\\\server\\Shared Docs\\Guide.md", "\\\\server\\Shared Docs\\Guide.md"],
81 ["\\\\server name\\share\\Guide Notes.md", null],
82 ["\\\\server\\Guide Notes.md", null],
83 ["~/Videos/clips/<thread-slug>/shot.png", null],
84 ["clips/<name>.mp4", null],
85 ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
86 expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
87 });
88});
89
90describe("parseMarkdownFileLink", () => {
91 it.each([
92 ["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],
93 ["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],
94 ["file://localhost/home/me/notes.md", "/home/me/notes.md"],
95 ["app/routes/posts.$postId.tsx", "app/routes/posts.$postId.tsx"],
96 ["src/routes/+page.svelte", "src/routes/+page.svelte"],
97 ["app/(marketing)/page.tsx:12", "app/(marketing)/page.tsx"],
98 ["pages/posts/%5Bid%5D.tsx", "pages/posts/[id].tsx"],
99 ["+page.svelte", "+page.svelte"],
100 ["[id].tsx", "[id].tsx"],
101 ["/\\server\\share\\x.ts", "/\\server\\share\\x.ts"],
102 ])("recognizes %s as a file", (href, path) => {
103 expect(parseMarkdownFileLink(href)?.path).toBe(path);
104 });
105
106 it.each([
107 "app/routes/posts.$postId.tsx",
108 "pages/posts/[id].tsx",
109 "docs/My File (draft).md",
110 "@scope/package.json",
111 "$postId.tsx",
112 ])("reads back the composer's link to %s", (path) => {
113 const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
114 expect(parseMarkdownFileLink(destination ?? "")?.path).toBe(path);
115 });
116
117 // regression: #41223 names the parser rejects bare; the composer adds `./` to them
118 it.each([
119 "C#/Program.cs",
120 "docs/R&D/notes.md",
121 "docs/Bob's notes.md",
122 "docs/a,b.ts",
123 "docs/100%.md",
124 "docs/why?.md",
125 "icon@2x.png",
126 ])("resolves the composer's link to %s", (path) => {
127 const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
128 expect(resolveMarkdownFileLinkTarget(destination ?? "", "/workspace/project")).toBe(
129 `/workspace/project/${path}`,
130 );
131 });
132
133 it.each([
134 "https://example.com/docs",
135 "/chat/settings#L3",
136 "TODO:12",
137 "patches",
138 "me@example.com",
139 "foo(bar)",
140 "$HOME",
141 "/tmp/claude-1000/...",
142 "/tmp/claude-1000/....:12",
143 "/tmp/claude-1000/%2E%2E%2E",
144 "\\foo.ts",
145 "sandbox://server/share/notes.txt",
146 "sandbox:/%2Fserver/share/notes.txt",
147 "sandbox:/%5Cserver/share/notes.txt",
148 "sandbox:/tmp/bad%00name.txt",
149 ])("does not treat %s as a file", (href) => {
150 expect(parseMarkdownFileLink(href)).toBeNull();
151 });
152
153 it("resolves a sandbox link as an absolute host path, even outside the workspace", () => {
154 const href = "Sandbox:/Users/me/agent-output/example/brief%20%23final.txt#L3C2";
155 expect(parseMarkdownFileLink(href)).toEqual({
156 path: "/Users/me/agent-output/example/brief #final.txt",
157 line: 3,
158 column: 2,
159 });
160 expect(resolveMarkdownFileLinkTarget(href, "/Users/me/repo")).toBe(
161 "/Users/me/agent-output/example/brief #final.txt:3:2",
162 );
163 });
164
165 it("recognizes a relative folder that ends in a slash", () => {
166 expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });
167 expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");
168 });
169
170 // regression: #16096 relative links with non-ASCII names open as files
171 it("recognizes relative paths with non-ASCII names", () => {
172 expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe(
173 "pastexam/線性代數/exam_易志偉.pdf",
174 );
175 expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf");
176 expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份");
177 });
178
179 it("accepts conventional extensionless names with or without a position", () => {
180 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
181 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
182 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
183 });
184
185 it("reads line ranges and opens them at their first line", () => {
186 expect(parseMarkdownFileLink("/repo/src/http.ts:42-64")).toEqual({
187 path: "/repo/src/http.ts",
188 line: 42,
189 endLine: 64,
190 });
191 expect(parseMarkdownFileLink("/repo/src/http.ts#L42-L64")).toEqual({
192 path: "/repo/src/http.ts",
193 line: 42,
194 endLine: 64,
195 });
196 });
197
198 it("reads positions from suffixes and line anchors", () => {
199 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
200 path: "/Users/julius/project/src/main.ts",
201 line: 42,
202 column: 7,
203 });
204 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
205 path: "\\\\server\\share\\src\\main.ts",
206 line: 42,
207 column: 7,
208 });
209 });
210});
211
212describe("workspaceRelativeFilePath", () => {
213 // regression: #12449 workspace root links resolve to "."
214 it.each([
215 ["/repo/project", "/repo/project", "."],
216 ["C:/", "c:/", "."],
217 ["/repo/project/a.ts", undefined, null],
218 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
219 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
220 });
221});
222
223describe("extractInlineCodeSpans", () => {
224 it.each([
225 ["``./a/shared.ts`` and `./b/shared.ts`", ["./a/shared.ts", "./b/shared.ts"]],
226 ["``a ` b`` and `./b/shared.ts`", ["a ` b", "./b/shared.ts"]],
227 ["``unclosed and `./b/shared.ts`", ["./b/shared.ts"]],
228 ["\\``./a/shared.ts` and `./b/shared.ts`", ["./a/shared.ts", "./b/shared.ts"]],
229 ["`a\\` and `./b/shared.ts`", ["a\\", "./b/shared.ts"]],
230 ["`unclosed\n`./b/shared.ts`", ["./b/shared.ts"]],
231 ])("matches complete backtick delimiter runs in %s", (markdown, spans) => {
232 expect(extractInlineCodeSpans(markdown)).toEqual(spans);
233 });
234});
235
236describe("extractMarkdownLinkHrefs", () => {
237 it.each([
238 ["[".repeat(32_000), []],
239 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
240 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
241 [
242 "[](src/index.ts) [](lib/index.ts)",
243 ["a.png", "src/index.ts", "b.png", "lib/index.ts"],
244 ],
245 [String.raw`[\](src/index.ts) [label!](ok)`, ["a.png", "ok"]],
246 ["See [the [docs] guide](docs/guide.md) and [[1]](src/a.ts)", ["docs/guide.md", "src/a.ts"]],
247 ["[a [b](x) [c \\[d](y) [e\\]](z)", ["x", "y", "z"]],
248 ["[a [b]\n\nc](src/a.ts) [outer [inner]()](src/b.ts)", []],
249 [`See ${serializeComposerFileLink("pages/posts/[id].tsx")} now`, ["pages/posts/%5Bid%5D.tsx"]],
250 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
251 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
252 });
253
254 it.each([
255 [
256 String.raw`[one](<./a/doc\> final.ts>) [two](<./b/doc\> final.ts>)`,
257 [String.raw`./a/doc\> final.ts`, String.raw`./b/doc\> final.ts`],
258 ],
259 [
260 String.raw`[one](<./a/doc\\> "title") [two](<./b/doc\\> (title))`,
261 [String.raw`./a/doc\\`, String.raw`./b/doc\\`],
262 ],
263 [
264 String.raw`[bad](<./x/dir\> text) [one](<./a/doc\\\> final.ts> "title") [two](<./b/doc\< final.ts>)`,
265 [String.raw`./a/doc\\\> final.ts`, String.raw`./b/doc\< final.ts`],
266 ],
267 [
268 "[a](/x/app/(a)/page.tsx) [b](/x/app/(b)/page.tsx)",
269 ["/x/app/(a)/page.tsx", "/x/app/(b)/page.tsx"],
270 ],
271 ['[a](/tmp/a(1).txt "title") after', ["/tmp/a(1).txt"]],
272 ["[a.ts](a/a.ts (first)) [a.ts](b/a.ts (second))", ["a/a.ts", "b/a.ts"]],
273 [
274 String.raw`[a](<a/a.ts> (escaped \(first\) and \\)) [b](b/a.ts (second))`,
275 ["a/a.ts", "b/a.ts"],
276 ],
277 [
278 "[a](a.ts (nested (title))) [b](b.ts (missing close) [c](c.ts (blank\n\nline)) [d](ok)",
279 ["ok"],
280 ],
281 [`[a](a.ts "it's a title") [b](b.ts 'a "quoted" title') [c](ok)`, ["a.ts", "b.ts", "ok"]],
282 [`[a.ts](src/a.ts "say \\"hello\\"") [a.ts](lib/a.ts)`, ["src/a.ts", "lib/a.ts"]],
283 [`[a](a.ts 'it\\'s') [b](b.ts "ends in \\\\") [c](ok)`, ["a.ts", "b.ts", "ok"]],
284 [`[a](a.ts "unclosed \\") [b](b.ts)`, ["b.ts"]],
285 [`[a](a.ts "mixed') [b](b.ts)`, ["b.ts"]],
286 [`[a](a.ts "it's\n\n[b](lib/a.ts)")`, ["lib/a.ts"]],
287 ["[a](a(b(c))d) [b](ok)", ["a(b(c))d", "ok"]],
288 ["[a](x\\)y) [b](x\\(y)", ["x\\)y", "x\\(y"]],
289 ["[a](unclosed(paren) [b](ok)", ["ok"]],
290 ["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]],
291 ["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]],
292 ])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => {
293 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
294 });
295});
296
297describe("resolveMarkdownFileLinkTarget", () => {
298 it("resolves a relative folder with a trailing slash against cwd", () => {
299 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
300 "/repo/project/patches/",
301 );
302 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
303 });
304
305 // regression: #33625 backslash-separated relative links open the workspace file
306 it.each([
307 ["src/Example.cs", "C:\\repo\\src\\Example.cs"],
308 ["src\\Example.cs", "C:\\repo\\src\\Example.cs"],
309 ["src%5CExample.cs", "C:\\repo\\src\\Example.cs"],
310 ["src\\Example.cs:3", "C:\\repo\\src\\Example.cs:3"],
311 ])("resolves the relative link %s against a windows cwd", (href, target) => {
312 expect(resolveMarkdownFileLinkTarget(href, "C:\\repo")).toBe(target);
313 });
314
315 it("keeps a backslash in a relative link that already names a file", () => {
316 expect(resolveMarkdownFileLinkTarget("./foo%5Cbar.ts", "/repo")).toBe("/repo/foo\\bar.ts");
317 });
318
319 it("does not treat filename line references as external schemes", () => {
320 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
321 "/Users/julius/project/script.ts:10",
322 );
323 });
324
325 it("maps #L line anchors to editor line suffixes", () => {
326 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
327 "/Users/julius/project/src/main.ts:42:7",
328 );
329 });
330
331 it("ignores external urls", () => {
332 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
333 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
334 });
335
336 it("does not double-decode file URLs", () => {
337 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
338 "/Users/julius/project/file%20name.md",
339 );
340 });
341
342 it("links a network path only inside the project's share", () => {
343 const cwd = "\\\\server\\share";
344 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
345 "\\\\server\\share\\workspace-image.svg",
346 );
347 // Opening these sent the Windows user's NTLM credentials to attacker.test.
348 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
349 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
350 }
351 });
352
353 it("keeps an encoded final space in the absolute target", () => {
354 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
355 "/tmp/repo/file.ts ",
356 );
357 });
358
359 it("normalizes slash-prefixed windows drive paths before resolving", () => {
360 expect(
361 resolveMarkdownFileLinkTarget(
362 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
363 ),
364 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
365 });
366
367 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
368 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
369 "c:/Users/x/shot.png",
370 );
371 });
372});