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

markdownLinks.test.ts

180 lines7.3 KB

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