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