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

filesystem.test.ts

107 lines3.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import { AuthFilesystemReadScope, AuthOrchestrationReadScope } from "@t3tools/contracts";
3​
4import {
5 createBrowseNavigationCoordinator,
6 getFilesystemBrowsePath,
7 resolveFilesystemReadAccess,
8} from "./filesystem.ts";
9​
10const SPLIT_SCOPES_SERVER = { serverUpdateScope: "environment:maintain" } as const;
11​
12describe("filesystem read access", () => {
13 it("waits for the initial catalog before declaring a missing environment disconnected", () => {
14 expect(
15 resolveFilesystemReadAccess({
16 isCatalogReady: false,
17 connection: null,
18 session: null,
19 sessionError: null,
20 }),
21 ).toEqual({ canReadFiles: false, isPending: true, error: null });
22 });
23​
24 it.each([false, true])(
25 "preserves a cached file grant offline with catalog ready=%s",
26 (isCatalogReady) => {
27 const input = {
28 isCatalogReady,
29 connection: { phase: "offline", error: null },
30 session: { authenticated: true, scopes: [AuthFilesystemReadScope] },
31 sessionError: null,
32 } as const;
33 expect(resolveFilesystemReadAccess(input)).toEqual({
34 canReadFiles: true,
35 isPending: false,
36 error: null,
37 });
38 expect(
39 resolveFilesystemReadAccess({ ...input, sessionError: "The session has expired." }),
40 ).toEqual({ canReadFiles: false, isPending: false, error: "The session has expired." });
41 },
42 );
43​
44 it.each([
45 { authenticated: true, scopes: [AuthOrchestrationReadScope], auth: SPLIT_SCOPES_SERVER },
46 { authenticated: false, scopes: [AuthFilesystemReadScope], auth: SPLIT_SCOPES_SERVER },
47 ] as const)("does not infer file access from an ungranted session", (session) => {
48 expect(
49 resolveFilesystemReadAccess({
50 isCatalogReady: true,
51 connection: { phase: "connected", error: null },
52 session,
53 sessionError: null,
54 }),
55 ).toEqual({ canReadFiles: false, isPending: false, error: null });
56 });
57​
58 it("falls back to the orchestration grant on a server that predates filesystem:read", () => {
59 expect(
60 resolveFilesystemReadAccess({
61 isCatalogReady: true,
62 connection: { phase: "connected", error: null },
63 session: { authenticated: true, scopes: [AuthOrchestrationReadScope], auth: {} },
64 sessionError: null,
65 }),
66 ).toEqual({ canReadFiles: true, isPending: false, error: null });
67 });
68});
69​
70describe("filesystem browse model", () => {
71 it("derives the browse target and navigation state", () => {
72 expect(getFilesystemBrowsePath("~/projects/t3")).toEqual({
73 isBrowsing: true,
74 directoryPath: "~/projects/",
75 filterQuery: "t3",
76 parentPath: "~/",
77 canBrowseUp: true,
78 });
79 expect(getFilesystemBrowsePath("C:\\Users\\test", "MacIntel").isBrowsing).toBe(false);
80 expect(getFilesystemBrowsePath("~/projects/", "", false).isBrowsing).toBe(false);
81 });
82});
83​
84describe("browse navigation", () => {
85 it("only commits the latest valid navigation", async () => {
86 const navigation = createBrowseNavigationCoordinator();
87 const first = Promise.withResolvers<void>();
88 const second = Promise.withResolvers<void>();
89 const commits: string[] = [];
90 const commit = (name: string) => () => commits.push(name);
91 const firstRun = navigation.run(() => first.promise, commit("first"));
92 const secondRun = navigation.run(() => second.promise, commit("second"));
93​
94 second.resolve();
95 await expect(secondRun).resolves.toBe(true);
96 first.resolve();
97 await expect(firstRun).resolves.toBe(false);
98​
99 const invalidated = Promise.withResolvers<void>();
100 const invalidatedRun = navigation.run(() => invalidated.promise, commit("stale"));
101 navigation.invalidate();
102 invalidated.resolve();
103​
104 await expect(invalidatedRun).resolves.toBe(false);
105 expect(commits).toEqual(["second"]);
106 });
107});