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

filesystem.test.ts

190 lines6.5 KB

1import { describe, expect, it } from "vite-plus/test";
2import { AuthFilesystemReadScope, AuthOrchestrationReadScope } from "@t3tools/contracts";
3​
4import {
5 canPreloadBrowsePath,
6 createBrowseNavigationCoordinator,
7 filterFilesystemBrowseEntries,
8 getFilesystemBrowsePath,
9 resolveFilesystemReadAccess,
10} from "./filesystem.ts";
11​
12const SPLIT_SCOPES_SERVER = { serverUpdateScope: "environment:maintain" } as const;
13​
14describe("filesystem read access", () => {
15 it("waits for the initial catalog before declaring a missing environment disconnected", () => {
16 expect(
17 resolveFilesystemReadAccess({
18 isCatalogReady: false,
19 connection: null,
20 session: null,
21 sessionError: null,
22 }),
23 ).toEqual({ canReadFiles: false, isPending: true, error: null });
24 });
25​
26 it("stops waiting when the loaded catalog has no matching environment", () => {
27 expect(
28 resolveFilesystemReadAccess({
29 isCatalogReady: true,
30 connection: null,
31 session: null,
32 sessionError: null,
33 }),
34 ).toEqual({
35 canReadFiles: false,
36 isPending: false,
37 error: "This environment is not connected.",
38 });
39 });
40​
41 it.each(["available", "offline", "error"] as const)(
42 "stops waiting for an unresolved session when the connection is %s",
43 (phase) => {
44 expect(
45 resolveFilesystemReadAccess({
46 isCatalogReady: true,
47 connection: { phase, error: null },
48 session: null,
49 sessionError: null,
50 }),
51 ).toEqual({
52 canReadFiles: false,
53 isPending: false,
54 error: "This environment is not connected.",
55 });
56 },
57 );
58​
59 it.each(["connected", "connecting", "reconnecting"] as const)(
60 "waits for the session check while %s",
61 (phase) => {
62 expect(
63 resolveFilesystemReadAccess({
64 isCatalogReady: true,
65 connection: { phase, error: null },
66 session: null,
67 sessionError: null,
68 }),
69 ).toEqual({ canReadFiles: false, isPending: true, error: null });
70 },
71 );
72​
73 it("reports the transport failure when the session cannot be checked", () => {
74 expect(
75 resolveFilesystemReadAccess({
76 isCatalogReady: true,
77 connection: { phase: "error", error: "The relay is unavailable." },
78 session: null,
79 sessionError: null,
80 }),
81 ).toEqual({ canReadFiles: false, isPending: false, error: "The relay is unavailable." });
82 });
83​
84 it.each([false, true])(
85 "preserves a cached file grant offline with catalog ready=%s",
86 (isCatalogReady) => {
87 const input = {
88 isCatalogReady,
89 connection: { phase: "offline", error: null },
90 session: { authenticated: true, scopes: [AuthFilesystemReadScope] },
91 sessionError: null,
92 } as const;
93 expect(resolveFilesystemReadAccess(input)).toEqual({
94 canReadFiles: true,
95 isPending: false,
96 error: null,
97 });
98 expect(
99 resolveFilesystemReadAccess({ ...input, sessionError: "The session has expired." }),
100 ).toEqual({ canReadFiles: false, isPending: false, error: "The session has expired." });
101 },
102 );
103​
104 it.each([
105 { authenticated: true, scopes: [AuthOrchestrationReadScope], auth: SPLIT_SCOPES_SERVER },
106 { authenticated: false, scopes: [AuthFilesystemReadScope], auth: SPLIT_SCOPES_SERVER },
107 ] as const)("does not infer file access from an ungranted session", (session) => {
108 expect(
109 resolveFilesystemReadAccess({
110 isCatalogReady: true,
111 connection: { phase: "connected", error: null },
112 session,
113 sessionError: null,
114 }),
115 ).toEqual({ canReadFiles: false, isPending: false, error: null });
116 });
117​
118 it("falls back to the orchestration grant on a server that predates filesystem:read", () => {
119 expect(
120 resolveFilesystemReadAccess({
121 isCatalogReady: true,
122 connection: { phase: "connected", error: null },
123 session: { authenticated: true, scopes: [AuthOrchestrationReadScope], auth: {} },
124 sessionError: null,
125 }),
126 ).toEqual({ canReadFiles: true, isPending: false, error: null });
127 });
128});
129​
130describe("filesystem browse model", () => {
131 it("derives the browse target and navigation state", () => {
132 expect(getFilesystemBrowsePath("~/projects/t3")).toEqual({
133 isBrowsing: true,
134 directoryPath: "~/projects/",
135 filterQuery: "t3",
136 parentPath: "~/",
137 canBrowseUp: true,
138 });
139 expect(getFilesystemBrowsePath("C:\\Users\\test", "MacIntel").isBrowsing).toBe(false);
140 expect(getFilesystemBrowsePath("~/projects/", "", false).isBrowsing).toBe(false);
141 });
142​
143 it("filters names, hidden directories, and exact matches consistently", () => {
144 const entries = [
145 { name: ".config", fullPath: "/Users/test/.config" },
146 { name: "Code", fullPath: "/Users/test/Code" },
147 { name: "codething", fullPath: "/Users/test/codething" },
148 ];
149​
150 expect(filterFilesystemBrowseEntries(entries, "co")).toEqual({
151 visibleEntries: entries.slice(1, 3),
152 exactEntry: null,
153 });
154 expect(filterFilesystemBrowseEntries(entries, "").visibleEntries).toEqual(entries.slice(1));
155 expect(filterFilesystemBrowseEntries(entries, ".").visibleEntries).toEqual(entries.slice(0, 1));
156 expect(filterFilesystemBrowseEntries(entries, "Code").exactEntry).toEqual(entries[1]);
157 });
158});
159​
160describe("browse navigation", () => {
161 it("only commits the latest valid navigation", async () => {
162 const navigation = createBrowseNavigationCoordinator();
163 const first = Promise.withResolvers<void>();
164 const second = Promise.withResolvers<void>();
165 const commits: string[] = [];
166 const commit = (name: string) => () => commits.push(name);
167 const firstRun = navigation.run(() => first.promise, commit("first"));
168 const secondRun = navigation.run(() => second.promise, commit("second"));
169​
170 second.resolve();
171 await expect(secondRun).resolves.toBe(true);
172 first.resolve();
173 await expect(firstRun).resolves.toBe(false);
174​
175 const invalidated = Promise.withResolvers<void>();
176 const invalidatedRun = navigation.run(() => invalidated.promise, commit("stale"));
177 navigation.invalidate();
178 invalidated.resolve();
179​
180 await expect(invalidatedRun).resolves.toBe(false);
181 expect(commits).toEqual(["second"]);
182 });
183​
184 it("only preloads connected environments", () => {
185 expect(canPreloadBrowsePath("connected")).toBe(true);
186 expect(canPreloadBrowsePath("offline")).toBe(false);
187 expect(canPreloadBrowsePath("reconnecting")).toBe(false);
188 expect(canPreloadBrowsePath(null)).toBe(false);
189 });
190});