Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 breadcrumbPathOf,
5 fileBreadcrumbChildren,
6 fileBreadcrumbParent,
7 fileBreadcrumbs,
8 isListableHostFolder,
9 joinHostPath,
10 retainedBreadcrumbTrail,
11 trailChildOf,
12} from "./filePath";
13
14describe("fileBreadcrumbs", () => {
15 it("builds project, directory, and file crumbs", () => {
16 expect(fileBreadcrumbs("t3code", "apps/web/src/main.tsx")).toEqual([
17 { label: "t3code", path: "", kind: "project" },
18 { label: "apps", path: "apps", kind: "directory" },
19 { label: "web", path: "apps/web", kind: "directory" },
20 { label: "src", path: "apps/web/src", kind: "directory" },
21 { label: "main.tsx", path: "apps/web/src/main.tsx", kind: "file" },
22 ]);
23 });
24
25 it("normalizes repeated separators", () => {
26 expect(fileBreadcrumbs("workspace", "src//index.ts").map((crumb) => crumb.label)).toEqual([
27 "workspace",
28 "src",
29 "index.ts",
30 ]);
31 });
32
33 it("starts host paths outside the workspace at the filesystem root", () => {
34 expect(fileBreadcrumbs("t3code", "/tmp/t3-cleanup/report.md")).toEqual([
35 { label: "tmp", path: "/tmp", kind: "directory" },
36 { label: "t3-cleanup", path: "/tmp/t3-cleanup", kind: "directory" },
37 { label: "report.md", path: "/tmp/t3-cleanup/report.md", kind: "file" },
38 ]);
39 expect(fileBreadcrumbs("t3code", "C:\\Temp\\report.md")).toEqual([
40 { label: "C:", path: "C:\\", kind: "directory" },
41 { label: "Temp", path: "C:\\Temp", kind: "directory" },
42 { label: "report.md", path: "C:\\Temp\\report.md", kind: "file" },
43 ]);
44 expect(fileBreadcrumbs("t3code", "\\\\server\\share\\report.md").map((c) => c.path)).toEqual([
45 "\\\\server",
46 "\\\\server\\share",
47 "\\\\server\\share\\report.md",
48 ]);
49 });
50});
51
52describe("breadcrumbPathOf", () => {
53 it.each([
54 ["", ""],
55 ["src", "src"],
56 ["src/", "src"],
57 ["src//components", "src/components"],
58 ["/tmp/t3-demo/", "/tmp/t3-demo"],
59 ["/", "/"],
60 ["C:\\Users\\me\\", "C:\\Users\\me"],
61 ["C:\\", "C:\\"],
62 ["\\\\server\\share\\", "\\\\server\\share"],
63 ])("writes %j as its crumb names it", (path, expected) => {
64 expect(breadcrumbPathOf(path)).toBe(expected);
65 });
66});
67
68describe("fileBreadcrumbChildren", () => {
69 const entries = [
70 { path: "README.md", kind: "file" as const },
71 { path: "src", kind: "directory" as const },
72 { path: "src-old", kind: "directory" as const },
73 { path: "src/index.ts", kind: "file" as const },
74 { path: "src/lib", kind: "directory" as const },
75 { path: "src/lib/file10.ts", kind: "file" as const },
76 { path: "src/lib/file2.ts", kind: "file" as const },
77 { path: "src-old/index.ts", kind: "file" as const },
78 ];
79
80 it("returns only the immediate children of the project root", () => {
81 expect(fileBreadcrumbChildren(entries, "")).toEqual([
82 { path: "src", kind: "directory", label: "src" },
83 { path: "src-old", kind: "directory", label: "src-old" },
84 { path: "README.md", kind: "file", label: "README.md" },
85 ]);
86 });
87
88 it("honors segment boundaries and sorts folders before files", () => {
89 expect(fileBreadcrumbChildren(entries, "src")).toEqual([
90 { path: "src/lib", kind: "directory", label: "lib" },
91 { path: "src/index.ts", kind: "file", label: "index.ts" },
92 ]);
93 });
94
95 it("uses natural file-name ordering and preserves input order for equivalent names", () => {
96 const files = ["file10.ts", "File2.ts", "file02.ts", "file2.ts"].map((name) => ({
97 path: `src/lib/${name}`,
98 kind: "file" as const,
99 }));
100
101 expect(fileBreadcrumbChildren(files, "src/lib").map((entry) => entry.label)).toEqual([
102 "File2.ts",
103 "file02.ts",
104 "file2.ts",
105 "file10.ts",
106 ]);
107 });
108
109 it("returns an empty list for an empty or missing directory", () => {
110 expect(fileBreadcrumbChildren(entries, "missing")).toEqual([]);
111 });
112});
113
114describe("retainedBreadcrumbTrail", () => {
115 it.each([
116 // Walking up keeps the deeper path, all the way to the workspace root.
117 ["apps/web/src", "apps/web", "apps/web/src"],
118 ["apps/web/src", "", "apps/web/src"],
119 // Walking back down inside the trail keeps it too.
120 ["apps/web/src", "apps/web/src", "apps/web/src"],
121 // Somewhere else starts over, including a sibling that shares a name prefix.
122 ["apps/web/src", "apps/mobile", "apps/mobile"],
123 ["src/lib", "src-old", "src-old"],
124 ["apps/web", "apps/web/src/main.tsx", "apps/web/src/main.tsx"],
125 // Workspace and host paths never share a trail.
126 ["apps/web/src", "/tmp/report", "/tmp/report"],
127 ["/tmp/report", "", ""],
128 ["C:\\Temp\\a", "", ""],
129 // Host folders outside the workspace keep their trail the same way.
130 ["/Users/me/projects/scope", "/Users/me", "/Users/me/projects/scope"],
131 ["/Users/me/projects/scope", "/Users/me/proj", "/Users/me/proj"],
132 ["/Volumes/Work/a", "/Volumes/Work/a/", "/Volumes/Work/a"],
133 ["C:\\Users\\me\\scope", "C:\\", "C:\\Users\\me\\scope"],
134 ["C:\\Users\\me\\scope", "C:\\Users", "C:\\Users\\me\\scope"],
135 ["C:\\Users\\me\\scope", "D:\\", "D:\\"],
136 ["\\\\nas\\share\\docs\\notes", "\\\\nas\\share", "\\\\nas\\share\\docs\\notes"],
137 ])("from trail %j to %j keeps %j", (trail, path, expected) => {
138 expect(retainedBreadcrumbTrail(trail, path)).toBe(expected);
139 });
140
141 it("starts at the path when nothing came before", () => {
142 expect(retainedBreadcrumbTrail(undefined, "apps")).toBe("apps");
143 });
144});
145
146describe("trailChildOf", () => {
147 it.each([
148 ["/Users/me", "/Users/me/projects/scope", "projects"],
149 ["/Users/me/projects/scope", "/Users/me/projects/scope", null],
150 ["/Users/me", "/Users/meg/a", null],
151 ["C:\\", "C:\\Users\\me", "Users"],
152 ["C:\\Users", "C:\\Users\\me\\scope", "me"],
153 ["\\\\nas\\share", "\\\\nas\\share\\docs\\notes", "docs"],
154 ])("in %j toward %j selects %j", (folder, trail, expected) => {
155 expect(trailChildOf(folder, trail)).toBe(expected);
156 });
157});
158
159describe("joinHostPath", () => {
160 it.each([
161 ["/Users/me", "projects/scope", "/Users/me/projects/scope"],
162 ["/", "Users", "/Users"],
163 ["C:\\", "Users/me", "C:\\Users\\me"],
164 ["C:\\Users", "me", "C:\\Users\\me"],
165 ["\\\\nas\\share", "docs/notes.md", "\\\\nas\\share\\docs\\notes.md"],
166 ])("joins %j and %j as %j", (folder, relativePath, expected) => {
167 expect(joinHostPath(folder, relativePath)).toBe(expected);
168 });
169});
170
171describe("isListableHostFolder", () => {
172 it.each([
173 ["/Users", true],
174 ["C:\\", true],
175 ["\\\\nas\\share", true],
176 // A UNC server is not a directory the server can list.
177 ["\\\\nas", false],
178 ["apps/web", false],
179 ])("%j is %j", (path, expected) => {
180 expect(isListableHostFolder(path)).toBe(expected);
181 });
182});
183
184describe("fileBreadcrumbParent", () => {
185 it.each([
186 ["src/lib", "src"],
187 ["src", ""],
188 ["", null],
189 ])("returns the parent of %j", (path, expected) => {
190 expect(fileBreadcrumbParent(path)).toBe(expected);
191 });
192});