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
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import { vi } from "vite-plus/test"; |
| 3 | |
| 4 | import { |
| 5 | activateWindowsForegroundWithApi, |
| 6 | type WindowsForegroundApi, |
| 7 | } from "./WindowsForeground.ts"; |
| 8 | |
| 9 | function nativeHandle(value: bigint, bytes = 8): Buffer { |
| 10 | const handle = Buffer.alloc(bytes); |
| 11 | if (bytes === 8) handle.writeBigUInt64LE(value); |
| 12 | else handle.writeUInt32LE(Number(value)); |
| 13 | return handle; |
| 14 | } |
| 15 | |
| 16 | function makeApi(input: { |
| 17 | readonly foregroundWindow?: bigint; |
| 18 | readonly currentThreadId?: number; |
| 19 | readonly foregroundThreadId?: number; |
| 20 | readonly attached?: boolean; |
| 21 | readonly activated?: boolean; |
| 22 | }) { |
| 23 | return { |
| 24 | getCurrentThreadId: vi.fn(() => input.currentThreadId ?? 10), |
| 25 | getForegroundWindow: vi.fn(() => input.foregroundWindow ?? 99n), |
| 26 | getWindowClassName: vi.fn(() => "Chrome_WidgetWin_1"), |
| 27 | getWindowThreadId: vi.fn(() => input.foregroundThreadId ?? 20), |
| 28 | getWindowThreadAndProcessId: vi.fn(() => ({ |
| 29 | threadId: input.foregroundThreadId ?? 20, |
| 30 | processId: 500, |
| 31 | })), |
| 32 | getWindowText: vi.fn(() => ""), |
| 33 | getWindowRect: vi.fn(() => undefined), |
| 34 | getProcessImagePath: vi.fn(() => ""), |
| 35 | isKeyDown: vi.fn(() => false), |
| 36 | attachThreadInput: vi.fn((_source, _target, attach) => |
| 37 | attach ? (input.attached ?? true) : true, |
| 38 | ), |
| 39 | setForegroundWindow: vi.fn(() => input.activated ?? true), |
| 40 | } satisfies WindowsForegroundApi; |
| 41 | } |
| 42 | |
| 43 | describe("Windows foreground activation", () => { |
| 44 | it.each([4, 8])("activates a %i-byte native window handle", (bytes) => { |
| 45 | const api = makeApi({}); |
| 46 | const handle = nativeHandle(41n, bytes); |
| 47 | |
| 48 | assert.isTrue(activateWindowsForegroundWithApi(handle, api)); |
| 49 | |
| 50 | assert.deepEqual(api.attachThreadInput.mock.calls, [ |
| 51 | [10, 20, true], |
| 52 | [10, 20, false], |
| 53 | ]); |
| 54 | assert.deepEqual(api.setForegroundWindow.mock.calls, [[41n]]); |
| 55 | }); |
| 56 | |
| 57 | it("rejects malformed native handles", () => { |
| 58 | const api = makeApi({}); |
| 59 | |
| 60 | assert.throws(() => activateWindowsForegroundWithApi(Buffer.alloc(6), api)); |
| 61 | assert.lengthOf(api.getForegroundWindow.mock.calls, 0); |
| 62 | }); |
| 63 | }); |