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 ])("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 ])("recognizes %s as a file", (href, path) => {
56 expect(parseMarkdownFileLink(href)?.path).toBe(path);
57 });
58
59 it.each([
60 "app/routes/posts.$postId.tsx",
61 "pages/posts/[id].tsx",
62 "docs/My File (draft).md",
63 "@scope/package.json",
64 "$postId.tsx",
65 ])("reads back the composer's link to %s", (path) => {
66 const destination = /\]\((.*)\)$/.exec(serializeComposerFileLink(path))?.[1];
67 expect(parseMarkdownFileLink(destination ?? "")?.path).toBe(path);
68 });
69
70 it.each([
71 "https://example.com/docs",
72 "/chat/settings#L3",
73 "TODO:12",
74 "patches",
75 "me@example.com",
76 "foo(bar)",
77 "$HOME",
78 ])("does not treat %s as a file", (href) => {
79 expect(parseMarkdownFileLink(href)).toBeNull();
80 });
81
82 it("recognizes a relative folder that ends in a slash", () => {
83 expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });
84 expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");
85 });
86
87 // regression: #16096 relative links with non-ASCII names open as files
88 it("recognizes relative paths with non-ASCII names", () => {
89 expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe(
90 "pastexam/線性代數/exam_易志偉.pdf",
91 );
92 expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf");
93 expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份");
94 });
95
96 it("accepts conventional extensionless names with or without a position", () => {
97 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
98 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
99 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
100 });
101
102 it("reads positions from suffixes and line anchors", () => {
103 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
104 path: "/Users/julius/project/src/main.ts",
105 line: 42,
106 column: 7,
107 });
108 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
109 path: "\\\\server\\share\\src\\main.ts",
110 line: 42,
111 column: 7,
112 });
113 });
114});
115
116describe("workspaceRelativeFilePath", () => {
117 // regression: #12449 workspace root links resolve to "."
118 it.each([
119 ["/repo/project", "/repo/project", "."],
120 ["C:/", "c:/", "."],
121 ["/repo/project/a.ts", undefined, null],
122 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
123 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
124 });
125});
126
127describe("extractMarkdownLinkHrefs", () => {
128 it.each([
129 ["[".repeat(32_000), []],
130 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
131 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
132 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
133 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
134 });
135});
136
137describe("resolveMarkdownFileLinkTarget", () => {
138 it("resolves a relative folder with a trailing slash against cwd", () => {
139 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
140 "/repo/project/patches/",
141 );
142 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
143 });
144
145 it("does not treat filename line references as external schemes", () => {
146 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
147 "/Users/julius/project/script.ts:10",
148 );
149 });
150
151 it("maps #L line anchors to editor line suffixes", () => {
152 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
153 "/Users/julius/project/src/main.ts:42:7",
154 );
155 });
156
157 it("ignores external urls", () => {
158 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
159 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
160 });
161
162 it("does not double-decode file URLs", () => {
163 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
164 "/Users/julius/project/file%20name.md",
165 );
166 });
167
168 it("links a network path only inside the project's share", () => {
169 const cwd = "\\\\server\\share";
170 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
171 "\\\\server\\share\\workspace-image.svg",
172 );
173 // Opening these sent the Windows user's NTLM credentials to attacker.test.
174 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
175 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
176 }
177 });
178
179 it("keeps an encoded final space in the absolute target", () => {
180 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
181 "/tmp/repo/file.ts ",
182 );
183 });
184
185 it("normalizes slash-prefixed windows drive paths before resolving", () => {
186 expect(
187 resolveMarkdownFileLinkTarget(
188 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
189 ),
190 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
191 });
192
193 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
194 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
195 "c:/Users/x/shot.png",
196 );
197 });
198});