@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 2,203 lines 75.5 KB
Raw
1 import * as MacPermissions from "../permissions/MacPermissions.ts";
2 import { assert, it } from "@effect/vitest";
3 import {
4 DEFAULT_CLIENT_SETTINGS,
5 DesktopPendingSnapShot,
6 type ClientSettings,
7 type DesktopSnapShotEvent,
8 } from "@t3tools/contracts";
9 import * as SecureRandom from "effect/crypto/SecureRandom";
10 import * as Effect from "effect/Effect";
11 import * as Fiber from "effect/Fiber";
12 import * as FileSystem from "effect/FileSystem";
13 import * as Layer from "effect/Layer";
14 import * as Option from "effect/Option";
15 import * as Path from "effect/Path";
16 import * as PlatformError from "effect/PlatformError";
17 import * as Schema from "effect/Schema";
18 import * as TestClock from "effect/testing/TestClock";
19 import type * as Electron from "electron";
20 import type { PortalShortcutState } from "./PortalCaptureShortcut.ts";
21 import { beforeEach, vi } from "vite-plus/test";
22
23 beforeEach(() => {
24 portalShortcutInstances.length = 0;
25 nextPortalState.value = undefined;
26 vi.stubEnv("NIRI_SOCKET", "");
27 vi.stubEnv("XDG_CURRENT_DESKTOP", "test-desktop");
28 transitionCapturePageMock.mockReset().mockResolvedValue(undefined);
29 transitionSnapshotMock.mockReset().mockResolvedValue(undefined);
30 prepareCaptureRevealMock.mockReset();
31 accessibilityListMock.mockReset().mockResolvedValue([]);
32 });
33
34 const {
35 activeWindowMock,
36 animationSettingsMock,
37 accessibilityProcessWarmMock,
38 accessibilityProcessCloseMock,
39 accessibilityProcessCoolMock,
40 accessibilityProcessReadMock,
41 accessibilityByPidMock,
42 accessibilityListMock,
43 accessibilityForegroundMock,
44 accessibilityTrustedMock,
45 allWindowsMock,
46 flashWindows,
47 focusedWindowMock,
48 getFileIconMock,
49 getSourcesMock,
50 macCaptureMock,
51 linuxCaptureMock,
52 linuxBackendMock,
53 niriShortcutMock,
54 niriShortcutStopMock,
55 mediaAccessStatusMock,
56 openExternalMock,
57 registerShortcutMock,
58 unregisterShortcutMock,
59 portalShortcutInstances,
60 prepareCaptureRevealMock,
61 nextPortalState,
62 regionCaptureMock,
63 screenToDipRectMock,
64 shortcutForkArgs,
65 shortcutForkOptions,
66 shortcutProcesses,
67 spawnedPollers,
68 thumbnailFromPathMock,
69 transitionCapturePageMock,
70 transitionScriptState,
71 transitionShowMock,
72 transitionSnapshotMock,
73 } = vi.hoisted(() => ({
74 activeWindowMock: vi.fn(),
75 animationSettingsMock: vi.fn(() => ({
76 prefersReducedMotion: true,
77 shouldRenderRichAnimation: false,
78 })),
79 accessibilityProcessWarmMock: vi.fn(),
80 accessibilityProcessCloseMock: vi.fn(),
81 accessibilityProcessCoolMock: vi.fn(),
82 accessibilityProcessReadMock: vi.fn<
83 (request: import("./SnapShotAccessibility.ts").SnapShotAccessibilityRequest) => {
84 started: Promise<void>;
85 result: Promise<
86 import("./SnapShotAccessibility.ts").CapturedWindowAccessibilityContext | undefined
87 >;
88 }
89 >(),
90 accessibilityByPidMock: vi.fn(),
91 accessibilityListMock: vi.fn(),
92 accessibilityForegroundMock: vi.fn(),
93 accessibilityTrustedMock: vi.fn((_prompt = false) => true),
94 allWindowsMock: vi.fn(
95 () =>
96 [] as Array<{
97 getBounds: () => Electron.Rectangle;
98 isDestroyed: () => boolean;
99 }>,
100 ),
101 flashWindows: [] as Array<{
102 bounds: Electron.Rectangle | null;
103 destroyed: boolean;
104 kind: "base" | "browser";
105 loadCount: number;
106 loadedUrls: Array<string>;
107 opacities: Array<number>;
108 options: Electron.BrowserWindowConstructorOptions;
109 scripts: Array<string>;
110 capturedRegions: Array<Electron.Rectangle>;
111 showCount: number;
112 resizeCount: number;
113 alwaysOnTopCalls: Array<[boolean, string | undefined]>;
114 }>,
115 focusedWindowMock: vi.fn(),
116 getFileIconMock: vi.fn(),
117 getSourcesMock: vi.fn(),
118 macCaptureMock: vi.fn(),
119 linuxCaptureMock: vi.fn<
120 () => Promise<import("./LinuxSnapShot.ts").LinuxWindowSnapshot | undefined>
121 >(async () => undefined),
122 linuxBackendMock: vi.fn(async () => "picker"),
123 niriShortcutStopMock: vi.fn(),
124 niriShortcutMock:
125 vi.fn<(appId: string, trigger: () => void, fail: () => void) => Promise<() => void>>(),
126 mediaAccessStatusMock: vi.fn(() => "not-determined"),
127 openExternalMock: vi.fn(() => Promise.resolve()),
128 registerShortcutMock: vi.fn(),
129 unregisterShortcutMock: vi.fn(),
130 nextPortalState: { value: undefined as PortalShortcutState | undefined },
131 portalShortcutInstances: [] as Array<{
132 state: PortalShortcutState;
133 onCapture: () => Promise<void>;
134 onStateChanged: () => void;
135 close: ReturnType<typeof vi.fn>;
136 configure: ReturnType<typeof vi.fn>;
137 hasSession: boolean;
138 }>,
139 prepareCaptureRevealMock: vi.fn(),
140 regionCaptureMock:
141 vi.fn<
142 (region: Electron.Rectangle) => Promise<{ width: number; height: number; png: Buffer }>
143 >(),
144 screenToDipRectMock: vi.fn((_window: unknown, bounds: Electron.Rectangle) => bounds),
145 shortcutForkArgs: [] as Array<ReadonlyArray<string>>,
146 shortcutForkOptions: [] as Array<{ env?: NodeJS.ProcessEnv }>,
147 shortcutProcesses: [] as Array<{
148 emit: (event: string, value?: unknown) => void;
149 kill: ReturnType<typeof vi.fn>;
150 on: (event: string, listener: (value: unknown) => void) => unknown;
151 once: (event: string, listener: (value: unknown) => void) => unknown;
152 }>,
153 spawnedPollers: [] as Array<{
154 args: ReadonlyArray<string>;
155 kill: ReturnType<typeof vi.fn>;
156 emitStderr: (text: string) => void;
157 emitExit: (code: number) => void;
158 }>,
159 thumbnailFromPathMock: vi.fn(),
160 transitionCapturePageMock: vi.fn<() => Promise<void>>(),
161 transitionScriptState: {
162 rejectFlight: false,
163 heldFlights: null as Array<() => void> | null,
164 },
165 transitionShowMock: vi.fn(),
166 transitionSnapshotMock: vi.fn<() => Promise<void>>(),
167 }));
168
169 vi.mock("./SnapShotAccessibilityProcess.ts", () => ({
170 makeSnapShotAccessibilityProcessPool: () => ({
171 warm: accessibilityProcessWarmMock,
172 cool: accessibilityProcessCoolMock,
173 read: accessibilityProcessReadMock,
174 close: accessibilityProcessCloseMock,
175 }),
176 }));
177 vi.mock("./ActiveWindow.ts", () => ({ activeWindow: activeWindowMock }));
178 vi.mock("./RegionSnapShot.ts", async (original) => ({
179 ...(await original<typeof import("./RegionSnapShot.ts")>()),
180 makeRegionSnapShotPool: () => ({
181 warm: vi.fn(),
182 cool: vi.fn(),
183 close: vi.fn(),
184 capture: regionCaptureMock,
185 }),
186 }));
187 vi.mock("./WindowsCaptureFeedback.ts", () => ({
188 showWindowsCaptureOverlay: (window: Electron.BaseWindow) => window.showInactive(),
189 }));
190 vi.mock("./MacSnapShot.ts", () => ({ captureMacWindowSnapshot: macCaptureMock }));
191 vi.mock("./LinuxSnapShot.ts", () => ({
192 captureLinuxWindow: linuxCaptureMock,
193 getLinuxCaptureSupport: async () => ({
194 linuxBackend: await linuxBackendMock(),
195 linuxFeedbackAvailable: false,
196 }),
197 }));
198 vi.mock("./NiriCaptureShortcut.ts", async (original) => ({
199 ...(await original<typeof import("./NiriCaptureShortcut.ts")>()),
200 startNiriCaptureShortcut: niriShortcutMock,
201 }));
202 vi.mock("./PortalCaptureShortcut.ts", async (original) => ({
203 ...(await original<typeof import("./PortalCaptureShortcut.ts")>()),
204 PortalCaptureShortcut: class {
205 state: PortalShortcutState = nextPortalState.value ?? {
206 shortcutRegistered: true,
207 shortcutPending: false,
208 shortcutLabel: "Ctrl+Shift+2",
209 shortcutMessage: "Desktop shortcut: Ctrl+Shift+2",
210 };
211 close = vi.fn();
212 configure = vi.fn(async () => {});
213 hasSession = true;
214 onCapture: () => Promise<void>;
215 onStateChanged: () => void;
216 constructor(
217 _appId: string,
218 _shortcut: unknown,
219 onCapture: () => Promise<void>,
220 onStateChanged: () => void,
221 ) {
222 this.onCapture = onCapture;
223 this.onStateChanged = onStateChanged;
224 portalShortcutInstances.push(this);
225 }
226 },
227 }));
228 vi.mock("node:child_process", () => ({
229 spawn: (_command: string, args: ReadonlyArray<string>) => {
230 const stderrListeners: Array<(chunk: Buffer) => void> = [];
231 const onceListeners = new Map<string, Array<(value?: unknown) => void>>();
232 const record = {
233 args,
234 kill: vi.fn(() => true),
235 emitStderr: (text: string) => {
236 for (const listener of stderrListeners) listener(Buffer.from(text));
237 },
238 emitExit: (code: number) => {
239 for (const listener of onceListeners.get("exit") ?? []) listener(code);
240 },
241 };
242 spawnedPollers.push(record);
243 const child = {
244 stderr: {
245 on: (_event: "data", listener: (chunk: Buffer) => void) => {
246 stderrListeners.push(listener);
247 return child;
248 },
249 },
250 once: (event: string, listener: (value?: unknown) => void) => {
251 onceListeners.set(event, [...(onceListeners.get(event) ?? []), listener]);
252 return child;
253 },
254 kill: record.kill,
255 };
256 queueMicrotask(() => record.emitStderr("ready\n"));
257 return child;
258 },
259 fork: (_path: string, args: ReadonlyArray<string>, options: { env?: NodeJS.ProcessEnv }) => {
260 const listeners = new Map<string, Array<(value: unknown) => void>>();
261 const process = {
262 emit: (event: string, value?: unknown) => {
263 for (const listener of listeners.get(event) ?? []) listener(value);
264 },
265 kill: vi.fn(() => true),
266 on: (event: string, listener: (value: unknown) => void) => {
267 listeners.set(event, [...(listeners.get(event) ?? []), listener]);
268 return process;
269 },
270 once: (event: string, listener: (value: unknown) => void) => {
271 const wrapped = (value: unknown) => {
272 listeners.set(
273 event,
274 (listeners.get(event) ?? []).filter((candidate) => candidate !== wrapped),
275 );
276 listener(value);
277 };
278 listeners.set(event, [...(listeners.get(event) ?? []), wrapped]);
279 return process;
280 },
281 };
282 shortcutForkArgs.push(args);
283 shortcutForkOptions.push(options);
284 shortcutProcesses.push(process);
285 queueMicrotask(() => process.emit("message", "ready"));
286 return process;
287 },
288 }));
289 vi.mock("electron", () => {
290 class BaseWindow {
291 protected readonly state: (typeof flashWindows)[number];
292
293 constructor(options: Electron.BrowserWindowConstructorOptions) {
294 this.state = {
295 bounds:
296 options.x === undefined ||
297 options.y === undefined ||
298 options.width === undefined ||
299 options.height === undefined
300 ? null
301 : { x: options.x, y: options.y, width: options.width, height: options.height },
302 destroyed: false,
303 kind: "base",
304 loadCount: 0,
305 loadedUrls: [],
306 opacities: options.opacity === undefined ? [] : [options.opacity],
307 options,
308 scripts: [],
309 capturedRegions: [],
310 showCount: 0,
311 resizeCount: 0,
312 alwaysOnTopCalls: [],
313 };
314 flashWindows.push(this.state);
315 }
316
317 destroy() {
318 this.state.destroyed = true;
319 }
320
321 getBounds() {
322 return this.state.bounds;
323 }
324
325 isDestroyed() {
326 return this.state.destroyed;
327 }
328
329 setBounds(bounds: Electron.Rectangle) {
330 this.state.resizeCount++;
331 this.state.bounds = bounds;
332 }
333
334 setIgnoreMouseEvents() {}
335
336 setOpacity(opacity: number) {
337 this.state.opacities.push(opacity);
338 }
339
340 setAlwaysOnTop(flag: boolean, level?: string) {
341 this.state.alwaysOnTopCalls.push([flag, level]);
342 }
343
344 showInactive() {
345 const shownBounds = transitionShowMock(this.state.bounds);
346 if (shownBounds !== undefined) this.state.bounds = shownBounds;
347 this.state.showCount += 1;
348 }
349 }
350
351 class BrowserWindow extends BaseWindow {
352 static getFocusedWindow() {
353 return focusedWindowMock();
354 }
355
356 static getAllWindows() {
357 return allWindowsMock();
358 }
359
360 readonly webContents;
361
362 constructor(options: Electron.BrowserWindowConstructorOptions) {
363 super(options);
364 this.state.kind = "browser";
365 this.webContents = {
366 executeJavaScript: async (script: string) => {
367 this.state.scripts.push(script);
368 if (script.startsWith("window.setCaptureSnapshot")) {
369 await transitionSnapshotMock();
370 }
371 if (script !== "window.playCaptureTransition()") return;
372 if (transitionScriptState.rejectFlight) {
373 transitionScriptState.rejectFlight = false;
374 throw new Error("transition failed");
375 }
376 const held = transitionScriptState.heldFlights;
377 if (held) await new Promise<void>((resolve) => held.push(resolve));
378 },
379 capturePage: async (bounds: Electron.Rectangle) => {
380 this.state.capturedRegions.push(bounds);
381 await transitionCapturePageMock();
382 },
383 };
384 }
385
386 loadURL(url: string) {
387 this.state.loadCount += 1;
388 this.state.loadedUrls.push(url);
389 return Promise.resolve();
390 }
391 }
392
393 return {
394 BaseWindow,
395 BrowserWindow,
396 app: { getFileIcon: getFileIconMock },
397 desktopCapturer: { getSources: getSourcesMock },
398 nativeImage: { createThumbnailFromPath: thumbnailFromPathMock },
399 globalShortcut: { register: registerShortcutMock, unregister: unregisterShortcutMock },
400 screen: {
401 getDisplayMatching: (bounds: Electron.Rectangle) =>
402 bounds.x < 0
403 ? { id: 1, bounds: { x: -1_920, y: 0, width: 1_920, height: 1_080 } }
404 : { id: 2, bounds: { x: 0, y: -200, width: 1_440, height: 900 } },
405 getAllDisplays: () => [
406 { id: 1, bounds: { x: -1_920, y: 0, width: 1_920, height: 1_080 } },
407 { id: 2, bounds: { x: 0, y: -200, width: 1_440, height: 900 } },
408 ],
409 getCursorScreenPoint: () => ({ x: 500, y: 500 }),
410 getDisplayNearestPoint: () => ({
411 bounds: { x: 100, y: 100, width: 800, height: 600 },
412 }),
413 getPrimaryDisplay: () => ({ bounds: { x: 0, y: 0, width: 1_440, height: 900 } }),
414 screenToDipRect: screenToDipRectMock,
415 },
416 shell: { openExternal: openExternalMock },
417 systemPreferences: {
418 getAnimationSettings: () => animationSettingsMock(),
419 getMediaAccessStatus: () => mediaAccessStatusMock(),
420 isTrustedAccessibilityClient: (prompt: boolean) => accessibilityTrustedMock(prompt),
421 },
422 };
423 });
424
425 import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
426 import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts";
427 import * as DesktopWindow from "../window/DesktopWindow.ts";
428 import * as DesktopSnapShot from "./DesktopSnapShot.ts";
429 import * as SnapShotAccessibility from "./SnapShotAccessibility.ts";
430 import { CaptureSetupRequiredError } from "./linuxCaptureSession.ts";
431
432 const accessibilityApp = {
433 byPid: accessibilityByPidMock,
434 list: accessibilityListMock,
435 foreground: accessibilityForegroundMock,
436 } as unknown as Parameters<typeof SnapShotAccessibility.readAccessibleWindowContextWithApp>[0];
437 const readAccessibleWindowContext = (
438 active: SnapShotAccessibility.AccessibleWindowIdentity,
439 platform: NodeJS.Platform,
440 sourceTitle: string,
441 imageSize: Electron.Size = {
442 width: Math.max(1, Math.round(active.bounds.width)),
443 height: Math.max(1, Math.round(active.bounds.height)),
444 },
445 ) =>
446 SnapShotAccessibility.readAccessibleWindowContextWithApp(accessibilityApp, {
447 active,
448 platform,
449 sourceTitle,
450 imageSize,
451 });
452 const readAccessibleWindowText = async (
453 active: SnapShotAccessibility.AccessibleWindowIdentity,
454 platform: NodeJS.Platform,
455 sourceTitle: string,
456 ) => (await readAccessibleWindowContext(active, platform, sourceTitle))?.accessibleText;
457 accessibilityProcessReadMock.mockImplementation((request) => ({
458 started: Promise.resolve(),
459 result: readAccessibleWindowContext(
460 request.active,
461 request.platform,
462 request.sourceTitle,
463 request.imageSize,
464 ),
465 }));
466 vi.mock("./GnomeCaptureSetup.ts", async (importOriginal) => ({
467 ...(await importOriginal<typeof import("./GnomeCaptureSetup.ts")>()),
468 GnomeCaptureSetup: class {
469 state = async () => ({ status: "enabled" as const, message: "Extension running" });
470 perform = async () => undefined;
471 close = () => undefined;
472 },
473 }));
474
475 const decodePendingMetadata = Schema.decodeUnknownEffect(
476 Schema.fromJsonString(DesktopPendingSnapShot),
477 );
478
479 import { SnapShotTransition, snapShotAnimationDisplayBounds } from "./SnapShotTransition.ts";
480 const layerTest = (
481 platform: NodeJS.Platform,
482 fileSystemOverrides: Parameters<typeof FileSystem.layerNoop>[0] = {},
483 initialSettings: Option.Option<ClientSettings> = Option.none(),
484 settingsGet: Effect.Effect<
485 Option.Option<ClientSettings>,
486 DesktopClientSettings.DesktopClientSettingsReadError
487 > = Effect.succeed(initialSettings),
488 ) =>
489 MacPermissions.layer.pipe(
490 Layer.provideMerge(
491 Layer.mergeAll(
492 Layer.succeed(
493 DesktopEnvironment.DesktopEnvironment,
494 DesktopEnvironment.DesktopEnvironment.of({
495 platform,
496 displayName: "T3 Code (Dev)",
497 stateDir: "/state",
498 linuxDesktopEntryName: "com.t3tools.T3Code.desktop",
499 appRoot: "/repo",
500 linuxApplicationsDir: "/test-data/applications",
501 } as DesktopEnvironment.DesktopEnvironment["Service"]),
502 ),
503 Layer.succeed(
504 DesktopClientSettings.DesktopClientSettings,
505 DesktopClientSettings.DesktopClientSettings.of({
506 get: settingsGet,
507 set: () => Effect.void,
508 }),
509 ),
510 Layer.succeed(
511 DesktopWindow.DesktopWindow,
512 DesktopWindow.DesktopWindow.of({
513 activate: Effect.void,
514 prepareCaptureReveal: Effect.sync(prepareCaptureRevealMock),
515 dispatchMenuAction: () => Effect.void,
516 dispatchSnapShotEvent: () => Effect.void,
517 } as unknown as DesktopWindow.DesktopWindow["Service"]),
518 ),
519 FileSystem.layerNoop(fileSystemOverrides),
520 Path.layer,
521 Layer.succeed(
522 SecureRandom.SecureRandom,
523 SecureRandom.make((size) => new Uint8Array(size)),
524 ),
525 ),
526 ),
527 );
528
529 const enabledSettings = (overrides: Partial<ClientSettings> = {}): ClientSettings => ({
530 ...DEFAULT_CLIENT_SETTINGS,
531 snapShotEnabled: true,
532 ...overrides,
533 });
534
535 function concurrentCaptureFixture(platform: NodeJS.Platform, animations: boolean) {
536 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
537 focusedWindowMock.mockReturnValue(undefined);
538 allWindowsMock.mockReturnValue([]);
539 registerShortcutMock.mockReset().mockReturnValue(true);
540 mediaAccessStatusMock.mockReturnValue("granted");
541 animationSettingsMock.mockReturnValue({
542 prefersReducedMotion: false,
543 shouldRenderRichAnimation: true,
544 });
545 flashWindows.length = 0;
546 const captures = ["Discord", "File Explorer", "Terminal"].map((title, index) => ({
547 title,
548 id: index + 42,
549 processId: index + 100,
550 png: Buffer.from([index, index + 1, index + 2]),
551 started: Promise.withResolvers<void>(),
552 pixels: Promise.withResolvers<void>(),
553 handoff: Promise.withResolvers<void>(),
554 context: Promise.withResolvers<SnapShotAccessibility.CapturedWindowAccessibilityContext>(),
555 oldOverlaysCleared: false,
556 oldNativeFeedbackClosed: false,
557 feedback: {
558 animationStarted: animations,
559 activate: async () => undefined,
560 animateTo: async () => undefined,
561 complete: vi.fn(async () => undefined),
562 close: vi.fn(),
563 },
564 }));
565 const [first, second, extra] = captures;
566 extra!.pixels.resolve();
567 extra!.context.resolve({ accessibleText: extra!.title });
568 const state = {
569 snapshots: 0,
570 handoffs: 0,
571 preparations: 0,
572 preparedWithoutOverlay: true,
573 failFirstPersistence: false,
574 };
575 const images = new Map<string, Uint8Array>();
576 const metadata = new Map<string, string>();
577 const readyIds: string[] = [];
578 const requestedIds: string[] = [];
579 const bounds = { x: 10, y: 20, width: 800, height: 600 };
580 const takeSnapshot = async () => {
581 const index = state.snapshots++;
582 const capture = captures[Math.min(index, captures.length - 1)]!;
583 capture.oldOverlaysCleared = flashWindows.every((window) => window.destroyed);
584 capture.oldNativeFeedbackClosed = captures
585 .slice(0, index)
586 .every((previous) => previous.feedback.close.mock.calls.length > 0);
587 capture.started.resolve();
588 await capture.pixels.promise;
589 return capture;
590 };
591 activeWindowMock.mockReset().mockImplementation(async () => {
592 const capture = captures[Math.min(state.snapshots, captures.length - 1)]!;
593 return {
594 platform: platform === "darwin" ? "macos" : "windows",
595 id: capture.id,
596 title: capture.title,
597 owner: { name: capture.title, processId: capture.processId },
598 bounds,
599 };
600 });
601 regionCaptureMock.mockReset().mockImplementation(async () => {
602 const capture = await takeSnapshot();
603 return { width: bounds.width, height: bounds.height, png: capture.png };
604 });
605 macCaptureMock.mockReset().mockImplementation(async (_active: unknown, imagePath: string) => {
606 const capture = await takeSnapshot();
607 images.set(imagePath, capture.png);
608 return { source: { name: capture.title }, png: capture.png };
609 });
610 linuxCaptureMock.mockReset().mockImplementation(async () => {
611 const capture = await takeSnapshot();
612 return {
613 png: capture.png,
614 window: {
615 title: capture.title,
616 appName: capture.title,
617 appIdentifier: `test.capture.${capture.id}`,
618 processId: capture.processId,
619 bounds,
620 },
621 feedback: capture.feedback,
622 };
623 });
624 const readAccessibility = accessibilityProcessReadMock.getMockImplementation()!;
625 accessibilityProcessReadMock.mockImplementation(({ active }) => ({
626 started: Promise.resolve(),
627 result: captures.find((capture) => capture.processId === active.owner.processId)!.context
628 .promise,
629 }));
630 const handoff = Effect.sync(() => captures[state.handoffs++]!.handoff.resolve());
631 let randomByte = 0;
632 const layer = Layer.mergeAll(
633 layerTest(platform, {
634 makeDirectory: () => Effect.void,
635 writeFile: (path, bytes) =>
636 Effect.sync(() => {
637 images.set(path, bytes);
638 }),
639 writeFileString: (path, text) => {
640 const pending = JSON.parse(text) as { source: { windowTitle: string } };
641 return state.failFirstPersistence && pending.source.windowTitle === first!.title
642 ? Effect.fail(
643 PlatformError.systemError({
644 _tag: "PermissionDenied",
645 module: "FileSystem",
646 method: "writeFileString",
647 }),
648 )
649 : Effect.sync(() => {
650 metadata.set(path, text);
651 });
652 },
653 rename: (from, to) =>
654 Effect.sync(() => {
655 const image = images.get(from);
656 if (image) {
657 images.set(to, image);
658 images.delete(from);
659 }
660 const text = metadata.get(from);
661 if (text) {
662 metadata.set(to, text);
663 metadata.delete(from);
664 }
665 }),
666 remove: (path) =>
667 Effect.sync(() => {
668 images.delete(path);
669 metadata.delete(path);
670 }),
671 readFile: (path) => Effect.sync(() => images.get(path)!),
672 readFileString: (path) => Effect.sync(() => metadata.get(path)!),
673 }),
674 Layer.succeed(
675 SecureRandom.SecureRandom,
676 SecureRandom.make((size) => new Uint8Array(size).fill(++randomByte)),
677 ),
678 Layer.succeed(
679 DesktopWindow.DesktopWindow,
680 DesktopWindow.DesktopWindow.of({
681 activate: handoff,
682 prepareCaptureReveal: Effect.sync(() => {
683 state.preparations++;
684 state.preparedWithoutOverlay &&= flashWindows.every((window) => window.destroyed);
685 }),
686 dispatchMenuAction: () => Effect.void,
687 dispatchSnapShotEvent: (event: DesktopSnapShotEvent) => {
688 switch (event.type) {
689 case "requested":
690 return Effect.sync(() => {
691 requestedIds.push(event.id);
692 });
693 case "started":
694 return handoff;
695 case "ready":
696 return Effect.sync(() => {
697 readyIds.push(event.id);
698 });
699 default:
700 return Effect.void;
701 }
702 },
703 } as unknown as DesktopWindow.DesktopWindow["Service"]),
704 ),
705 );
706 return {
707 first: first!,
708 second: second!,
709 state,
710 readyIds,
711 requestedIds,
712 layer,
713 settings: {
714 ...DEFAULT_CLIENT_SETTINGS,
715 snapShotEnabled: true,
716 snapShotIncludeAccessibility: true,
717 snapShotAnimations: animations,
718 snapShotFlash: true,
719 snapShotShortcut: {
720 key: "2",
721 ctrlKey: true,
722 shiftKey: true,
723 altKey: false,
724 metaKey: false,
725 modKey: false,
726 },
727 },
728 trigger: () =>
729 platform === "linux"
730 ? portalShortcutInstances.at(-1)!.onCapture()
731 : (registerShortcutMock.mock.calls.at(-1)![1] as () => Promise<void>)(),
732 releaseAll: () => {
733 for (const capture of captures) {
734 capture.pixels.resolve();
735 capture.context.resolve({ accessibleText: capture.title });
736 }
737 },
738 reset: () => {
739 focusedWindowMock.mockReset();
740 linuxCaptureMock.mockReset().mockResolvedValue(undefined);
741 accessibilityProcessReadMock.mockReset().mockImplementation(readAccessibility);
742 mediaAccessStatusMock.mockReturnValue("not-determined");
743 animationSettingsMock.mockReturnValue({
744 prefersReducedMotion: true,
745 shouldRenderRichAnimation: false,
746 });
747 vi.unstubAllEnvs();
748 },
749 };
750 }
751
752 it.effect.each(["darwin", "win32"] as const)(
753 "drops shortcut bursts during flight and accepts a fresh capture after landing on %s",
754 (platform) => {
755 const fixture = concurrentCaptureFixture(platform, true);
756 fixture.releaseAll();
757 const flying = Promise.withResolvers<void>();
758 const heldFlights: Array<() => void> = [];
759 const push = heldFlights.push.bind(heldFlights);
760 vi.spyOn(heldFlights, "push").mockImplementation((...resolvers) => {
761 const count = push(...resolvers);
762 flying.resolve();
763 return count;
764 });
765 transitionScriptState.heldFlights = heldFlights;
766 return Effect.scoped(
767 Effect.gen(function* () {
768 const service = yield* DesktopSnapShot.make;
769 yield* service.configure(fixture.settings);
770 yield* Effect.promise(fixture.trigger);
771 const firstId = fixture.readyIds[0]!;
772 const landing = yield* service
773 .setAnimationDestination(firstId, {
774 frame: { x: 100, y: 400, width: 208, height: 112 },
775 backgroundColor: "#fff",
776 borderColor: "#ccc",
777 borderWidth: 1,
778 cornerRadius: 8,
779 scaleFactor: 1,
780 })
781 .pipe(Effect.forkChild({ startImmediately: true }));
782 yield* Effect.promise(() => flying.promise);
783 const overlayCount = flashWindows.length;
784
785 for (let repeat = 0; repeat < 20; repeat++) {
786 yield* TestClock.adjust("200 millis");
787 yield* Effect.promise(fixture.trigger);
788 }
789 assert.equal(fixture.state.snapshots, 1);
790 assert.lengthOf(fixture.readyIds, 1);
791 assert.lengthOf(flashWindows, overlayCount);
792 assert.isTrue(flashWindows.every((window) => !window.destroyed));
793
794 for (const finish of heldFlights) finish();
795 yield* Fiber.join(landing);
796 yield* Effect.promise(fixture.trigger);
797 assert.equal(fixture.state.snapshots, 2);
798 assert.lengthOf(fixture.readyIds, 2);
799 assert.isTrue(fixture.second.oldOverlaysCleared);
800 }),
801 ).pipe(
802 Effect.provide(fixture.layer),
803 Effect.ensuring(
804 Effect.sync(() => {
805 for (const finish of heldFlights) finish();
806 transitionScriptState.heldFlights = null;
807 fixture.reset();
808 }),
809 ),
810 );
811 },
812 );
813
814 it.effect.each([
815 { length: 1_000, suffix: "", expectedLength: 1_000 },
816 { length: 1_001, suffix: "", expectedLength: 1_000 },
817 { length: 999, suffix: "😀", expectedLength: 999 },
818 ])(
819 "preserves captures with native metadata of length $length and suffix $suffix",
820 ({ length, suffix, expectedLength }) => {
821 const png = Buffer.from([1, 2, 3]);
822 const title = "t".repeat(length) + suffix;
823 const appName = "a".repeat(254) + "😀";
824 const appIdentifier = "b".repeat(254) + "😀";
825 activeWindowMock.mockReset().mockResolvedValue({
826 platform: "macos",
827 id: 42,
828 title,
829 owner: { name: appName, bundleId: appIdentifier, processId: 123 },
830 bounds: { x: 10, y: 20, width: 800, height: 600 },
831 });
832 focusedWindowMock.mockReturnValue(undefined);
833 allWindowsMock.mockReturnValue([]);
834 macCaptureMock.mockReset().mockResolvedValue({ source: { name: title }, png });
835 accessibilityProcessReadMock.mockReturnValueOnce({
836 started: Promise.resolve(),
837 result: Promise.resolve(undefined),
838 });
839 let metadata = "";
840 const layer = layerTest("darwin", {
841 makeDirectory: () => Effect.void,
842 rename: () => Effect.void,
843 writeFileString: (_, text) =>
844 Effect.sync(() => {
845 metadata = text;
846 }),
847 readDirectory: () => Effect.succeed(["capture.json"]),
848 readFileString: () => Effect.succeed(metadata),
849 readFile: () => Effect.succeed(png),
850 });
851 return Effect.scoped(
852 Effect.gen(function* () {
853 const service = yield* DesktopSnapShot.make;
854 yield* service.configure(enabledSettings());
855 yield* service.capture;
856 const pending = yield* service.listPending;
857 assert.lengthOf(pending, 1);
858 const capture = yield* service.read(pending[0]!.id);
859 assert.equal(capture.dataUrl, "data:image/png;base64,AQID");
860 assert.equal(capture.source.windowTitle, "t".repeat(expectedLength));
861 assert.equal(capture.source.appName, "a".repeat(254));
862 assert.equal(capture.source.appIdentifier, "b".repeat(254));
863 }),
864 ).pipe(Effect.provide(layer));
865 },
866 );
867
868 it.effect.each([
869 { platform: "win32", animations: true },
870 { platform: "darwin", animations: false },
871 { platform: "linux", animations: true },
872 ] as const)(
873 "captures again while accessibility is pending on $platform (animations: $animations)",
874 ({ platform, animations }) => {
875 const fixture = concurrentCaptureFixture(platform, animations);
876 return Effect.scoped(
877 Effect.gen(function* () {
878 const service = yield* DesktopSnapShot.make;
879 yield* service.configure(fixture.settings);
880 const first = yield* Effect.promise(fixture.trigger).pipe(
881 Effect.forkChild({ startImmediately: true }),
882 );
883 yield* Effect.promise(() => fixture.first.started.promise);
884 assert.lengthOf(fixture.requestedIds, 1);
885 assert.lengthOf(fixture.readyIds, 0);
886 fixture.first.pixels.resolve();
887 yield* Effect.promise(() => fixture.first.handoff.promise);
888 assert.lengthOf(fixture.readyIds, 0);
889
890 yield* TestClock.adjust("200 millis");
891 fixture.second.pixels.resolve();
892 const second = yield* Effect.promise(fixture.trigger).pipe(
893 Effect.forkChild({ startImmediately: true }),
894 );
895 yield* Effect.promise(() => fixture.second.handoff.promise);
896 assert.equal(fixture.state.snapshots, 2);
897 assert.isTrue(fixture.second.oldOverlaysCleared);
898 assert.equal(fixture.state.preparations, platform === "win32" ? 2 : 0);
899 assert.isTrue(fixture.state.preparedWithoutOverlay);
900 if (platform === "linux") assert.isTrue(fixture.second.oldNativeFeedbackClosed);
901 const secondOverlays = flashWindows.filter((window) => !window.destroyed);
902 if (platform !== "linux") assert.isNotEmpty(secondOverlays);
903
904 fixture.second.context.resolve({ accessibleText: "Explorer accessibility" });
905 yield* Fiber.join(second);
906 assert.lengthOf(fixture.readyIds, 1);
907 fixture.first.context.resolve({ accessibleText: "Discord accessibility" });
908 yield* Fiber.join(first);
909 assert.lengthOf(fixture.readyIds, 2);
910 assert.equal(new Set(fixture.readyIds).size, 2);
911 assert.deepEqual(fixture.readyIds, fixture.requestedIds.toReversed());
912 const newer = yield* service.read(fixture.readyIds[0]!);
913 const older = yield* service.read(fixture.readyIds[1]!);
914 assert.equal(newer.source.windowTitle, fixture.second.title);
915 assert.equal(newer.source.accessibleText, "Explorer accessibility");
916 assert.equal(
917 newer.dataUrl,
918 `data:image/png;base64,${fixture.second.png.toString("base64")}`,
919 );
920 assert.equal(older.source.windowTitle, fixture.first.title);
921 assert.equal(older.source.accessibleText, "Discord accessibility");
922 assert.equal(
923 older.dataUrl,
924 `data:image/png;base64,${fixture.first.png.toString("base64")}`,
925 );
926
927 yield* service.acknowledge(fixture.readyIds[1]!);
928 assert.isTrue(secondOverlays.every((window) => !window.destroyed));
929 if (platform === "linux") {
930 assert.lengthOf(fixture.second.feedback.close.mock.calls, 0);
931 assert.lengthOf(fixture.second.feedback.complete.mock.calls, 0);
932 }
933 }).pipe(Effect.ensuring(Effect.sync(fixture.releaseAll))),
934 ).pipe(Effect.provide(fixture.layer), Effect.ensuring(Effect.sync(fixture.reset)));
935 },
936 );
937
938 it.effect.each(["succeeds", "fails"] as const)(
939 "keeps the newer snapshot exclusive when older persistence %s",
940 (outcome) => {
941 const fixture = concurrentCaptureFixture("win32", true);
942 fixture.state.failFirstPersistence = outcome === "fails";
943 return Effect.scoped(
944 Effect.gen(function* () {
945 const service = yield* DesktopSnapShot.make;
946 yield* service.configure(fixture.settings);
947 fixture.first.pixels.resolve();
948 const first = yield* Effect.promise(fixture.trigger).pipe(
949 Effect.forkChild({ startImmediately: true }),
950 );
951 yield* Effect.promise(() => fixture.first.handoff.promise);
952 yield* TestClock.adjust("200 millis");
953 const second = yield* Effect.promise(fixture.trigger).pipe(
954 Effect.forkChild({ startImmediately: true }),
955 );
956 yield* Effect.promise(() => fixture.second.started.promise);
957
958 yield* TestClock.adjust("200 millis");
959 yield* Effect.promise(fixture.trigger);
960 assert.equal(fixture.state.snapshots, 2);
961 fixture.first.context.resolve({ accessibleText: "Discord accessibility" });
962 yield* Fiber.join(first);
963 assert.lengthOf(fixture.readyIds, outcome === "succeeds" ? 1 : 0);
964 yield* TestClock.adjust("200 millis");
965 yield* Effect.promise(fixture.trigger);
966 assert.equal(fixture.state.snapshots, 2);
967
968 fixture.second.pixels.resolve();
969 yield* Effect.promise(() => fixture.second.handoff.promise);
970 fixture.second.context.resolve({ accessibleText: "Explorer accessibility" });
971 yield* Fiber.join(second);
972 assert.equal(fixture.state.handoffs, 2);
973 const newer = yield* service.read(fixture.readyIds.at(-1)!);
974 assert.equal(newer.source.windowTitle, fixture.second.title);
975 assert.equal(newer.source.accessibleText, "Explorer accessibility");
976 }).pipe(Effect.ensuring(Effect.sync(fixture.releaseAll))),
977 ).pipe(Effect.provide(fixture.layer), Effect.ensuring(Effect.sync(fixture.reset)));
978 },
979 );
980
981 it.effect("captures and reads Windows windows in physical pixels on a scaled display", () => {
982 const png = Buffer.from([1, 2, 3]);
983 const active = {
984 platform: "windows",
985 id: 42,
986 title: "Editor",
987 owner: { name: "Editor", processId: 123 },
988 bounds: { x: 10, y: 20, width: 800, height: 600 },
989 } as const;
990 const window = (name: string, value: string) => ({
991 role: "window",
992 name,
993 bounds: active.bounds,
994 children: async () => [
995 {
996 role: "button",
997 name: "Save",
998 bounds: { x: 210, y: 220, width: 200, height: 100 },
999 children: async () => [],
1000 },
1001 ],
1002 tree: async () => ({ name, value, children: [] }),
1003 });
1004 activeWindowMock.mockReset().mockResolvedValue(active);
1005 screenToDipRectMock.mockImplementation(() => ({ x: 5, y: 10, width: 400, height: 300 }));
1006 regionCaptureMock.mockReset().mockResolvedValue({ width: 800, height: 600, png });
1007 accessibilityForegroundMock.mockReset().mockResolvedValue({
1008 pid: 123,
1009 children: async () => [window("Settings", "Wrong window"), window("Editor", "Scaled text")],
1010 });
1011 let metadata = "";
1012 return Effect.scoped(
1013 Effect.gen(function* () {
1014 const service = yield* DesktopSnapShot.make;
1015 yield* service.configure(enabledSettings());
1016 yield* service.capture;
1017 const saved = yield* decodePendingMetadata(metadata);
1018 assert.deepEqual(regionCaptureMock.mock.calls[0]?.[0], active.bounds);
1019 assert.include(saved.source.accessibleText, "Scaled text");
1020 assert.deepEqual(
1021 saved.source.accessibility?.format === "element-tree"
1022 ? saved.source.accessibility.root.children[0]?.bounds
1023 : undefined,
1024 { x: 200, y: 200, width: 200, height: 100 },
1025 );
1026 }),
1027 ).pipe(
1028 Effect.provide(
1029 layerTest("win32", {
1030 makeDirectory: () => Effect.void,
1031 rename: () => Effect.void,
1032 writeFile: () => Effect.void,
1033 writeFileString: (_, text) =>
1034 Effect.sync(() => {
1035 metadata = text;
1036 }),
1037 }),
1038 ),
1039 Effect.ensuring(
1040 Effect.sync(() => {
1041 screenToDipRectMock.mockImplementation((_window, bounds) => bounds);
1042 }),
1043 ),
1044 );
1045 });
1046
1047 it.effect.each([
1048 { backend: "gnome", screenshot: "PNG" },
1049 { backend: "niri", screenshot: "PNG" },
1050 { backend: "kde", screenshot: "PNG" },
1051 { backend: "gnome", screenshot: "invalid" },
1052 ] as const)(
1053 "persists $backend accessibility text with a $screenshot screenshot",
1054 ({ backend, screenshot }) => {
1055 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1056 flashWindows.length = 0;
1057 const window = {
1058 title: "Editor",
1059 appName: "Text Editor",
1060 appIdentifier: "org.gnome.TextEditor.desktop",
1061 processId: 123,
1062 bounds: {
1063 x: backend === "niri" ? 0 : 479,
1064 y: backend === "niri" ? 0 : 342,
1065 width: 700,
1066 height: backend === "kde" ? 549 : 520,
1067 },
1068 ...(backend === "niri" ? { accessibilityBoundsReliable: false } : {}),
1069 ...(backend === "kde" ? { clientBounds: { x: 479, y: 371, width: 700, height: 520 } } : {}),
1070 };
1071 // The captured pixels differ from the compositor's logical window bounds.
1072 const imageSize = screenshot === "PNG" ? { width: 1400, height: 1040 } : window.bounds;
1073 const png =
1074 screenshot === "PNG"
1075 ? Buffer.from(
1076 "iVBORw0KGgoAAAANSUhEUgAABXgAAAQQAQAAAABwoA4ZAAAAyElEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHgYyx4AAepc/c0AAAAASUVORK5CYII=",
1077 "base64",
1078 )
1079 : Buffer.from([1, 2, 3]);
1080 linuxCaptureMock.mockResolvedValueOnce({ png, window });
1081 activeWindowMock.mockClear();
1082 getSourcesMock.mockClear();
1083 accessibilityByPidMock.mockReset().mockResolvedValue({
1084 children: async () => [
1085 {
1086 role: "window",
1087 name: "Editor",
1088 bounds: { x: 0, y: 0, width: 700, height: 520 },
1089 active: true,
1090 children: async () => [
1091 {
1092 role: "button",
1093 name: "Save",
1094 bounds: { x: 10, y: 20, width: 80, height: 24 },
1095 focused: true,
1096 actions: ["press"],
1097 children: async () => [],
1098 },
1099 ],
1100 tree: async () => ({ name: "Editor", value: "Verified text", children: [] }),
1101 },
1102 ],
1103 });
1104 let metadata = "";
1105 return Effect.scoped(
1106 Effect.gen(function* () {
1107 const service = yield* DesktopSnapShot.make;
1108 yield* service.configure(enabledSettings());
1109 yield* service.capture;
1110 assert.lengthOf(activeWindowMock.mock.calls, 0);
1111 assert.lengthOf(getSourcesMock.mock.calls, 0);
1112 assert.deepEqual(accessibilityByPidMock.mock.calls, [[123, { timeout: 0 }]]);
1113 const saved = yield* decodePendingMetadata(metadata);
1114 assert.equal(saved.source.appName, "Text Editor");
1115 assert.equal(saved.source.appIdentifier, window.appIdentifier);
1116 assert.include(saved.source.accessibleText, "Verified text");
1117 assert.deepInclude(saved.source.accessibility, {
1118 format: "element-tree",
1119 coordinateSpace: "captured-image",
1120 imageSize: { width: imageSize.width, height: imageSize.height },
1121 truncated: false,
1122 });
1123 assert.deepInclude(
1124 saved.source.accessibility?.format === "element-tree"
1125 ? saved.source.accessibility.root
1126 : undefined,
1127 {
1128 role: "window",
1129 name: "Editor",
1130 bounds: { x: 0, y: 0, width: imageSize.width, height: imageSize.height },
1131 state: { active: true },
1132 },
1133 );
1134 assert.isNull(
1135 saved.source.accessibility?.format === "element-tree"
1136 ? saved.source.accessibility.root.children[0]?.bounds
1137 : undefined,
1138 );
1139 assert.lengthOf(flashWindows, 0);
1140 }),
1141 ).pipe(
1142 Effect.provide(
1143 layerTest("linux", {
1144 makeDirectory: () => Effect.void,
1145 rename: () => Effect.void,
1146 writeFile: () => Effect.void,
1147 writeFileString: (_, text) =>
1148 Effect.sync(() => {
1149 metadata = text;
1150 }),
1151 }),
1152 ),
1153 Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
1154 );
1155 },
1156 );
1157
1158 it("covers straddling captures and intervening monitors without spanning unrelated displays", () => {
1159 const displays = [
1160 { bounds: { x: -1_920, y: 0, width: 1_920, height: 1_080 } },
1161 { bounds: { x: 0, y: 0, width: 1_440, height: 900 } },
1162 { bounds: { x: 1_440, y: 0, width: 1_280, height: 720 } },
1163 { bounds: { x: 0, y: -1_080, width: 1_920, height: 1_080 } },
1164 ];
1165 const source = { x: -200, y: 100, width: 800, height: 600 };
1166 assert.deepEqual(
1167 snapShotAnimationDisplayBounds(displays, source, source),
1168 displays.slice(0, 2).map((display) => display.bounds),
1169 );
1170 assert.deepEqual(
1171 snapShotAnimationDisplayBounds(
1172 displays,
1173 { x: -1_800, y: 100, width: 800, height: 600 },
1174 { x: 1_600, y: 200, width: 200, height: 112 },
1175 ),
1176 displays.slice(0, 3).map((display) => display.bounds),
1177 );
1178 });
1179
1180 it("cancels a late display while its snapshot is still decoding", async () => {
1181 flashWindows.length = 0;
1182 const transition = new SnapShotTransition({ boundOverlayToCaptureDisplays: true });
1183 const decoding = Promise.withResolvers<void>();
1184 const decoded = Promise.withResolvers<void>();
1185
1186 try {
1187 await transition.begin(
1188 "capture-1",
1189 { x: -1_800, y: 50, width: 900, height: 600 },
1190 "data:image/png;base64,",
1191 false,
1192 );
1193 transitionSnapshotMock.mockImplementationOnce(() => {
1194 decoding.resolve();
1195 return decoded.promise;
1196 });
1197 transition.animateTo("capture-1", {
1198 frame: { x: 600, y: 400, width: 208, height: 112 },
1199 backgroundColor: "#fff",
1200 borderColor: "#ccc",
1201 borderWidth: 1,
1202 cornerRadius: 8,
1203 scaleFactor: 1,
1204 });
1205 const landing = transition.waitForLanding("capture-1");
1206 await decoding.promise;
1207
1208 transition.dismiss("capture-1");
1209 decoded.resolve();
1210 await landing;
1211
1212 assert.lengthOf(flashWindows, 2);
1213 assert.strictEqual(flashWindows[1]?.showCount, 0);
1214 for (const window of flashWindows) {
1215 assert.isTrue(window.destroyed);
1216 }
1217 } finally {
1218 decoded.resolve();
1219 transition.dispose();
1220 }
1221 });
1222
1223 it("does not flash a dismissed overlay after its initial compositor receipt arrives", async () => {
1224 flashWindows.length = 0;
1225 const transition = new SnapShotTransition({ waitForCompositorFrame: true });
1226 const readingFrame = Promise.withResolvers<void>();
1227 const frame = Promise.withResolvers<void>();
1228 transitionCapturePageMock.mockImplementation(() => {
1229 readingFrame.resolve();
1230 return frame.promise;
1231 });
1232
1233 try {
1234 const begin = transition.begin(
1235 "capture-1",
1236 { x: 100, y: 50, width: 900, height: 600 },
1237 "data:image/png;base64,",
1238 true,
1239 );
1240 await readingFrame.promise;
1241 assert.isFalse(transition.isAnimating);
1242 transition.dismiss("capture-1");
1243 assert.isFalse(transition.isAnimating);
1244 frame.resolve();
1245 await begin;
1246
1247 for (const window of flashWindows) {
1248 assert.isTrue(window.destroyed);
1249 }
1250 } finally {
1251 frame.resolve();
1252 transition.dispose();
1253 }
1254 });
1255
1256 it("does not start a dismissed transition after its compositor receipt arrives", async () => {
1257 flashWindows.length = 0;
1258 const transition = new SnapShotTransition({ waitForCompositorFrame: true });
1259 const readingFrame = Promise.withResolvers<void>();
1260 const frame = Promise.withResolvers<void>();
1261 try {
1262 await transition.begin(
1263 "capture-1",
1264 { x: 100, y: 50, width: 900, height: 600 },
1265 "data:image/png;base64,",
1266 false,
1267 );
1268 transitionCapturePageMock.mockImplementation(() => {
1269 readingFrame.resolve();
1270 return frame.promise;
1271 });
1272 transition.animateTo("capture-1", {
1273 frame: { x: 600, y: 400, width: 208, height: 112 },
1274 backgroundColor: "#fff",
1275 borderColor: "#ccc",
1276 borderWidth: 1,
1277 cornerRadius: 8,
1278 scaleFactor: 1,
1279 });
1280 const landing = transition.waitForLanding("capture-1");
1281 await readingFrame.promise;
1282 assert.isTrue(transition.isAnimating);
1283 transition.dismiss("capture-1");
1284 assert.isFalse(transition.isAnimating);
1285 frame.resolve();
1286 await landing;
1287
1288 assert.isTrue(flashWindows[0]?.destroyed);
1289 } finally {
1290 frame.resolve();
1291 transition.dispose();
1292 }
1293 });
1294
1295 it("does not let a failed transition fail landing or capture completion", async () => {
1296 flashWindows.length = 0;
1297 transitionScriptState.rejectFlight = true;
1298 const transition = new SnapShotTransition();
1299
1300 try {
1301 await transition.begin(
1302 "capture-1",
1303 { x: 100, y: 50, width: 900, height: 600 },
1304 "data:image/png;base64,",
1305 false,
1306 );
1307 transition.animateTo("capture-1", {
1308 frame: { x: 20, y: 20, width: 208, height: 112 },
1309 backgroundColor: "#fff",
1310 borderColor: "#ccc",
1311 borderWidth: 1,
1312 cornerRadius: 8,
1313 scaleFactor: 1,
1314 });
1315
1316 await transition.waitForLanding("capture-1");
1317 assert.isFalse(transition.isAnimating);
1318 await transition.complete("capture-1");
1319
1320 assert.isTrue(flashWindows[0]?.destroyed);
1321 } finally {
1322 transitionScriptState.rejectFlight = false;
1323 transition.dispose();
1324 }
1325 });
1326
1327 it.each(["client", "frame"] as const)(
1328 "maps KDE %s accessibility coordinates into the decorated screenshot",
1329 async (rootArea) => {
1330 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1331 const bounds = { x: 100, y: 200, width: 800, height: 600 };
1332 const clientBounds = { x: 100, y: 229, width: 800, height: 571 };
1333 accessibilityByPidMock.mockReset().mockResolvedValue({
1334 children: async () => [
1335 {
1336 role: "window",
1337 name: "Editor",
1338 bounds: rootArea === "client" ? clientBounds : bounds,
1339 tree: async () => ({ name: "Editor", children: [{ name: "Save", children: [] }] }),
1340 children: async () => [
1341 {
1342 role: "button",
1343 name: "Save",
1344 bounds: { x: 120, y: 249, width: 100, height: 50 },
1345 children: async () => [],
1346 },
1347 ],
1348 },
1349 ],
1350 });
1351 try {
1352 const context = await readAccessibleWindowContext(
1353 { title: "Editor", bounds, clientBounds, owner: { processId: 123 } },
1354 "linux",
1355 "Editor",
1356 { width: 1600, height: 1200 },
1357 );
1358 assert.equal(context?.accessibility?.format, "element-tree");
1359 assert.deepEqual(
1360 context?.accessibility?.format === "element-tree"
1361 ? context.accessibility.root.children[0]?.bounds
1362 : undefined,
1363 { x: 40, y: 98, width: 200, height: 100 },
1364 );
1365 } finally {
1366 vi.unstubAllEnvs();
1367 }
1368 },
1369 );
1370
1371 it.each(["darwin", "win32", "linux"] as const)(
1372 "extracts the same structured accessibility tree on %s",
1373 async (platform) => {
1374 const bounds = { x: 100, y: 200, width: 800, height: 600 };
1375 const window = {
1376 role: "window",
1377 name: "Editor",
1378 bounds,
1379 tree: async () => ({
1380 name: "Editor",
1381 children: [
1382 { name: "Save", children: [] },
1383 { name: "Below scroll view", children: [] },
1384 ],
1385 }),
1386 children: async () => [
1387 {
1388 role: "button",
1389 name: "Save",
1390 bounds: { x: 300, y: 350, width: 100, height: 50 },
1391 children: async () => [],
1392 },
1393 {
1394 role: "static_text",
1395 name: "Below scroll view",
1396 bounds: { x: 300, y: 1_000, width: 100, height: 50 },
1397 visible: false,
1398 children: async () => [],
1399 },
1400 ],
1401 };
1402 accessibilityByPidMock.mockReset().mockResolvedValue({ children: async () => [window] });
1403 accessibilityForegroundMock
1404 .mockReset()
1405 .mockResolvedValue({ pid: 123, children: async () => [window] });
1406
1407 const result = await readAccessibleWindowContext(
1408 { title: "Editor", bounds, owner: { processId: 123 } },
1409 platform,
1410 "Editor",
1411 { width: 1_600, height: 1_200 },
1412 );
1413
1414 assert.equal(result?.accessibility?.format, "element-tree");
1415 assert.equal(
1416 result?.accessibility?.format === "element-tree"
1417 ? result.accessibility.root.children[0]?.name
1418 : undefined,
1419 "Save",
1420 );
1421 assert.deepInclude(
1422 result?.accessibility?.format === "element-tree"
1423 ? result.accessibility.root.children[1]
1424 : undefined,
1425 { name: "Below scroll view", bounds: null, state: { visible: false } },
1426 );
1427 assert.lengthOf(accessibilityByPidMock.mock.calls, platform === "win32" ? 0 : 1);
1428 assert.lengthOf(accessibilityForegroundMock.mock.calls, platform === "win32" ? 1 : 0);
1429 },
1430 );
1431
1432 it.each(["darwin", "win32"] as const)(
1433 "still requires matching accessibility screen positions on %s",
1434 async (platform) => {
1435 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1436 const tree = vi.fn(async () => ({ value: "Wrong window", children: [] }));
1437 const window = {
1438 name: "Editor",
1439 bounds: { x: 0, y: 0, width: 700, height: 520 },
1440 tree,
1441 };
1442 accessibilityByPidMock.mockReset().mockResolvedValue({ children: async () => [window] });
1443 accessibilityForegroundMock
1444 .mockReset()
1445 .mockResolvedValue({ pid: 123, children: async () => [window] });
1446 try {
1447 assert.isUndefined(
1448 await readAccessibleWindowText(
1449 {
1450 title: "Editor",
1451 bounds: { x: 479, y: 342, width: 700, height: 520 },
1452 owner: { processId: 123 },
1453 },
1454 platform,
1455 "Editor",
1456 ),
1457 );
1458 assert.lengthOf(tree.mock.calls, 0);
1459 } finally {
1460 vi.unstubAllEnvs();
1461 }
1462 },
1463 );
1464
1465 it.each([
1466 ["darwin", "", "value", 650],
1467 ["linux", "wayland", "value", 650],
1468 ["darwin", "", "name", 100],
1469 ] as const)(
1470 "preserves long text outside the scroll view on %s %s (%s)",
1471 async (platform, session, field, lines) => {
1472 const text = `${"Document line\n".repeat(lines)}End of document`;
1473 vi.stubEnv("XDG_SESSION_TYPE", session);
1474 const bounds = { x: 0, y: 0, width: 800, height: 600 };
1475 const window = {
1476 role: "window",
1477 name: "Editor",
1478 bounds,
1479 tree: async () => ({ name: "Editor", children: [{ [field]: text, children: [] }] }),
1480 children: async () => [
1481 { role: "text_area", [field]: text, bounds, children: async () => [] },
1482 ],
1483 };
1484 accessibilityByPidMock.mockReset().mockResolvedValue({ children: async () => [window] });
1485 accessibilityForegroundMock
1486 .mockReset()
1487 .mockResolvedValue({ pid: 123, children: async () => [window] });
1488 try {
1489 const result = await readAccessibleWindowContext(
1490 { title: "Editor", bounds, owner: { processId: 123 } },
1491 platform,
1492 "Editor",
1493 );
1494 assert.deepEqual(result?.accessibility, {
1495 format: "flat-text",
1496 text: `Editor\n${text}`,
1497 truncated: false,
1498 });
1499 } finally {
1500 vi.unstubAllEnvs();
1501 }
1502 },
1503 );
1504
1505 it.each([
1506 { names: ["⠙ t3code"], expected: "Verified text" },
1507 { names: ["⠋ t3code", "⠙ t3code"], expected: undefined },
1508 ])("reads a changing Wayland title only when unambiguous: $names", async ({ names, expected }) => {
1509 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1510 const tree = vi.fn(async () => ({ value: "Verified text", children: [] }));
1511 accessibilityByPidMock.mockReset().mockResolvedValue({
1512 children: async () =>
1513 names.map((name) => ({
1514 name,
1515 bounds: { x: 0, y: 0, width: 700, height: 520 },
1516 tree,
1517 })),
1518 });
1519 try {
1520 assert.strictEqual(
1521 await readAccessibleWindowText(
1522 {
1523 title: "⠋ t3code",
1524 bounds: { x: 479, y: 342, width: 700, height: 520 },
1525 owner: { processId: 123 },
1526 },
1527 "linux",
1528 "⠋ t3code",
1529 ),
1530 expected,
1531 );
1532 assert.deepEqual(accessibilityByPidMock.mock.calls, [[123, { timeout: 0 }]]);
1533 assert.lengthOf(tree.mock.calls, expected ? 1 : 0);
1534 } finally {
1535 vi.unstubAllEnvs();
1536 }
1537 });
1538
1539 it.each([
1540 ["com.google.Chrome", "Google Chrome – Profile"],
1541 ["org.chromium.Chromium", "Chromium"],
1542 ])("reads macOS %s page context with an accessibility title suffix", async (bundleId, suffix) => {
1543 const title = "Issues · pingdotgg/t3code";
1544 const bounds = { x: 0, y: 33, width: 1470, height: 857 };
1545 const window = {
1546 role: "window",
1547 name: `${title} - ${suffix}`,
1548 bounds,
1549 tree: async () => ({ name: title, children: [{ name: "New issue", children: [] }] }),
1550 children: async () => [{ role: "button", name: "New issue", bounds, children: async () => [] }],
1551 };
1552 accessibilityByPidMock.mockReset().mockImplementation(async (pid: number) => ({
1553 children: async () => (pid === 42 ? [window] : []),
1554 }));
1555
1556 const context = await readAccessibleWindowContext(
1557 { title, bounds, owner: { processId: 42, bundleId } },
1558 "darwin",
1559 title,
1560 );
1561
1562 assert.equal(context?.accessibleText, `${title}\nNew issue`);
1563 assert.equal(context?.accessibility?.format, "element-tree");
1564 assert.equal(
1565 context?.accessibility?.format === "element-tree"
1566 ? context.accessibility.root.children[0]?.name
1567 : undefined,
1568 "New issue",
1569 );
1570 });
1571
1572 it("does not borrow another Chrome process context when the captured PID has no windows", async () => {
1573 const title = "Issues · pingdotgg/t3code";
1574 const bounds = { x: 0, y: 33, width: 1470, height: 857 };
1575 const otherWindow = {
1576 role: "window",
1577 name: `${title} - Google Chrome – Profile`,
1578 bounds,
1579 tree: async () => ({
1580 name: title,
1581 children: [{ name: "Other process private text", children: [] }],
1582 }),
1583 children: async () => [],
1584 };
1585 accessibilityByPidMock.mockReset().mockResolvedValue({
1586 pid: 42,
1587 children: async () => [],
1588 });
1589 accessibilityListMock.mockResolvedValue([
1590 { pid: 43, bundleId: "com.google.Chrome", children: async () => [otherWindow] },
1591 ]);
1592
1593 assert.isUndefined(
1594 await readAccessibleWindowContext(
1595 { title, bounds, owner: { processId: 42, bundleId: "com.google.Chrome" } },
1596 "darwin",
1597 title,
1598 ),
1599 );
1600 });
1601
1602 it("reads Flatpak app text from the AT-SPI proxy when the compositor PID misses", async () => {
1603 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1604 const bounds = { x: 0, y: 0, width: 1_920, height: 1_048 };
1605 const tree = vi.fn(async () => ({
1606 name: "Issue — Zen Browser",
1607 children: [{ name: "New Tab", children: [] }],
1608 }));
1609 const proxyWindow = {
1610 role: "window",
1611 name: "Issue — Zen Browser",
1612 bounds,
1613 tree,
1614 children: async () => [],
1615 };
1616 accessibilityByPidMock
1617 .mockReset()
1618 .mockRejectedValue(
1619 new Error("XA11Y_SELECTOR_NOT_MATCHED: No element matched selector: application[pid=207651]"),
1620 );
1621 accessibilityListMock.mockReset().mockResolvedValue([
1622 {
1623 pid: 1,
1624 children: async () => [
1625 {
1626 role: "window",
1627 name: "Files",
1628 bounds,
1629 tree: async () => ({ name: "Files", children: [] }),
1630 },
1631 ],
1632 },
1633 { pid: 207646, children: async () => [proxyWindow] },
1634 ]);
1635 try {
1636 assert.strictEqual(
1637 await readAccessibleWindowText(
1638 {
1639 title: "Issue — Zen Browser",
1640 bounds: { x: 0, y: 0, width: 1_920, height: 1_048 },
1641 owner: { processId: 207651 },
1642 },
1643 "linux",
1644 "Issue — Zen Browser",
1645 ),
1646 "Issue — Zen Browser\nNew Tab",
1647 );
1648 assert.deepEqual(accessibilityByPidMock.mock.calls, [[207651, { timeout: 0 }]]);
1649 assert.lengthOf(accessibilityListMock.mock.calls, 1);
1650 assert.lengthOf(tree.mock.calls, 1);
1651 } finally {
1652 accessibilityListMock.mockReset().mockResolvedValue([]);
1653 vi.unstubAllEnvs();
1654 }
1655 });
1656
1657 it("reads GTK4 app text from one unnamed PID-scoped window of the captured size", async () => {
1658 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1659 const bounds = { x: 0, y: 0, width: 1_920, height: 1_048 };
1660 const tree = vi.fn(async () => ({
1661 name: "System Monitor",
1662 children: [{ name: "CPU", children: [] }],
1663 }));
1664 accessibilityByPidMock.mockReset().mockResolvedValue({
1665 children: async () => [
1666 {
1667 role: "group",
1668 name: null,
1669 bounds,
1670 tree,
1671 children: async () => [
1672 { role: "page_tab_list", name: "Processes", bounds, children: async () => [] },
1673 ],
1674 },
1675 ],
1676 });
1677 accessibilityListMock.mockReset().mockResolvedValue([]);
1678 try {
1679 assert.strictEqual(
1680 await readAccessibleWindowText(
1681 {
1682 title: "System Monitor",
1683 bounds: { x: 12, y: 48, width: 1_920, height: 1_048 },
1684 owner: { processId: 210600 },
1685 },
1686 "linux",
1687 "System Monitor",
1688 ),
1689 "System Monitor\nCPU",
1690 );
1691 assert.deepEqual(accessibilityByPidMock.mock.calls, [[210600, { timeout: 0 }]]);
1692 assert.lengthOf(accessibilityListMock.mock.calls, 0);
1693 assert.lengthOf(tree.mock.calls, 1);
1694 } finally {
1695 accessibilityListMock.mockReset().mockResolvedValue([]);
1696 vi.unstubAllEnvs();
1697 }
1698 });
1699
1700 it("does not guess GTK4 app text when two unnamed PID windows share the captured size", async () => {
1701 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1702 const bounds = { x: 0, y: 0, width: 1_920, height: 1_048 };
1703 const tree = vi.fn(async () => ({ value: "Wrong window", children: [] }));
1704 accessibilityByPidMock.mockReset().mockResolvedValue({
1705 children: async () => [
1706 { role: "group", name: null, bounds, tree },
1707 { role: "group", name: null, bounds: { ...bounds, x: 12 }, tree },
1708 ],
1709 });
1710 accessibilityListMock.mockReset().mockResolvedValue([
1711 {
1712 pid: 99,
1713 children: async () => [{ role: "window", name: "System Monitor", bounds, tree }],
1714 },
1715 ]);
1716 try {
1717 assert.isUndefined(
1718 await readAccessibleWindowText(
1719 {
1720 title: "System Monitor",
1721 bounds: { x: 12, y: 48, width: 1_920, height: 1_048 },
1722 owner: { processId: 210600 },
1723 },
1724 "linux",
1725 "System Monitor",
1726 ),
1727 );
1728 assert.lengthOf(accessibilityListMock.mock.calls, 0);
1729 assert.lengthOf(tree.mock.calls, 0);
1730 } finally {
1731 accessibilityListMock.mockReset().mockResolvedValue([]);
1732 vi.unstubAllEnvs();
1733 }
1734 });
1735
1736 it("keeps a truncated element tree when the flat text read fails", async () => {
1737 const bounds = { x: 0, y: 0, width: 800, height: 600 };
1738 accessibilityByPidMock.mockReset().mockResolvedValue({
1739 children: async () => [
1740 {
1741 role: "window",
1742 name: "Editor",
1743 bounds,
1744 tree: async () => {
1745 throw new Error("Text read failed");
1746 },
1747 children: async () => [
1748 {
1749 role: "text_area",
1750 value: "x".repeat(8_001),
1751 bounds: { x: 10, y: 50, width: 700, height: 500 },
1752 children: async () => [],
1753 },
1754 ],
1755 },
1756 ],
1757 });
1758
1759 const result = await readAccessibleWindowContext(
1760 { title: "Editor", bounds, owner: { processId: 123 } },
1761 "darwin",
1762 "Editor",
1763 );
1764 assert.equal(result?.accessibility?.format, "element-tree");
1765 assert.isTrue(result?.accessibility?.truncated);
1766 assert.deepEqual(
1767 result?.accessibility?.format === "element-tree"
1768 ? result.accessibility.root.children[0]?.bounds
1769 : undefined,
1770 { x: 10, y: 50, width: 700, height: 500 },
1771 );
1772 });
1773
1774 it.effect("flags revoked macOS permissions on read and re-registers once they return", () => {
1775 accessibilityTrustedMock.mockReset().mockReturnValue(true);
1776 mediaAccessStatusMock.mockReset().mockReturnValue("granted");
1777 registerShortcutMock.mockReset().mockReturnValue(true);
1778
1779 return Effect.scoped(
1780 Effect.gen(function* () {
1781 const service = yield* DesktopSnapShot.make;
1782 yield* service.configure({ ...DEFAULT_CLIENT_SETTINGS, snapShotEnabled: true });
1783 assert.isNull((yield* service.state).message);
1784
1785 mediaAccessStatusMock.mockReturnValue("denied");
1786 const revoked = yield* service.state;
1787 assert.equal(
1788 revoked.message,
1789 "Allow Screen Recording in System Settings, then restart T3 Code.",
1790 );
1791 assert.deepEqual(revoked.macPermissions, { screenRecording: false, accessibility: true });
1792
1793 accessibilityTrustedMock.mockReturnValue(false);
1794 yield* service.configure({
1795 ...DEFAULT_CLIENT_SETTINGS,
1796 snapShotEnabled: true,
1797 snapShotIncludeAccessibility: false,
1798 });
1799 const blocked = yield* service.state;
1800 assert.equal(
1801 blocked.message,
1802 "Allow Screen Recording in System Settings, then restart T3 Code.",
1803 );
1804 assert.isFalse(blocked.shortcutRegistered);
1805
1806 mediaAccessStatusMock.mockReturnValue("granted");
1807 const recovered = yield* service.state;
1808 assert.isNull(recovered.message);
1809 assert.isTrue(recovered.shortcutRegistered);
1810 }),
1811 ).pipe(Effect.provide(layerTest("darwin")));
1812 });
1813
1814 it.effect("an unrelated client-setting change keeps the registered shortcut", () => {
1815 registerShortcutMock.mockReset().mockReturnValue(true);
1816 unregisterShortcutMock.mockReset();
1817 const settings = enabledSettings({
1818 snapShotShortcut: {
1819 key: "k",
1820 metaKey: false,
1821 ctrlKey: true,
1822 shiftKey: false,
1823 altKey: true,
1824 modKey: false,
1825 },
1826 });
1827
1828 return Effect.scoped(
1829 Effect.gen(function* () {
1830 const service = yield* DesktopSnapShot.make;
1831 yield* service.configure(settings);
1832 assert.deepEqual(
1833 registerShortcutMock.mock.calls.map(([accelerator]) => accelerator),
1834 ["Control+Alt+K"],
1835 );
1836
1837 yield* service.configure({ ...settings, wordWrap: !settings.wordWrap });
1838 assert.lengthOf(unregisterShortcutMock.mock.calls, 0);
1839 assert.lengthOf(registerShortcutMock.mock.calls, 1);
1840 assert.isTrue((yield* service.state).shortcutRegistered);
1841
1842 yield* service.configure({
1843 ...settings,
1844 snapShotShortcut: { ...settings.snapShotShortcut, key: "j" },
1845 });
1846 assert.deepEqual(unregisterShortcutMock.mock.calls, [["Control+Alt+K"]]);
1847 assert.deepEqual(
1848 registerShortcutMock.mock.calls.map(([accelerator]) => accelerator),
1849 ["Control+Alt+K", "Control+Alt+J"],
1850 );
1851 }),
1852 ).pipe(Effect.provide(layerTest("win32")));
1853 });
1854
1855 it.effect("capture fails closed while snapshots are disabled", () => {
1856 activeWindowMock.mockClear();
1857 getSourcesMock.mockClear();
1858 regionCaptureMock.mockClear();
1859
1860 return Effect.scoped(
1861 Effect.gen(function* () {
1862 const service = yield* DesktopSnapShot.make;
1863 yield* service.configure({ ...DEFAULT_CLIENT_SETTINGS, snapShotEnabled: false });
1864 const failure = yield* Effect.flip(service.capture);
1865 assert.equal(failure.operation, "disabled");
1866 assert.lengthOf(activeWindowMock.mock.calls, 0);
1867 assert.lengthOf(getSourcesMock.mock.calls, 0);
1868 assert.lengthOf(regionCaptureMock.mock.calls, 0);
1869 }),
1870 ).pipe(Effect.provide(layerTest("win32")));
1871 });
1872
1873 it.effect("points capture setup failures at SnapShot settings", () => {
1874 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1875 linuxCaptureMock.mockRejectedValueOnce(
1876 new CaptureSetupRequiredError({ state: "Install the helper." }),
1877 );
1878 return Effect.scoped(
1879 Effect.gen(function* () {
1880 const service = yield* DesktopSnapShot.make;
1881 yield* service.configure(enabledSettings());
1882 const failure = yield* Effect.flip(service.capture);
1883 assert.equal(failure.operation, "setup-required");
1884 }),
1885 ).pipe(
1886 Effect.provide(layerTest("linux", { makeDirectory: () => Effect.void })),
1887 Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
1888 );
1889 });
1890
1891 it.effect(
1892 "keeps saved preferences but rechecks access and shortcut delivery after changing desktops",
1893 () => {
1894 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1895 vi.stubEnv("NIRI_SOCKET", "/test/niri.sock");
1896 vi.stubEnv("FLATPAK_ID", "");
1897 vi.stubEnv("SNAP", "");
1898 registerShortcutMock.mockReset().mockReturnValue(true);
1899 niriShortcutMock.mockReset().mockResolvedValue(niriShortcutStopMock);
1900 const settings: ClientSettings = {
1901 ...DEFAULT_CLIENT_SETTINGS,
1902 snapShotEnabled: true,
1903 snapShotShortcut: {
1904 key: "2",
1905 ctrlKey: true,
1906 shiftKey: true,
1907 altKey: false,
1908 metaKey: false,
1909 modKey: false,
1910 },
1911 snapShotPlaySound: false,
1912 snapShotAnimations: false,
1913 };
1914 return Effect.gen(function* () {
1915 for (const [desktop, backend] of [
1916 ["GNOME", "gnome-extension"],
1917 ["niri", "niri"],
1918 ["KDE", "kde"],
1919 ["Hyprland", "hyprland"],
1920 ["GNOME", "gnome-extension"],
1921 ] as const) {
1922 vi.stubEnv("XDG_CURRENT_DESKTOP", desktop);
1923 linuxBackendMock.mockResolvedValue(backend);
1924 yield* Effect.scoped(
1925 Effect.gen(function* () {
1926 const service = yield* DesktopSnapShot.make;
1927 yield* service.initialize;
1928 const state = yield* service.state;
1929 assert.equal(state.linuxDesktop, desktop.toLowerCase());
1930 assert.equal(state.linuxBackend, backend);
1931 assert.deepEqual(state.shortcut, settings.snapShotShortcut);
1932 assert.isFalse(state.shortcutVerified);
1933 assert.equal(state.gnomeExtension?.status, desktop === "GNOME" ? "enabled" : undefined);
1934 assert.equal(state.kdeHelper?.status, desktop === "KDE" ? "not-installed" : undefined);
1935 assert.equal(
1936 state.hyprlandHelper?.status,
1937 desktop === "Hyprland" ? "not-installed" : undefined,
1938 );
1939 assert.equal(
1940 Boolean(state.shortcutBinding),
1941 desktop === "niri" || desktop === "Hyprland",
1942 );
1943 const trigger =
1944 desktop === "niri"
1945 ? niriShortcutMock.mock.calls.at(-1)![1]
1946 : portalShortcutInstances.at(-1)!.onCapture;
1947 yield* Effect.promise(async () => {
1948 await trigger();
1949 });
1950 assert.isTrue((yield* service.state).shortcutVerified);
1951 }),
1952 ).pipe(Effect.provide(layerTest("linux", {}, Option.some(settings))));
1953 }
1954 }).pipe(
1955 Effect.ensuring(
1956 Effect.sync(() => {
1957 linuxBackendMock.mockResolvedValue("picker");
1958 vi.unstubAllEnvs();
1959 }),
1960 ),
1961 );
1962 },
1963 );
1964
1965 it.effect("verifies only native shortcut delivery and captures normally", () => {
1966 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
1967 vi.stubEnv("XDG_CURRENT_DESKTOP", "test-desktop");
1968 registerShortcutMock.mockReset().mockReturnValue(true);
1969 linuxCaptureMock.mockClear().mockResolvedValue({
1970 png: Buffer.from([1, 2, 3]),
1971 window: {
1972 title: "Shortcut capture",
1973 appName: "Text Editor",
1974 appIdentifier: "org.kde.kwrite",
1975 processId: 123,
1976 bounds: { x: 0, y: 0, width: 700, height: 520 },
1977 },
1978 });
1979 const settings = {
1980 ...DEFAULT_CLIENT_SETTINGS,
1981 snapShotEnabled: true,
1982 snapShotIncludeAccessibility: false,
1983 snapShotShortcut: {
1984 key: "2",
1985 ctrlKey: true,
1986 shiftKey: true,
1987 altKey: false,
1988 metaKey: false,
1989 modKey: false,
1990 },
1991 };
1992 return Effect.scoped(
1993 Effect.gen(function* () {
1994 const service = yield* DesktopSnapShot.make;
1995 yield* service.configure(settings);
1996 assert.isFalse((yield* service.state).shortcutVerified);
1997 yield* service.capture;
1998 assert.isFalse((yield* service.state).shortcutVerified);
1999 linuxCaptureMock.mockClear();
2000 const trigger = portalShortcutInstances.at(-1)!.onCapture;
2001 yield* service.setShortcutSuppressed(true);
2002 yield* Effect.promise(trigger);
2003 assert.isFalse((yield* service.state).shortcutVerified);
2004 assert.lengthOf(linuxCaptureMock.mock.calls, 0);
2005 yield* service.setShortcutSuppressed(false);
2006 yield* Effect.promise(trigger);
2007 assert.isTrue((yield* service.state).shortcutVerified);
2008 assert.lengthOf(linuxCaptureMock.mock.calls, 1);
2009 yield* service.setup("retry-shortcut");
2010 assert.equal(portalShortcutInstances.length, 1);
2011 assert.equal(portalShortcutInstances[0]!.configure.mock.calls.length, 1);
2012 yield* service.configure({ ...settings, snapShotEnabled: false });
2013 assert.isFalse((yield* service.state).shortcutVerified);
2014 }),
2015 ).pipe(
2016 Effect.provide(
2017 layerTest("linux", {
2018 makeDirectory: () => Effect.void,
2019 rename: () => Effect.void,
2020 writeFile: () => Effect.void,
2021 writeFileString: () => Effect.void,
2022 }),
2023 ),
2024 Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
2025 );
2026 });
2027
2028 it.effect(
2029 "preserves an approved portal session for cosmetic changes and retires it when keys change",
2030 () => {
2031 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
2032 const settings = {
2033 ...DEFAULT_CLIENT_SETTINGS,
2034 snapShotEnabled: true,
2035 snapShotShortcut: {
2036 key: "2",
2037 ctrlKey: true,
2038 shiftKey: true,
2039 altKey: false,
2040 metaKey: false,
2041 modKey: false,
2042 },
2043 };
2044 return Effect.scoped(
2045 Effect.gen(function* () {
2046 const service = yield* DesktopSnapShot.make;
2047 yield* service.configure(settings);
2048 const first = portalShortcutInstances[0]!;
2049 yield* Effect.promise(first.onCapture);
2050 yield* service.configure({
2051 ...settings,
2052 snapShotPlaySound: false,
2053 snapShotFlash: false,
2054 });
2055 assert.equal(portalShortcutInstances.length, 1);
2056 assert.equal(first.close.mock.calls.length, 0);
2057 assert.isTrue((yield* service.state).shortcutVerified);
2058 yield* service.configure({
2059 ...settings,
2060 snapShotShortcut: { ...settings.snapShotShortcut, key: "8" },
2061 });
2062 assert.equal(first.close.mock.calls.length, 1);
2063 assert.equal(portalShortcutInstances.length, 2);
2064 assert.isFalse((yield* service.state).shortcutVerified);
2065 yield* Effect.promise(first.onCapture);
2066 assert.isFalse((yield* service.state).shortcutVerified);
2067 const current = portalShortcutInstances[1]!;
2068 yield* Effect.promise(current.onCapture);
2069 assert.isTrue((yield* service.state).shortcutVerified);
2070 yield* service.configure({ ...settings, snapShotEnabled: false });
2071 assert.equal(current.close.mock.calls.length, 1);
2072 yield* Effect.promise(current.onCapture);
2073 assert.isFalse((yield* service.state).shortcutVerified);
2074 assert.isFalse((yield* service.state).shortcutRegistered);
2075 }),
2076 ).pipe(
2077 Effect.provide(layerTest("linux")),
2078 Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
2079 );
2080 },
2081 );
2082
2083 it.effect(
2084 "keeps Hyprland's stable action with a modifier-pair preference and releases it when disabled",
2085 () => {
2086 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
2087 vi.stubEnv("XDG_CURRENT_DESKTOP", "Hyprland");
2088 vi.stubEnv("FLATPAK_ID", "");
2089 vi.stubEnv("SNAP", "");
2090 nextPortalState.value = {
2091 shortcutRegistered: false,
2092 shortcutActionRegistered: true,
2093 shortcutPending: false,
2094 shortcutMessage: "Managed by Hyprland",
2095 };
2096 const settings = { ...DEFAULT_CLIENT_SETTINGS, snapShotEnabled: true };
2097 return Effect.scoped(
2098 Effect.gen(function* () {
2099 const service = yield* DesktopSnapShot.make;
2100 yield* service.configure(settings);
2101 assert.lengthOf(portalShortcutInstances, 1);
2102 const first = portalShortcutInstances[0]!;
2103 assert.isTrue((yield* service.state).shortcutActionRegistered);
2104 const check = yield* service.checkShortcut(settings.snapShotShortcut);
2105 assert.isFalse(check.available);
2106 assert.include(check.message, "Hyprland config");
2107 yield* service.configure({ ...settings, snapShotPlaySound: false });
2108 assert.lengthOf(portalShortcutInstances, 1);
2109 assert.lengthOf(first.close.mock.calls, 0);
2110 yield* service.configure({ ...settings, snapShotEnabled: false });
2111 assert.lengthOf(first.close.mock.calls, 1);
2112 assert.notEqual((yield* service.state).shortcutActionRegistered, true);
2113 }),
2114 ).pipe(
2115 Effect.provide(layerTest("linux")),
2116 Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
2117 );
2118 },
2119 );
2120
2121 it.effect("waits to apply settings while permissions are pending", () => {
2122 accessibilityTrustedMock.mockReturnValue(true);
2123 mediaAccessStatusMock.mockReturnValueOnce("not-determined").mockReturnValue("granted");
2124 let finishPermissionRequest: (() => void) | undefined;
2125 getSourcesMock.mockImplementationOnce(
2126 () =>
2127 new Promise<Array<never>>((resolve) => {
2128 finishPermissionRequest = () => resolve([]);
2129 }),
2130 );
2131 const layer = layerTest("darwin");
2132
2133 return Effect.scoped(
2134 Effect.gen(function* () {
2135 const service = yield* DesktopSnapShot.make;
2136 const enabled = { ...DEFAULT_CLIENT_SETTINGS, snapShotEnabled: true };
2137 const permissionFiber = yield* service.requestPermissions(true).pipe(Effect.forkScoped);
2138 yield* Effect.yieldNow;
2139 if (!finishPermissionRequest) throw new Error("Permission request did not start");
2140 const finishPermission = finishPermissionRequest;
2141
2142 const configureFiber = yield* service.configure(enabled).pipe(Effect.forkScoped);
2143 yield* Effect.yieldNow;
2144 assert.isFalse((yield* service.state).shortcutRegistered);
2145 finishPermission();
2146 yield* Fiber.join(permissionFiber);
2147 yield* Fiber.join(configureFiber);
2148
2149 const state = yield* service.state;
2150 assert.isTrue(state.shortcutRegistered);
2151 }),
2152 ).pipe(Effect.provide(layer));
2153 });
2154
2155 it.effect.each([false, true])(
2156 "tests macOS capture without publishing it and cleans up, failure=%s",
2157 (fails) => {
2158 const active = {
2159 platform: "macos",
2160 id: 42,
2161 title: "Setup",
2162 owner: { name: "T3 Code", processId: 123, path: "/Applications/T3 Code.app" },
2163 bounds: { x: 0, y: 0, width: 800, height: 600 },
2164 };
2165 activeWindowMock.mockReset().mockResolvedValue(active);
2166 macCaptureMock.mockReset();
2167 if (fails) macCaptureMock.mockRejectedValue(new Error("Capture denied"));
2168 else macCaptureMock.mockResolvedValue({ source: { name: "Setup" }, png: Buffer.from("png") });
2169 accessibilityProcessReadMock.mockClear();
2170 const cleanup = vi.fn();
2171 return Effect.scoped(
2172 Effect.gen(function* () {
2173 const service = yield* DesktopSnapShot.make;
2174 if (fails) {
2175 const error = yield* service.setup("test-mac-capture").pipe(Effect.flip);
2176 assert.equal(error.reason, "setup-failed");
2177 assert.equal(
2178 error.message,
2179 'Could not capture a test snapshot. Capturing the window failed. macOS grants Screen Recording and Accessibility to each app separately. In System Settings, open Privacy & Security and check that "T3 Code (Dev)" is allowed for both.',
2180 );
2181 } else yield* service.setup("test-mac-capture");
2182 assert.equal(macCaptureMock.mock.calls.length, 1);
2183 assert.equal(macCaptureMock.mock.calls[0]?.[0], active);
2184 assert.equal(macCaptureMock.mock.calls[0]?.[1], "/tmp/setup-test/test.png");
2185 assert.equal(cleanup.mock.calls.length, 1);
2186 assert.equal(accessibilityProcessReadMock.mock.calls.length, 0);
2187 assert.deepEqual(yield* service.listPending, []);
2188 }),
2189 ).pipe(
2190 Effect.provide(
2191 layerTest("darwin", {
2192 makeTempDirectoryScoped: () =>
2193 Effect.acquireRelease(Effect.succeed("/tmp/setup-test"), () =>
2194 Effect.sync(() => {
2195 cleanup();
2196 }),
2197 ),
2198 readDirectory: () => Effect.succeed([]),
2199 }),
2200 ),
2201 );
2202 },
2203 );