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