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 ["src/main.ts/", null],
36 ["./patches/", "./patches/"],
37 // regression: #30757 an agent's shortened path is not a file
38 ["/tmp/claude-1000/...", null],
39 ["/tmp/claude-1000/...:12", null],
40 ["/tmp/.../scratchpad/notes.md", null],
41 ["~/projects/…/notes.md", null],
42 ["src/.../main.ts", null],
43 ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
44 expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
45 });
46});
47
48describe("parseMarkdownFileLink", () => {
49 it.each([
50 ["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],
51 ["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],
52 ["file://localhost/home/me/notes.md", "/home/me/notes.md"],
53 ["app/routes/posts.$postId.tsx", "app/routes/posts.$postId.tsx"],
54 ["src/routes/+page.svelte", "src/routes/+page.svelte"],
55 ["app/(marketing)/page.tsx:12", "app/(marketing)/page.tsx"],
56 ["pages/posts/%5Bid%5D.tsx", "pages/posts/[id].tsx"],
57 ["+page.svelte", "+page.svelte"],
58 ["[id].tsx", "[id].tsx"],
59 ["/\\server\\share\\x.ts", "/\\server\\share\\x.ts"],
60 ])("recognizes %s as a file", (href, path) => {
61 expect(parseMarkdownFileLink(href)?.path).toBe(path);
62 });
63
64 it.each([
65 "app/routes/posts.$postId.tsx",
66 "pages/posts/[id].tsx",
67 "docs/My File (draft).md",
68 "@scope/package.json",
69 "$postId.tsx",
70 ])("reads back the composer's link to %s", (path) => {
71 const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
72 expect(parseMarkdownFileLink(destination ?? "")?.path).toBe(path);
73 });
74
75 it.each([
76 "https://example.com/docs",
77 "/chat/settings#L3",
78 "TODO:12",
79 "patches",
80 "me@example.com",
81 "foo(bar)",
82 "$HOME",
83 "\\foo.ts",
84 ])("does not treat %s as a file", (href) => {
85 expect(parseMarkdownFileLink(href)).toBeNull();
86 });
87
88 it("recognizes a relative folder that ends in a slash", () => {
89 expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });
90 expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");
91 });
92
93 // regression: #16096 relative links with non-ASCII names open as files
94 it("recognizes relative paths with non-ASCII names", () => {
95 expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe(
96 "pastexam/線性代數/exam_易志偉.pdf",
97 );
98 expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf");
99 expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份");
100 });
101
102 it("accepts conventional extensionless names with or without a position", () => {
103 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
104 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
105 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
106 });
107
108 it("reads line ranges and opens them at their first line", () => {
109 expect(parseMarkdownFileLink("/repo/src/http.ts:42-64")).toEqual({
110 path: "/repo/src/http.ts",
111 line: 42,
112 endLine: 64,
113 });
114 expect(parseMarkdownFileLink("/repo/src/http.ts#L42-L64")).toEqual({
115 path: "/repo/src/http.ts",
116 line: 42,
117 endLine: 64,
118 });
119 });
120
121 it("reads positions from suffixes and line anchors", () => {
122 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
123 path: "/Users/julius/project/src/main.ts",
124 line: 42,
125 column: 7,
126 });
127 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
128 path: "\\\\server\\share\\src\\main.ts",
129 line: 42,
130 column: 7,
131 });
132 });
133});
134
135describe("workspaceRelativeFilePath", () => {
136 // regression: #12449 workspace root links resolve to "."
137 it.each([
138 ["/repo/project", "/repo/project", "."],
139 ["C:/", "c:/", "."],
140 ["/repo/project/a.ts", undefined, null],
141 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
142 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
143 });
144});
145
146describe("extractMarkdownLinkHrefs", () => {
147 it.each([
148 ["[".repeat(32_000), []],
149 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
150 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
151 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
152 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
153 });
154});
155
156describe("resolveMarkdownFileLinkTarget", () => {
157 it("resolves a relative folder with a trailing slash against cwd", () => {
158 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
159 "/repo/project/patches/",
160 );
161 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
162 });
163
164 // regression: #33625 backslash-separated relative links open the workspace file
165 it.each([
166 ["src/Example.cs", "C:\\repo\\src\\Example.cs"],
167 ["src\\Example.cs", "C:\\repo\\src\\Example.cs"],
168 ["src%5CExample.cs", "C:\\repo\\src\\Example.cs"],
169 ["src\\Example.cs:3", "C:\\repo\\src\\Example.cs:3"],
170 ])("resolves the relative link %s against a windows cwd", (href, target) => {
171 expect(resolveMarkdownFileLinkTarget(href, "C:\\repo")).toBe(target);
172 });
173
174 it("keeps a backslash in a relative link that already names a file", () => {
175 expect(resolveMarkdownFileLinkTarget("./foo%5Cbar.ts", "/repo")).toBe("/repo/./foo\\bar.ts");
176 });
177
178 it("does not treat filename line references as external schemes", () => {
179 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
180 "/Users/julius/project/script.ts:10",
181 );
182 });
183
184 it("maps #L line anchors to editor line suffixes", () => {
185 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
186 "/Users/julius/project/src/main.ts:42:7",
187 );
188 });
189
190 it("ignores external urls", () => {
191 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
192 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
193 });
194
195 it("does not double-decode file URLs", () => {
196 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
197 "/Users/julius/project/file%20name.md",
198 );
199 });
200
201 it("links a network path only inside the project's share", () => {
202 const cwd = "\\\\server\\share";
203 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
204 "\\\\server\\share\\workspace-image.svg",
205 );
206 // Opening these sent the Windows user's NTLM credentials to attacker.test.
207 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
208 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
209 }
210 });
211
212 it("keeps an encoded final space in the absolute target", () => {
213 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
214 "/tmp/repo/file.ts ",
215 );
216 });
217
218 it("normalizes slash-prefixed windows drive paths before resolving", () => {
219 expect(
220 resolveMarkdownFileLinkTarget(
221 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
222 ),
223 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
224 });
225
226 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
227 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
228 "c:/Users/x/shot.png",
229 );
230 });
231});