Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 fileBasename,
5 inlineCodeFilePathCandidate,
6 isMarkdownFileLinkLabel,
7 parseFileUrlHref,
8 parseMarkdownFileLink,
9 splitFilePathPosition,
10 workspaceRelativeFilePath,
11} from "./markdownLinks.ts";
12
13describe("isMarkdownFileLinkLabel", () => {
14 it.each([
15 ["validates the input", "/repo/src/example.ts:12", false],
16 ["read src/example.ts", "/repo/src/example.ts:12", false],
17 ["example.ts?why this matters", "/repo/src/example.ts", false],
18 ["example.ts", "/repo/src/example.ts:12", true],
19 ["example.ts:12", "/repo/src/example.ts:12", true],
20 ["example.ts:99", "/repo/src/example.ts:12", false],
21 ["example.ts:12:2", "/repo/src/example.ts:12:2", true],
22 ["example.ts:12:3", "/repo/src/example.ts:12:2", false],
23 ["example.ts:12", "/repo/src/example.ts", false],
24 ["src/example.ts:12", "/repo/src/example.ts:12", true],
25 ["./src/example.ts", "/repo/src/example.ts", true],
26 ["/repo/src/example.ts", "/repo/src/example.ts", true],
27 ["src/", "/home/me/project/src/", true],
28 ["EXAMPLE.TS", "C:/repo/src/example.ts:12", true],
29 ["file name.ts", "file:///repo/file%20name.ts", true],
30 ["", "/repo/src/example.ts", true],
31 ])("classifies %s for %s", (label, href, expected) => {
32 expect(isMarkdownFileLinkLabel(label, href)).toBe(expected);
33 });
34});
35
36describe("inlineCodeFilePathCandidate", () => {
37 it.each([
38 ["src\\main.ts", "src/main.ts"],
39 ["C:\\Users\\demo\\image.png", "C:\\Users\\demo\\image.png"],
40 ["\\\\server\\share\\image.png", "\\\\server\\share\\image.png"],
41 ["conf.d/nginx.conf", "conf.d/nginx.conf"],
42 ["script.pl:10", "script.pl:10"],
43 ["node.meta", null],
44 ["Recorded evidence here: /tmp/image.png", null],
45 ["origin/main", null],
46 ["127.0.0.1:3000", null],
47 ["example.com/index.html", null],
48 ["example.pl/index.html", null],
49 ["z-ai/glm-5.3", null],
50 ["z-ai/glm-5.3:12", null],
51 ["python/3.12", null],
52 ["Qwen/Qwen2.5-Coder", null],
53 ["meta-llama/Llama-3.1-8B", null],
54 ["share/man/ls.1", "share/man/ls.1"],
55 ["usr/lib/libfoo.so.1", "usr/lib/libfoo.so.1"],
56 ["vendor/jquery-3.6.0.min.js", "vendor/jquery-3.6.0.min.js"],
57 ["./models/glm-5.3", "./models/glm-5.3"],
58 ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => {
59 expect(inlineCodeFilePathCandidate(source)).toBe(candidate);
60 });
61});
62
63describe("parseFileUrlHref", () => {
64 it.each([
65 ["file:///Users/julius/project/src/main.ts#L42", "/Users/julius/project/src/main.ts", "#L42"],
66 [
67 "file:///D:/Programme/t3code/OpenInPicker.tsx#L69",
68 "D:/Programme/t3code/OpenInPicker.tsx",
69 "#L69",
70 ],
71 ["file://server/share/workspace-image.svg", "\\\\server\\share\\workspace-image.svg", ""],
72 ["file://localhost/home/me/notes.md", "/home/me/notes.md", ""],
73 ])("parses %s", (href, path, hash) => {
74 expect(parseFileUrlHref(href)).toEqual({ path, hash });
75 });
76
77 it("keeps percent-encoding so the caller decodes once", () => {
78 expect(parseFileUrlHref("file:///Users/julius/project/file%2520name.md")?.path).toBe(
79 "/Users/julius/project/file%2520name.md",
80 );
81 expect(parseFileUrlHref("file:///c%3A/Users/x/shot.png")?.path).toBe("/c%3A/Users/x/shot.png");
82 });
83
84 it.each(["https://example.com/a.ts", "file://%", "/Users/julius/a.ts"])("rejects %s", (href) => {
85 expect(parseFileUrlHref(href)).toBeNull();
86 });
87});
88
89describe("splitFilePathPosition", () => {
90 it.each([
91 ["src/main.ts", "", { path: "src/main.ts" }],
92 ["src/main.ts:12", "", { path: "src/main.ts", line: 12 }],
93 ["src/main.ts:12:5", "", { path: "src/main.ts", line: 12, column: 5 }],
94 ["src/main.ts", "#L18C2", { path: "src/main.ts", line: 18, column: 2 }],
95 ["src/main.ts:3", "#L18C2", { path: "src/main.ts", line: 3 }],
96 ["src/main.ts:0", "", { path: "src/main.ts" }],
97 ["src/main.ts", "#section", { path: "src/main.ts" }],
98 ])("splits %s%s", (path, hash, expected) => {
99 expect(splitFilePathPosition(path, hash)).toEqual(expected);
100 });
101});
102
103describe("parseMarkdownFileLink", () => {
104 // Both clients consume this table, so a path the web app recognizes is one
105 // the mobile app recognizes too.
106 it.each([
107 ["/Users/julius/project/AGENTS.md", "/Users/julius/project/AGENTS.md"],
108 ["/home/me/notes.md", "/home/me/notes.md"],
109 ["/usr/local/bin/tool", "/usr/local/bin/tool"],
110 ["/workspace/Makefile", "/workspace/Makefile"],
111 ["/tmp/favicons/", "/tmp/favicons/"],
112 ["C:\\Users\\mike\\project\\src\\main.ts", "C:\\Users\\mike\\project\\src\\main.ts"],
113 ["C:%5Crepo%5Cimage.png", "C:\\repo\\image.png"],
114 ["\\\\server\\share\\image.png", "\\\\server\\share\\image.png"],
115 ["/D:/Programme/t3code/OpenInPicker.tsx", "D:/Programme/t3code/OpenInPicker.tsx"],
116 ["</D:/Programme/t3code/ChatMarkdown.tsx:1>", "D:/Programme/t3code/ChatMarkdown.tsx"],
117 ["file:///Users/julius/project/file%2520name.md", "/Users/julius/project/file%20name.md"],
118 ["file://server/share/workspace-image.svg", "\\\\server\\share\\workspace-image.svg"],
119 ["file://localhost/home/me/notes.md", "/home/me/notes.md"],
120 ["apps/mobile/src/index.ts:10", "apps/mobile/src/index.ts"],
121 ["docs/My%20Folder/checklist.xml", "docs/My Folder/checklist.xml"],
122 ["Updated%20cutover%20checklist.md", "Updated cutover checklist.md"],
123 ["./scripts/deploy", "./scripts/deploy"],
124 ["~/notes/today.md", "~/notes/today.md"],
125 ["AGENTS.md", "AGENTS.md"],
126 ["script.ts:10", "script.ts"],
127 ["/tmp/clip%23one.mp4#t=2", "/tmp/clip#one.mp4"],
128 ])("recognizes %s as a file", (href, path) => {
129 expect(parseMarkdownFileLink(href)?.path).toBe(path);
130 });
131
132 it.each([
133 "",
134 "#anchor",
135 "//cdn.example.com/clip.mp4",
136 "https://example.com/docs",
137 "mailto:someone@example.com",
138 "javascript:alert(1)",
139 "/chat/settings",
140 "/chat/settings#L3",
141 "/app#L1",
142 "readme",
143 "TODO:12",
144 ])("does not treat %s as a file", (href) => {
145 expect(parseMarkdownFileLink(href)).toBeNull();
146 });
147
148 it("accepts conventional extensionless names with or without a position", () => {
149 expect(parseMarkdownFileLink("Makefile")).toEqual({ path: "Makefile" });
150 expect(parseMarkdownFileLink("Dockerfile:8")).toEqual({ path: "Dockerfile", line: 8 });
151 expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
152 });
153
154 it("reads positions from suffixes and line anchors", () => {
155 expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
156 path: "/Users/julius/project/src/main.ts",
157 line: 42,
158 column: 7,
159 });
160 expect(parseMarkdownFileLink("file://server/share/src/main.ts#L42C7")).toMatchObject({
161 path: "\\\\server\\share\\src\\main.ts",
162 line: 42,
163 column: 7,
164 });
165 });
166});
167
168describe("fileBasename", () => {
169 it.each([
170 ["/tmp/favicons/", "favicons"],
171 ["C:\\Users\\kelchm\\.claude\\", ".claude"],
172 ["/tmp/", "tmp"],
173 ["AGENTS.md", "AGENTS.md"],
174 ["/", "/"],
175 ])("labels %s as %s", (path, basename) => {
176 expect(fileBasename(path)).toBe(basename);
177 });
178});
179
180describe("workspaceRelativeFilePath", () => {
181 it.each([
182 ["/repo/project", "/repo/project", "."],
183 ["/repo/project/", "/repo/project/", "."],
184 ["/", "/", "."],
185 ["C:/USERS/mike/project", "c:/users/MIKE/project", "."],
186 ["C:/", "c:/", "."],
187 ["/repo/project/src/main.ts", "/repo/project", "src/main.ts"],
188 ["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],
189 ["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
190 ["/C:/Users/mike/t3code/apps/web/a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
191 ["/Repo/Project/src/main.ts", "/repo/project", null],
192 ["/tmp/case/project/probe.txt", "/tmp/case/Project", null],
193 ["//tmp/case/project/probe.txt", "//tmp/case/Project", null],
194 ["/tmp/case/Project/probe.txt", "/tmp/case/Project", "probe.txt"],
195 ["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
196 ["/C:/USERS/mike/t3code/main.ts", "/c:/users/MIKE/t3code", "main.ts"],
197 ["\\\\server\\share\\PROJECT\\main.ts", "\\\\Server\\Share\\Project", "main.ts"],
198 ["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
199 ["C:/Users/MIKE/main.ts", "c:/", "Users/MIKE/main.ts"],
200 ["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
201 ["/tmp/repo/file.ts ", "/tmp/repo", "file.ts "],
202 ["/tmp/report.ts", "/repo/project", null],
203 ["/repo/project-two/a.ts", "/repo/project", null],
204 ["/repo/project/a.ts", undefined, null],
205 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
206 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
207 });
208});