Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Option from "effect/Option";
6import * as Fiber from "effect/Fiber";
7import * as Layer from "effect/Layer";
8import type * as Electron from "electron";
9import { beforeEach, vi } from "vite-plus/test";
10
11const {
12 activateWindowsForegroundMock,
13 appFocusMock,
14 browserWindowMock,
15 foregroundWindowMock,
16 getAllWindowsMock,
17 getFocusedWindowMock,
18 loadWindowsForegroundApiMock,
19 shellHostedForegroundMock,
20 startWindowsForegroundFocusThreadMock,
21 windowsForegroundFocusMock,
22 windowsForegroundPrepareMock,
23 windowsForegroundCloseMock,
24} = vi.hoisted(() => ({
25 activateWindowsForegroundMock: vi.fn(),
26 appFocusMock: vi.fn(),
27 browserWindowMock: vi.fn(function BrowserWindowMock() {}),
28 foregroundWindowMock: vi.fn<() => bigint>(),
29 getAllWindowsMock: vi.fn(),
30 getFocusedWindowMock: vi.fn(),
31 loadWindowsForegroundApiMock: vi.fn<() => Promise<{ getForegroundWindow: () => bigint }>>(),
32 shellHostedForegroundMock: vi.fn(),
33 startWindowsForegroundFocusThreadMock: vi.fn(),
34 windowsForegroundFocusMock: vi.fn(),
35 windowsForegroundPrepareMock: vi.fn(),
36 windowsForegroundCloseMock: vi.fn(),
37}));
38
39vi.mock("./WindowsForeground.ts", () => ({
40 activateWindowsForeground: activateWindowsForegroundMock,
41 isWindowsShellHostedForeground: shellHostedForegroundMock,
42 loadWindowsForegroundApi: loadWindowsForegroundApiMock,
43}));
44
45vi.mock("./WindowsForegroundFocusThread.ts", () => ({
46 startWindowsForegroundFocusThread: startWindowsForegroundFocusThreadMock,
47}));
48
49vi.mock("electron", () => ({
50 app: {
51 focus: appFocusMock,
52 },
53 BrowserWindow: Object.assign(browserWindowMock, {
54 getAllWindows: getAllWindowsMock,
55 getFocusedWindow: getFocusedWindowMock,
56 }),
57}));
58
59import * as ElectronWindow from "./ElectronWindow.ts";
60
61const layerTestFor = (platform: NodeJS.Platform) =>
62 ElectronWindow.layer.pipe(Layer.provide(Layer.succeed(HostProcess.Platform, platform)));
63
64const layerTest = layerTestFor("linux");
65
66function makeBrowserWindow(input: { readonly id: number; readonly destroyed: boolean }) {
67 return {
68 id: input.id,
69 isDestroyed: vi.fn(() => input.destroyed),
70 webContents: { getType: () => "window" },
71 } as unknown as Electron.BrowserWindow;
72}
73
74function makeWindowsRevealWindow() {
75 return {
76 id: 41,
77 isDestroyed: vi.fn(() => false),
78 isFocused: vi.fn(() => false),
79 isMinimized: vi.fn(() => false),
80 isVisible: vi.fn(() => true),
81 show: vi.fn(),
82 moveTop: vi.fn(),
83 focus: vi.fn(),
84 getTitle: vi.fn(() => "T3 Code (Dev)"),
85 getBounds: vi.fn(() => ({ x: 100, y: 50, width: 1_200, height: 800 })),
86 getContentBounds: vi.fn(() => ({ x: 108, y: 50, width: 1_184, height: 792 })),
87 getNativeWindowHandle: vi.fn(() => Buffer.from([41, 0, 0, 0])),
88 restore: vi.fn(),
89 };
90}
91
92describe("ElectronWindow", () => {
93 it.effect(
94 "keeps offscreen previews out of app activation and broadcasts, and closes them on shutdown",
95 () =>
96 Effect.gen(function* () {
97 const preview = {
98 id: 2,
99 isDestroyed: () => false,
100 webContents: { getType: () => "offscreen", send: vi.fn() },
101 destroy: vi.fn(),
102 };
103 const main = {
104 id: 1,
105 isDestroyed: () => false,
106 webContents: { getType: () => "window", send: vi.fn() },
107 destroy: vi.fn(),
108 };
109 getAllWindowsMock.mockReturnValue([preview, main]);
110 const windows = yield* ElectronWindow.ElectronWindow;
111 assert.equal(Option.getOrNull(yield* windows.currentMainOrFirst)?.id, main.id);
112 yield* windows.sendAll("desktop:update", { ready: true });
113 assert.equal(preview.webContents.send.mock.calls.length, 0);
114 assert.deepEqual(main.webContents.send.mock.calls, [["desktop:update", { ready: true }]]);
115 const synced: number[] = [];
116 yield* windows.syncAllAppearance((window) =>
117 Effect.sync(() => {
118 synced.push(window.id);
119 }),
120 );
121 assert.deepEqual(synced, [1]);
122 yield* windows.destroyAll;
123 assert.equal(preview.destroy.mock.calls.length, 1);
124 assert.equal(main.destroy.mock.calls.length, 1);
125 }).pipe(Effect.provide(layerTest)),
126 );
127
128 beforeEach(() => {
129 activateWindowsForegroundMock.mockReset().mockResolvedValue(undefined);
130 appFocusMock.mockReset();
131 browserWindowMock.mockReset();
132 foregroundWindowMock.mockReset().mockReturnValue(0n);
133 getAllWindowsMock.mockReset();
134 getFocusedWindowMock.mockReset();
135 loadWindowsForegroundApiMock
136 .mockReset()
137 .mockImplementation(() => Promise.resolve({ getForegroundWindow: foregroundWindowMock }));
138 shellHostedForegroundMock.mockReset().mockResolvedValue(false);
139 startWindowsForegroundFocusThreadMock.mockReset().mockReturnValue({
140 prepare: windowsForegroundPrepareMock,
141 focus: windowsForegroundFocusMock,
142 close: windowsForegroundCloseMock,
143 });
144 windowsForegroundFocusMock.mockReset().mockResolvedValue(false);
145 windowsForegroundPrepareMock.mockReset().mockResolvedValue(false);
146 windowsForegroundCloseMock.mockReset();
147 });
148
149 it.effect("preserves schema-safe creation context and the Electron cause", () =>
150 Effect.gen(function* () {
151 const cause = new Error("native BrowserWindow construction failed");
152 browserWindowMock.mockImplementationOnce(function BrowserWindowFailure() {
153 throw cause;
154 });
155 const options = {
156 title: "T3 Code",
157 width: 1100,
158 height: 780,
159 minWidth: 840,
160 minHeight: 620,
161 show: false,
162 modal: false,
163 frame: true,
164 transparent: false,
165 backgroundColor: "#101010",
166 icon: {} as Electron.NativeImage,
167 webPreferences: {
168 preload: "/tmp/preload.js",
169 partition: "persist:t3code-preview-test",
170 sandbox: true,
171 contextIsolation: true,
172 nodeIntegration: false,
173 webviewTag: true,
174 spellcheck: true,
175 },
176 } satisfies Electron.BrowserWindowConstructorOptions;
177 const electronWindow = yield* ElectronWindow.ElectronWindow;
178
179 const error = yield* electronWindow.create(options).pipe(Effect.flip);
180
181 assert.instanceOf(error, ElectronWindow.ElectronWindowCreateError);
182 assert.deepEqual(error.options, {
183 title: "T3 Code",
184 width: 1100,
185 height: 780,
186 minWidth: 840,
187 minHeight: 620,
188 show: false,
189 modal: false,
190 frame: true,
191 transparent: false,
192 backgroundColor: "#101010",
193 webPreferences: {
194 preload: "/tmp/preload.js",
195 partition: "persist:t3code-preview-test",
196 backgroundThrottling: null,
197 sandbox: true,
198 contextIsolation: true,
199 nodeIntegration: false,
200 webviewTag: true,
201 },
202 });
203 assert.isFalse("icon" in error.options);
204 assert.isFalse("spellcheck" in error.options.webPreferences);
205 assert.strictEqual(error.cause, cause);
206 assert.equal(error.message, 'Failed to create Electron BrowserWindow "T3 Code" (1100x780).');
207 assert.notInclude(error.message, cause.message);
208 assert.deepEqual(browserWindowMock.mock.calls, [[options]]);
209 }).pipe(Effect.provide(layerTest)),
210 );
211
212 it.effect("skips windows destroyed before appearance sync runs", () =>
213 Effect.gen(function* () {
214 const liveWindow = makeBrowserWindow({ id: 1, destroyed: false });
215 const destroyedWindow = makeBrowserWindow({ id: 2, destroyed: true });
216 getAllWindowsMock.mockReturnValue([destroyedWindow, liveWindow]);
217
218 const syncedWindows: Electron.BrowserWindow[] = [];
219 const electronWindow = yield* ElectronWindow.ElectronWindow;
220 yield* electronWindow.syncAllAppearance((window) =>
221 Effect.sync(() => {
222 syncedWindows.push(window);
223 }),
224 );
225
226 assert.deepEqual(syncedWindows, [liveWindow]);
227 }).pipe(Effect.provide(layerTest)),
228 );
229
230 it.effect("preserves window enumeration failures as structured defects", () =>
231 Effect.gen(function* () {
232 const cause = new Error("window enumeration failed");
233 getAllWindowsMock.mockImplementationOnce(() => {
234 throw cause;
235 });
236
237 const electronWindow = yield* ElectronWindow.ElectronWindow;
238 const exit = yield* Effect.exit(electronWindow.currentMainOrFirst);
239
240 assert.equal(exit._tag, "Failure");
241 if (exit._tag === "Failure") {
242 const error = Cause.squash(exit.cause);
243 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
244 assert.equal(error.operation, "list-windows");
245 assert.equal(error.platform, "linux");
246 assert.isNull(error.windowId);
247 assert.isNull(error.channel);
248 assert.strictEqual(error.cause, cause);
249 assert.notInclude(error.message, cause.message);
250 }
251 }).pipe(Effect.provide(layerTest)),
252 );
253
254 it.effect("preserves reveal failures with the target window", () =>
255 Effect.gen(function* () {
256 const cause = new Error("window restore failed");
257 const window = {
258 id: 41,
259 isDestroyed: vi.fn(() => false),
260 isMinimized: vi.fn(() => true),
261 restore: vi.fn(() => {
262 throw cause;
263 }),
264 } as unknown as Electron.BrowserWindow;
265
266 const electronWindow = yield* ElectronWindow.ElectronWindow;
267 const exit = yield* Effect.exit(electronWindow.reveal(window));
268
269 assert.equal(exit._tag, "Failure");
270 if (exit._tag === "Failure") {
271 const error = Cause.squash(exit.cause);
272 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
273 assert.equal(error.operation, "reveal-window");
274 assert.equal(error.windowId, 41);
275 assert.isNull(error.channel);
276 assert.strictEqual(error.cause, cause);
277 }
278 }).pipe(Effect.provide(layerTest)),
279 );
280
281 it.effect("uses native Windows activation without querying the foreground window", () =>
282 Effect.gen(function* () {
283 const operations: Array<string> = [];
284 appFocusMock.mockImplementation(() => operations.push("app-focus"));
285 activateWindowsForegroundMock.mockImplementation(async () => {
286 operations.push("native-activation");
287 });
288 const window = {
289 ...makeWindowsRevealWindow(),
290 show: vi.fn(() => operations.push("show")),
291 moveTop: vi.fn(() => operations.push("move-top")),
292 focus: vi.fn(() => operations.push("focus")),
293 } as unknown as Electron.BrowserWindow;
294 const electronWindow = yield* ElectronWindow.ElectronWindow;
295 yield* electronWindow.prepareReveal(window);
296
297 yield* electronWindow.reveal(window);
298
299 assert.deepEqual(operations, ["app-focus", "show", "move-top", "focus", "native-activation"]);
300 assert.lengthOf(loadWindowsForegroundApiMock.mock.calls, 0);
301 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 0);
302 }).pipe(Effect.provide(layerTestFor("win32"))),
303 );
304
305 it.effect("focuses the exact T3 window before activating from a shell-hosted app", () =>
306 Effect.gen(function* () {
307 const operations: Array<string> = [];
308 shellHostedForegroundMock.mockResolvedValue(true);
309 windowsForegroundFocusMock.mockImplementation(async () => {
310 operations.push("native-focus");
311 return true;
312 });
313 activateWindowsForegroundMock.mockImplementation(async () => {
314 operations.push("native-activation");
315 });
316 const window = {
317 ...makeWindowsRevealWindow(),
318 show: vi.fn(() => operations.push("show")),
319 moveTop: vi.fn(() => operations.push("move-top")),
320 focus: vi.fn(() => operations.push("focus")),
321 } as unknown as Electron.BrowserWindow;
322 appFocusMock.mockImplementation(() => operations.push("app-focus"));
323 const electronWindow = yield* ElectronWindow.ElectronWindow;
324 yield* electronWindow.prepareReveal(window);
325
326 yield* electronWindow.reveal(window);
327
328 assert.deepEqual(operations, [
329 "app-focus",
330 "show",
331 "move-top",
332 "focus",
333 "native-focus",
334 "native-activation",
335 ]);
336 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 1);
337 assert.deepEqual(windowsForegroundFocusMock.mock.calls, [
338 [
339 {
340 windowId: 41,
341 processId: process.pid,
342 title: "T3 Code (Dev)",
343 bounds: { x: 100, y: 50, width: 1_200, height: 800 },
344 contentBounds: { x: 108, y: 50, width: 1_184, height: 792 },
345 },
346 ],
347 ]);
348 }).pipe(Effect.provide(layerTestFor("win32"))),
349 );
350
351 it.effect("prepares the exact T3 window before a capture overlay", () =>
352 Effect.gen(function* () {
353 windowsForegroundPrepareMock.mockResolvedValue(true);
354 const window = makeWindowsRevealWindow();
355 const electronWindow = yield* ElectronWindow.ElectronWindow;
356
357 const prepared = yield* electronWindow.prepareReveal(
358 window as unknown as Electron.BrowserWindow,
359 );
360
361 assert.isTrue(prepared);
362 assert.deepEqual(windowsForegroundPrepareMock.mock.calls, [
363 [
364 {
365 windowId: 41,
366 processId: process.pid,
367 title: "T3 Code (Dev)",
368 bounds: { x: 100, y: 50, width: 1_200, height: 800 },
369 contentBounds: { x: 108, y: 50, width: 1_184, height: 792 },
370 },
371 ],
372 ]);
373 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 0);
374 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 0);
375 }).pipe(Effect.provide(layerTestFor("win32"))),
376 );
377
378 it.effect.each([4, 8])(
379 "skips the focus worker only when the foreground matches the %i-byte HWND",
380 (handleBytes) =>
381 Effect.gen(function* () {
382 const window = makeWindowsRevealWindow();
383 activateWindowsForegroundMock.mockRejectedValueOnce(
384 new Error("Windows initially refused foreground activation"),
385 );
386 const hwnd = handleBytes === 4 ? 0xf123_4567n : 0x1_f123_4567n;
387 const handle = Buffer.alloc(handleBytes);
388 if (handleBytes === 4) handle.writeUInt32LE(Number(hwnd));
389 else handle.writeBigUInt64LE(hwnd);
390 window.getNativeWindowHandle.mockReturnValue(handle);
391 foregroundWindowMock.mockReturnValue(hwnd);
392 const electronWindow = yield* ElectronWindow.ElectronWindow;
393 yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow);
394 window.getTitle.mockClear();
395
396 yield* electronWindow.reveal(window as unknown as Electron.BrowserWindow);
397
398 assert.lengthOf(foregroundWindowMock.mock.calls, 1);
399 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 0);
400 assert.lengthOf(window.getTitle.mock.calls, 0);
401 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 2);
402 }).pipe(Effect.provide(layerTestFor("win32"))),
403 );
404
405 it.effect.each([42n, 0n])(
406 "focuses through the worker when the foreground HWND is %s instead of the target",
407 (foreground) =>
408 Effect.gen(function* () {
409 const window = makeWindowsRevealWindow();
410 activateWindowsForegroundMock.mockRejectedValueOnce(
411 new Error("Windows initially refused foreground activation"),
412 );
413 foregroundWindowMock.mockReturnValue(foreground);
414 const electronWindow = yield* ElectronWindow.ElectronWindow;
415 yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow);
416
417 yield* electronWindow.reveal(window as unknown as Electron.BrowserWindow);
418
419 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 1);
420 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 2);
421 }).pipe(Effect.provide(layerTestFor("win32"))),
422 );
423
424 it.effect("continues native focus when the foreground query fails", () =>
425 Effect.gen(function* () {
426 const window = makeWindowsRevealWindow();
427 activateWindowsForegroundMock.mockRejectedValueOnce(
428 new Error("Windows initially refused foreground activation"),
429 );
430 loadWindowsForegroundApiMock.mockRejectedValue(new Error("Foreground query unavailable"));
431 const electronWindow = yield* ElectronWindow.ElectronWindow;
432 yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow);
433
434 yield* electronWindow.reveal(window as unknown as Electron.BrowserWindow);
435
436 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 1);
437 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 2);
438 }).pipe(Effect.provide(layerTestFor("win32"))),
439 );
440
441 it.effect("does not fail reveal when native focus rejects", () =>
442 Effect.gen(function* () {
443 const window = makeWindowsRevealWindow();
444 activateWindowsForegroundMock.mockRejectedValueOnce(
445 new Error("Windows initially refused foreground activation"),
446 );
447 windowsForegroundFocusMock.mockRejectedValue(new Error("Focus rejected"));
448 const electronWindow = yield* ElectronWindow.ElectronWindow;
449 yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow);
450
451 yield* electronWindow.reveal(window as unknown as Electron.BrowserWindow);
452
453 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 1);
454 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 2);
455 }).pipe(Effect.provide(layerTestFor("win32"))),
456 );
457
458 it.effect("fails reveal when Windows refuses foreground activation", () =>
459 Effect.gen(function* () {
460 const cause = new Error("Windows refused foreground activation");
461 const window = makeWindowsRevealWindow();
462 activateWindowsForegroundMock.mockRejectedValue(cause);
463 const electronWindow = yield* ElectronWindow.ElectronWindow;
464 yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow);
465
466 const exit = yield* Effect.exit(
467 electronWindow.reveal(window as unknown as Electron.BrowserWindow),
468 );
469
470 assert.equal(exit._tag, "Failure");
471 if (exit._tag === "Failure") {
472 const error = Cause.squash(exit.cause);
473 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
474 assert.equal(error.operation, "reveal-window");
475 assert.strictEqual(error.cause, cause);
476 }
477 assert.deepEqual(activateWindowsForegroundMock.mock.calls, [
478 [window.getNativeWindowHandle.mock.results[0]?.value],
479 [window.getNativeWindowHandle.mock.results[1]?.value],
480 ]);
481 }).pipe(Effect.provide(layerTestFor("win32"))),
482 );
483
484 it.effect("cancels native focus when destroyed during the foreground query", () =>
485 Effect.gen(function* () {
486 const window = makeWindowsRevealWindow();
487 activateWindowsForegroundMock.mockRejectedValueOnce(
488 new Error("Windows initially refused foreground activation"),
489 );
490 const queryStarted = Promise.withResolvers<void>();
491 const foreground = Promise.withResolvers<{ getForegroundWindow: () => bigint }>();
492 loadWindowsForegroundApiMock.mockImplementation(() => {
493 queryStarted.resolve();
494 return foreground.promise;
495 });
496 const electronWindow = yield* ElectronWindow.ElectronWindow;
497 yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow);
498 window.getTitle.mockClear();
499
500 const revealFiber = yield* electronWindow
501 .reveal(window as unknown as Electron.BrowserWindow)
502 .pipe(Effect.forkChild({ startImmediately: true }));
503 yield* Effect.promise(() => queryStarted.promise);
504 window.isDestroyed.mockReturnValue(true);
505 foreground.resolve({ getForegroundWindow: foregroundWindowMock });
506 yield* Fiber.join(revealFiber);
507
508 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 0);
509 assert.lengthOf(window.getNativeWindowHandle.mock.calls, 1);
510 assert.lengthOf(window.getTitle.mock.calls, 0);
511 }).pipe(Effect.provide(layerTestFor("win32"))),
512 );
513
514 it.effect("preserves message delivery failures with window and channel context", () =>
515 Effect.gen(function* () {
516 const cause = new Error("renderer send failed");
517 const window = {
518 id: 42,
519 isDestroyed: vi.fn(() => false),
520 webContents: {
521 getType: () => "window",
522 send: vi.fn(() => {
523 throw cause;
524 }),
525 },
526 } as unknown as Electron.BrowserWindow;
527 getAllWindowsMock.mockReturnValueOnce([window]);
528
529 const electronWindow = yield* ElectronWindow.ElectronWindow;
530 const exit = yield* Effect.exit(electronWindow.sendAll("desktop:update", { ready: true }));
531
532 assert.equal(exit._tag, "Failure");
533 if (exit._tag === "Failure") {
534 const error = Cause.squash(exit.cause);
535 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
536 assert.equal(error.operation, "send-window-message");
537 assert.equal(error.windowId, 42);
538 assert.equal(error.channel, "desktop:update");
539 assert.strictEqual(error.cause, cause);
540 }
541 }).pipe(Effect.provide(layerTest)),
542 );
543
544 it.effect("preserves destroy failures and continues with later windows", () =>
545 Effect.gen(function* () {
546 const cause = new Error("window destroy failed");
547 const window = {
548 id: 43,
549 destroy: vi.fn(() => {
550 throw cause;
551 }),
552 } as unknown as Electron.BrowserWindow;
553 const laterWindow = {
554 id: 44,
555 destroy: vi.fn(),
556 } as unknown as Electron.BrowserWindow;
557 getAllWindowsMock.mockReturnValueOnce([window, laterWindow]);
558
559 const electronWindow = yield* ElectronWindow.ElectronWindow;
560 const exit = yield* Effect.exit(electronWindow.destroyAll);
561
562 assert.equal(exit._tag, "Failure");
563 if (exit._tag === "Failure") {
564 const error = Cause.squash(exit.cause);
565 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
566 assert.equal(error.operation, "destroy-window");
567 assert.equal(error.windowId, 43);
568 assert.isNull(error.channel);
569 assert.strictEqual(error.cause, cause);
570 }
571 assert.equal(vi.mocked(laterWindow.destroy).mock.calls.length, 1);
572 }).pipe(Effect.provide(layerTest)),
573 );
574 it.effect("an ordinary reveal on Windows does not touch the Win32 foreground helpers", () =>
575 Effect.gen(function* () {
576 const window = makeWindowsRevealWindow();
577 const electronWindow = yield* ElectronWindow.ElectronWindow;
578
579 yield* electronWindow.reveal(window as unknown as Electron.BrowserWindow);
580
581 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 0);
582 assert.lengthOf(windowsForegroundFocusMock.mock.calls, 0);
583 assert.lengthOf(shellHostedForegroundMock.mock.calls, 0);
584 assert.lengthOf(startWindowsForegroundFocusThreadMock.mock.calls, 0);
585 assert.lengthOf(window.focus.mock.calls, 1);
586 }).pipe(Effect.provide(layerTestFor("win32"))),
587 );
588
589 it.effect("a capture reveal on Windows uses the Win32 path only once", () =>
590 Effect.gen(function* () {
591 const window = makeWindowsRevealWindow();
592 const electronWindow = yield* ElectronWindow.ElectronWindow;
593
594 yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow);
595 yield* electronWindow.reveal(window as unknown as Electron.BrowserWindow);
596 yield* electronWindow.reveal(window as unknown as Electron.BrowserWindow);
597
598 assert.lengthOf(activateWindowsForegroundMock.mock.calls, 1);
599 assert.lengthOf(window.focus.mock.calls, 2);
600 }).pipe(Effect.provide(layerTestFor("win32"))),
601 );
602
603 it.effect("starts the Windows focus worker lazily and closes it with the layer", () =>
604 Effect.gen(function* () {
605 yield* ElectronWindow.ElectronWindow.pipe(Effect.provide(layerTestFor("win32")));
606 assert.lengthOf(startWindowsForegroundFocusThreadMock.mock.calls, 0);
607 assert.lengthOf(windowsForegroundCloseMock.mock.calls, 0);
608
609 yield* Effect.gen(function* () {
610 const electronWindow = yield* ElectronWindow.ElectronWindow;
611 const window = makeWindowsRevealWindow() as unknown as Electron.BrowserWindow;
612 yield* electronWindow.prepareReveal(window);
613 yield* electronWindow.prepareReveal(window);
614 assert.lengthOf(startWindowsForegroundFocusThreadMock.mock.calls, 1);
615 assert.lengthOf(windowsForegroundCloseMock.mock.calls, 0);
616 }).pipe(Effect.provide(layerTestFor("win32")));
617 assert.lengthOf(windowsForegroundCloseMock.mock.calls, 1);
618 }),
619 );
620});