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

markdownLinks.test.ts

168 lines6.4 KB

1import { workspaceRelativeFilePath } from "./path.ts";
2import { describe, expect, it } from "vite-plus/test";
3import { extractMarkdownLinkHrefs, resolveMarkdownFileLinkTarget } from "./markdownLinks.ts";
4​
5import {
6 inlineCodeFilePathCandidate,
7 isMarkdownFileLinkLabel,
8 parseMarkdownFileLink,
9} from "./markdownLinks.ts";
10​
11describe("isMarkdownFileLinkLabel", () => {
12 // regression: #15509 descriptive labels are not mistaken for the linked file name
13 it.each([
14 ["validates the input", "/repo/src/example.ts:12", false],
15 ["example.ts:12", "/repo/src/example.ts:12", true],
16 ["example.ts:99", "/repo/src/example.ts:12", false],
17 ])("classifies %s for %s", (label, href, expected) => {
18 expect(isMarkdownFileLinkLabel(label, href)).toBe(expected);
19 });
20});
21​
22describe("inlineCodeFilePathCandidate", () => {
23 // regression: #13909 model ids in inline code do not become file chips
24 it.each([
25 ["src\\main.ts", "src/main.ts"],
26 ["origin/main", null],
27 ["z-ai/glm-5.3", null],
28 ["share/man/ls.1", "share/man/ls.1"],
29 ["src/main.ts/", null],
30 ["./patches/", "./patches/"],
31 ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
32 expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
33 });
34});
35​
36describe("parseMarkdownFileLink", () => {
37 it.each([
38 ["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],
39 ["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],
40 ["file://localhost/home/me/notes.md", "/home/me/notes.md"],
41 ])("recognizes %s as a file", (href, path) => {
42 expect(parseMarkdownFileLink(href)?.path).toBe(path);
43 });
44​
45 it.each(["https://example.com/docs", "/chat/settings#L3", "TODO:12", "patches"])(
46 "does not treat %s as a file",
47 (href) => {
48 expect(parseMarkdownFileLink(href)).toBeNull();
49 },
50 );
51​
52 it("recognizes a relative folder that ends in a slash", () => {
53 expect(parseMarkdownFileLink("patches/")).toEqual({ path: "patches/" });
54 expect(parseMarkdownFileLink("docs/My%20Folder/")?.path).toBe("docs/My Folder/");
55 });
56​
57 // regression: #16096 relative links with non-ASCII names open as files
58 it("recognizes relative paths with non-ASCII names", () => {
59 expect(parseMarkdownFileLink("pastexam/線性代數/exam_易志偉.pdf")?.path).toBe(
60 "pastexam/線性代數/exam_易志偉.pdf",
61 );
62 expect(parseMarkdownFileLink("Résumé%20final.pdf")?.path).toBe("Résumé final.pdf");
63 expect(parseMarkdownFileLink("資料.備份")?.path).toBe("資料.備份");
64 });
65​
66 it("accepts conventional extensionless names with or without a position", () => {
67 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
68 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
69 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
70 });
71​
72 it("reads positions from suffixes and line anchors", () => {
73 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
74 path: "/Users/julius/project/src/main.ts",
75 line: 42,
76 column: 7,
77 });
78 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
79 path: "\\\\server\\share\\src\\main.ts",
80 line: 42,
81 column: 7,
82 });
83 });
84});
85​
86describe("workspaceRelativeFilePath", () => {
87 // regression: #12449 workspace root links resolve to "."
88 it.each([
89 ["/repo/project", "/repo/project", "."],
90 ["C:/", "c:/", "."],
91 ["/repo/project/a.ts", undefined, null],
92 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
93 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
94 });
95});
96​
97describe("extractMarkdownLinkHrefs", () => {
98 it.each([
99 ["[".repeat(32_000), []],
100 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
101 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
102 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
103 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
104 });
105});
106​
107describe("resolveMarkdownFileLinkTarget", () => {
108 it("resolves a relative folder with a trailing slash against cwd", () => {
109 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
110 "/repo/project/patches/",
111 );
112 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
113 });
114​
115 it("does not treat filename line references as external schemes", () => {
116 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
117 "/Users/julius/project/script.ts:10",
118 );
119 });
120​
121 it("maps #L line anchors to editor line suffixes", () => {
122 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
123 "/Users/julius/project/src/main.ts:42:7",
124 );
125 });
126​
127 it("ignores external urls", () => {
128 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
129 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
130 });
131​
132 it("does not double-decode file URLs", () => {
133 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
134 "/Users/julius/project/file%20name.md",
135 );
136 });
137​
138 it("links a network path only inside the project's share", () => {
139 const cwd = "\\\\server\\share";
140 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
141 "\\\\server\\share\\workspace-image.svg",
142 );
143 // Opening these sent the Windows user's NTLM credentials to attacker.test.
144 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
145 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
146 }
147 });
148​
149 it("keeps an encoded final space in the absolute target", () => {
150 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
151 "/tmp/repo/file.ts ",
152 );
153 });
154​
155 it("normalizes slash-prefixed windows drive paths before resolving", () => {
156 expect(
157 resolveMarkdownFileLinkTarget(
158 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
159 ),
160 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
161 });
162​
163 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
164 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
165 "c:/Users/x/shot.png",
166 );
167 });
168});