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

fileLinks.test.ts

76 lines3.0 KB

1import { describe, expect, it } from "vite-plus/test";
2import { parseFileUrlHref, splitFilePathPosition, resolvePathLinkTarget } from "./fileLinks.ts";
3​
4describe("parseFileUrlHref", () => {
5 it.each([
6 [
7 "file:///D:/Programme/t3code/OpenInPicker.tsx#L69",
8 "D:/Programme/t3code/OpenInPicker.tsx",
9 "#L69",
10 ],
11 ["file://server/share/workspace-image.svg", "\\\\server\\share\\workspace-image.svg", ""],
12 ["file://localhost/home/me/notes.md", "/home/me/notes.md", ""],
13 ])("parses %s", (href, path, hash) => {
14 expect(parseFileUrlHref(href)).toEqual({ path, hash });
15 });
16​
17 it("keeps percent-encoding so the caller decodes once", () => {
18 expect(parseFileUrlHref("file:///Users/julius/project/file%2520name.md")?.path).toBe(
19 "/Users/julius/project/file%2520name.md",
20 );
21 expect(parseFileUrlHref("file:///c%3A/Users/x/shot.png")?.path).toBe("/c%3A/Users/x/shot.png");
22 });
23​
24 it.each(["https://example.com/a.ts", "file://%", "/Users/julius/a.ts"])("rejects %s", (href) => {
25 expect(parseFileUrlHref(href)).toBeNull();
26 });
27});
28​
29describe("splitFilePathPosition", () => {
30 it.each([
31 ["src/main.ts:12:5", "", { path: "src/main.ts", line: 12, column: 5 }],
32 ["src/main.ts", "#L18C2", { path: "src/main.ts", line: 18, column: 2 }],
33 ["src/main.ts:3", "#L18C2", { path: "src/main.ts", line: 3 }],
34 ["src/main.ts:0", "", { path: "src/main.ts" }],
35 ])("splits %s%s", (path, hash, expected) => {
36 expect(splitFilePathPosition(path, hash)).toEqual(expected);
37 });
38});
39​
40describe("resolvePathLinkTarget", () => {
41 // regression: #21199 Windows terminal file links resolve against a Windows cwd
42 it.each([
43 [
44 String.raw`src\index.ts:12:7`,
45 String.raw`C:\Users\Jane Doe\project`,
46 String.raw`C:\Users\Jane Doe\project\src\index.ts:12:7`,
47 ],
48 [
49 String.raw`.\src\index.ts:12`,
50 String.raw`\\server\share\project`,
51 String.raw`\\server\share\project\.\src\index.ts:12`,
52 ],
53 ["src/index.ts:12", "/home/jane/project", "/home/jane/project/src/index.ts:12"],
54 ])("resolves %s using the connected environment cwd %s", (path, cwd, expected) => {
55 expect(resolvePathLinkTarget(path, cwd)).toBe(expected);
56 });
57​
58 it("keeps Windows absolute paths with forward slashes unchanged", () => {
59 expect(
60 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
61 ).toBe("C:/Users/julius/project/src/main.ts:12");
62 });
63​
64 // regression: #19627 Windows home paths match case-insensitively
65 it.each([
66 ["C:\\users\\alice\\project", "C:\\users\\alice\\notes.md:3"],
67 ["C:/uSeRs/alice/project", "C:/uSeRs/alice\\notes.md:3"],
68 ])("resolves home paths against the Windows cwd %s", (cwd, expected) => {
69 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe(expected);
70 });
71 it("keeps home paths unchanged when no supported home can be inferred", () => {
72 for (const cwd of ["C:/work/project", "/Home/alice/project", "/users/alice/project"]) {
73 expect(resolvePathLinkTarget("~/notes.md:3", cwd)).toBe("~/notes.md:3");
74 }
75 });
76});