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

markdown-links.test.ts

300 lines11.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 resolveInlineCodeFileLinkMeta,
5 resolveMarkdownFileLinkMeta,
6 resolveMarkdownFileLinkTarget,
7 rewriteMarkdownFileUriHref,
8} from "./markdown-links";
9​
10describe("rewriteMarkdownFileUriHref", () => {
11 it("rewrites file uri hrefs into direct path hrefs", () => {
12 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/src/main.ts#L42")).toBe(
13 "/Users/julius/project/src/main.ts#L42",
14 );
15 });
16​
17 it("preserves encoded octets so file paths are decoded only once later", () => {
18 expect(rewriteMarkdownFileUriHref("file:///Users/julius/project/file%2520name.md")).toBe(
19 "/Users/julius/project/file%2520name.md",
20 );
21 });
22​
23 it("normalizes file uri hrefs for windows drive paths", () => {
24 expect(
25 rewriteMarkdownFileUriHref(
26 "file:///D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
27 ),
28 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69");
29 });
30​
31 it("unwraps angle-bracketed file uri hrefs", () => {
32 expect(
33 rewriteMarkdownFileUriHref(" <file:///D:/Programme/t3code/apps/web/src/markdown-links.ts> "),
34 ).toBe("D:/Programme/t3code/apps/web/src/markdown-links.ts");
35 });
36});
37​
38describe("resolveMarkdownFileLinkTarget", () => {
39 it("resolves absolute posix file paths", () => {
40 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/AGENTS.md")).toBe(
41 "/Users/julius/project/AGENTS.md",
42 );
43 });
44​
45 it("resolves relative file paths against cwd", () => {
46 expect(resolveMarkdownFileLinkTarget("src/processRunner.ts:71", "/Users/julius/project")).toBe(
47 "/Users/julius/project/src/processRunner.ts:71",
48 );
49 });
50​
51 it("does not treat filename line references as external schemes", () => {
52 expect(resolveMarkdownFileLinkTarget("script.ts:10", "/Users/julius/project")).toBe(
53 "/Users/julius/project/script.ts:10",
54 );
55 });
56​
57 it("resolves bare file names against cwd", () => {
58 expect(resolveMarkdownFileLinkTarget("AGENTS.md", "/Users/julius/project")).toBe(
59 "/Users/julius/project/AGENTS.md",
60 );
61 });
62​
63 it("maps #L line anchors to editor line suffixes", () => {
64 expect(resolveMarkdownFileLinkTarget("/Users/julius/project/src/main.ts#L42C7")).toBe(
65 "/Users/julius/project/src/main.ts:42:7",
66 );
67 });
68​
69 it("ignores external urls", () => {
70 expect(resolveMarkdownFileLinkTarget("https://example.com/docs")).toBeNull();
71 });
72​
73 it("does not double-decode file URLs", () => {
74 expect(resolveMarkdownFileLinkTarget("file:///Users/julius/project/file%2520name.md")).toBe(
75 "/Users/julius/project/file%20name.md",
76 );
77 });
78​
79 it("formats tooltip display paths relative to the cwd when possible", () => {
80 expect(
81 resolveMarkdownFileLinkMeta(
82 "file:///C:/Users/mike/dev-stuff/t3code/apps/web/src/session-logic.ts#L501",
83 "C:/Users/mike/dev-stuff/t3code",
84 ),
85 ).toMatchObject({
86 displayPath: "t3code/apps/web/src/session-logic.ts:501",
87 workspaceRelativePath: "apps/web/src/session-logic.ts",
88 });
89 });
90​
91 it("formats tooltip display paths relative to the cwd for slash-prefixed windows paths", () => {
92 expect(
93 resolveMarkdownFileLinkMeta(
94 "/C:/Users/mike/dev-stuff/t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
95 "C:/Users/mike/dev-stuff/t3code",
96 ),
97 ).toMatchObject({
98 displayPath:
99 "t3code/apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
100 workspaceRelativePath:
101 "apps/web/src/components/chat/MessagesTimeline.virtualization.browser.tsx",
102 });
103 });
104​
105 it("does not create a preview path for files outside the workspace", () => {
106 expect(resolveMarkdownFileLinkMeta("/tmp/report.ts", "/repo/project")).toMatchObject({
107 workspaceRelativePath: null,
108 });
109 });
110​
111 it("normalizes slash-prefixed windows drive paths before resolving", () => {
112 expect(
113 resolveMarkdownFileLinkTarget(
114 "/D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx#L69",
115 ),
116 ).toBe("D:/Programme/t3code/apps/web/src/components/chat/OpenInPicker.tsx:69");
117 });
118​
119 it("resolves angle-bracketed windows drive paths", () => {
120 expect(
121 resolveMarkdownFileLinkTarget(
122 "</D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1>",
123 ),
124 ).toBe("D:/Programme/t3code/apps/web/src/components/ChatMarkdown.tsx:1");
125 });
126​
127 it("does not treat app routes as file links", () => {
128 expect(resolveMarkdownFileLinkTarget("/chat/settings")).toBeNull();
129 });
130});
131​
132describe("resolveInlineCodeFileLinkMeta", () => {
133 it("links relative paths with file extensions", () => {
134 expect(
135 resolveInlineCodeFileLinkMeta(".plans/worktree-management-v1.md", "/Users/julius/project"),
136 ).toMatchObject({
137 targetPath: "/Users/julius/project/.plans/worktree-management-v1.md",
138 basename: "worktree-management-v1.md",
139 });
140 });
141​
142 it("links absolute posix paths", () => {
143 expect(resolveInlineCodeFileLinkMeta("/Users/julius/project/AGENTS.md")).toMatchObject({
144 targetPath: "/Users/julius/project/AGENTS.md",
145 });
146 expect(resolveInlineCodeFileLinkMeta("/usr/local/bin/tool")).toMatchObject({
147 targetPath: "/usr/local/bin/tool",
148 });
149 expect(resolveInlineCodeFileLinkMeta("/workspace/Makefile")).toMatchObject({
150 basename: "Makefile",
151 });
152 expect(resolveInlineCodeFileLinkMeta("/chat/settings")).toBeNull();
153 });
154​
155 it("links windows drive paths", () => {
156 expect(resolveInlineCodeFileLinkMeta("C:\\Users\\mike\\project\\src\\main.ts")).toMatchObject({
157 basename: "main.ts",
158 });
159 });
160​
161 it("links relative paths with line positions", () => {
162 expect(
163 resolveInlineCodeFileLinkMeta("src/processRunner.ts:71", "/Users/julius/project"),
164 ).toMatchObject({
165 targetPath: "/Users/julius/project/src/processRunner.ts:71",
166 line: 71,
167 });
168 });
169​
170 it("links bare filenames only when a line suffix marks them as file references", () => {
171 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).toMatchObject({
172 targetPath: "/Users/julius/project/script.ts:10",
173 line: 10,
174 });
175 expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
176 });
177​
178 it("links extensionless bare filenames with a line suffix", () => {
179 expect(resolveInlineCodeFileLinkMeta("Makefile:12", "/Users/julius/project")).toMatchObject({
180 targetPath: "/Users/julius/project/Makefile:12",
181 basename: "Makefile",
182 line: 12,
183 });
184 expect(resolveInlineCodeFileLinkMeta("Dockerfile:8:2", "/Users/julius/project")).toMatchObject({
185 line: 8,
186 column: 2,
187 });
188 expect(resolveInlineCodeFileLinkMeta("Makefile:12")).toBeNull();
189 });
190​
191 it("does not treat arbitrary name:digits shapes as files", () => {
192 expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
193 expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
194 expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
195 expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
196 expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
197 });
198​
199 it("links dot-prefixed relative paths without extensions", () => {
200 expect(
201 resolveInlineCodeFileLinkMeta("./scripts/deploy", "/Users/julius/project"),
202 ).toMatchObject({
203 basename: "deploy",
204 });
205 });
206​
207 it("links relative windows-style paths by normalizing backslashes", () => {
208 expect(resolveInlineCodeFileLinkMeta("src\\main.ts", "/Users/julius/project")).toMatchObject({
209 targetPath: "/Users/julius/project/src/main.ts",
210 basename: "main.ts",
211 });
212 expect(
213 resolveInlineCodeFileLinkMeta(".\\scripts\\deploy", "/Users/julius/project"),
214 ).toMatchObject({
215 basename: "deploy",
216 });
217 });
218​
219 it("ignores hosts, ports, and versions", () => {
220 expect(resolveInlineCodeFileLinkMeta("127.0.0.1:3000", "/Users/julius/project")).toBeNull();
221 expect(resolveInlineCodeFileLinkMeta("localhost:3000", "/Users/julius/project")).toBeNull();
222 expect(
223 resolveInlineCodeFileLinkMeta("example.com/index.html", "/Users/julius/project"),
224 ).toBeNull();
225 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
226 expect(resolveInlineCodeFileLinkMeta("10.0.0.1:80:1", "/Users/julius/project")).toBeNull();
227 expect(
228 resolveInlineCodeFileLinkMeta("localhost/index.html", "/Users/julius/project"),
229 ).toBeNull();
230 expect(
231 resolveInlineCodeFileLinkMeta("example.uk/index.html", "/Users/julius/project"),
232 ).toBeNull();
233 });
234​
235 it("still links files whose extension merely resembles a tld", () => {
236 expect(resolveInlineCodeFileLinkMeta("script.ts:10", "/Users/julius/project")).not.toBeNull();
237 expect(resolveInlineCodeFileLinkMeta("src/setup.sh:3", "/Users/julius/project")).not.toBeNull();
238 expect(resolveInlineCodeFileLinkMeta("Makefile.in:12", "/Users/julius/project")).not.toBeNull();
239 expect(
240 resolveInlineCodeFileLinkMeta("conf.d/nginx.conf", "/Users/julius/project"),
241 ).not.toBeNull();
242 });
243​
244 it("prefers file over country-code host when a line suffix is present", () => {
245 expect(resolveInlineCodeFileLinkMeta("script.pl:10", "/Users/julius/project")).toMatchObject({
246 targetPath: "/Users/julius/project/script.pl:10",
247 line: 10,
248 });
249 expect(resolveInlineCodeFileLinkMeta("model.pt:3", "/Users/julius/project")).not.toBeNull();
250 expect(
251 resolveInlineCodeFileLinkMeta("example.pl/index.html", "/Users/julius/project"),
252 ).toBeNull();
253 expect(resolveInlineCodeFileLinkMeta("example.com:8080", "/Users/julius/project")).toBeNull();
254 });
255​
256 it("ignores commands, flags, and expressions", () => {
257 expect(resolveInlineCodeFileLinkMeta("git worktree list --porcelain")).toBeNull();
258 expect(resolveInlineCodeFileLinkMeta("node.meta", "/Users/julius/project")).toBeNull();
259 expect(resolveInlineCodeFileLinkMeta("pnpm install", "/Users/julius/project")).toBeNull();
260 expect(resolveInlineCodeFileLinkMeta("src/**/*.ts", "/Users/julius/project")).toBeNull();
261 });
262​
263 it("ignores extension-less relative segments like git refs and directories", () => {
264 expect(resolveInlineCodeFileLinkMeta("origin/main", "/Users/julius/project")).toBeNull();
265 expect(resolveInlineCodeFileLinkMeta("apps/web", "/Users/julius/project")).toBeNull();
266 });
267​
268 it("ignores external urls", () => {
269 expect(resolveInlineCodeFileLinkMeta("https://example.com/docs.html")).toBeNull();
270 });
271​
272 it("ignores relative paths without a cwd to resolve against", () => {
273 expect(resolveInlineCodeFileLinkMeta(".plans/worktree-management-v1.md")).toBeNull();
274 });
275});
276​
277describe("directory paths with a trailing separator", () => {
278 it("keeps the final segment for a POSIX directory path", () => {
279 expect(resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project")).toMatchObject({
280 basename: "favicons",
281 });
282 });
283​
284 it("keeps the final segment for a Windows directory path", () => {
285 expect(
286 resolveMarkdownFileLinkMeta("C:\\Users\\kelchm\\.claude\\", "/repo/project"),
287 ).toMatchObject({ basename: ".claude" });
288 });
289​
290 it("matches the label of the same path without a trailing separator", () => {
291 const withSlash = resolveMarkdownFileLinkMeta("/tmp/favicons/", "/repo/project");
292 const withoutSlash = resolveMarkdownFileLinkMeta("/tmp/favicons", "/repo/project");
293 expect(withSlash?.basename).toBe(withoutSlash?.basename);
294 });
295​
296 it("does not produce an empty label for the filesystem root", () => {
297 const meta = resolveMarkdownFileLinkMeta("/tmp/", "/repo/project");
298 expect(meta?.basename).not.toBe("");
299 });
300});