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