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

markdownLinks.test.ts

295 lines11.7 KB

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 // regression: #41223 names the parser rejects bare; the composer adds `./` to them
99 it.each([
100 "C#/Program.cs",
101 "docs/R&D/notes.md",
102 "docs/Bob's notes.md",
103 "docs/a,b.ts",
104 "docs/100%.md",
105 "docs/why?.md",
106 "icon@2x.png",
107 ])("resolves the composer's link to %s", (path) => {
108 const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
109 expect(resolveMarkdownFileLinkTarget(destination ?? "", "/workspace/project")).toBe(
110 `/workspace/project/${path}`,
111 );
112 });
113​
114 it.each([
115 "https://example.com/docs",
116 "/chat/settings#L3",
117 "TODO:12",
118 "patches",
119 "me@example.com",
120 "foo(bar)",
121 "$HOME",
122 "/tmp/claude-1000/...",
123 "/tmp/claude-1000/....:12",
124 "/tmp/claude-1000/%2E%2E%2E",
125 "\\foo.ts",
126 ])("does not treat %s as a file", (href) => {
127 expect(parseMarkdownFileLink(href)).toBeNull();
128 });
129​
130 it("recognizes a relative folder that ends in a slash", () => {
131 expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });
132 expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");
133 });
134​
135 // regression: #16096 relative links with non-ASCII names open as files
136 it("recognizes relative paths with non-ASCII names", () => {
137 expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe(
138 "pastexam/線性代數/exam_易志偉.pdf",
139 );
140 expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf");
141 expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份");
142 });
143​
144 it("accepts conventional extensionless names with or without a position", () => {
145 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
146 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
147 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
148 });
149​
150 it("reads line ranges and opens them at their first line", () => {
151 expect(parseMarkdownFileLink("/repo/src/http.ts:42-64")).toEqual({
152 path: "/repo/src/http.ts",
153 line: 42,
154 endLine: 64,
155 });
156 expect(parseMarkdownFileLink("/repo/src/http.ts#L42-L64")).toEqual({
157 path: "/repo/src/http.ts",
158 line: 42,
159 endLine: 64,
160 });
161 });
162​
163 it("reads positions from suffixes and line anchors", () => {
164 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
165 path: "/Users/julius/project/src/main.ts",
166 line: 42,
167 column: 7,
168 });
169 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
170 path: "\\\\server\\share\\src\\main.ts",
171 line: 42,
172 column: 7,
173 });
174 });
175});
176​
177describe("workspaceRelativeFilePath", () => {
178 // regression: #12449 workspace root links resolve to "."
179 it.each([
180 ["/repo/project", "/repo/project", "."],
181 ["C:/", "c:/", "."],
182 ["/repo/project/a.ts", undefined, null],
183 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
184 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
185 });
186});
187​
188describe("extractMarkdownLinkHrefs", () => {
189 it.each([
190 ["[".repeat(32_000), []],
191 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
192 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
193 ["See [the [docs] guide](docs/guide.md) and [[1]](src/a.ts)", ["docs/guide.md", "src/a.ts"]],
194 ["[a [b](x) [c \\[d](y) [e\\]](z)", ["x", "y", "z"]],
195 ["[a [b]\n\nc](src/a.ts) [outer [inner]()](src/b.ts)", []],
196 [`See ${serializeComposerFileLink("pages/posts/[id].tsx")} now`, ["pages/posts/%5Bid%5D.tsx"]],
197 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
198 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
199 });
200​
201 it.each([
202 [
203 "[a](/x/app/(a)/page.tsx) [b](/x/app/(b)/page.tsx)",
204 ["/x/app/(a)/page.tsx", "/x/app/(b)/page.tsx"],
205 ],
206 ['[a](/tmp/a(1).txt "title") after', ["/tmp/a(1).txt"]],
207 [`[a](a.ts "it's a title") [b](b.ts 'a "quoted" title') [c](ok)`, ["a.ts", "b.ts", "ok"]],
208 [`[a](a.ts "mixed') [b](b.ts)`, ["b.ts"]],
209 [`[a](a.ts "it's\n\n[b](lib/a.ts)")`, ["lib/a.ts"]],
210 ["[a](a(b(c))d) [b](ok)", ["a(b(c))d", "ok"]],
211 ["[a](x\\)y) [b](x\\(y)", ["x\\)y", "x\\(y"]],
212 ["[a](unclosed(paren) [b](ok)", ["ok"]],
213 ["[a](" + "(".repeat(32) + "x" + ")".repeat(32) + ")", ["(".repeat(32) + "x" + ")".repeat(32)]],
214 ["[a](" + "(".repeat(33) + "x" + ")".repeat(33) + ") [b](ok)", ["ok"]],
215 ])("keeps balanced parentheses in a destination like the parser: %s", (markdown, hrefs) => {
216 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
217 });
218});
219​
220describe("resolveMarkdownFileLinkTarget", () => {
221 it("resolves a relative folder with a trailing slash against cwd", () => {
222 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
223 "/repo/project/patches/",
224 );
225 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
226 });
227​
228 // regression: #33625 backslash-separated relative links open the workspace file
229 it.each([
230 ["src/Example.cs", "C:\\repo\\src\\Example.cs"],
231 ["src\\Example.cs", "C:\\repo\\src\\Example.cs"],
232 ["src%5CExample.cs", "C:\\repo\\src\\Example.cs"],
233 ["src\\Example.cs:3", "C:\\repo\\src\\Example.cs:3"],
234 ])("resolves the relative link %s against a windows cwd", (href, target) => {
235 expect(resolveMarkdownFileLinkTarget(href, "C:\\repo")).toBe(target);
236 });
237​
238 it("keeps a backslash in a relative link that already names a file", () => {
239 expect(resolveMarkdownFileLinkTarget("./foo%5Cbar.ts", "/repo")).toBe("/repo/foo\\bar.ts");
240 });
241​
242 it("does not treat filename line references as external schemes", () => {
243 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
244 "/Users/julius/project/script.ts:10",
245 );
246 });
247​
248 it("maps #L line anchors to editor line suffixes", () => {
249 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
250 "/Users/julius/project/src/main.ts:42:7",
251 );
252 });
253​
254 it("ignores external urls", () => {
255 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
256 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
257 });
258​
259 it("does not double-decode file URLs", () => {
260 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
261 "/Users/julius/project/file%20name.md",
262 );
263 });
264​
265 it("links a network path only inside the project's share", () => {
266 const cwd = "\\\\server\\share";
267 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
268 "\\\\server\\share\\workspace-image.svg",
269 );
270 // Opening these sent the Windows user's NTLM credentials to attacker.test.
271 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
272 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
273 }
274 });
275​
276 it("keeps an encoded final space in the absolute target", () => {
277 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
278 "/tmp/repo/file.ts ",
279 );
280 });
281​
282 it("normalizes slash-prefixed windows drive paths before resolving", () => {
283 expect(
284 resolveMarkdownFileLinkTarget(
285 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
286 ),
287 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
288 });
289​
290 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
291 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
292 "c:/Users/x/shot.png",
293 );
294 });
295});