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

markdownLinks.test.ts

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