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

fileLinks.test.ts

178 lines6.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 ])("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​
83describe("parseFileUrlHref", () => {
84 it.each([
85 [
86 "file:///D:/Programme/t3code/OpenInPicker.tsx#L69",
87 "D:/Programme/t3code/OpenInPicker.tsx",
88 "#L69",
89 ],
90 ["file://server/share/workspace-image.svg", "\\\\server\\share\\workspace-image.svg", ""],
91 ["file://localhost/home/me/notes.md", "/home/me/notes.md", ""],
92 ])("parses %s", (href, path, hash) => {
93 expect(parseFileUrlHref(href)).toEqual({ path, hash });
94 });
95​
96 it("keeps percent-encoding so the caller decodes once", () => {
97 expect(parseFileUrlHref("file:///Users/julius/project/file%2520name.md")?.path).toBe(
98 "/Users/julius/project/file%2520name.md",
99 );
100 expect(parseFileUrlHref("file:///c%3A/Users/x/shot.png")?.path).toBe("/c%3A/Users/x/shot.png");
101 });
102​
103 it.each(["https://example.com/a.ts", "file://%", "/Users/julius/a.ts"])("rejects %s", (href) => {
104 expect(parseFileUrlHref(href)).toBeNull();
105 });
106​
107 it("rejects one-letter schemes that Chromium on Windows reads as drive letters", () => {
108 const NativeURL = URL;
109 using _url = vi.spyOn(globalThis, "URL").mockImplementation(function (input: string | URL) {
110 const drive = /^([a-z]):/i.exec(String(input));
111 return drive
112 ? new NativeURL(`file:///${drive[1]!.toUpperCase()}:/${String(input).slice(2)}`)
113 : new NativeURL(input);
114 });
115 expect(parseFileUrlHref("d:x")).toBeNull();
116 });
117});
118​
119describe("splitFilePathPosition", () => {
120 it.each([
121 ["src/main.ts:12:5", "", { path: "src/main.ts", line: 12, column: 5 }],
122 ["src/main.ts", "#L18C2", { path: "src/main.ts", line: 18, column: 2 }],
123 ["src/main.ts:3", "#L18C2", { path: "src/main.ts", line: 3 }],
124 ["src/main.ts:0", "", { path: "src/main.ts" }],
125 ["src/main.ts:42-64", "", { path: "src/main.ts", line: 42, endLine: 64 }],
126 ["src/main.ts", "#L42-L64", { path: "src/main.ts", line: 42, endLine: 64 }],
127 ["src/main.ts:42-42", "", { path: "src/main.ts", line: 42 }],
128 ])("splits %s%s", (path, hash, expected) => {
129 expect(splitFilePathPosition(path, hash)).toEqual(expected);
130 });
131});
132​
133describe("resolvePathLinkTarget", () => {
134 // regression: #21199 Windows terminal file links resolve against a Windows cwd
135 it.each([
136 [
137 String.raw`src\index.ts:12:7`,
138 String.raw`C:\Users\Jane Doe\project`,
139 String.raw`C:\Users\Jane Doe\project\src\index.ts:12:7`,
140 ],
141 [
142 String.raw`.\src\index.ts:12`,
143 String.raw`\\server\share\project`,
144 String.raw`\\server\share\project\.\src\index.ts:12`,
145 ],
146 ["src/index.ts:12", "/home/jane/project", "/home/jane/project/src/index.ts:12"],
147 ])("resolves %s using the connected environment cwd %s", (path, cwd, expected) => {
148 expect(resolvePathLinkTarget(path, cwd)).toBe(expected);
149 });
150​
151 it("keeps Windows absolute paths with forward slashes unchanged", () => {
152 expect(
153 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
154 ).toBe("C:/Users/julius/project/src/main.ts:12");
155 });
156​
157 // regression: #19627 Windows home paths match case-insensitively
158 it.each([
159 ["C:\\users\\alice\\project", "C:\\users\\alice\\notes.md:3"],
160 ["C:/uSeRs/alice/project", "C:/uSeRs/alice\\notes.md:3"],
161 ])("resolves home paths against the Windows cwd %s", (cwd, expected) => {
162 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe(expected);
163 });
164 it("keeps home paths unchanged when no supported home can be inferred", () => {
165 for (const cwd of ["C:/work/project", "/Home/alice/project", "/users/alice/project"]) {
166 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe("~/notes.md:3");
167 }
168 });
169});
170​
171describe("fileLinkLabel", () => {
172 it.each([
173 [{ path: "/repo/src/http.ts", line: 42, column: 7 }, "http.ts · L42:C7"],
174 [{ path: "/repo/src/http.ts", line: 42, endLine: 64 }, "http.ts · L42–64"],
175 ])("labels %o", (file, label) => {
176 expect(fileLinkLabel(file, new Map())).toBe(label);
177 });
178});