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 it("builds home-relative breadcrumbs with navigable child folders", () => {
52 expect(fileBreadcrumbs("t3code", "~/.claude/CLAUDE.md")).toEqual([
53 { label: "~", path: "~", kind: "directory" },
54 { label: ".claude", path: "~/.claude", kind: "directory" },
55 { label: "CLAUDE.md", path: "~/.claude/CLAUDE.md", kind: "file" },
56 ]);
57 expect(isListableHostFolder("~/.claude")).toBe(true);
58 });
59});
60
61describe("breadcrumbPathOf", () => {
62 it.each([
63 ["", ""],
64 ["src", "src"],
65 ["src/", "src"],
66 ["src//components", "src/components"],
67 ["/tmp/t3-demo/", "/tmp/t3-demo"],
68 ["/", "/"],
69 ["C:\\Users\\me\\", "C:\\Users\\me"],
70 ["C:\\", "C:\\"],
71 ["\\\\server\\share\\", "\\\\server\\share"],
72 ])("writes %j as its crumb names it", (path, expected) => {
73 expect(breadcrumbPathOf(path)).toBe(expected);
74 });
75});
76
77describe("fileBreadcrumbChildren", () => {
78 const entries = [
79 { path: "README.md", kind: "file" as const },
80 { path: "src", kind: "directory" as const },
81 { path: "src-old", kind: "directory" as const },
82 { path: "src/index.ts", kind: "file" as const },
83 { path: "src/lib", kind: "directory" as const },
84 { path: "src/lib/file10.ts", kind: "file" as const },
85 { path: "src/lib/file2.ts", kind: "file" as const },
86 { path: "src-old/index.ts", kind: "file" as const },
87 ];
88
89 it("returns only the immediate children of the project root", () => {
90 expect(fileBreadcrumbChildren(entries, "")).toEqual([
91 { path: "src", kind: "directory", label: "src" },
92 { path: "src-old", kind: "directory", label: "src-old" },
93 { path: "README.md", kind: "file", label: "README.md" },
94 ]);
95 });
96
97 it("honors segment boundaries and sorts folders before files", () => {
98 expect(fileBreadcrumbChildren(entries, "src")).toEqual([
99 { path: "src/lib", kind: "directory", label: "lib" },
100 { path: "src/index.ts", kind: "file", label: "index.ts" },
101 ]);
102 });
103
104 it("uses natural file-name ordering and preserves input order for equivalent names", () => {
105 const files = ["file10.ts", "File2.ts", "file02.ts", "file2.ts"].map((name) => ({
106 path: `src/lib/${name}`,
107 kind: "file" as const,
108 }));
109
110 expect(fileBreadcrumbChildren(files, "src/lib").map((entry) => entry.label)).toEqual([
111 "File2.ts",
112 "file02.ts",
113 "file2.ts",
114 "file10.ts",
115 ]);
116 });
117
118 it("returns an empty list for an empty or missing directory", () => {
119 expect(fileBreadcrumbChildren(entries, "missing")).toEqual([]);
120 });
121});
122
123describe("retainedBreadcrumbTrail", () => {
124 it.each([
125 // Walking up keeps the deeper path, all the way to the workspace root.
126 ["apps/web/src", "apps/web", "apps/web/src"],
127 ["apps/web/src", "", "apps/web/src"],
128 // Walking back down inside the trail keeps it too.
129 ["apps/web/src", "apps/web/src", "apps/web/src"],
130 // Somewhere else starts over, including a sibling that shares a name prefix.
131 ["apps/web/src", "apps/mobile", "apps/mobile"],
132 ["src/lib", "src-old", "src-old"],
133 ["apps/web", "apps/web/src/main.tsx", "apps/web/src/main.tsx"],
134 // Workspace and host paths never share a trail.
135 ["apps/web/src", "/tmp/report", "/tmp/report"],
136 ["/tmp/report", "", ""],
137 ["C:\\Temp\\a", "", ""],
138 // Host folders outside the workspace keep their trail the same way.
139 ["/Users/me/projects/scope", "/Users/me", "/Users/me/projects/scope"],
140 ["/Users/me/projects/scope", "/Users/me/proj", "/Users/me/proj"],
141 ["/Volumes/Work/a", "/Volumes/Work/a/", "/Volumes/Work/a"],
142 ["C:\\Users\\me\\scope", "C:\\", "C:\\Users\\me\\scope"],
143 ["C:\\Users\\me\\scope", "C:\\Users", "C:\\Users\\me\\scope"],
144 ["C:\\Users\\me\\scope", "D:\\", "D:\\"],
145 ["\\\\nas\\share\\docs\\notes", "\\\\nas\\share", "\\\\nas\\share\\docs\\notes"],
146 ])("from trail %j to %j keeps %j", (trail, path, expected) => {
147 expect(retainedBreadcrumbTrail(trail, path)).toBe(expected);
148 });
149
150 it("starts at the path when nothing came before", () => {
151 expect(retainedBreadcrumbTrail(undefined, "apps")).toBe("apps");
152 });
153});
154
155describe("trailChildOf", () => {
156 it.each([
157 ["/Users/me", "/Users/me/projects/scope", "projects"],
158 ["/Users/me/projects/scope", "/Users/me/projects/scope", null],
159 ["/Users/me", "/Users/meg/a", null],
160 ["C:\\", "C:\\Users\\me", "Users"],
161 ["C:\\Users", "C:\\Users\\me\\scope", "me"],
162 ["\\\\nas\\share", "\\\\nas\\share\\docs\\notes", "docs"],
163 ])("in %j toward %j selects %j", (folder, trail, expected) => {
164 expect(trailChildOf(folder, trail)).toBe(expected);
165 });
166});
167
168describe("joinHostPath", () => {
169 it.each([
170 ["/Users/me", "projects/scope", "/Users/me/projects/scope"],
171 ["/", "Users", "/Users"],
172 ["C:\\", "Users/me", "C:\\Users\\me"],
173 ["C:\\Users", "me", "C:\\Users\\me"],
174 ["\\\\nas\\share", "docs/notes.md", "\\\\nas\\share\\docs\\notes.md"],
175 ])("joins %j and %j as %j", (folder, relativePath, expected) => {
176 expect(joinHostPath(folder, relativePath)).toBe(expected);
177 });
178});
179
180describe("isListableHostFolder", () => {
181 it.each([
182 ["/Users", true],
183 ["C:\\", true],
184 ["\\\\nas\\share", true],
185 // A UNC server is not a directory the server can list.
186 ["\\\\nas", false],
187 ["apps/web", false],
188 ])("%j is %j", (path, expected) => {
189 expect(isListableHostFolder(path)).toBe(expected);
190 });
191});
192
193describe("fileBreadcrumbParent", () => {
194 it.each([
195 ["src/lib", "src"],
196 ["src", ""],
197 ["", null],
198 ])("returns the parent of %j", (path, expected) => {
199 expect(fileBreadcrumbParent(path)).toBe(expected);
200 });
201});