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

filesystem.test.ts

216 lines8.4 KB

1import { describe, expect, it } from "vite-plus/test";
2import { AuthFilesystemReadScope, AuthOrchestrationReadScope } from "@t3tools/contracts";
3​
4import {
5 createBrowseNavigationCoordinator,
6 filterFilesystemBrowseEntries,
7 getFilesystemBrowsePath,
8 getFilesystemBrowseTabCompletion,
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.each([false, true])(
27 "preserves a cached file grant offline with catalog ready=%s",
28 (isCatalogReady) => {
29 const input = {
30 isCatalogReady,
31 connection: { phase: "offline", error: null },
32 session: { authenticated: true, scopes: [AuthFilesystemReadScope] },
33 sessionError: null,
34 } as const;
35 expect(resolveFilesystemReadAccess(input)).toEqual({
36 canReadFiles: true,
37 isPending: false,
38 error: null,
39 });
40 expect(
41 resolveFilesystemReadAccess({ ...input, sessionError: "The session has expired." }),
42 ).toEqual({ canReadFiles: false, isPending: false, error: "The session has expired." });
43 },
44 );
45​
46 it.each([
47 { authenticated: true, scopes: [AuthOrchestrationReadScope], auth: SPLIT_SCOPES_SERVER },
48 { authenticated: false, scopes: [AuthFilesystemReadScope], auth: SPLIT_SCOPES_SERVER },
49 ] as const)("does not infer file access from an ungranted session", (session) => {
50 expect(
51 resolveFilesystemReadAccess({
52 isCatalogReady: true,
53 connection: { phase: "connected", error: null },
54 session,
55 sessionError: null,
56 }),
57 ).toEqual({ canReadFiles: false, isPending: false, error: null });
58 });
59​
60 it("falls back to the orchestration grant on a server that predates filesystem:read", () => {
61 expect(
62 resolveFilesystemReadAccess({
63 isCatalogReady: true,
64 connection: { phase: "connected", error: null },
65 session: { authenticated: true, scopes: [AuthOrchestrationReadScope], auth: {} },
66 sessionError: null,
67 }),
68 ).toEqual({ canReadFiles: true, isPending: false, error: null });
69 });
70});
71​
72describe("filesystem browse model", () => {
73 it("derives the browse target and navigation state", () => {
74 expect(getFilesystemBrowsePath("~/projects/t3")).toEqual({
75 isBrowsing: true,
76 directoryPath: "~/projects/",
77 filterQuery: "t3",
78 parentPath: "~/",
79 canBrowseUp: true,
80 });
81 expect(getFilesystemBrowsePath("D:", "Win32")).toEqual({
82 isBrowsing: true,
83 directoryPath: "D:\\",
84 filterQuery: "",
85 parentPath: null,
86 canBrowseUp: false,
87 });
88 expect(getFilesystemBrowsePath("C:\\Users\\test", "MacIntel").isBrowsing).toBe(false);
89 expect(getFilesystemBrowsePath("~/projects/", "", false).isBrowsing).toBe(false);
90 });
91​
92 it("completes Tab like a shell", () => {
93 const entry = (name: string) => ({ name, fullPath: `/Users/me/${name}` });
94 expect(getFilesystemBrowseTabCompletion([entry("Creations")], "Creation")).toEqual({
95 name: "Creations",
96 isComplete: true,
97 });
98 expect(getFilesystemBrowseTabCompletion([entry("OpenSourced"), entry("OpenAI")], "op")).toEqual(
99 { name: "Open", isComplete: false },
100 );
101 expect(getFilesystemBrowseTabCompletion([entry("Open"), entry("Opal")], "Op")).toBeNull();
102 expect(getFilesystemBrowseTabCompletion([entry("openAI"), entry("OpenSource")], "o")).toEqual({
103 name: "open",
104 isComplete: false,
105 });
106 expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😁two")], "")).toBeNull();
107 expect(getFilesystemBrowseTabCompletion([entry("ΟΣα"), entry("οσβ")], "ο")).toBeNull();
108 expect(getFilesystemBrowseTabCompletion([entry("İfoo"), entry("İfar")], "i\u0307")).toEqual({
109 name: "İf",
110 isComplete: false,
111 });
112 expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😀other")], "😀")).toEqual({
113 name: "😀o",
114 isComplete: false,
115 });
116 expect(getFilesystemBrowseTabCompletion([], "x")).toBeNull();
117 // The list also shows substring and fuzzy matches; Tab completes only the typed name.
118 expect(getFilesystemBrowseTabCompletion([entry("t3code"), entry("my-t3code")], "t3")).toEqual({
119 name: "t3code",
120 isComplete: true,
121 });
122 expect(
123 getFilesystemBrowseTabCompletion(
124 [entry("OpenSourced"), entry("OpenAI"), entry("reopen"), entry("photos")],
125 "op",
126 ),
127 ).toEqual({ name: "Open", isComplete: false });
128 expect(getFilesystemBrowseTabCompletion([entry("t3code")], "tcd")).toBeNull();
129 });
130​
131 it("matches folder names stored decomposed, as macOS Finder names them", () => {
132 const entry = { name: "De\u0301veloppement", fullPath: "/Users/me/De\u0301veloppement" };
133 expect(filterFilesystemBrowseEntries([entry], "d\u00e9v")).toEqual({
134 visibleEntries: [entry],
135 exactEntry: null,
136 });
137 expect(filterFilesystemBrowseEntries([entry], "De").visibleEntries).toEqual([entry]);
138 expect(filterFilesystemBrowseEntries([entry], "dévpt").visibleEntries).toEqual([entry]);
139 expect(filterFilesystemBrowseEntries([entry], "D\u00e9veloppement").exactEntry).toBe(entry);
140 // Linux keeps both spellings as separate folders; the one typed exactly wins.
141 const composed = { name: "D\u00e9veloppement", fullPath: "/home/me/D\u00e9veloppement" };
142 expect(filterFilesystemBrowseEntries([entry, composed], "D\u00e9veloppement").exactEntry).toBe(
143 composed,
144 );
145 // Two spellings that both normalize to the query are ambiguous; neither is picked.
146 const angstroms = [
147 { name: "\u212B", fullPath: "/home/me/\u212B" },
148 { name: "A\u030A", fullPath: "/home/me/A\u030A" },
149 ];
150 expect(filterFilesystemBrowseEntries(angstroms, "\u00C5").exactEntry).toBeNull();
151 });
152});
153​
154describe("browse navigation", () => {
155 it("only commits the latest valid navigation", async () => {
156 const navigation = createBrowseNavigationCoordinator();
157 const first = Promise.withResolvers<void>();
158 const second = Promise.withResolvers<void>();
159 const commits: string[] = [];
160 const commit = (name: string) => () => commits.push(name);
161 const firstRun = navigation.run(() => first.promise, commit("first"));
162 const secondRun = navigation.run(() => second.promise, commit("second"));
163​
164 second.resolve();
165 await expect(secondRun).resolves.toBe(true);
166 first.resolve();
167 await expect(firstRun).resolves.toBe(false);
168​
169 const invalidated = Promise.withResolvers<void>();
170 const invalidatedRun = navigation.run(() => invalidated.promise, commit("stale"));
171 navigation.invalidate();
172 invalidated.resolve();
173​
174 await expect(invalidatedRun).resolves.toBe(false);
175 expect(commits).toEqual(["second"]);
176 });
177});
178​
179describe("browse entry filtering", () => {
180 const entries = ["my-t3code", "t3code", "cotton", "T3Code-old", ".t3", "docs"].map((name) => ({
181 name,
182 fullPath: `~/code/${name}`,
183 }));
184 const names = (query: string) =>
185 filterFilesystemBrowseEntries(entries, query).visibleEntries.map((entry) => entry.name);
186​
187 it("keeps every visible entry in order for an empty query", () => {
188 expect(names("")).toEqual(["my-t3code", "t3code", "cotton", "T3Code-old", "docs"]);
189 });
190​
191 it("matches substrings and subsequences, best match first", () => {
192 expect(names("code")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
193 expect(names("t3c")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
194 expect(names("tcd")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
195 expect(names("xyz")).toEqual([]);
196 });
197​
198 it("ranks a word-start match in a long name above any substring match", () => {
199 const longName = `${"a".repeat(240)}-foo`;
200 const folders = [
201 { name: "xfoo", fullPath: "~/xfoo" },
202 { name: longName, fullPath: "~/long" },
203 ];
204 expect(
205 filterFilesystemBrowseEntries(folders, "foo").visibleEntries.map((entry) => entry.name),
206 ).toEqual([longName, "xfoo"]);
207 });
208​
209 it("shows dot folders only when the query starts with a dot", () => {
210 expect(names("t3")).not.toContain(".t3");
211 expect(filterFilesystemBrowseEntries(entries, ".t3")).toEqual({
212 visibleEntries: [entries[4]],
213 exactEntry: entries[4],
214 });
215 });
216});