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

markdownLinks.test.ts

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