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

path.test.ts

60 lines2.6 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 isExplicitRelativePath,
4 isUncPath,
5 isWindowsAbsolutePath,
6 isWindowsDrivePath,
7 newProjectFolderName,
8 normalizeProjectPathForComparison,
9 normalizeProjectPathForDispatch,
10} from "./path.ts";
11​
12describe("path helpers", () => {
13 it("detects windows drive paths", () => {
14 expect(isWindowsDrivePath("C:\\repo")).toBe(true);
15 expect(isWindowsDrivePath("D:/repo")).toBe(true);
16 expect(isWindowsDrivePath("/repo")).toBe(false);
17 });
18​
19 it("detects UNC paths", () => {
20 expect(isUncPath("\\\\server\\share\\repo")).toBe(true);
21 expect(isUncPath("C:\\repo")).toBe(false);
22 });
23​
24 it("detects windows absolute paths", () => {
25 expect(isWindowsAbsolutePath("C:\\repo")).toBe(true);
26 expect(isWindowsAbsolutePath("\\\\server\\share\\repo")).toBe(true);
27 expect(isWindowsAbsolutePath("./repo")).toBe(false);
28 });
29​
30 it("detects explicit relative paths", () => {
31 expect(isExplicitRelativePath(".")).toBe(true);
32 expect(isExplicitRelativePath("..")).toBe(true);
33 expect(isExplicitRelativePath("./repo")).toBe(true);
34 expect(isExplicitRelativePath("..\\repo")).toBe(true);
35 expect(isExplicitRelativePath("~/repo")).toBe(false);
36 });
37​
38 it("normalizes a bare Windows drive root the same as one with a trailing separator", () => {
39 // `C:`, `C:\` and `C:/` all refer to the drive root and must compare equal.
40 expect(normalizeProjectPathForDispatch("C:")).toBe("C:\\");
41 expect(normalizeProjectPathForComparison("C:")).toBe("c:\\");
42 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:\\"));
43 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:/"));
44 // Non-root drive paths keep their trailing separator trimmed as before.
45 expect(normalizeProjectPathForDispatch("C:\\repo\\")).toBe("C:\\repo");
46 });
47​
48 it("names a new project's folder from any typed name", () => {
49 expect(newProjectFolderName("Pinball Stats")).toBe("pinball-stats");
50 expect(newProjectFolderName(" Café & Crème! ")).toBe("cafe-creme");
51 expect(newProjectFolderName("../../etc")).toBe("etc");
52 // Nothing usable left, so the server falls back to a fixed name.
53 expect(newProjectFolderName("🎱🎱")).toBe("project");
54 expect(newProjectFolderName(`${"a".repeat(63)} b`)).toBe("a".repeat(63));
55 // Windows cannot make folders with device names.
56 expect(newProjectFolderName("Con")).toBe("con-project");
57 expect(newProjectFolderName("LPT1")).toBe("lpt1-project");
58 expect(newProjectFolderName("console")).toBe("console");
59 });
60});