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

markdownLinks.test.ts

170 lines6.9 KB

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