@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 512 lines 17 KB
Raw
1 import { it as effectIt } from "@effect/vitest";
2 import * as HostProcess from "@t3tools/shared/HostProcess";
3 import { parseKeybindingShortcut } from "@t3tools/shared/keybindings";
4 import * as Effect from "effect/Effect";
5 import { describe, expect, it } from "vite-plus/test";
6
7 import {
8 accessibleWindowElementTree,
9 accessibleWindowText,
10 compactAccessibilityTree,
11 findAccessibleWindow,
12 isWaylandSession,
13 toElectronAccelerator,
14 } from "./snapShot.ts";
15
16 describe("accessibleWindowText", () => {
17 it("caps large text without splitting a surrogate pair", () => {
18 expect(
19 accessibleWindowText(
20 {
21 value: "abc😀def",
22 children: [],
23 },
24 5,
25 ),
26 ).toBe("abc😀");
27 });
28
29 it("stops traversing very large trees", () => {
30 expect(
31 accessibleWindowText(
32 {
33 children: [
34 ...Array.from({ length: 10_000 }, () => ({ children: [] })),
35 { value: "past node limit", children: [] },
36 ],
37 },
38 100,
39 ),
40 ).not.toContain("past node limit");
41 });
42 });
43
44 describe("accessibleWindowElementTree", () => {
45 it("maps element bounds into captured-image pixels and keeps semantic state", async () => {
46 const tree = await accessibleWindowElementTree(
47 {
48 role: "window",
49 name: "Editor",
50 bounds: { x: 100, y: 200, width: 800, height: 600 },
51 active: true,
52 children: async () => [
53 {
54 role: "button",
55 name: "Save",
56 description: "Save the document",
57 bounds: { x: 300, y: 350, width: 100, height: 50 },
58 focused: true,
59 actions: ["press", "press"],
60 children: async () => [],
61 },
62 ],
63 },
64 { x: 100, y: 200, width: 800, height: 600 },
65 { width: 1_600, height: 1_200 },
66 );
67
68 expect(tree).toEqual({
69 truncated: false,
70 root: {
71 role: "window",
72 name: "Editor",
73 bounds: { x: 0, y: 0, width: 1_600, height: 1_200 },
74 state: { active: true },
75 children: [
76 {
77 role: "button",
78 name: "Save",
79 description: "Save the document",
80 bounds: { x: 400, y: 300, width: 200, height: 100 },
81 state: { focused: true },
82 actions: ["press"],
83 children: [],
84 },
85 ],
86 },
87 });
88 });
89
90 it("uses null child bounds when a Wayland provider reports only the root origin", async () => {
91 const tree = await accessibleWindowElementTree(
92 {
93 role: "window",
94 bounds: { x: 0, y: 0, width: 800, height: 600 },
95 children: async () => [
96 {
97 role: "button",
98 name: "Save",
99 bounds: { x: 0, y: 0, width: 100, height: 50 },
100 children: async () => [],
101 },
102 ],
103 },
104 { x: 0, y: 0, width: 800, height: 600 },
105 { width: 1_600, height: 1_200 },
106 { locationsReliable: true, verifyDescendantLocations: true },
107 );
108
109 expect(tree?.root.bounds).toEqual({ x: 0, y: 0, width: 1_600, height: 1_200 });
110 expect(tree?.root.children[0]?.bounds).toBeNull();
111 });
112
113 it("keeps Wayland child bounds after observing real position variation", async () => {
114 const tree = await accessibleWindowElementTree(
115 {
116 role: "window",
117 bounds: { x: 0, y: 0, width: 800, height: 600 },
118 children: async () => [
119 {
120 role: "button",
121 name: "Save",
122 bounds: { x: 20, y: 30, width: 100, height: 50 },
123 children: async () => [],
124 },
125 ],
126 },
127 { x: 0, y: 0, width: 800, height: 600 },
128 { width: 1_600, height: 1_200 },
129 { locationsReliable: true, verifyDescendantLocations: true },
130 );
131
132 expect(tree?.root.children[0]?.bounds).toEqual({ x: 40, y: 60, width: 200, height: 100 });
133 });
134
135 it("does not mistake a title-bar offset for varying descendant positions", async () => {
136 const tree = await accessibleWindowElementTree(
137 {
138 role: "window",
139 bounds: { x: 100, y: 229, width: 800, height: 571 },
140 children: async () => [
141 {
142 role: "button",
143 name: "Save",
144 bounds: { x: 100, y: 229, width: 100, height: 50 },
145 children: async () => [],
146 },
147 ],
148 },
149 { x: 100, y: 200, width: 800, height: 600 },
150 { width: 1_600, height: 1_200 },
151 { locationsReliable: true, verifyDescendantLocations: true },
152 );
153 expect(tree?.root.children[0]?.bounds).toBeNull();
154 });
155
156 it("collapses anonymous wrapper chains and removes duplicate text fields", async () => {
157 const tree = await accessibleWindowElementTree(
158 {
159 role: "window",
160 name: "Editor",
161 bounds: { x: 0, y: 0, width: 800, height: 600 },
162 children: async () => [
163 {
164 role: "group",
165 bounds: { x: 0, y: 0, width: 800, height: 600 },
166 children: async () => [
167 {
168 role: "group",
169 bounds: { x: 0, y: 0, width: 800, height: 600 },
170 children: async () => [
171 {
172 role: "button",
173 name: "Save",
174 value: "Save",
175 description: "Save",
176 bounds: { x: 20, y: 30, width: 80, height: 24 },
177 actions: ["press"],
178 children: async () => [],
179 },
180 {
181 role: "group",
182 bounds: null,
183 children: async () => [],
184 },
185 ],
186 },
187 ],
188 },
189 ],
190 },
191 { x: 0, y: 0, width: 800, height: 600 },
192 { width: 800, height: 600 },
193 );
194
195 expect(tree?.root.children).toEqual([
196 {
197 role: "button",
198 name: "Save",
199 bounds: { x: 20, y: 30, width: 80, height: 24 },
200 actions: ["press"],
201 children: [],
202 },
203 ]);
204 });
205
206 it("marks trees truncated before their serialized payload grows without bound", async () => {
207 const tree = await accessibleWindowElementTree(
208 {
209 role: "window",
210 bounds: { x: 0, y: 0, width: 800, height: 600 },
211 children: async () =>
212 Array.from({ length: 10 }, (_, index) => ({
213 role: "text",
214 value: `${index}${"x".repeat(8_000)}`,
215 bounds: null,
216 children: async () => [],
217 })),
218 },
219 { x: 0, y: 0, width: 800, height: 600 },
220 { width: 800, height: 600 },
221 );
222
223 expect(tree?.truncated).toBe(true);
224 expect(tree?.root.children.length).toBeLessThan(10);
225 expect(JSON.stringify(tree).length).toBeLessThanOrEqual(32_000);
226 });
227 });
228
229 describe("compactAccessibilityTree", () => {
230 it("keeps anonymous groups that organize multiple semantic children", () => {
231 const compacted = compactAccessibilityTree({
232 role: "window",
233 bounds: { x: 0, y: 0, width: 800, height: 600 },
234 children: [
235 {
236 role: "group",
237 bounds: { x: 0, y: 0, width: 800, height: 100 },
238 children: [
239 { role: "button", name: "Back", bounds: null, children: [] },
240 { role: "button", name: "Forward", bounds: null, children: [] },
241 ],
242 },
243 ],
244 });
245
246 expect(compacted.root.children[0]?.role).toBe("group");
247 expect(compacted.root.children[0]?.children).toHaveLength(2);
248 });
249 });
250
251 describe("findAccessibleWindow", () => {
252 const captured = {
253 title: "Editor",
254 bounds: { x: 100, y: 200, width: 800, height: 600 },
255 };
256
257 const chromeCapture = {
258 title: "Issues · pingdotgg/t3code",
259 bounds: captured.bounds,
260 platform: "darwin" as const,
261 bundleId: "com.google.Chrome",
262 };
263 const chromeWindow = {
264 name: `${chromeCapture.title} - Google Chrome – Profile`,
265 bounds: chromeCapture.bounds,
266 active: true,
267 };
268
269 it("uses a browser suffix only when no exact title matches", () => {
270 const exact = { name: chromeCapture.title, bounds: chromeCapture.bounds };
271 expect(findAccessibleWindow([chromeWindow, exact], chromeCapture)).toBe(exact);
272 expect(findAccessibleWindow([chromeWindow], chromeCapture)).toBe(chromeWindow);
273 });
274
275 it("rejects browser suffix matches with wrong bounds, page titles, or ambiguous windows", () => {
276 expect(
277 findAccessibleWindow(
278 [{ ...chromeWindow, bounds: { ...chromeWindow.bounds, x: 0 } }],
279 chromeCapture,
280 ),
281 ).toBeUndefined();
282 expect(
283 findAccessibleWindow([chromeWindow, { ...chromeWindow, active: false }], chromeCapture),
284 ).toBeUndefined();
285 expect(
286 findAccessibleWindow([chromeWindow], { ...chromeCapture, title: "Issues" }),
287 ).toBeUndefined();
288 expect(
289 findAccessibleWindow([chromeWindow], {
290 ...chromeCapture,
291 title: `${chromeCapture.title} - Google Chrome`,
292 }),
293 ).toBeUndefined();
294 });
295
296 it("keeps strict titles outside macOS Chrome and Chromium", () => {
297 for (const identity of [
298 { platform: "linux" as const, bundleId: "com.google.Chrome" },
299 { platform: "win32" as const, bundleId: "com.google.Chrome" },
300 { platform: "darwin" as const, bundleId: "com.example.Editor" },
301 { platform: "darwin" as const, bundleId: undefined },
302 ]) {
303 expect(
304 findAccessibleWindow([chromeWindow], { ...chromeCapture, ...identity }),
305 ).toBeUndefined();
306 }
307 });
308
309 it("matches one window by its captured bounds", () => {
310 const windows = [
311 { name: "Private", bounds: { x: 0, y: 0, width: 400, height: 300 } },
312 { name: "Editor", bounds: { x: 101, y: 199, width: 800, height: 601 } },
313 ];
314
315 expect(findAccessibleWindow(windows, captured)).toBe(windows[1]);
316 });
317
318 it("uses the captured source title when the active title changed", () => {
319 const windows = [{ name: "Editor — saved", bounds: captured.bounds }];
320
321 expect(
322 findAccessibleWindow(windows, {
323 ...captured,
324 title: "Editor — saving",
325 sourceTitle: "Editor — saved",
326 }),
327 ).toBe(windows[0]);
328 });
329
330 it("uses the active match to disambiguate equal windows", () => {
331 const windows = [
332 { name: "Editor", bounds: captured.bounds, active: false },
333 { name: "Editor", bounds: captured.bounds, active: true },
334 ];
335
336 expect(findAccessibleWindow(windows, captured)).toBe(windows[1]);
337 });
338
339 it("does not match equal bounds with a different title", () => {
340 expect(
341 findAccessibleWindow(
342 [{ name: "Private", bounds: { x: 100, y: 200, width: 800, height: 600 } }],
343 captured,
344 ),
345 ).toBeUndefined();
346 });
347
348 it("matches a Wayland window whose accessibility provider omits its screen position", () => {
349 const captured = {
350 title: "hello world (Draft) - Text Editor",
351 bounds: { x: 479, y: 342, width: 700, height: 520 },
352 };
353 const windows = [{ name: captured.title, bounds: { x: 0, y: 0, width: 700, height: 520 } }];
354
355 expect(findAccessibleWindow(windows, captured, "wayland")).toBe(windows[0]);
356 expect(findAccessibleWindow(windows, captured)).toBeUndefined();
357 });
358
359 it("matches a decorated Wayland window by its verified client size", () => {
360 const clientBounds = { x: 100, y: 229, width: 800, height: 571 };
361 const windows = [{ name: captured.title, bounds: { ...clientBounds, x: 0, y: 0 } }];
362 expect(findAccessibleWindow(windows, { ...captured, clientBounds }, "wayland")).toBe(
363 windows[0],
364 );
365 expect(findAccessibleWindow(windows, { ...captured, clientBounds })).toBeUndefined();
366 expect(findAccessibleWindow(windows, captured, "wayland")).toBeUndefined();
367 });
368
369 it("does not guess between client-size and frame-size matches", () => {
370 const clientBounds = { x: 100, y: 229, width: 800, height: 571 };
371 const windows = [
372 { name: captured.title, bounds: clientBounds },
373 { name: captured.title, bounds: captured.bounds },
374 ];
375 expect(findAccessibleWindow(windows, { ...captured, clientBounds }, "wayland")).toBeUndefined();
376 expect(
377 findAccessibleWindow(
378 [
379 { name: "Private", bounds: clientBounds },
380 { name: captured.title, bounds: { ...clientBounds, height: 550 } },
381 ],
382 { ...captured, clientBounds },
383 "wayland",
384 ),
385 ).toBeUndefined();
386 });
387
388 it("distinguishes same-title Wayland windows by size", () => {
389 const windows = [
390 { name: "Editor", bounds: { x: 0, y: 0, width: 400, height: 300 } },
391 { name: "Editor", bounds: { x: 0, y: 0, width: 801, height: 599 } },
392 ];
393
394 expect(findAccessibleWindow(windows, captured, "wayland")).toBe(windows[1]);
395 });
396
397 it("does not match an untitled Wayland window by size alone", () => {
398 const windows = [{ name: "", bounds: captured.bounds }];
399
400 expect(findAccessibleWindow(windows, { ...captured, title: "" }, "wayland")).toBeUndefined();
401 });
402
403 it("accepts one PID-scoped untitled window whose bounds match", () => {
404 const windows = [{ name: null, bounds: captured.bounds }];
405
406 expect(
407 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
408 ).toBe(windows[0]);
409 expect(findAccessibleWindow(windows, captured, "wayland")).toBeUndefined();
410 });
411
412 it("does not fall back to a differently titled PID-scoped window with matching bounds", () => {
413 const windows = [{ name: "Preferences", bounds: captured.bounds }];
414
415 expect(
416 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
417 ).toBeUndefined();
418 });
419
420 it("does not use unique bounds when a titled match is already ambiguous", () => {
421 const windows = [
422 { name: "Editor", bounds: captured.bounds },
423 { name: "Editor", bounds: { ...captured.bounds, x: 0, y: 0 } },
424 ];
425
426 expect(
427 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
428 ).toBeUndefined();
429 });
430
431 it.each([
432 ["⠋ t3code", "t3code"],
433 ["t3code", "⠙ t3code"],
434 ])("matches a Wayland spinner starting or stopping: %s → %s", (title, name) => {
435 const windows = [{ name, bounds: captured.bounds }];
436
437 expect(findAccessibleWindow(windows, { ...captured, title }, "wayland")).toBe(windows[0]);
438 });
439
440 it.each([
441 ["⠋ t3code", "⠙ private"],
442 ["t3code ⠋", "t3code ⠙"],
443 ["⠋t3code", "⠙t3code"],
444 ["⠁ t3code", "⠙ t3code"],
445 ["⠋", "⠋"],
446 ])("does not guess a Wayland title match: %s → %s", (title, name) => {
447 expect(
448 findAccessibleWindow([{ name, bounds: captured.bounds }], { ...captured, title }, "wayland"),
449 ).toBeUndefined();
450 });
451 });
452
453 describe("toElectronAccelerator", () => {
454 // The settings recorder stores shortcuts through parseKeybindingShortcut, which lowercases keys.
455 it.each([
456 ["mod+shift+arrowup", "CommandOrControl+Shift+Up"],
457 ["mod+shift+arrowdown", "CommandOrControl+Shift+Down"],
458 ["ctrl+alt+arrowleft", "Control+Alt+Left"],
459 ["ctrl+alt+arrowright", "Control+Alt+Right"],
460 ["mod+shift+esc", "CommandOrControl+Shift+Esc"],
461 ["mod+shift+space", "CommandOrControl+Shift+Space"],
462 ["mod+shift+2", "CommandOrControl+Shift+2"],
463 ])("maps a recorded %s to an Electron accelerator", (recorded, accelerator) => {
464 expect(toElectronAccelerator(parseKeybindingShortcut(recorded)!)).toBe(accelerator);
465 });
466 });
467
468 describe("isWaylandSession", () => {
469 effectIt.effect(
470 "falls back to a live runtime directory socket when session variables are stripped",
471 () =>
472 Effect.gen(function* () {
473 if ((yield* HostProcess.Platform) !== "linux") return;
474 yield* Effect.promise(async () => {
475 const { mkdtemp, rm, writeFile } = await import("node:fs/promises");
476 const { createServer } = await import("node:net");
477 const { tmpdir } = await import("node:os");
478 const { join } = await import("node:path");
479 const runtimeDirectory = await mkdtemp(join(tmpdir(), "t3-wayland-"));
480 const socketPath = join(runtimeDirectory, "wayland-0");
481 const server = createServer();
482 try {
483 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(false);
484 await writeFile(socketPath, "");
485 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(false);
486 await rm(socketPath);
487 await new Promise<void>((resolve, reject) => {
488 server.once("error", reject);
489 server.listen(socketPath, resolve);
490 });
491 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(true);
492 expect(
493 isWaylandSession("linux", {
494 XDG_RUNTIME_DIR: runtimeDirectory,
495 XDG_SESSION_TYPE: "x11",
496 }),
497 ).toBe(false);
498 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: "/nonexistent-t3-test" })).toBe(
499 false,
500 );
501 } finally {
502 if (server.listening) {
503 await new Promise<void>((resolve, reject) => {
504 server.close((error) => (error ? reject(error) : resolve()));
505 });
506 }
507 await rm(runtimeDirectory, { recursive: true, force: true });
508 }
509 });
510 }),
511 );
512 });