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

filesystem.test.ts

70 lines2.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 canPreloadBrowsePath,
5 createBrowseNavigationCoordinator,
6 filterFilesystemBrowseEntries,
7 getFilesystemBrowsePath,
8} from "./filesystem.ts";
9​
10describe("filesystem browse model", () => {
11 it("derives the browse target and navigation state", () => {
12 expect(getFilesystemBrowsePath("~/projects/t3")).toEqual({
13 isBrowsing: true,
14 directoryPath: "~/projects/",
15 filterQuery: "t3",
16 parentPath: "~/",
17 canBrowseUp: true,
18 });
19 expect(getFilesystemBrowsePath("C:\\Users\\test", "MacIntel").isBrowsing).toBe(false);
20 expect(getFilesystemBrowsePath("~/projects/", "", false).isBrowsing).toBe(false);
21 });
22​
23 it("filters names, hidden directories, and exact matches consistently", () => {
24 const entries = [
25 { name: ".config", fullPath: "/Users/test/.config" },
26 { name: "Code", fullPath: "/Users/test/Code" },
27 { name: "codething", fullPath: "/Users/test/codething" },
28 ];
29​
30 expect(filterFilesystemBrowseEntries(entries, "co")).toEqual({
31 visibleEntries: entries.slice(1, 3),
32 exactEntry: null,
33 });
34 expect(filterFilesystemBrowseEntries(entries, "").visibleEntries).toEqual(entries.slice(1));
35 expect(filterFilesystemBrowseEntries(entries, ".").visibleEntries).toEqual(entries.slice(0, 1));
36 expect(filterFilesystemBrowseEntries(entries, "Code").exactEntry).toEqual(entries[1]);
37 });
38});
39​
40describe("browse navigation", () => {
41 it("only commits the latest valid navigation", async () => {
42 const navigation = createBrowseNavigationCoordinator();
43 const first = Promise.withResolvers<void>();
44 const second = Promise.withResolvers<void>();
45 const commits: string[] = [];
46 const commit = (name: string) => () => commits.push(name);
47 const firstRun = navigation.run(() => first.promise, commit("first"));
48 const secondRun = navigation.run(() => second.promise, commit("second"));
49​
50 second.resolve();
51 await expect(secondRun).resolves.toBe(true);
52 first.resolve();
53 await expect(firstRun).resolves.toBe(false);
54​
55 const invalidated = Promise.withResolvers<void>();
56 const invalidatedRun = navigation.run(() => invalidated.promise, commit("stale"));
57 navigation.invalidate();
58 invalidated.resolve();
59​
60 await expect(invalidatedRun).resolves.toBe(false);
61 expect(commits).toEqual(["second"]);
62 });
63​
64 it("only preloads connected environments", () => {
65 expect(canPreloadBrowsePath("connected")).toBe(true);
66 expect(canPreloadBrowsePath("offline")).toBe(false);
67 expect(canPreloadBrowsePath("reconnecting")).toBe(false);
68 expect(canPreloadBrowsePath(null)).toBe(false);
69 });
70});