Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 resolveInlineCodeFileLinkMeta,
5 resolveMarkdownFileLinkMeta,
6 rewriteMarkdownFileUriHref,
7 shouldOpenMarkdownFileLinkInBrowserByDefault,
8 shouldOpenMarkdownFileLinkInEditor,
9} from "./markdown-links";
10
11describe("shouldOpenMarkdownFileLinkInEditor", () => {
12 it("uses command-click on macOS", () => {
13 expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "MacIntel")).toBe(
14 true,
15 );
16 expect(shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "MacIntel")).toBe(
17 false,
18 );
19 });
20
21 it("uses control-click on other platforms", () => {
22 expect(
23 shouldOpenMarkdownFileLinkInEditor({ metaKey: false, ctrlKey: true }, "Linux x86_64"),
24 ).toBe(true);
25 expect(
26 shouldOpenMarkdownFileLinkInEditor({ metaKey: true, ctrlKey: false }, "Linux x86_64"),
27 ).toBe(false);
28 });
29});
30
31describe("shouldOpenMarkdownFileLinkInBrowserByDefault", () => {
32 it("keeps PDFs browser-first while source files open in the file viewer", () => {
33 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.pdf")).toBe(true);
34 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.PDF?download=1")).toBe(true);
35 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.html")).toBe(false);
36 expect(shouldOpenMarkdownFileLinkInBrowserByDefault("report.xml")).toBe(false);
37 });
38});
39
40describe("rewriteMarkdownFileUriHref", () => {
41 it("rewrites file uri hrefs into direct path hrefs", () => {
42 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/src/main.ts#L42")).toBe(
43 "/Users/julius/project/src/main.ts#L42",
44 );
45 });
46
47 it("preserves encoded octets so file paths are decoded only once later", () => {
48 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(
49 "/Users/julius/project/file%2520name.md",
50 );
51 });
52
53 it("normalizes file uri hrefs for windows drive paths", () => {
54 expect(
55 rewriteMarkdownFileUriHref(
56 "file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
57 ),
58 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
59 });
60
61 it("preserves file uri authorities as windows UNC paths", () => {
62 expect(rewriteMarkdownFileUriHref("file://server/share/workspace-image.svg")).toBe(
63 "\\\\server\\share\\workspace-image.svg",
64 );
65 });
66
67 it("treats a localhost file uri as a local path", () => {
68 expect(rewriteMarkdownFileUriHref("file://localhost/home/me/notes.md")).toBe(
69 "/home/me/notes.md",
70 );
71 });
72
73 it("unwraps angle-bracketed file uri hrefs", () => {
74 expect(
75 rewriteMarkdownFileUriHref(" <file:///D:/Programme/t3code/apps/web/src/markdown-links.ts> "),
76 ).toBe("D:/Programme/t3code/apps/web/src/markdown-links.ts");
77 });
78});
79
80describe("relative links inside a rendered host file", () => {
81 it("anchor to the file's directory while workspace membership follows cwd", () => {
82 const meta = resolveMarkdownFileLinkMeta("appendix.md", "/repo", "/tmp/report");
83 expect(meta).toMatchObject({
84 filePath: "/tmp/report/appendix.md",
85 workspaceRelativePath: null,
86 });
87 const inline = resolveInlineCodeFileLinkMeta("Makefile:12", "/repo", "/tmp/report");
88 expect(inline).toMatchObject({ filePath: "/tmp/report/Makefile", line: 12 });
89 expect(resolveMarkdownFileLinkMeta("src/main.ts", "/repo", "/repo/docs")).toMatchObject({
90 filePath: "/repo/docs/src/main.ts",
91 workspaceRelativePath: "docs/src/main.ts",
92 });
93 });
94});
95
96describe("resolveInlineCodeFileLinkMeta", () => {
97 it("links relative paths with file extensions", () => {
98 expect(
99 resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md", "/Users/julius/project"),
100 ).toMatchObject({
101 targetPath: "/Users/julius/project/docs/internals/workspace-layout.md",
102 basename: "workspace-layout.md",
103 });
104 });
105
106 it("links absolute posix paths", () => {
107 expect(resolveInlineCodeFileLinkMeta("/Users/julius/project/AGENTS.md")).toMatchObject({
108 targetPath: "/Users/julius/project/AGENTS.md",
109 });
110 expect(resolveInlineCodeFileLinkMeta("/usr/local/bin/tool")).toMatchObject({
111 targetPath: "/usr/local/bin/tool",
112 });
113 expect(resolveInlineCodeFileLinkMeta("/workspace/Makefile")).toMatchObject({
114 basename: "Makefile",
115 });
116 expect(resolveInlineCodeFileLinkMeta("/chat/settings")).toBeNull();
117 });
118
119 it("links windows drive paths", () => {
120 expect(resolveInlineCodeFileLinkMeta("C:\\Users\\mike\\project\\src\\main.ts")).toMatchObject({
121 basename: "main.ts",
122 });
123 });
124
125 it("links relative paths with line positions", () => {
126 expect(
127 resolveInlineCodeFileLinkMeta("src/processRunner.ts:71", "/Users/julius/project"),
128 ).toMatchObject({
129 targetPath: "/Users/julius/project/src/processRunner.ts:71",
130 line: 71,
131 });
132 });
133
134 it("links bare filenames only when a line suffix marks them as file references", () => {
135 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
136 targetPath: "/Users/julius/project/script.ts:10",
137 line: 10,
138 });
139 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
140 });
141
142 it("links extensionless bare filenames with a line suffix", () => {
143 expect(resolveInlineCodeFileLinkMeta("Makefile:12", "/Users/julius/project")).toMatchObject({
144 targetPath: "/Users/julius/project/Makefile:12",
145 basename: "Makefile",
146 line: 12,
147 });
148 expect(resolveInlineCodeFileLinkMeta("Dockerfile:8:2", "/Users/julius/project")).toMatchObject({
149 line: 8,
150 column: 2,
151 });
152 expect(resolveInlineCodeFileLinkMeta("Makefile:12")).toBeNull();
153 });
154
155 it("does not treat arbitrary name:digits shapes as files", () => {
156 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
157 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
158 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
159 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
160 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
161 });
162
163 it("links dot-prefixed relative paths without extensions", () => {
164 expect(
165 resolveInlineCodeFileLinkMeta("./scripts/deploy", "/Users/julius/project"),
166 ).toMatchObject({
167 basename: "deploy",
168 });
169 });
170
171 it("links relative windows-style paths by normalizing backslashes", () => {
172 expect(resolveInlineCodeFileLinkMeta("src\\main.ts", "/Users/julius/project")).toMatchObject({
173 targetPath: "/Users/julius/project/src/main.ts",
174 basename: "main.ts",
175 });
176 expect(
177 resolveInlineCodeFileLinkMeta(".\\scripts\\deploy", "/Users/julius/project"),
178 ).toMatchObject({
179 basename: "deploy",
180 });
181 });
182
183 it("ignores hosts, ports, and versions", () => {
184 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
185 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
186 expect(
187 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
188 ).toBeNull();
189 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
190 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
191 expect(
192 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
193 ).toBeNull();
194 expect(
195 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
196 ).toBeNull();
197 });
198
199 it("still links files whose extension merely resembles a tld", () => {
200 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).not.toBeNull();
201 expect(resolveInlineCodeFileLinkMeta("src/setup.sh:3", "/Users/julius/project")).not.toBeNull();
202 expect(resolveInlineCodeFileLinkMeta("Makefile.in:12", "/Users/julius/project")).not.toBeNull();
203 expect(
204 resolveInlineCodeFileLinkMeta("conf.d/nginx.conf", "/Users/julius/project"),
205 ).not.toBeNull();
206 });
207
208 it("prefers file over country-code host when a line suffix is present", () => {
209 expect(resolveInlineCodeFileLinkMeta("script.pl:10", "/Users/julius/project")).toMatchObject({
210 targetPath: "/Users/julius/project/script.pl:10",
211 line: 10,
212 });
213 expect(resolveInlineCodeFileLinkMeta("model.pt:3", "/Users/julius/project")).not.toBeNull();
214 expect(
215 resolveInlineCodeFileLinkMeta("example.pl/index.html", "/Users/julius/project"),
216 ).toBeNull();
217 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
218 });
219
220 it("ignores commands, flags, and expressions", () => {
221 expect(resolveInlineCodeFileLinkMeta("git worktree list --porcelain")).toBeNull();
222 expect(resolveInlineCodeFileLinkMeta("node.meta", "/Users/julius/project")).toBeNull();
223 expect(resolveInlineCodeFileLinkMeta("pnpm install", "/Users/julius/project")).toBeNull();
224 expect(resolveInlineCodeFileLinkMeta("src/**/*.ts", "/Users/julius/project")).toBeNull();
225 });
226
227 it("ignores extension-less relative segments like git refs and directories", () => {
228 expect(resolveInlineCodeFileLinkMeta("origin/main", "/Users/julius/project")).toBeNull();
229 expect(resolveInlineCodeFileLinkMeta("apps/web", "/Users/julius/project")).toBeNull();
230 });
231
232 it("ignores external urls", () => {
233 expect(resolveInlineCodeFileLinkMeta("https://example.com/docs.html")).toBeNull();
234 });
235
236 it("ignores relative paths without a cwd to resolve against", () => {
237 expect(resolveInlineCodeFileLinkMeta("docs/internals/workspace-layout.md")).toBeNull();
238 });
239});
240
241describe("directory paths with a trailing separator", () => {
242 it("keeps the final segment for a POSIX directory path", () => {
243 expect(resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project")).toMatchObject({
244 basename: "favicons",
245 });
246 });
247
248 it("keeps the final segment for a Windows directory path", () => {
249 expect(
250 resolveMarkdownFileLinkMeta("C:\\Users\\kelchm\\.claude\\", "/repo/project"),
251 ).toMatchObject({ basename: ".claude" });
252 });
253
254 it("matches the label of the same path without a trailing separator", () => {
255 const withSlash = resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project");
256 const withoutSlash = resolveMarkdownFileLinkMeta("/tmp/favicons", "/repo/project");
257 expect(withSlash?.basename).toBe(withoutSlash?.basename);
258 });
259
260 it("does not produce an empty label for the filesystem root", () => {
261 const meta = resolveMarkdownFileLinkMeta("/tmp/", "/repo/project");
262 expect(meta?.basename).not.toBe("");
263 });
264});
265
266it("routes the project-root code link to the workspace explorer", () => {
267 const cwd = "/Users/saphid/.t3/worktrees/ov2-standalone-20260918";
268 expect(resolveInlineCodeFileLinkMeta(cwd, cwd)).toMatchObject({
269 workspaceRelativePath: ".",
270 filePath: cwd,
271 });
272});