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