Open History to see the last commit
1import { it as effectIt } from "@effect/vitest";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import { parseKeybindingShortcut } from "@t3tools/shared/keybindings";
4import * as Effect from "effect/Effect";
5import { describe, expect, it } from "vite-plus/test";
6
7import {
8 accessibleWindowElementTree,
9 accessibleWindowText,
10 compactAccessibilityTree,
11 findAccessibleWindow,
12 isWaylandSession,
13 toElectronAccelerator,
14} from "./snapShot.ts";
15
16describe("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
44describe("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 it("stops descending a cyclic tree", async () => {
229 const group = {
230 role: "group",
231 name: "Loop",
232 bounds: null,
233 children: async () => [group],
234 };
235 const tree = await accessibleWindowElementTree(
236 { role: "window", name: "Inbox", bounds: null, children: async () => [group] },
237 { x: 0, y: 0, width: 800, height: 600 },
238 { width: 800, height: 600 },
239 );
240
241 expect(tree?.truncated).toBe(true);
242 expect(tree?.root.name).toBe("Inbox");
243 });
244});
245
246describe("compactAccessibilityTree", () => {
247 it("keeps anonymous groups that organize multiple semantic children", () => {
248 const compacted = compactAccessibilityTree({
249 role: "window",
250 bounds: { x: 0, y: 0, width: 800, height: 600 },
251 children: [
252 {
253 role: "group",
254 bounds: { x: 0, y: 0, width: 800, height: 100 },
255 children: [
256 { role: "button", name: "Back", bounds: null, children: [] },
257 { role: "button", name: "Forward", bounds: null, children: [] },
258 ],
259 },
260 ],
261 });
262
263 expect(compacted.root.children[0]?.role).toBe("group");
264 expect(compacted.root.children[0]?.children).toHaveLength(2);
265 });
266});
267
268describe("findAccessibleWindow", () => {
269 const captured = {
270 title: "Editor",
271 bounds: { x: 100, y: 200, width: 800, height: 600 },
272 };
273
274 const chromeCapture = {
275 title: "Issues · pingdotgg/t3code",
276 bounds: captured.bounds,
277 platform: "darwin" as const,
278 bundleId: "com.google.Chrome",
279 };
280 const chromeWindow = {
281 name: `${chromeCapture.title} - Google Chrome – Profile`,
282 bounds: chromeCapture.bounds,
283 active: true,
284 };
285
286 it("uses a browser suffix only when no exact title matches", () => {
287 const exact = { name: chromeCapture.title, bounds: chromeCapture.bounds };
288 expect(findAccessibleWindow([chromeWindow, exact], chromeCapture)).toBe(exact);
289 expect(findAccessibleWindow([chromeWindow], chromeCapture)).toBe(chromeWindow);
290 });
291
292 it("rejects browser suffix matches with wrong bounds, page titles, or ambiguous windows", () => {
293 expect(
294 findAccessibleWindow(
295 [{ ...chromeWindow, bounds: { ...chromeWindow.bounds, x: 0 } }],
296 chromeCapture,
297 ),
298 ).toBeUndefined();
299 expect(
300 findAccessibleWindow([chromeWindow, { ...chromeWindow, active: false }], chromeCapture),
301 ).toBeUndefined();
302 expect(
303 findAccessibleWindow([chromeWindow], { ...chromeCapture, title: "Issues" }),
304 ).toBeUndefined();
305 expect(
306 findAccessibleWindow([chromeWindow], {
307 ...chromeCapture,
308 title: `${chromeCapture.title} - Google Chrome`,
309 }),
310 ).toBeUndefined();
311 });
312
313 it("keeps strict titles outside macOS Chrome and Chromium", () => {
314 for (const identity of [
315 { platform: "linux" as const, bundleId: "com.google.Chrome" },
316 { platform: "win32" as const, bundleId: "com.google.Chrome" },
317 { platform: "darwin" as const, bundleId: "com.example.Editor" },
318 { platform: "darwin" as const, bundleId: undefined },
319 ]) {
320 expect(
321 findAccessibleWindow([chromeWindow], { ...chromeCapture, ...identity }),
322 ).toBeUndefined();
323 }
324 });
325
326 it("matches one window by its captured bounds", () => {
327 const windows = [
328 { name: "Private", bounds: { x: 0, y: 0, width: 400, height: 300 } },
329 { name: "Editor", bounds: { x: 101, y: 199, width: 800, height: 601 } },
330 ];
331
332 expect(findAccessibleWindow(windows, captured)).toBe(windows[1]);
333 });
334
335 it("uses the captured source title when the active title changed", () => {
336 const windows = [{ name: "Editor — saved", bounds: captured.bounds }];
337
338 expect(
339 findAccessibleWindow(windows, {
340 ...captured,
341 title: "Editor — saving",
342 sourceTitle: "Editor — saved",
343 }),
344 ).toBe(windows[0]);
345 });
346
347 it("uses the active match to disambiguate equal windows", () => {
348 const windows = [
349 { name: "Editor", bounds: captured.bounds, active: false },
350 { name: "Editor", bounds: captured.bounds, active: true },
351 ];
352
353 expect(findAccessibleWindow(windows, captured)).toBe(windows[1]);
354 });
355
356 it("does not match equal bounds with a different title", () => {
357 expect(
358 findAccessibleWindow(
359 [{ name: "Private", bounds: { x: 100, y: 200, width: 800, height: 600 } }],
360 captured,
361 ),
362 ).toBeUndefined();
363 });
364
365 it("matches a Wayland window whose accessibility provider omits its screen position", () => {
366 const captured = {
367 title: "hello world (Draft) - Text Editor",
368 bounds: { x: 479, y: 342, width: 700, height: 520 },
369 };
370 const windows = [{ name: captured.title, bounds: { x: 0, y: 0, width: 700, height: 520 } }];
371
372 expect(findAccessibleWindow(windows, captured, "wayland")).toBe(windows[0]);
373 expect(findAccessibleWindow(windows, captured)).toBeUndefined();
374 });
375
376 it("matches a decorated Wayland window by its verified client size", () => {
377 const clientBounds = { x: 100, y: 229, width: 800, height: 571 };
378 const windows = [{ name: captured.title, bounds: { ...clientBounds, x: 0, y: 0 } }];
379 expect(findAccessibleWindow(windows, { ...captured, clientBounds }, "wayland")).toBe(
380 windows[0],
381 );
382 expect(findAccessibleWindow(windows, { ...captured, clientBounds })).toBeUndefined();
383 expect(findAccessibleWindow(windows, captured, "wayland")).toBeUndefined();
384 });
385
386 it("does not guess between client-size and frame-size matches", () => {
387 const clientBounds = { x: 100, y: 229, width: 800, height: 571 };
388 const windows = [
389 { name: captured.title, bounds: clientBounds },
390 { name: captured.title, bounds: captured.bounds },
391 ];
392 expect(findAccessibleWindow(windows, { ...captured, clientBounds }, "wayland")).toBeUndefined();
393 expect(
394 findAccessibleWindow(
395 [
396 { name: "Private", bounds: clientBounds },
397 { name: captured.title, bounds: { ...clientBounds, height: 550 } },
398 ],
399 { ...captured, clientBounds },
400 "wayland",
401 ),
402 ).toBeUndefined();
403 });
404
405 it("distinguishes same-title Wayland windows by size", () => {
406 const windows = [
407 { name: "Editor", bounds: { x: 0, y: 0, width: 400, height: 300 } },
408 { name: "Editor", bounds: { x: 0, y: 0, width: 801, height: 599 } },
409 ];
410
411 expect(findAccessibleWindow(windows, captured, "wayland")).toBe(windows[1]);
412 });
413
414 it("does not match an untitled Wayland window by size alone", () => {
415 const windows = [{ name: "", bounds: captured.bounds }];
416
417 expect(findAccessibleWindow(windows, { ...captured, title: "" }, "wayland")).toBeUndefined();
418 });
419
420 it("accepts one PID-scoped untitled window whose bounds match", () => {
421 const windows = [{ name: null, bounds: captured.bounds }];
422
423 expect(
424 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
425 ).toBe(windows[0]);
426 expect(findAccessibleWindow(windows, captured, "wayland")).toBeUndefined();
427 });
428
429 it("does not fall back to a differently titled PID-scoped window with matching bounds", () => {
430 const windows = [{ name: "Preferences", bounds: captured.bounds }];
431
432 expect(
433 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
434 ).toBeUndefined();
435 });
436
437 it("does not use unique bounds when a titled match is already ambiguous", () => {
438 const windows = [
439 { name: "Editor", bounds: captured.bounds },
440 { name: "Editor", bounds: { ...captured.bounds, x: 0, y: 0 } },
441 ];
442
443 expect(
444 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
445 ).toBeUndefined();
446 });
447
448 it.each([
449 ["⠋ t3code", "t3code"],
450 ["t3code", "⠙ t3code"],
451 ])("matches a Wayland spinner starting or stopping: %s → %s", (title, name) => {
452 const windows = [{ name, bounds: captured.bounds }];
453
454 expect(findAccessibleWindow(windows, { ...captured, title }, "wayland")).toBe(windows[0]);
455 });
456
457 it.each([
458 ["⠋ t3code", "⠙ private"],
459 ["t3code ⠋", "t3code ⠙"],
460 ["⠋t3code", "⠙t3code"],
461 ["⠁ t3code", "⠙ t3code"],
462 ["⠋", "⠋"],
463 ])("does not guess a Wayland title match: %s → %s", (title, name) => {
464 expect(
465 findAccessibleWindow([{ name, bounds: captured.bounds }], { ...captured, title }, "wayland"),
466 ).toBeUndefined();
467 });
468});
469
470describe("toElectronAccelerator", () => {
471 // The settings recorder stores shortcuts through parseKeybindingShortcut, which lowercases keys.
472 it.each([
473 ["mod+shift+arrowup", "CommandOrControl+Shift+Up"],
474 ["mod+shift+arrowdown", "CommandOrControl+Shift+Down"],
475 ["ctrl+alt+arrowleft", "Control+Alt+Left"],
476 ["ctrl+alt+arrowright", "Control+Alt+Right"],
477 ["mod+shift+esc", "CommandOrControl+Shift+Esc"],
478 ["mod+shift+space", "CommandOrControl+Shift+Space"],
479 ["mod+shift+2", "CommandOrControl+Shift+2"],
480 ])("maps a recorded %s to an Electron accelerator", (recorded, accelerator) => {
481 expect(toElectronAccelerator(parseKeybindingShortcut(recorded)!)).toBe(accelerator);
482 });
483});
484
485describe("isWaylandSession", () => {
486 effectIt.effect(
487 "falls back to a live runtime directory socket when session variables are stripped",
488 () =>
489 Effect.gen(function* () {
490 if ((yield* HostProcess.Platform) !== "linux") return;
491 yield* Effect.promise(async () => {
492 const { mkdtemp, rm, writeFile } = await import("node:fs/promises");
493 const { createServer } = await import("node:net");
494 const { tmpdir } = await import("node:os");
495 const { join } = await import("node:path");
496 const runtimeDirectory = await mkdtemp(join(tmpdir(), "t3-wayland-"));
497 const socketPath = join(runtimeDirectory, "wayland-0");
498 const server = createServer();
499 try {
500 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(false);
501 await writeFile(socketPath, "");
502 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(false);
503 await rm(socketPath);
504 await new Promise<void>((resolve, reject) => {
505 server.once("error", reject);
506 server.listen(socketPath, resolve);
507 });
508 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(true);
509 expect(
510 isWaylandSession("linux", {
511 XDG_RUNTIME_DIR: runtimeDirectory,
512 XDG_SESSION_TYPE: "x11",
513 }),
514 ).toBe(false);
515 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: "/nonexistent-t3-test" })).toBe(
516 false,
517 );
518 } finally {
519 if (server.listening) {
520 await new Promise<void>((resolve, reject) => {
521 server.close((error) => (error ? reject(error) : resolve()));
522 });
523 }
524 await rm(runtimeDirectory, { recursive: true, force: true });
525 }
526 });
527 }),
528 );
529});