@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 190 lines 7.11 KB
Raw
1 import { describe, expect, it, vi } from "vite-plus/test";
2 import {
3 buildFileLinkParentSuffixByPath,
4 fileLinkLabel,
5 parseFileUrlHref,
6 splitFilePathPosition,
7 resolvePathLinkTarget,
8 } from "./fileLinks.ts";
9
10 describe("buildFileLinkParentSuffixByPath", () => {
11 it.each([
12 {
13 paths: ["", "/", "index.ts", "/repo/src/index.ts"],
14 suffixes: [["/repo/src/index.ts", "repo/src"]],
15 },
16 { paths: ["/repo/a.ts", "/repo/b.ts", "/repo/a.ts"], suffixes: [] },
17 {
18 paths: ["/repo/src/index.ts", "/repo/test/index.ts"],
19 suffixes: [
20 ["/repo/src/index.ts", "repo/src"],
21 ["/repo/test/index.ts", "repo/test"],
22 ],
23 },
24 {
25 paths: ["/repo/a/src/index.ts", "/repo/b/src/index.ts"],
26 suffixes: [
27 ["/repo/a/src/index.ts", "a/src"],
28 ["/repo/b/src/index.ts", "b/src"],
29 ],
30 },
31 {
32 paths: ["/a/pkg/src/index.ts", "/b/pkg/src/index.ts"],
33 suffixes: [
34 ["/a/pkg/src/index.ts", "a/pkg/src"],
35 ["/b/pkg/src/index.ts", "b/pkg/src"],
36 ],
37 },
38 {
39 paths: ["src/index.ts", "/repo/src/index.ts"],
40 suffixes: [
41 ["src/index.ts", "src"],
42 ["/repo/src/index.ts", "repo/src"],
43 ],
44 },
45 {
46 paths: ["/repo//src/index.ts", "/repo/src/index.ts"],
47 suffixes: [
48 ["/repo//src/index.ts", "repo/src"],
49 ["/repo/src/index.ts", "repo/src"],
50 ],
51 },
52 { paths: [String.raw`C:\repo\src\index.ts`, "C:/repo/src/index.ts"], suffixes: [] },
53 {
54 paths: [String.raw`C:\repo\src\index.ts`, String.raw`D:\repo\src\index.ts`],
55 suffixes: [
56 ["C:/repo/src/index.ts", "C:/repo/src"],
57 ["D:/repo/src/index.ts", "D:/repo/src"],
58 ],
59 },
60 {
61 paths: [String.raw`\\one\share\src\index.ts`, String.raw`\\two\share\src\index.ts`],
62 suffixes: [
63 ["//one/share/src/index.ts", "one/share/src"],
64 ["//two/share/src/index.ts", "two/share/src"],
65 ],
66 },
67 ])("preserves suffixes for $paths", ({ paths, suffixes }) => {
68 expect([...buildFileLinkParentSuffixByPath(paths)]).toEqual(suffixes);
69 });
70
71 it("bounds suffix construction by path count for a same-basename group", () => {
72 const paths = Array.from({ length: 128 }, (_, i) => `/repo/packages/pkg-${i}/src/index.ts`);
73 using joins = vi.spyOn(Array.prototype, "join");
74 const result = buildFileLinkParentSuffixByPath(paths);
75 const suffixConstructions = joins.mock.calls.length;
76
77 expect(result).toEqual(new Map(paths.map((path, i) => [path, `pkg-${i}/src`])));
78 // Count work instead of elapsed time so a pairwise scan fails on every machine.
79 expect(suffixConstructions).toBeLessThan(paths.length * 20);
80 });
81 });
82
83 describe("parseFileUrlHref", () => {
84 it.each([
85 [
86 "file:///D:/Programme/t3code/OpenInPicker.tsx#L69",
87 "D:/Programme/t3code/OpenInPicker.tsx",
88 "#L69",
89 ],
90 [
91 "file://server/share/workspace-image.svg",
92 "\\\\server\\share\\workspace-image.svg",
93 "",
94 "box-node",
95 ],
96 ["file://localhost/home/me/notes.md", "/home/me/notes.md", ""],
97 // GNU `ls --hyperlink` names the machine's own hostname.
98 ["file://box-node/var/tmp/report.txt", "/var/tmp/report.txt", "", "Box-Node"],
99 ["file://1234/var/tmp/report.txt", "/var/tmp/report.txt", "", "1234"],
100 ["file://böx/var/tmp/report.txt", "/var/tmp/report.txt", "", "böx"],
101 ["file://server/share/report.txt", "\\\\server\\share\\report.txt", "", "09"],
102 ["file://a/share/report.txt", "\\\\a\\share\\report.txt", "", "a#b"],
103 ])("parses %s", (href, path, hash, localHostname?: string) => {
104 expect(parseFileUrlHref(href, localHostname)).toEqual({ path, hash });
105 });
106
107 it("keeps percent-encoding so the caller decodes once", () => {
108 expect(parseFileUrlHref("file:///Users/julius/project/file%2520name.md")?.path).toBe(
109 "/Users/julius/project/file%2520name.md",
110 );
111 expect(parseFileUrlHref("file:///c%3A/Users/x/shot.png")?.path).toBe("/c%3A/Users/x/shot.png");
112 });
113
114 it.each(["https://example.com/a.ts", "file://%", "/Users/julius/a.ts"])("rejects %s", (href) => {
115 expect(parseFileUrlHref(href)).toBeNull();
116 });
117
118 it("rejects one-letter schemes that Chromium on Windows reads as drive letters", () => {
119 const NativeURL = URL;
120 using _url = vi.spyOn(globalThis, "URL").mockImplementation(function (input: string | URL) {
121 const drive = /^([a-z]):/i.exec(String(input));
122 return drive
123 ? new NativeURL(`file:///${drive[1]!.toUpperCase()}:/${String(input).slice(2)}`)
124 : new NativeURL(input);
125 });
126 expect(parseFileUrlHref("d:x")).toBeNull();
127 });
128 });
129
130 describe("splitFilePathPosition", () => {
131 it.each([
132 ["src/main.ts:12:5", "", { path: "src/main.ts", line: 12, column: 5 }],
133 ["src/main.ts", "#L18C2", { path: "src/main.ts", line: 18, column: 2 }],
134 ["src/main.ts:3", "#L18C2", { path: "src/main.ts", line: 3 }],
135 ["src/main.ts:0", "", { path: "src/main.ts" }],
136 ["src/main.ts:42-64", "", { path: "src/main.ts", line: 42, endLine: 64 }],
137 ["src/main.ts", "#L42-L64", { path: "src/main.ts", line: 42, endLine: 64 }],
138 ["src/main.ts:42-42", "", { path: "src/main.ts", line: 42 }],
139 ])("splits %s%s", (path, hash, expected) => {
140 expect(splitFilePathPosition(path, hash)).toEqual(expected);
141 });
142 });
143
144 describe("resolvePathLinkTarget", () => {
145 // regression: #21199 Windows terminal file links resolve against a Windows cwd
146 it.each([
147 [
148 String.raw`src\index.ts:12:7`,
149 String.raw`C:\Users\Jane Doe\project`,
150 String.raw`C:\Users\Jane Doe\project\src\index.ts:12:7`,
151 ],
152 [
153 String.raw`.\src\index.ts:12`,
154 String.raw`\\server\share\project`,
155 String.raw`\\server\share\project\.\src\index.ts:12`,
156 ],
157 ["src/index.ts:12", "/home/jane/project", "/home/jane/project/src/index.ts:12"],
158 ["./reports/draft.md:2", "/home/jane/project", "/home/jane/project/reports/draft.md:2"],
159 ])("resolves %s using the connected environment cwd %s", (path, cwd, expected) => {
160 expect(resolvePathLinkTarget(path, cwd)).toBe(expected);
161 });
162
163 it("keeps Windows absolute paths with forward slashes unchanged", () => {
164 expect(
165 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
166 ).toBe("C:/Users/julius/project/src/main.ts:12");
167 });
168
169 // regression: #19627 Windows home paths match case-insensitively
170 it.each([
171 ["C:\\users\\alice\\project", "C:\\users\\alice\\notes.md:3"],
172 ["C:/uSeRs/alice/project", "C:/uSeRs/alice\\notes.md:3"],
173 ])("resolves home paths against the Windows cwd %s", (cwd, expected) => {
174 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe(expected);
175 });
176 it("keeps home paths unchanged when no supported home can be inferred", () => {
177 for (const cwd of ["C:/work/project", "/Home/alice/project", "/users/alice/project"]) {
178 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe("~/notes.md:3");
179 }
180 });
181 });
182
183 describe("fileLinkLabel", () => {
184 it.each([
185 [{ path: "/repo/src/http.ts", line: 42, column: 7 }, "http.ts · L42:C7"],
186 [{ path: "/repo/src/http.ts", line: 42, endLine: 64 }, "http.ts · L42–64"],
187 ])("labels %o", (file, label) => {
188 expect(fileLinkLabel(file, new Map())).toBe(label);
189 });
190 });