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

filesystem.test.ts

243 lines9.7 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("browses up from a double-slash POSIX path but stops at a Windows share", () => {
93 expect(getFilesystemBrowsePath("//tmp/scout-first/", "Linux")).toMatchObject({
94 parentPath: "/tmp/",
95 canBrowseUp: true,
96 });
97 expect(getFilesystemBrowsePath("//server/share/", "Win32")).toMatchObject({
98 parentPath: null,
99 canBrowseUp: false,
100 });
101 });
102​
103 it("completes Tab like a shell", () => {
104 const entry = (name: string) => ({ name, fullPath: `/Users/me/${name}` });
105 expect(getFilesystemBrowseTabCompletion([entry("Creations")], "Creation")).toEqual({
106 name: "Creations",
107 isComplete: true,
108 });
109 expect(getFilesystemBrowseTabCompletion([entry("OpenSourced"), entry("OpenAI")], "op")).toEqual(
110 { name: "Open", isComplete: false },
111 );
112 expect(getFilesystemBrowseTabCompletion([entry("Open"), entry("Opal")], "Op")).toBeNull();
113 expect(getFilesystemBrowseTabCompletion([entry("openAI"), entry("OpenSource")], "o")).toEqual({
114 name: "open",
115 isComplete: false,
116 });
117 expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😁two")], "")).toBeNull();
118 expect(getFilesystemBrowseTabCompletion([entry("ΟΣα"), entry("οσβ")], "ο")).toBeNull();
119 expect(getFilesystemBrowseTabCompletion([entry("İfoo"), entry("İfar")], "i\u0307")).toEqual({
120 name: "İf",
121 isComplete: false,
122 });
123 expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😀other")], "😀")).toEqual({
124 name: "😀o",
125 isComplete: false,
126 });
127 expect(getFilesystemBrowseTabCompletion([], "x")).toBeNull();
128 // The list also shows substring and fuzzy matches; Tab completes only the typed name.
129 expect(getFilesystemBrowseTabCompletion([entry("t3code"), entry("my-t3code")], "t3")).toEqual({
130 name: "t3code",
131 isComplete: true,
132 });
133 expect(
134 getFilesystemBrowseTabCompletion(
135 [entry("OpenSourced"), entry("OpenAI"), entry("reopen"), entry("photos")],
136 "op",
137 ),
138 ).toEqual({ name: "Open", isComplete: false });
139 expect(getFilesystemBrowseTabCompletion([entry("t3code")], "tcd")).toBeNull();
140 // A decomposed and a composed name share their composed prefix.
141 expect(
142 getFilesystemBrowseTabCompletion([entry("e\u0301clair"), entry("\u00e9cole")], "\u00e9"),
143 ).toEqual({ name: "\u00e9c", isComplete: false });
144 // Names that already share a raw prefix keep their own spelling.
145 expect(getFilesystemBrowseTabCompletion([entry("e\u0301"), entry("e\u0301cole")], "e")).toEqual(
146 { name: "e\u0301", isComplete: false },
147 );
148 // Names that differ only in normalization are two folders on Linux; Tab never picks one.
149 expect(
150 getFilesystemBrowseTabCompletion([entry("e\u0301"), entry("\u00e9")], "\u00e9"),
151 ).toBeNull();
152 // The composed comparison must still extend what was typed.
153 expect(
154 getFilesystemBrowseTabCompletion([entry("e\u0301\u0323a"), entry("e\u0323\u0301b")], "e"),
155 ).toBeNull();
156 });
157​
158 it("matches folder names stored decomposed, as macOS Finder names them", () => {
159 const entry = { name: "De\u0301veloppement", fullPath: "/Users/me/De\u0301veloppement" };
160 expect(filterFilesystemBrowseEntries([entry], "d\u00e9v")).toEqual({
161 visibleEntries: [entry],
162 exactEntry: null,
163 });
164 expect(filterFilesystemBrowseEntries([entry], "De").visibleEntries).toEqual([entry]);
165 expect(filterFilesystemBrowseEntries([entry], "dévpt").visibleEntries).toEqual([entry]);
166 expect(filterFilesystemBrowseEntries([entry], "D\u00e9veloppement").exactEntry).toBe(entry);
167 // Linux keeps both spellings as separate folders; the one typed exactly wins.
168 const composed = { name: "D\u00e9veloppement", fullPath: "/home/me/D\u00e9veloppement" };
169 expect(filterFilesystemBrowseEntries([entry, composed], "D\u00e9veloppement").exactEntry).toBe(
170 composed,
171 );
172 // Two spellings that both normalize to the query are ambiguous; neither is picked.
173 const angstroms = [
174 { name: "\u212B", fullPath: "/home/me/\u212B" },
175 { name: "A\u030A", fullPath: "/home/me/A\u030A" },
176 ];
177 expect(filterFilesystemBrowseEntries(angstroms, "\u00C5").exactEntry).toBeNull();
178 });
179});
180​
181describe("browse navigation", () => {
182 it("only commits the latest valid navigation", async () => {
183 const navigation = createBrowseNavigationCoordinator();
184 const first = Promise.withResolvers<void>();
185 const second = Promise.withResolvers<void>();
186 const commits: string[] = [];
187 const commit = (name: string) => () => commits.push(name);
188 const firstRun = navigation.run(() => first.promise, commit("first"));
189 const secondRun = navigation.run(() => second.promise, commit("second"));
190​
191 second.resolve();
192 await expect(secondRun).resolves.toBe(true);
193 first.resolve();
194 await expect(firstRun).resolves.toBe(false);
195​
196 const invalidated = Promise.withResolvers<void>();
197 const invalidatedRun = navigation.run(() => invalidated.promise, commit("stale"));
198 navigation.invalidate();
199 invalidated.resolve();
200​
201 await expect(invalidatedRun).resolves.toBe(false);
202 expect(commits).toEqual(["second"]);
203 });
204});
205​
206describe("browse entry filtering", () => {
207 const entries = ["my-t3code", "t3code", "cotton", "T3Code-old", ".t3", "docs"].map((name) => ({
208 name,
209 fullPath: `~/code/${name}`,
210 }));
211 const names = (query: string) =>
212 filterFilesystemBrowseEntries(entries, query).visibleEntries.map((entry) => entry.name);
213​
214 it("keeps every visible entry in order for an empty query", () => {
215 expect(names("")).toEqual(["my-t3code", "t3code", "cotton", "T3Code-old", "docs"]);
216 });
217​
218 it("matches substrings and subsequences, best match first", () => {
219 expect(names("code")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
220 expect(names("t3c")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
221 expect(names("tcd")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
222 expect(names("xyz")).toEqual([]);
223 });
224​
225 it("ranks a word-start match in a long name above any substring match", () => {
226 const longName = `${"a".repeat(240)}-foo`;
227 const folders = [
228 { name: "xfoo", fullPath: "~/xfoo" },
229 { name: longName, fullPath: "~/long" },
230 ];
231 expect(
232 filterFilesystemBrowseEntries(folders, "foo").visibleEntries.map((entry) => entry.name),
233 ).toEqual([longName, "xfoo"]);
234 });
235​
236 it("shows dot folders only when the query starts with a dot", () => {
237 expect(names("t3")).not.toContain(".t3");
238 expect(filterFilesystemBrowseEntries(entries, ".t3")).toEqual({
239 visibleEntries: [entries[4]],
240 exactEntry: entries[4],
241 });
242 });
243});