Open History to see the last commit
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} from "./path.ts";
13
14describe("path helpers", () => {
15 it("detects windows drive paths", () => {
16 expect(isWindowsDrivePath("C:\\repo")).toBe(true);
17 expect(isWindowsDrivePath("D:/repo")).toBe(true);
18 expect(isWindowsDrivePath("/repo")).toBe(false);
19 });
20
21 it("detects UNC paths", () => {
22 expect(isUncPath("\\\\server\\share\\repo")).toBe(true);
23 expect(isUncPath("C:\\repo")).toBe(false);
24 });
25
26 it("detects windows absolute paths", () => {
27 expect(isWindowsAbsolutePath("C:\\repo")).toBe(true);
28 expect(isWindowsAbsolutePath("\\\\server\\share\\repo")).toBe(true);
29 expect(isWindowsAbsolutePath("./repo")).toBe(false);
30 });
31
32 it("detects explicit relative paths", () => {
33 expect(isExplicitRelativePath(".")).toBe(true);
34 expect(isExplicitRelativePath("..")).toBe(true);
35 expect(isExplicitRelativePath("./repo")).toBe(true);
36 expect(isExplicitRelativePath("..\\repo")).toBe(true);
37 expect(isExplicitRelativePath("~/repo")).toBe(false);
38 });
39
40 it("normalizes a bare Windows drive root the same as one with a trailing separator", () => {
41 // `C:`, `C:\` and `C:/` all refer to the drive root and must compare equal.
42 expect(normalizeProjectPathForDispatch("C:")).toBe("C:\\");
43 expect(normalizeProjectPathForComparison("C:")).toBe("c:\\");
44 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:\\"));
45 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:/"));
46 // Non-root drive paths keep their trailing separator trimmed as before.
47 expect(normalizeProjectPathForDispatch("C:\\repo\\")).toBe("C:\\repo");
48 });
49
50 it("names a new project's folder from any typed name", () => {
51 expect(newProjectFolderName("Pinball Stats")).toBe("pinball-stats");
52 expect(newProjectFolderName(" Café & Crème! ")).toBe("cafe-creme");
53 expect(newProjectFolderName("../../etc")).toBe("etc");
54 // Nothing usable left, so the server falls back to a fixed name.
55 expect(newProjectFolderName("🎱🎱")).toBe("project");
56 expect(newProjectFolderName(`${"a".repeat(63)} b`)).toBe("a".repeat(63));
57 // Windows cannot make folders with device names.
58 expect(newProjectFolderName("Con")).toBe("con-project");
59 expect(newProjectFolderName("LPT1")).toBe("lpt1-project");
60 expect(newProjectFolderName("console")).toBe("console");
61 });
62});
63
64describe("fileBasename", () => {
65 it.each([
66 ["/tmp/favicons/", "favicons"],
67 ["C:\\Users\\kelchm\\.claude\\", ".claude"],
68 ["/tmp/", "tmp"],
69 ["AGENTS.md", "AGENTS.md"],
70 ["/", "/"],
71 ])("labels %s as %s", (path, basename) => {
72 expect(fileBasename(path)).toBe(basename);
73 });
74});
75
76describe("workspaceRelativeFilePath", () => {
77 it.each([
78 ["/repo/project/src/main.ts", "/repo/project", "src/main.ts"],
79 ["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],
80 ["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
81 ["/C:/Users/mike/t3code/apps/web/a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
82 ["/Repo/Project/src/main.ts", "/repo/project", null],
83 ["/tmp/case/project/probe.txt", "/tmp/case/Project", null],
84 ["//tmp/case/project/probe.txt", "//tmp/case/Project", null],
85 ["/tmp/case/Project/probe.txt", "/tmp/case/Project", "probe.txt"],
86 ["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
87 ["/C:/USERS/mike/t3code/main.ts", "/c:/users/MIKE/t3code", "main.ts"],
88 ["\\\\server\\share\\PROJECT\\main.ts", "\\\\Server\\Share\\Project", "main.ts"],
89 ["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
90 ["C:/Users/MIKE/main.ts", "c:/", "Users/MIKE/main.ts"],
91 ["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
92 ["/tmp/repo/file.ts ", "/tmp/repo", "file.ts "],
93 ["/tmp/report.ts", "/repo/project", null],
94 ["/repo/project-two/a.ts", "/repo/project", null],
95 ["/repo/project/a.ts", undefined, null],
96 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
97 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
98 });
99});