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

fileLinks.test.ts

93 lines3.6 KB

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