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

path.test.ts

108 lines4.6 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 fileBasename,
4 workspaceRelativeFilePath,
5 isExplicitRelativePath,
6 isUncPath,
7 isWindowsAbsolutePath,
8 isWindowsDrivePath,
9 newProjectFolderName,
10 normalizeProjectPathForComparison,
11 normalizeProjectPathForDispatch,
12 resolveWorkspaceFilePath,
13} from "./path.ts";
14​
15describe("path helpers", () => {
16 it.each([
17 ["report:123", "/workspace", "/workspace/report:123"],
18 ["/workspace/report:12:3", "/other", "/workspace/report:12:3"],
19 ["src/file", "C:\\Workspace\\", "C:\\Workspace\\src\\file"],
20 ["C:/Workspace/file", "/other", "C:/Workspace/file"],
21 ["file", "\\\\host\\share\\", "\\\\host\\share\\file"],
22 ["file", "/", "/file"],
23 ["~/report:123", "/workspace", "~/report:123"],
24 ])("resolves literal path %s within %s", (path, root, expected) => {
25 expect(resolveWorkspaceFilePath(path, root)).toBe(expected);
26 });
27​
28 it("detects windows drive paths", () => {
29 expect(isWindowsDrivePath("C:\\repo")).toBe(true);
30 expect(isWindowsDrivePath("D:/repo")).toBe(true);
31 expect(isWindowsDrivePath("/repo")).toBe(false);
32 });
33​
34 it("detects UNC paths", () => {
35 expect(isUncPath("\\\\server\\share\\repo")).toBe(true);
36 expect(isUncPath("C:\\repo")).toBe(false);
37 });
38​
39 it("detects windows absolute paths", () => {
40 expect(isWindowsAbsolutePath("C:\\repo")).toBe(true);
41 expect(isWindowsAbsolutePath("\\\\server\\share\\repo")).toBe(true);
42 expect(isWindowsAbsolutePath("./repo")).toBe(false);
43 });
44​
45 it("detects explicit relative paths", () => {
46 expect(isExplicitRelativePath(".")).toBe(true);
47 expect(isExplicitRelativePath("..")).toBe(true);
48 expect(isExplicitRelativePath("./repo")).toBe(true);
49 expect(isExplicitRelativePath("..\\repo")).toBe(true);
50 expect(isExplicitRelativePath("~/repo")).toBe(false);
51 });
52​
53 it("normalizes a bare Windows drive root the same as one with a trailing separator", () => {
54 expect(normalizeProjectPathForDispatch("C:")).toBe("C:\\");
55 expect(normalizeProjectPathForComparison("C:")).toBe("c:\\");
56 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:\\"));
57 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:/"));
58 expect(normalizeProjectPathForDispatch("C:\\repo\\")).toBe("C:\\repo");
59 });
60​
61 it("names a new project's folder from any typed name", () => {
62 expect(newProjectFolderName("Pinball Stats")).toBe("pinball-stats");
63 expect(newProjectFolderName(" Café & Crème! ")).toBe("cafe-creme");
64 expect(newProjectFolderName("../../etc")).toBe("etc");
65 expect(newProjectFolderName("🎱🎱")).toBe("project");
66 expect(newProjectFolderName(`${"a".repeat(63)} b`)).toBe("a".repeat(63));
67 expect(newProjectFolderName("Con")).toBe("con-project");
68 expect(newProjectFolderName("LPT1")).toBe("lpt1-project");
69 expect(newProjectFolderName("console")).toBe("console");
70 });
71});
72​
73describe("fileBasename", () => {
74 it.each([
75 ["/tmp/favicons/", "favicons"],
76 ["C:\\Users\\kelchm\\.claude\\", ".claude"],
77 ["/tmp/", "tmp"],
78 ["AGENTS.md", "AGENTS.md"],
79 ["/", "/"],
80 ])("labels %s as %s", (path, basename) => {
81 expect(fileBasename(path)).toBe(basename);
82 });
83});
84​
85describe("workspaceRelativeFilePath", () => {
86 it.each([
87 ["/repo/project/src/main.ts", "/repo/project", "src/main.ts"],
88 ["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],
89 ["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
90 ["/C:/Users/mike/t3code/apps/web/a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
91 ["/Repo/Project/src/main.ts", "/repo/project", null],
92 ["/tmp/case/project/probe.txt", "/tmp/case/Project", null],
93 ["//tmp/case/project/probe.txt", "//tmp/case/Project", null],
94 ["/tmp/case/Project/probe.txt", "/tmp/case/Project", "probe.txt"],
95 ["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
96 ["/C:/USERS/mike/t3code/main.ts", "/c:/users/MIKE/t3code", "main.ts"],
97 ["\\\\server\\share\\PROJECT\\main.ts", "\\\\Server\\Share\\Project", "main.ts"],
98 ["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
99 ["C:/Users/MIKE/main.ts", "c:/", "Users/MIKE/main.ts"],
100 ["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
101 ["/tmp/repo/file.ts ", "/tmp/repo", "file.ts "],
102 ["/tmp/report.ts", "/repo/project", null],
103 ["/repo/project-two/a.ts", "/repo/project", null],
104 ["/repo/project/a.ts", undefined, null],
105 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
106 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
107 });
108});