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, vi } from "vite-plus/test";
5
6import {
7 accessibleWindowElementTree,
8 accessibleWindowText,
9 capturedImageBounds,
10 compactAccessibilityTree,
11 findAccessibleWindow,
12 findCaptureSource,
13 hideAndWaitForBlur,
14 isWaylandSession,
15 snapShotShortcutRegistrationFailureMessage,
16 snapShotShortcutSystemConflict,
17 toElectronAccelerator,
18} from "./snapShot.ts";
19import { DesktopSnapShotError } from "./DesktopSnapShot.ts";
20
21describe("window capture errors", () => {
22 it.each([
23 ["unsupported", "SnapShots are not supported here."],
24 ["disabled", "Enable SnapShots in Settings first."],
25 ["no-window-selected", "No window was selected."],
26 ["window-unavailable", "The active window is not available for capture."],
27 ["capture", "Could not capture the active window."],
28 ] as const)("keeps %s failures user-facing", (operation, message) => {
29 expect(new DesktopSnapShotError({ operation }).message).toBe(message);
30 });
31});
32
33describe("accessibleWindowText", () => {
34 it("keeps unique names and values in tree order", () => {
35 expect(
36 accessibleWindowText(
37 {
38 name: "Settings",
39 children: [
40 {
41 name: "General",
42 children: [],
43 },
44 {
45 name: "Name",
46 value: "Bilal",
47 children: [],
48 },
49 ],
50 },
51 100,
52 ),
53 ).toBe("Settings\nGeneral\nName\nBilal");
54 });
55
56 it("caps large text without splitting a surrogate pair", () => {
57 expect(
58 accessibleWindowText(
59 {
60 value: "abc😀def",
61 children: [],
62 },
63 5,
64 ),
65 ).toBe("abc😀");
66 });
67
68 it("stops traversing very large trees", () => {
69 expect(
70 accessibleWindowText(
71 {
72 children: [
73 ...Array.from({ length: 10_000 }, () => ({ children: [] })),
74 { value: "past node limit", children: [] },
75 ],
76 },
77 100,
78 ),
79 ).not.toContain("past node limit");
80 });
81});
82
83describe("accessibleWindowElementTree", () => {
84 it("maps element bounds into captured-image pixels and keeps semantic state", async () => {
85 const tree = await accessibleWindowElementTree(
86 {
87 role: "window",
88 name: "Editor",
89 bounds: { x: 100, y: 200, width: 800, height: 600 },
90 active: true,
91 children: async () => [
92 {
93 role: "button",
94 name: "Save",
95 description: "Save the document",
96 bounds: { x: 300, y: 350, width: 100, height: 50 },
97 focused: true,
98 actions: ["press", "press"],
99 children: async () => [],
100 },
101 ],
102 },
103 { x: 100, y: 200, width: 800, height: 600 },
104 { width: 1_600, height: 1_200 },
105 );
106
107 expect(tree).toEqual({
108 truncated: false,
109 root: {
110 role: "window",
111 name: "Editor",
112 bounds: { x: 0, y: 0, width: 1_600, height: 1_200 },
113 state: { active: true },
114 children: [
115 {
116 role: "button",
117 name: "Save",
118 description: "Save the document",
119 bounds: { x: 400, y: 300, width: 200, height: 100 },
120 state: { focused: true },
121 actions: ["press"],
122 children: [],
123 },
124 ],
125 },
126 });
127 });
128
129 it("uses null child bounds when a Wayland provider reports only the root origin", async () => {
130 const tree = await accessibleWindowElementTree(
131 {
132 role: "window",
133 bounds: { x: 0, y: 0, width: 800, height: 600 },
134 children: async () => [
135 {
136 role: "button",
137 name: "Save",
138 bounds: { x: 0, y: 0, width: 100, height: 50 },
139 children: async () => [],
140 },
141 ],
142 },
143 { x: 0, y: 0, width: 800, height: 600 },
144 { width: 1_600, height: 1_200 },
145 { locationsReliable: true, verifyDescendantLocations: true },
146 );
147
148 expect(tree?.root.bounds).toEqual({ x: 0, y: 0, width: 1_600, height: 1_200 });
149 expect(tree?.root.children[0]?.bounds).toBeNull();
150 });
151
152 it("keeps Wayland child bounds after observing real position variation", async () => {
153 const tree = await accessibleWindowElementTree(
154 {
155 role: "window",
156 bounds: { x: 0, y: 0, width: 800, height: 600 },
157 children: async () => [
158 {
159 role: "button",
160 name: "Save",
161 bounds: { x: 20, y: 30, width: 100, height: 50 },
162 children: async () => [],
163 },
164 ],
165 },
166 { x: 0, y: 0, width: 800, height: 600 },
167 { width: 1_600, height: 1_200 },
168 { locationsReliable: true, verifyDescendantLocations: true },
169 );
170
171 expect(tree?.root.children[0]?.bounds).toEqual({ x: 40, y: 60, width: 200, height: 100 });
172 });
173
174 it("does not mistake a title-bar offset for varying descendant positions", async () => {
175 const tree = await accessibleWindowElementTree(
176 {
177 role: "window",
178 bounds: { x: 100, y: 229, width: 800, height: 571 },
179 children: async () => [
180 {
181 role: "button",
182 name: "Save",
183 bounds: { x: 100, y: 229, width: 100, height: 50 },
184 children: async () => [],
185 },
186 ],
187 },
188 { x: 100, y: 200, width: 800, height: 600 },
189 { width: 1_600, height: 1_200 },
190 { locationsReliable: true, verifyDescendantLocations: true },
191 );
192 expect(tree?.root.children[0]?.bounds).toBeNull();
193 });
194
195 it("collapses anonymous wrapper chains and removes duplicate text fields", async () => {
196 const tree = await accessibleWindowElementTree(
197 {
198 role: "window",
199 name: "Editor",
200 bounds: { x: 0, y: 0, width: 800, height: 600 },
201 children: async () => [
202 {
203 role: "group",
204 bounds: { x: 0, y: 0, width: 800, height: 600 },
205 children: async () => [
206 {
207 role: "group",
208 bounds: { x: 0, y: 0, width: 800, height: 600 },
209 children: async () => [
210 {
211 role: "button",
212 name: "Save",
213 value: "Save",
214 description: "Save",
215 bounds: { x: 20, y: 30, width: 80, height: 24 },
216 actions: ["press"],
217 children: async () => [],
218 },
219 {
220 role: "group",
221 bounds: null,
222 children: async () => [],
223 },
224 ],
225 },
226 ],
227 },
228 ],
229 },
230 { x: 0, y: 0, width: 800, height: 600 },
231 { width: 800, height: 600 },
232 );
233
234 expect(tree?.root.children).toEqual([
235 {
236 role: "button",
237 name: "Save",
238 bounds: { x: 20, y: 30, width: 80, height: 24 },
239 actions: ["press"],
240 children: [],
241 },
242 ]);
243 });
244
245 it("marks trees truncated before their serialized payload grows without bound", async () => {
246 const tree = await accessibleWindowElementTree(
247 {
248 role: "window",
249 bounds: { x: 0, y: 0, width: 800, height: 600 },
250 children: async () =>
251 Array.from({ length: 10 }, (_, index) => ({
252 role: "text",
253 value: `${index}${"x".repeat(8_000)}`,
254 bounds: null,
255 children: async () => [],
256 })),
257 },
258 { x: 0, y: 0, width: 800, height: 600 },
259 { width: 800, height: 600 },
260 );
261
262 expect(tree?.truncated).toBe(true);
263 expect(tree?.root.children.length).toBeLessThan(10);
264 expect(JSON.stringify(tree).length).toBeLessThanOrEqual(32_000);
265 });
266
267 it.each(["name", "value", "description"] as const)(
268 "reports clipped %s text in partial trees",
269 async (field) => {
270 const tree = await accessibleWindowElementTree(
271 {
272 role: "window",
273 children: async () => [{ role: "text_area", [field]: "x".repeat(8_001) }],
274 },
275 { x: 0, y: 0, width: 800, height: 600 },
276 { width: 800, height: 600 },
277 );
278
279 expect(tree?.truncated).toBe(true);
280 expect(tree?.root.children[0]?.[field]?.length).toBeLessThan(8_001);
281 },
282 );
283});
284
285describe("compactAccessibilityTree", () => {
286 it("keeps anonymous groups that organize multiple semantic children", () => {
287 const compacted = compactAccessibilityTree({
288 role: "window",
289 bounds: { x: 0, y: 0, width: 800, height: 600 },
290 children: [
291 {
292 role: "group",
293 bounds: { x: 0, y: 0, width: 800, height: 100 },
294 children: [
295 { role: "button", name: "Back", bounds: null, children: [] },
296 { role: "button", name: "Forward", bounds: null, children: [] },
297 ],
298 },
299 ],
300 });
301
302 expect(compacted.root.children[0]?.role).toBe("group");
303 expect(compacted.root.children[0]?.children).toHaveLength(2);
304 });
305});
306
307describe("capturedImageBounds", () => {
308 it("clips locations to the attached image", () => {
309 expect(
310 capturedImageBounds(
311 { x: 50, y: 150, width: 200, height: 200 },
312 { x: 100, y: 200, width: 800, height: 600 },
313 { width: 1_600, height: 1_200 },
314 ),
315 ).toEqual({ x: 0, y: 0, width: 300, height: 300 });
316 });
317
318 it("returns null when an accessibility provider omits a location", () => {
319 expect(
320 capturedImageBounds(
321 null,
322 { x: 0, y: 0, width: 800, height: 600 },
323 { width: 800, height: 600 },
324 ),
325 ).toBeNull();
326 });
327});
328
329describe("findAccessibleWindow", () => {
330 const captured = {
331 title: "Editor",
332 bounds: { x: 100, y: 200, width: 800, height: 600 },
333 };
334
335 const chromeCapture = {
336 title: "Issues · pingdotgg/t3code",
337 bounds: captured.bounds,
338 platform: "darwin" as const,
339 bundleId: "com.google.Chrome",
340 };
341 const chromeWindow = {
342 name: `${chromeCapture.title} - Google Chrome – Profile`,
343 bounds: chromeCapture.bounds,
344 active: true,
345 };
346
347 it("uses a browser suffix only when no exact title matches", () => {
348 const exact = { name: chromeCapture.title, bounds: chromeCapture.bounds };
349 expect(findAccessibleWindow([chromeWindow, exact], chromeCapture)).toBe(exact);
350 expect(findAccessibleWindow([chromeWindow], chromeCapture)).toBe(chromeWindow);
351 });
352
353 it("rejects browser suffix matches with wrong bounds, page titles, or ambiguous windows", () => {
354 expect(
355 findAccessibleWindow(
356 [{ ...chromeWindow, bounds: { ...chromeWindow.bounds, x: 0 } }],
357 chromeCapture,
358 ),
359 ).toBeUndefined();
360 expect(
361 findAccessibleWindow([chromeWindow, { ...chromeWindow, active: false }], chromeCapture),
362 ).toBeUndefined();
363 expect(
364 findAccessibleWindow([chromeWindow], { ...chromeCapture, title: "Issues" }),
365 ).toBeUndefined();
366 expect(
367 findAccessibleWindow([chromeWindow], {
368 ...chromeCapture,
369 title: `${chromeCapture.title} - Google Chrome`,
370 }),
371 ).toBeUndefined();
372 });
373
374 it("keeps strict titles outside macOS Chrome and Chromium", () => {
375 for (const identity of [
376 { platform: "linux" as const, bundleId: "com.google.Chrome" },
377 { platform: "win32" as const, bundleId: "com.google.Chrome" },
378 { platform: "darwin" as const, bundleId: "com.example.Editor" },
379 { platform: "darwin" as const, bundleId: undefined },
380 ]) {
381 expect(
382 findAccessibleWindow([chromeWindow], { ...chromeCapture, ...identity }),
383 ).toBeUndefined();
384 }
385 });
386
387 it("matches one window by its captured bounds", () => {
388 const windows = [
389 { name: "Private", bounds: { x: 0, y: 0, width: 400, height: 300 } },
390 { name: "Editor", bounds: { x: 101, y: 199, width: 800, height: 601 } },
391 ];
392
393 expect(findAccessibleWindow(windows, captured)).toBe(windows[1]);
394 });
395
396 it("uses the matched source title when macOS omits the active title", () => {
397 const windows = [{ name: "Editor", bounds: captured.bounds }];
398 expect(
399 findAccessibleWindow(windows, {
400 ...captured,
401 title: "",
402 sourceTitle: "Editor",
403 }),
404 ).toBe(windows[0]);
405 });
406
407 it("uses the captured source title when the active title changed", () => {
408 const windows = [{ name: "Editor — saved", bounds: captured.bounds }];
409
410 expect(
411 findAccessibleWindow(windows, {
412 ...captured,
413 title: "Editor — saving",
414 sourceTitle: "Editor — saved",
415 }),
416 ).toBe(windows[0]);
417 });
418
419 it("uses the active match to disambiguate equal windows", () => {
420 const windows = [
421 { name: "Editor", bounds: captured.bounds, active: false },
422 { name: "Editor", bounds: captured.bounds, active: true },
423 ];
424
425 expect(findAccessibleWindow(windows, captured)).toBe(windows[1]);
426 });
427
428 it("does not match equal bounds with a different title", () => {
429 expect(
430 findAccessibleWindow(
431 [{ name: "Private", bounds: { x: 100, y: 200, width: 800, height: 600 } }],
432 captured,
433 ),
434 ).toBeUndefined();
435 });
436
437 it("does not use a title match when the bounds differ", () => {
438 expect(
439 findAccessibleWindow(
440 [{ name: "Editor", bounds: { x: 0, y: 0, width: 800, height: 600 } }],
441 captured,
442 ),
443 ).toBeUndefined();
444 });
445
446 it("matches a Wayland window whose accessibility provider omits its screen position", () => {
447 const captured = {
448 title: "hello world (Draft) - Text Editor",
449 bounds: { x: 479, y: 342, width: 700, height: 520 },
450 };
451 const windows = [{ name: captured.title, bounds: { x: 0, y: 0, width: 700, height: 520 } }];
452
453 expect(findAccessibleWindow(windows, captured, "wayland")).toBe(windows[0]);
454 expect(findAccessibleWindow(windows, captured)).toBeUndefined();
455 });
456
457 it("matches a decorated Wayland window by its verified client size", () => {
458 const clientBounds = { x: 100, y: 229, width: 800, height: 571 };
459 const windows = [{ name: captured.title, bounds: { ...clientBounds, x: 0, y: 0 } }];
460 expect(findAccessibleWindow(windows, { ...captured, clientBounds }, "wayland")).toBe(
461 windows[0],
462 );
463 expect(findAccessibleWindow(windows, { ...captured, clientBounds })).toBeUndefined();
464 expect(findAccessibleWindow(windows, captured, "wayland")).toBeUndefined();
465 });
466
467 it("does not guess between client-size and frame-size matches", () => {
468 const clientBounds = { x: 100, y: 229, width: 800, height: 571 };
469 const windows = [
470 { name: captured.title, bounds: clientBounds },
471 { name: captured.title, bounds: captured.bounds },
472 ];
473 expect(findAccessibleWindow(windows, { ...captured, clientBounds }, "wayland")).toBeUndefined();
474 expect(
475 findAccessibleWindow(
476 [
477 { name: "Private", bounds: clientBounds },
478 { name: captured.title, bounds: { ...clientBounds, height: 550 } },
479 ],
480 { ...captured, clientBounds },
481 "wayland",
482 ),
483 ).toBeUndefined();
484 });
485
486 it("distinguishes same-title Wayland windows by size", () => {
487 const windows = [
488 { name: "Editor", bounds: { x: 0, y: 0, width: 400, height: 300 } },
489 { name: "Editor", bounds: { x: 0, y: 0, width: 801, height: 599 } },
490 ];
491
492 expect(findAccessibleWindow(windows, captured, "wayland")).toBe(windows[1]);
493 });
494
495 it.each([
496 { name: "Private", bounds: { x: 0, y: 0, width: 800, height: 600 } },
497 { name: "Editor", bounds: { x: 0, y: 0, width: 400, height: 600 } },
498 { name: "Editor", bounds: { x: 0, y: 0, width: 800, height: 300 } },
499 { name: "Editor", bounds: null },
500 ])("rejects an unverified Wayland window: %j", (window) => {
501 expect(findAccessibleWindow([window], captured, "wayland")).toBeUndefined();
502 });
503
504 it("rejects ambiguous Wayland windows even when one reports the captured screen position", () => {
505 const windows = [
506 { name: "Editor", bounds: captured.bounds },
507 { name: "Editor", bounds: { x: 0, y: 0, width: 800, height: 600 } },
508 ];
509
510 expect(findAccessibleWindow(windows, captured, "wayland")).toBeUndefined();
511 });
512
513 it("does not match an untitled Wayland window by size alone", () => {
514 const windows = [{ name: "", bounds: captured.bounds }];
515
516 expect(findAccessibleWindow(windows, { ...captured, title: "" }, "wayland")).toBeUndefined();
517 });
518
519 it("accepts one PID-scoped untitled window whose bounds match", () => {
520 const windows = [{ name: null, bounds: captured.bounds }];
521
522 expect(
523 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
524 ).toBe(windows[0]);
525 expect(findAccessibleWindow(windows, captured, "wayland")).toBeUndefined();
526 });
527
528 it("rejects ambiguous PID-scoped untitled windows even when bounds match", () => {
529 const windows = [
530 { name: null, bounds: captured.bounds },
531 { name: "", bounds: { ...captured.bounds, x: 0, y: 0 } },
532 ];
533
534 expect(
535 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
536 ).toBeUndefined();
537 });
538
539 it("does not fall back to a differently titled PID-scoped window with matching bounds", () => {
540 const windows = [{ name: "Preferences", bounds: captured.bounds }];
541
542 expect(
543 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
544 ).toBeUndefined();
545 });
546
547 it("does not use unique bounds when a titled match is already ambiguous", () => {
548 const windows = [
549 { name: "Editor", bounds: captured.bounds },
550 { name: "Editor", bounds: { ...captured.bounds, x: 0, y: 0 } },
551 ];
552
553 expect(
554 findAccessibleWindow(windows, captured, "wayland", { allowUntitledUniqueBounds: true }),
555 ).toBeUndefined();
556 });
557
558 it.each(["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])(
559 "ignores a leading Wayland title spinner frame %s",
560 (frame) => {
561 const windows = [{ name: `${frame} t3code`, bounds: captured.bounds }];
562
563 expect(findAccessibleWindow(windows, { ...captured, title: "⠋ t3code" }, "wayland")).toBe(
564 windows[0],
565 );
566 },
567 );
568
569 it.each([
570 ["⠋ t3code", "t3code"],
571 ["t3code", "⠙ t3code"],
572 ])("matches a Wayland spinner starting or stopping: %s → %s", (title, name) => {
573 const windows = [{ name, bounds: captured.bounds }];
574
575 expect(findAccessibleWindow(windows, { ...captured, title }, "wayland")).toBe(windows[0]);
576 });
577
578 it.each([
579 ["⠋ t3code", "⠙ private"],
580 ["t3code ⠋", "t3code ⠙"],
581 ["⠋t3code", "⠙t3code"],
582 ["⠁ t3code", "⠙ t3code"],
583 ["⠋", "⠋"],
584 ])("does not guess a Wayland title match: %s → %s", (title, name) => {
585 expect(
586 findAccessibleWindow([{ name, bounds: captured.bounds }], { ...captured, title }, "wayland"),
587 ).toBeUndefined();
588 });
589
590 it("rejects matching spinners when the window sizes differ", () => {
591 expect(
592 findAccessibleWindow(
593 [{ name: "⠙ t3code", bounds: { ...captured.bounds, width: 400 } }],
594 { ...captured, title: "⠋ t3code" },
595 "wayland",
596 ),
597 ).toBeUndefined();
598 });
599
600 it("rejects ambiguous normalized titles even if one matches the captured spinner exactly", () => {
601 const windows = [
602 { name: "⠋ t3code", bounds: captured.bounds },
603 { name: "⠙ t3code", bounds: captured.bounds },
604 ];
605
606 expect(
607 findAccessibleWindow(windows, { ...captured, title: "⠋ t3code" }, "wayland"),
608 ).toBeUndefined();
609 });
610
611 it("keeps exact title matching outside Wayland", () => {
612 expect(
613 findAccessibleWindow([{ name: "⠙ t3code", bounds: captured.bounds }], {
614 ...captured,
615 title: "⠋ t3code",
616 }),
617 ).toBeUndefined();
618 });
619});
620
621describe("hideAndWaitForBlur", () => {
622 it("waits for a delayed blur after hiding the window", async () => {
623 let blur: (() => void) | undefined;
624 let settled = false;
625 const hidden = hideAndWaitForBlur({
626 hide: () => undefined,
627 once: (_event, listener) => {
628 blur = listener;
629 },
630 removeListener: () => undefined,
631 }).then(() => {
632 settled = true;
633 });
634
635 await Promise.resolve();
636 expect(settled).toBe(false);
637 blur?.();
638 await hidden;
639 expect(settled).toBe(true);
640 });
641
642 it("rejects when the hidden window never blurs", async () => {
643 vi.useFakeTimers();
644 try {
645 let rejected = false;
646 const hidden = hideAndWaitForBlur({
647 hide: () => undefined,
648 once: () => undefined,
649 removeListener: () => undefined,
650 }).catch(() => {
651 rejected = true;
652 });
653
654 await vi.advanceTimersByTimeAsync(1_000);
655 expect(rejected).toBe(true);
656 await hidden;
657 } finally {
658 vi.useRealTimers();
659 }
660 });
661});
662
663describe("toElectronAccelerator", () => {
664 it("converts the default portable shortcut", () => {
665 expect(
666 toElectronAccelerator({
667 key: "2",
668 metaKey: false,
669 ctrlKey: false,
670 shiftKey: true,
671 altKey: false,
672 modKey: true,
673 }),
674 ).toBe("CommandOrControl+Shift+2");
675 });
676
677 it("maps the portable meta key to Super", () => {
678 expect(
679 toElectronAccelerator({
680 key: "k",
681 metaKey: true,
682 ctrlKey: false,
683 shiftKey: false,
684 altKey: false,
685 modKey: false,
686 }),
687 ).toBe("Super+K");
688 });
689
690 it("normalizes Electron key names", () => {
691 expect(
692 toElectronAccelerator({
693 key: "ArrowUp",
694 metaKey: false,
695 ctrlKey: true,
696 shiftKey: false,
697 altKey: true,
698 modKey: false,
699 }),
700 ).toBe("Control+Alt+Up");
701 });
702});
703
704describe("findCaptureSource", () => {
705 const sources = [
706 { id: "window:42:0", name: "Terminal" },
707 { id: "window:84:0", name: "Editor" },
708 ];
709
710 it("matches the native window id before its title", () => {
711 expect(
712 findCaptureSource(sources, {
713 id: 84,
714 title: "Changed title",
715 }),
716 ).toEqual(sources[1]);
717 });
718
719 it("falls back to a unique title match", () => {
720 expect(
721 findCaptureSource(sources, {
722 id: 100,
723 title: "Terminal",
724 }),
725 ).toEqual(sources[0]);
726 });
727
728 it("does not guess when a title is ambiguous", () => {
729 expect(
730 findCaptureSource(
731 [
732 { id: "window:42:0", name: "Editor" },
733 { id: "window:84:0", name: "Editor" },
734 ],
735 {
736 id: 100,
737 title: "Editor",
738 },
739 ),
740 ).toBeUndefined();
741 });
742});
743
744describe("isWaylandSession", () => {
745 it.each([
746 ["linux", { XDG_SESSION_TYPE: "wayland" }, true],
747 ["linux", { WAYLAND_DISPLAY: "wayland-0" }, true],
748 ["linux", { XDG_SESSION_TYPE: "x11" }, false],
749 ["darwin", { XDG_SESSION_TYPE: "wayland", WAYLAND_DISPLAY: "wayland-0" }, false],
750 ] as const)("detects %s session %o as portal=%s", (platform, environment, expected) => {
751 expect(isWaylandSession(platform, environment)).toBe(expected);
752 });
753
754 effectIt.effect(
755 "falls back to a live runtime directory socket when session variables are stripped",
756 () =>
757 Effect.gen(function* () {
758 if ((yield* HostProcess.Platform) !== "linux") return;
759 yield* Effect.promise(async () => {
760 const { mkdtemp, rm, writeFile } = await import("node:fs/promises");
761 const { createServer } = await import("node:net");
762 const { tmpdir } = await import("node:os");
763 const { join } = await import("node:path");
764 const runtimeDirectory = await mkdtemp(join(tmpdir(), "t3-wayland-"));
765 const socketPath = join(runtimeDirectory, "wayland-0");
766 const server = createServer();
767 try {
768 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(false);
769 await writeFile(socketPath, "");
770 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(false);
771 await rm(socketPath);
772 await new Promise<void>((resolve, reject) => {
773 server.once("error", reject);
774 server.listen(socketPath, resolve);
775 });
776 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: runtimeDirectory })).toBe(true);
777 expect(
778 isWaylandSession("linux", {
779 XDG_RUNTIME_DIR: runtimeDirectory,
780 XDG_SESSION_TYPE: "x11",
781 }),
782 ).toBe(false);
783 expect(isWaylandSession("linux", { XDG_RUNTIME_DIR: "/nonexistent-t3-test" })).toBe(
784 false,
785 );
786 } finally {
787 if (server.listening) {
788 await new Promise<void>((resolve, reject) => {
789 server.close((error) => (error ? reject(error) : resolve()));
790 });
791 }
792 await rm(runtimeDirectory, { recursive: true, force: true });
793 }
794 });
795 }),
796 );
797});
798
799describe("snapShotShortcutRegistrationFailureMessage", () => {
800 it("distinguishes a modifier listener failure from a reserved key chord", () => {
801 expect(
802 snapShotShortcutRegistrationFailureMessage({ kind: "both-shift-keys" }, "darwin"),
803 ).toMatch(/Shift \+ Shift is not available/);
804 expect(
805 snapShotShortcutRegistrationFailureMessage(
806 { kind: "modifier-pair", modifier: "meta" },
807 "darwin",
808 ),
809 ).toMatch(/Command \+ Command is not available/);
810 expect(
811 snapShotShortcutRegistrationFailureMessage(
812 { kind: "modifier-pair", modifier: "meta" },
813 "linux",
814 ),
815 ).toMatch(/Super \+ Super is not available/);
816 expect(
817 snapShotShortcutRegistrationFailureMessage(
818 {
819 key: "2",
820 metaKey: false,
821 ctrlKey: false,
822 shiftKey: true,
823 altKey: false,
824 modKey: true,
825 },
826 "darwin",
827 ),
828 ).toMatch(/already used/);
829 });
830});
831
832describe("snapShotShortcutSystemConflict", () => {
833 it("blocks shortcuts that would break typing or common app actions", () => {
834 expect(
835 snapShotShortcutSystemConflict({
836 key: "s",
837 metaKey: false,
838 ctrlKey: false,
839 shiftKey: true,
840 altKey: false,
841 modKey: false,
842 }),
843 ).toMatch(/typing/);
844 expect(
845 snapShotShortcutSystemConflict({
846 key: "c",
847 metaKey: false,
848 ctrlKey: false,
849 shiftKey: false,
850 altKey: false,
851 modKey: true,
852 }),
853 ).toMatch(/Copy/);
854 });
855
856 it("allows a specific multi-modifier shortcut", () => {
857 expect(
858 snapShotShortcutSystemConflict({
859 key: "2",
860 metaKey: false,
861 ctrlKey: false,
862 shiftKey: true,
863 altKey: false,
864 modKey: true,
865 }),
866 ).toBeNull();
867 });
868});