Open History to see the last commit
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("C:\\Users\\test", "MacIntel").isBrowsing).toBe(false);
82 expect(getFilesystemBrowsePath("~/projects/", "", false).isBrowsing).toBe(false);
83 });
84
85 it("completes Tab like a shell", () => {
86 const entry = (name: string) => ({ name, fullPath: `/Users/me/${name}` });
87 expect(getFilesystemBrowseTabCompletion([entry("Creations")], "Creation")).toEqual({
88 name: "Creations",
89 isComplete: true,
90 });
91 expect(getFilesystemBrowseTabCompletion([entry("OpenSourced"), entry("OpenAI")], "op")).toEqual(
92 { name: "Open", isComplete: false },
93 );
94 expect(getFilesystemBrowseTabCompletion([entry("Open"), entry("Opal")], "Op")).toBeNull();
95 expect(getFilesystemBrowseTabCompletion([entry("openAI"), entry("OpenSource")], "o")).toEqual({
96 name: "open",
97 isComplete: false,
98 });
99 expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😁two")], "")).toBeNull();
100 expect(getFilesystemBrowseTabCompletion([entry("ΟΣα"), entry("οσβ")], "ο")).toBeNull();
101 expect(getFilesystemBrowseTabCompletion([entry("İfoo"), entry("İfar")], "i\u0307")).toEqual({
102 name: "İf",
103 isComplete: false,
104 });
105 expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😀other")], "😀")).toEqual({
106 name: "😀o",
107 isComplete: false,
108 });
109 expect(getFilesystemBrowseTabCompletion([], "x")).toBeNull();
110 });
111
112 it("matches folder names stored decomposed, as macOS Finder names them", () => {
113 const entry = { name: "De\u0301veloppement", fullPath: "/Users/me/De\u0301veloppement" };
114 expect(filterFilesystemBrowseEntries([entry], "d\u00e9v")).toEqual({
115 visibleEntries: [entry],
116 exactEntry: null,
117 });
118 expect(filterFilesystemBrowseEntries([entry], "De").visibleEntries).toEqual([entry]);
119 expect(filterFilesystemBrowseEntries([entry], "dévpt").visibleEntries).toEqual([entry]);
120 expect(filterFilesystemBrowseEntries([entry], "D\u00e9veloppement").exactEntry).toBe(entry);
121 // Linux keeps both spellings as separate folders; the one typed exactly wins.
122 const composed = { name: "D\u00e9veloppement", fullPath: "/home/me/D\u00e9veloppement" };
123 expect(filterFilesystemBrowseEntries([entry, composed], "D\u00e9veloppement").exactEntry).toBe(
124 composed,
125 );
126 // Two spellings that both normalize to the query are ambiguous; neither is picked.
127 const angstroms = [
128 { name: "\u212B", fullPath: "/home/me/\u212B" },
129 { name: "A\u030A", fullPath: "/home/me/A\u030A" },
130 ];
131 expect(filterFilesystemBrowseEntries(angstroms, "\u00C5").exactEntry).toBeNull();
132 });
133});
134
135describe("browse navigation", () => {
136 it("only commits the latest valid navigation", async () => {
137 const navigation = createBrowseNavigationCoordinator();
138 const first = Promise.withResolvers<void>();
139 const second = Promise.withResolvers<void>();
140 const commits: string[] = [];
141 const commit = (name: string) => () => commits.push(name);
142 const firstRun = navigation.run(() => first.promise, commit("first"));
143 const secondRun = navigation.run(() => second.promise, commit("second"));
144
145 second.resolve();
146 await expect(secondRun).resolves.toBe(true);
147 first.resolve();
148 await expect(firstRun).resolves.toBe(false);
149
150 const invalidated = Promise.withResolvers<void>();
151 const invalidatedRun = navigation.run(() => invalidated.promise, commit("stale"));
152 navigation.invalidate();
153 invalidated.resolve();
154
155 await expect(invalidatedRun).resolves.toBe(false);
156 expect(commits).toEqual(["second"]);
157 });
158});
159
160describe("browse entry filtering", () => {
161 const entries = ["my-t3code", "t3code", "cotton", "T3Code-old", ".t3", "docs"].map((name) => ({
162 name,
163 fullPath: `~/code/${name}`,
164 }));
165 const names = (query: string) =>
166 filterFilesystemBrowseEntries(entries, query).visibleEntries.map((entry) => entry.name);
167
168 it("keeps every visible entry in order for an empty query", () => {
169 expect(names("")).toEqual(["my-t3code", "t3code", "cotton", "T3Code-old", "docs"]);
170 });
171
172 it("matches substrings and subsequences, best match first", () => {
173 expect(names("code")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
174 expect(names("t3c")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
175 expect(names("tcd")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
176 expect(names("xyz")).toEqual([]);
177 });
178
179 it("ranks a word-start match in a long name above any substring match", () => {
180 const longName = `${"a".repeat(240)}-foo`;
181 const folders = [
182 { name: "xfoo", fullPath: "~/xfoo" },
183 { name: longName, fullPath: "~/long" },
184 ];
185 expect(
186 filterFilesystemBrowseEntries(folders, "foo").visibleEntries.map((entry) => entry.name),
187 ).toEqual([longName, "xfoo"]);
188 });
189
190 it("shows dot folders only when the query starts with a dot", () => {
191 expect(names("t3")).not.toContain(".t3");
192 expect(filterFilesystemBrowseEntries(entries, ".t3")).toEqual({
193 visibleEntries: [entries[4]],
194 exactEntry: entries[4],
195 });
196 });
197});