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

markdownLinks.test.ts

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