The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Fiber from "effect/Fiber"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import type * as Electron from "electron"; |
| 8 | import { beforeEach, vi } from "vite-plus/test"; |
| 9 | |
| 10 | const { |
| 11 | activateWindowsForegroundMock, |
| 12 | appFocusMock, |
| 13 | browserWindowMock, |
| 14 | foregroundWindowMock, |
| 15 | getAllWindowsMock, |
| 16 | getFocusedWindowMock, |
| 17 | loadWindowsForegroundApiMock, |
| 18 | shellHostedForegroundMock, |
| 19 | startWindowsForegroundFocusThreadMock, |
| 20 | windowsForegroundFocusMock, |
| 21 | windowsForegroundPrepareMock, |
| 22 | windowsForegroundCloseMock, |
| 23 | } = vi.hoisted(() => ({ |
| 24 | activateWindowsForegroundMock: vi.fn(), |
| 25 | appFocusMock: vi.fn(), |
| 26 | browserWindowMock: vi.fn(function BrowserWindowMock() {}), |
| 27 | foregroundWindowMock: vi.fn<() => bigint>(), |
| 28 | getAllWindowsMock: vi.fn(), |
| 29 | getFocusedWindowMock: vi.fn(), |
| 30 | loadWindowsForegroundApiMock: vi.fn<() => Promise<{ getForegroundWindow: () => bigint }>>(), |
| 31 | shellHostedForegroundMock: vi.fn(), |
| 32 | startWindowsForegroundFocusThreadMock: vi.fn(), |
| 33 | windowsForegroundFocusMock: vi.fn(), |
| 34 | windowsForegroundPrepareMock: vi.fn(), |
| 35 | windowsForegroundCloseMock: vi.fn(), |
| 36 | })); |
| 37 | |
| 38 | vi.mock("./WindowsForeground.ts", () => ({ |
| 39 | activateWindowsForeground: activateWindowsForegroundMock, |
| 40 | isWindowsShellHostedForeground: shellHostedForegroundMock, |
| 41 | loadWindowsForegroundApi: loadWindowsForegroundApiMock, |
| 42 | })); |
| 43 | |
| 44 | vi.mock("./WindowsForegroundFocusThread.ts", () => ({ |
| 45 | startWindowsForegroundFocusThread: startWindowsForegroundFocusThreadMock, |
| 46 | })); |
| 47 | |
| 48 | vi.mock("electron", () => ({ |
| 49 | app: { |
| 50 | focus: appFocusMock, |
| 51 | }, |
| 52 | BrowserWindow: Object.assign(browserWindowMock, { |
| 53 | getAllWindows: getAllWindowsMock, |
| 54 | getFocusedWindow: getFocusedWindowMock, |
| 55 | }), |
| 56 | screen: { |
| 57 | // A 200% display, so the focus target must carry converted bounds. |
| 58 | dipToScreenRect: (_window: unknown, rect: Electron.Rectangle) => ({ |
| 59 | x: rect.x * 2, |
| 60 | y: rect.y * 2, |
| 61 | width: rect.width * 2, |
| 62 | height: rect.height * 2, |
| 63 | }), |
| 64 | }, |
| 65 | })); |
| 66 | |
| 67 | import * as ElectronWindow from "./ElectronWindow.ts"; |
| 68 | |
| 69 | const layerTestFor = (platform: NodeJS.Platform) => |
| 70 | ElectronWindow.layer.pipe(Layer.provide(Layer.succeed(HostProcess.Platform, platform))); |
| 71 | |
| 72 | const layerTest = layerTestFor("linux"); |
| 73 | |
| 74 | function 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 | |
| 92 | describe("ElectronWindow", () => { |
| 93 | beforeEach(() => { |
| 94 | activateWindowsForegroundMock.mockReset().mockResolvedValue(undefined); |
| 95 | appFocusMock.mockReset(); |
| 96 | browserWindowMock.mockReset(); |
| 97 | foregroundWindowMock.mockReset().mockReturnValue(0n); |
| 98 | getAllWindowsMock.mockReset(); |
| 99 | getFocusedWindowMock.mockReset(); |
| 100 | loadWindowsForegroundApiMock |
| 101 | .mockReset() |
| 102 | .mockImplementation(() => Promise.resolve({ getForegroundWindow: foregroundWindowMock })); |
| 103 | shellHostedForegroundMock.mockReset().mockResolvedValue(false); |
| 104 | startWindowsForegroundFocusThreadMock.mockReset().mockReturnValue({ |
| 105 | prepare: windowsForegroundPrepareMock, |
| 106 | focus: windowsForegroundFocusMock, |
| 107 | close: windowsForegroundCloseMock, |
| 108 | }); |
| 109 | windowsForegroundFocusMock.mockReset().mockResolvedValue(false); |
| 110 | windowsForegroundPrepareMock.mockReset().mockResolvedValue(false); |
| 111 | windowsForegroundCloseMock.mockReset(); |
| 112 | }); |
| 113 | |
| 114 | it.effect("cancels native focus when destroyed during the foreground query", () => |
| 115 | Effect.gen(function* () { |
| 116 | const window = makeWindowsRevealWindow(); |
| 117 | activateWindowsForegroundMock.mockRejectedValueOnce( |
| 118 | new Error("Windows initially refused foreground activation"), |
| 119 | ); |
| 120 | const queryStarted = Promise.withResolvers<void>(); |
| 121 | const foreground = Promise.withResolvers<{ getForegroundWindow: () => bigint }>(); |
| 122 | loadWindowsForegroundApiMock.mockImplementation(() => { |
| 123 | queryStarted.resolve(); |
| 124 | return foreground.promise; |
| 125 | }); |
| 126 | const electronWindow = yield* ElectronWindow.ElectronWindow; |
| 127 | yield* electronWindow.prepareReveal(window as unknown as Electron.BrowserWindow); |
| 128 | assert.deepEqual(windowsForegroundPrepareMock.mock.calls[0]?.[0], { |
| 129 | windowId: 41, |
| 130 | processId: process.pid, |
| 131 | title: "T3 Code (Dev)", |
| 132 | bounds: { x: 200, y: 100, width: 2_400, height: 1_600 }, |
| 133 | contentBounds: { x: 216, y: 100, width: 2_368, height: 1_584 }, |
| 134 | }); |
| 135 | window.getTitle.mockClear(); |
| 136 | |
| 137 | const revealFiber = yield* electronWindow |
| 138 | .reveal(window as unknown as Electron.BrowserWindow) |
| 139 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 140 | yield* Effect.promise(() => queryStarted.promise); |
| 141 | window.isDestroyed.mockReturnValue(true); |
| 142 | foreground.resolve({ getForegroundWindow: foregroundWindowMock }); |
| 143 | yield* Fiber.join(revealFiber); |
| 144 | |
| 145 | assert.lengthOf(windowsForegroundFocusMock.mock.calls, 0); |
| 146 | assert.lengthOf(window.getNativeWindowHandle.mock.calls, 1); |
| 147 | assert.lengthOf(window.getTitle.mock.calls, 0); |
| 148 | }).pipe(Effect.provide(layerTestFor("win32"))), |
| 149 | ); |
| 150 | |
| 151 | it.effect("reports which windows were created with a title bar overlay", () => |
| 152 | Effect.gen(function* () { |
| 153 | const framed = { isDestroyed: () => false }; |
| 154 | const overlay = { isDestroyed: () => false }; |
| 155 | browserWindowMock |
| 156 | .mockImplementationOnce(function FramedWindowMock() { |
| 157 | return framed; |
| 158 | }) |
| 159 | .mockImplementationOnce(function OverlayWindowMock() { |
| 160 | return overlay; |
| 161 | }); |
| 162 | const electronWindow = yield* ElectronWindow.ElectronWindow; |
| 163 | yield* electronWindow.create({}); |
| 164 | yield* electronWindow.create({ |
| 165 | titleBarStyle: "hidden", |
| 166 | titleBarOverlay: { color: "#01000000", height: 40 }, |
| 167 | }); |
| 168 | getAllWindowsMock.mockReturnValue([framed, overlay, { isDestroyed: () => false }]); |
| 169 | |
| 170 | const seen: Array<boolean> = []; |
| 171 | yield* electronWindow.syncAllAppearance((_window, { titleBarOverlay }) => |
| 172 | Effect.sync(() => seen.push(titleBarOverlay)), |
| 173 | ); |
| 174 | |
| 175 | assert.deepEqual(seen, [false, true, false]); |
| 176 | }).pipe(Effect.provide(layerTestFor("win32"))), |
| 177 | ); |
| 178 | |
| 179 | it.effect("preserves destroy failures and continues with later windows", () => |
| 180 | Effect.gen(function* () { |
| 181 | const cause = new Error("window destroy failed"); |
| 182 | const window = { |
| 183 | id: 43, |
| 184 | destroy: vi.fn(() => { |
| 185 | throw cause; |
| 186 | }), |
| 187 | } as unknown as Electron.BrowserWindow; |
| 188 | const laterWindow = { |
| 189 | id: 44, |
| 190 | destroy: vi.fn(), |
| 191 | } as unknown as Electron.BrowserWindow; |
| 192 | getAllWindowsMock.mockReturnValueOnce([window, laterWindow]); |
| 193 | |
| 194 | const electronWindow = yield* ElectronWindow.ElectronWindow; |
| 195 | const exit = yield* Effect.exit(electronWindow.destroyAll); |
| 196 | |
| 197 | assert.equal(exit._tag, "Failure"); |
| 198 | if (exit._tag === "Failure") { |
| 199 | const error = Cause.squash(exit.cause); |
| 200 | assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError); |
| 201 | assert.equal(error.operation, "destroy-window"); |
| 202 | assert.equal(error.windowId, 43); |
| 203 | assert.isNull(error.channel); |
| 204 | assert.strictEqual(error.cause, cause); |
| 205 | } |
| 206 | assert.equal(vi.mocked(laterWindow.destroy).mock.calls.length, 1); |
| 207 | }).pipe(Effect.provide(layerTest)), |
| 208 | ); |
| 209 | |
| 210 | it.effect("never falls back to an auxiliary window as the main window", () => |
| 211 | Effect.gen(function* () { |
| 212 | const electronWindow = yield* ElectronWindow.ElectronWindow; |
| 213 | const auxiliary = yield* electronWindow.create({}, { auxiliary: true }); |
| 214 | const ordinary = { isDestroyed: () => false } as unknown as Electron.BrowserWindow; |
| 215 | Object.assign(auxiliary, { isDestroyed: () => false }); |
| 216 | |
| 217 | getAllWindowsMock.mockReturnValue([auxiliary]); |
| 218 | assert.isTrue((yield* electronWindow.currentMainOrFirst)._tag === "None"); |
| 219 | |
| 220 | getAllWindowsMock.mockReturnValue([auxiliary, ordinary]); |
| 221 | const current = yield* electronWindow.currentMainOrFirst; |
| 222 | assert.isTrue(current._tag === "Some" && current.value === ordinary); |
| 223 | }).pipe(Effect.provide(layerTest)), |
| 224 | ); |
| 225 | }); |