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

fileLinks.test.ts

249 lines9.5 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import {
3 buildFileLinkParentSuffixByPath,
4 fileLinkLabel,
5 parseFileUrlHref,
6 splitFilePathPosition,
7 resolvePathLinkTarget,
8} from "./fileLinks.ts";
9​
10describe("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 { paths: [String.raw`/C:/repo/src\index.ts`, "/C:/repo/src/index.ts"], suffixes: [] },
68 {
69 paths: [String.raw`//one/share/src\index.ts`, String.raw`\\one\share\src\index.ts`],
70 suffixes: [],
71 },
72 {
73 paths: [String.raw`/repo/D:\notes/report.md`, "/repo/D:/notes/report.md"],
74 suffixes: [
75 [String.raw`/repo/D:\notes/report.md`, String.raw`repo/D:\notes`],
76 ["/repo/D:/notes/report.md", "D:/notes"],
77 ],
78 },
79 { paths: [String.raw`/repo/a/build\log.txt`, "/repo/b/log.txt"], suffixes: [] },
80 {
81 paths: ["/repo/draft/Café.md", "/repo/published/Cafe\u0301.md"],
82 suffixes: [
83 ["/repo/draft/Café.md", "repo/draft"],
84 ["/repo/published/Cafe\u0301.md", "repo/published"],
85 ],
86 },
87 {
88 paths: ["/repo/one/Caf\u00e9/notes/Caf\u00e9.md", "/repo/two/Cafe\u0301/notes/Cafe\u0301.md"],
89 suffixes: [
90 ["/repo/one/Caf\u00e9/notes/Caf\u00e9.md", "one/Caf\u00e9/notes"],
91 ["/repo/two/Cafe\u0301/notes/Cafe\u0301.md", "two/Caf\u00e9/notes"],
92 ],
93 },
94 ])("preserves suffixes for $paths", ({ paths, suffixes }) => {
95 expect([...buildFileLinkParentSuffixByPath(paths)]).toEqual(suffixes);
96 });
97​
98 it("bounds suffix construction by path count for a same-basename group", () => {
99 const paths = Array.from({ length: 128 }, (_, i) => `/repo/packages/pkg-${i}/src/index.ts`);
100 using joins = vi.spyOn(Array.prototype, "join");
101 const result = buildFileLinkParentSuffixByPath(paths);
102 const suffixConstructions = joins.mock.calls.length;
103​
104 expect(result).toEqual(new Map(paths.map((path, i) => [path, `pkg-${i}/src`])));
105 // Count work instead of elapsed time so a pairwise scan fails on every machine.
106 expect(suffixConstructions).toBeLessThan(paths.length * 20);
107 });
108});
109​
110describe("parseFileUrlHref", () => {
111 it.each([
112 [
113 "file:///D:/Programme/t3code/OpenInPicker.tsx#L69",
114 "D:/Programme/t3code/OpenInPicker.tsx",
115 "#L69",
116 ],
117 [
118 "file://server/share/workspace-image.svg",
119 "\\\\server\\share\\workspace-image.svg",
120 "",
121 "box-node",
122 ],
123 ["file://localhost/home/me/notes.md", "/home/me/notes.md", ""],
124 // GNU `ls --hyperlink` names the machine's own hostname.
125 ["file://box-node/var/tmp/report.txt", "/var/tmp/report.txt", "", "Box-Node"],
126 ["file://1234/var/tmp/report.txt", "/var/tmp/report.txt", "", "1234"],
127 ["file://böx/var/tmp/report.txt", "/var/tmp/report.txt", "", "böx"],
128 ["file://server/share/report.txt", "\\\\server\\share\\report.txt", "", "09"],
129 ["file://a/share/report.txt", "\\\\a\\share\\report.txt", "", "a#b"],
130 ])("parses %s", (href, path, hash, localHostname?: string) => {
131 expect(parseFileUrlHref(href, localHostname)).toEqual({ path, hash });
132 });
133​
134 it("keeps percent-encoding so the caller decodes once", () => {
135 expect(parseFileUrlHref("file:///Users/julius/project/file%2520name.md")?.path).toBe(
136 "/Users/julius/project/file%2520name.md",
137 );
138 expect(parseFileUrlHref("file:///c%3A/Users/x/shot.png")?.path).toBe("/c%3A/Users/x/shot.png");
139 });
140​
141 it.each(["https://example.com/a.ts", "file://%", "/Users/julius/a.ts"])("rejects %s", (href) => {
142 expect(parseFileUrlHref(href)).toBeNull();
143 });
144​
145 it("rejects one-letter schemes that Chromium on Windows reads as drive letters", () => {
146 const NativeURL = URL;
147 using _url = vi.spyOn(globalThis, "URL").mockImplementation(function (input: string | URL) {
148 const drive = /^([a-z]):/i.exec(String(input));
149 return drive
150 ? new NativeURL(`file:///${drive[1]!.toUpperCase()}:/${String(input).slice(2)}`)
151 : new NativeURL(input);
152 });
153 expect(parseFileUrlHref("d:x")).toBeNull();
154 });
155});
156​
157describe("splitFilePathPosition", () => {
158 it.each([
159 ["src/main.ts:12:5", "", { path: "src/main.ts", line: 12, column: 5 }],
160 ["src/main.ts", "#L18C2", { path: "src/main.ts", line: 18, column: 2 }],
161 ["src/main.ts:3", "#L18C2", { path: "src/main.ts", line: 3 }],
162 ["src/main.ts:0", "", { path: "src/main.ts" }],
163 ["src/main.ts:42-64", "", { path: "src/main.ts", line: 42, endLine: 64 }],
164 ["src/main.ts", "#L42-L64", { path: "src/main.ts", line: 42, endLine: 64 }],
165 ["src/main.ts:42-42", "", { path: "src/main.ts", line: 42 }],
166 ])("splits %s%s", (path, hash, expected) => {
167 expect(splitFilePathPosition(path, hash)).toEqual(expected);
168 });
169});
170​
171describe("resolvePathLinkTarget", () => {
172 // regression: #21199 Windows terminal file links resolve against a Windows cwd
173 it.each([
174 [
175 String.raw`src\index.ts:12:7`,
176 String.raw`C:\Users\Jane Doe\project`,
177 String.raw`C:\Users\Jane Doe\project\src\index.ts:12:7`,
178 ],
179 [
180 String.raw`.\src\index.ts:12`,
181 String.raw`\\server\share\project`,
182 String.raw`\\server\share\project\.\src\index.ts:12`,
183 ],
184 ["src/index.ts:12", "/home/jane/project", "/home/jane/project/src/index.ts:12"],
185 ["./reports/draft.md:2", "/home/jane/project", "/home/jane/project/reports/draft.md:2"],
186 ["./note.txt", "/tmp/photos\\", "/tmp/photos\\/note.txt"],
187 ["./note.txt", "/tmp/C:\\files", "/tmp/C:\\files/note.txt"],
188 ["~\\note.txt", "/tmp/C:\\files", "/tmp/C:\\files/~\\note.txt"],
189 ])("resolves %s using the connected environment cwd %s", (path, cwd, expected) => {
190 expect(resolvePathLinkTarget(path, cwd)).toBe(expected);
191 });
192​
193 it("keeps Windows absolute paths with forward slashes unchanged", () => {
194 expect(
195 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
196 ).toBe("C:/Users/julius/project/src/main.ts:12");
197 });
198​
199 // regression: #19627 Windows home paths match case-insensitively
200 it.each([
201 ["C:\\users\\alice\\project", "C:\\users\\alice\\notes.md:3"],
202 ["C:/uSeRs/alice/project", "C:/uSeRs/alice\\notes.md:3"],
203 ])("resolves home paths against the Windows cwd %s", (cwd, expected) => {
204 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe(expected);
205 });
206 it("keeps home paths unchanged when no supported home can be inferred", () => {
207 for (const cwd of ["C:/work/project", "/Home/alice/project", "/users/alice/project"]) {
208 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe("~/notes.md:3");
209 }
210 });
211});
212​
213describe("fileLinkLabel", () => {
214 it.each([
215 [{ path: "/repo/src/http.ts", line: 42, column: 7 }, "http.ts · L42:C7"],
216 [{ path: "/repo/src/http.ts", line: 42, endLine: 64 }, "http.ts · L42–64"],
217 // macOS file names can be NFD (decomposed Hangul jamo); the label shows composed syllables.
218 [{ path: "/repo/\u1109\u1175\u110b\u1161\u11ab" }, "\uc2dc\uc548"],
219 ])("labels %o", (file, label) => {
220 expect(fileLinkLabel(file, new Map())).toBe(label);
221 });
222​
223 it("composes an NFD parent-folder suffix", () => {
224 const path = "/repo/\u1109\u1175\u110b\u1161\u11ab/a.txt";
225 expect(
226 fileLinkLabel({ path }, buildFileLinkParentSuffixByPath([path, "/repo/other/a.txt"])),
227 ).toBe("a.txt · repo/\uc2dc\uc548");
228 });
229​
230 it("disambiguates POSIX parent folders with literal backslashes", () => {
231 const backslashParent = String.raw`/repo/D:\notes/report.md`;
232 const nestedParent = "/repo/D:/notes/report.md";
233 const suffixes = buildFileLinkParentSuffixByPath([backslashParent, nestedParent]);
234​
235 expect(fileLinkLabel({ path: backslashParent }, suffixes)).toBe(
236 String.raw`report.md · repo/D:\notes`,
237 );
238 expect(fileLinkLabel({ path: nestedParent }, suffixes)).toBe("report.md · D:/notes");
239 expect(
240 fileLinkLabel(
241 { path: String.raw`C:\repo\src\index.ts` },
242 buildFileLinkParentSuffixByPath([
243 String.raw`C:\repo\src\index.ts`,
244 String.raw`D:\repo\src\index.ts`,
245 ]),
246 ),
247 ).toBe("index.ts · C:/repo/src");
248 });
249});