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

markdownLinks.test.ts

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