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

markdownLinks.test.ts

159 lines5.9 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 it("accepts conventional extensionless names with or without a position", () => {
58 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
59 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
60 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
61 });
62​
63 it("reads positions from suffixes and line anchors", () => {
64 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
65 path: "/Users/julius/project/src/main.ts",
66 line: 42,
67 column: 7,
68 });
69 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
70 path: "\\\\server\\share\\src\\main.ts",
71 line: 42,
72 column: 7,
73 });
74 });
75});
76​
77describe("workspaceRelativeFilePath", () => {
78 // regression: #12449 workspace root links resolve to "."
79 it.each([
80 ["/repo/project", "/repo/project", "."],
81 ["C:/", "c:/", "."],
82 ["/repo/project/a.ts", undefined, null],
83 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
84 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
85 });
86});
87​
88describe("extractMarkdownLinkHrefs", () => {
89 it.each([
90 ["[".repeat(32_000), []],
91 ["[a](<foo)bar> invalid [b](ok)", ["<foo", "ok"]],
92 ["[[[nested](src/main.ts) [unfinished", ["src/main.ts"]],
93 ])("extracts links from incomplete and nested text", (markdown, hrefs) => {
94 expect(extractMarkdownLinkHrefs(markdown)).toEqual(hrefs);
95 });
96});
97​
98describe("resolveMarkdownFileLinkTarget", () => {
99 it("resolves a relative folder with a trailing slash against cwd", () => {
100 expect(resolveMarkdownFileLinkTarget("patches/", "/repo/project")).toBe(
101 "/repo/project/patches/",
102 );
103 expect(resolveMarkdownFileLinkTarget("patches", "/repo/project")).toBeNull();
104 });
105​
106 it("does not treat filename line references as external schemes", () => {
107 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
108 "/Users/julius/project/script.ts:10",
109 );
110 });
111​
112 it("maps #L line anchors to editor line suffixes", () => {
113 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
114 "/Users/julius/project/src/main.ts:42:7",
115 );
116 });
117​
118 it("ignores external urls", () => {
119 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
120 expect(resolveMarkdownFileLinkTarget("//cdn.example.com/clip.mp4", "/workspace")).toBeNull();
121 });
122​
123 it("does not double-decode file URLs", () => {
124 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
125 "/Users/julius/project/file%20name.md",
126 );
127 });
128​
129 it("links a network path only inside the project's share", () => {
130 const cwd = "\\\\server\\share";
131 expect(resolveMarkdownFileLinkTarget("file://server/share/workspace-image.svg", cwd)).toBe(
132 "\\\\server\\share\\workspace-image.svg",
133 );
134 // Opening these sent the Windows user's NTLM credentials to attacker.test.
135 for (const href of ["file://attacker.test/s/x.ts", "\\\\attacker.test\\s\\x.ts"]) {
136 expect(resolveMarkdownFileLinkTarget(href, cwd)).toBeNull();
137 }
138 });
139​
140 it("keeps an encoded final space in the absolute target", () => {
141 expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
142 "/tmp/repo/file.ts ",
143 );
144 });
145​
146 it("normalizes slash-prefixed windows drive paths before resolving", () => {
147 expect(
148 resolveMarkdownFileLinkTarget(
149 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
150 ),
151 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
152 });
153​
154 it("decodes an encoded drive colon in a file uri before dropping its slash", () => {
155 expect(resolveMarkdownFileLinkTarget("file:///c%3A/Users/x/shot.png")).toBe(
156 "c:/Users/x/shot.png",
157 );
158 });
159});