The best way to control Claude Code, Codex and more from any computer or phone

main
ts 209 lines 8.26 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2 import { AuthFilesystemReadScope, AuthOrchestrationReadScope } from "@t3tools/contracts";
3
4 import {
5 createBrowseNavigationCoordinator,
6 filterFilesystemBrowseEntries,
7 getFilesystemBrowsePath,
8 getFilesystemBrowseTabCompletion,
9 resolveFilesystemReadAccess,
10 } from "./filesystem.ts";
11
12 const SPLIT_SCOPES_SERVER = { serverUpdateScope: "environment:maintain" } as const;
13
14 describe("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
72 describe("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 // The list also shows substring and fuzzy matches; Tab completes only the typed name.
111 expect(getFilesystemBrowseTabCompletion([entry("t3code"), entry("my-t3code")], "t3")).toEqual({
112 name: "t3code",
113 isComplete: true,
114 });
115 expect(
116 getFilesystemBrowseTabCompletion(
117 [entry("OpenSourced"), entry("OpenAI"), entry("reopen"), entry("photos")],
118 "op",
119 ),
120 ).toEqual({ name: "Open", isComplete: false });
121 expect(getFilesystemBrowseTabCompletion([entry("t3code")], "tcd")).toBeNull();
122 });
123
124 it("matches folder names stored decomposed, as macOS Finder names them", () => {
125 const entry = { name: "De\u0301veloppement", fullPath: "/Users/me/De\u0301veloppement" };
126 expect(filterFilesystemBrowseEntries([entry], "d\u00e9v")).toEqual({
127 visibleEntries: [entry],
128 exactEntry: null,
129 });
130 expect(filterFilesystemBrowseEntries([entry], "De").visibleEntries).toEqual([entry]);
131 expect(filterFilesystemBrowseEntries([entry], "dévpt").visibleEntries).toEqual([entry]);
132 expect(filterFilesystemBrowseEntries([entry], "D\u00e9veloppement").exactEntry).toBe(entry);
133 // Linux keeps both spellings as separate folders; the one typed exactly wins.
134 const composed = { name: "D\u00e9veloppement", fullPath: "/home/me/D\u00e9veloppement" };
135 expect(filterFilesystemBrowseEntries([entry, composed], "D\u00e9veloppement").exactEntry).toBe(
136 composed,
137 );
138 // Two spellings that both normalize to the query are ambiguous; neither is picked.
139 const angstroms = [
140 { name: "\u212B", fullPath: "/home/me/\u212B" },
141 { name: "A\u030A", fullPath: "/home/me/A\u030A" },
142 ];
143 expect(filterFilesystemBrowseEntries(angstroms, "\u00C5").exactEntry).toBeNull();
144 });
145 });
146
147 describe("browse navigation", () => {
148 it("only commits the latest valid navigation", async () => {
149 const navigation = createBrowseNavigationCoordinator();
150 const first = Promise.withResolvers<void>();
151 const second = Promise.withResolvers<void>();
152 const commits: string[] = [];
153 const commit = (name: string) => () => commits.push(name);
154 const firstRun = navigation.run(() => first.promise, commit("first"));
155 const secondRun = navigation.run(() => second.promise, commit("second"));
156
157 second.resolve();
158 await expect(secondRun).resolves.toBe(true);
159 first.resolve();
160 await expect(firstRun).resolves.toBe(false);
161
162 const invalidated = Promise.withResolvers<void>();
163 const invalidatedRun = navigation.run(() => invalidated.promise, commit("stale"));
164 navigation.invalidate();
165 invalidated.resolve();
166
167 await expect(invalidatedRun).resolves.toBe(false);
168 expect(commits).toEqual(["second"]);
169 });
170 });
171
172 describe("browse entry filtering", () => {
173 const entries = ["my-t3code", "t3code", "cotton", "T3Code-old", ".t3", "docs"].map((name) => ({
174 name,
175 fullPath: `~/code/${name}`,
176 }));
177 const names = (query: string) =>
178 filterFilesystemBrowseEntries(entries, query).visibleEntries.map((entry) => entry.name);
179
180 it("keeps every visible entry in order for an empty query", () => {
181 expect(names("")).toEqual(["my-t3code", "t3code", "cotton", "T3Code-old", "docs"]);
182 });
183
184 it("matches substrings and subsequences, best match first", () => {
185 expect(names("code")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
186 expect(names("t3c")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
187 expect(names("tcd")).toEqual(["t3code", "T3Code-old", "my-t3code"]);
188 expect(names("xyz")).toEqual([]);
189 });
190
191 it("ranks a word-start match in a long name above any substring match", () => {
192 const longName = `${"a".repeat(240)}-foo`;
193 const folders = [
194 { name: "xfoo", fullPath: "~/xfoo" },
195 { name: longName, fullPath: "~/long" },
196 ];
197 expect(
198 filterFilesystemBrowseEntries(folders, "foo").visibleEntries.map((entry) => entry.name),
199 ).toEqual([longName, "xfoo"]);
200 });
201
202 it("shows dot folders only when the query starts with a dot", () => {
203 expect(names("t3")).not.toContain(".t3");
204 expect(filterFilesystemBrowseEntries(entries, ".t3")).toEqual({
205 visibleEntries: [entries[4]],
206 exactEntry: entries[4],
207 });
208 });
209 });