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 * as NodeEvents from "node:events"; |
| 2 | import * as NodeVM from "node:vm"; |
| 3 | import { beforeEach, expect, it, vi } from "vite-plus/test"; |
| 4 | import { watchMacSettingsWindow } from "./MacSettingsWindow.ts"; |
| 5 | |
| 6 | const mocks = vi.hoisted(() => ({ spawn: vi.fn() })); |
| 7 | vi.mock("node:child_process", () => ({ spawn: mocks.spawn })); |
| 8 | let child: NodeEvents.EventEmitter & { |
| 9 | stdout: NodeEvents.EventEmitter & { setEncoding: ReturnType<typeof vi.fn> }; |
| 10 | kill: ReturnType<typeof vi.fn>; |
| 11 | }; |
| 12 | beforeEach(() => { |
| 13 | child = Object.assign(new NodeEvents.EventEmitter(), { |
| 14 | stdout: Object.assign(new NodeEvents.EventEmitter(), { setEncoding: vi.fn() }), |
| 15 | kill: vi.fn(), |
| 16 | }); |
| 17 | mocks.spawn.mockReturnValue(child); |
| 18 | }); |
| 19 | it("decodes partial updates and stops the one watcher process on disposal", () => { |
| 20 | const changed = vi.fn(); |
| 21 | const unavailable = vi.fn(); |
| 22 | const stop = watchMacSettingsWindow(changed, unavailable); |
| 23 | const state = { x: 10, y: 20, width: 723, height: 719, frontmost: true }; |
| 24 | const line = JSON.stringify(state); |
| 25 | child.stdout.emit("data", line.slice(0, 8)); |
| 26 | expect(changed).not.toHaveBeenCalled(); |
| 27 | child.stdout.emit("data", line.slice(8) + "\nnull\n"); |
| 28 | expect(changed.mock.calls).toEqual([[state], [null]]); |
| 29 | stop(); |
| 30 | expect(child.kill).toHaveBeenCalledOnce(); |
| 31 | child.emit("exit", 0); |
| 32 | expect(changed).toHaveBeenCalledTimes(2); |
| 33 | }); |
| 34 | it("distinguishes unavailable tracking from a valid absent window", () => { |
| 35 | const changed = vi.fn(); |
| 36 | const unavailable = vi.fn(); |
| 37 | const stop = watchMacSettingsWindow(changed, unavailable); |
| 38 | child.stdout.emit("data", '{"x":"bad"}\n'); |
| 39 | expect(changed).not.toHaveBeenCalled(); |
| 40 | expect(unavailable).toHaveBeenCalledOnce(); |
| 41 | child.emit("error", new Error("spawn failed")); |
| 42 | expect(changed).not.toHaveBeenCalled(); |
| 43 | expect(unavailable).toHaveBeenCalledTimes(2); |
| 44 | stop(); |
| 45 | }); |
| 46 | it("polls while its parent lives and stops after reparenting to launchd", () => { |
| 47 | watchMacSettingsWindow(vi.fn(), vi.fn())(); |
| 48 | const script = mocks.spawn.mock.lastCall?.[1][3]; |
| 49 | const imports = new Set<string>(); |
| 50 | const writeData = vi.fn(); |
| 51 | const poll = vi.fn(() => ({ count: 0 })); |
| 52 | const sleep = vi.fn(() => { |
| 53 | if (sleep.mock.calls.length > 2) throw new Error("tracker kept polling after parent exit"); |
| 54 | }); |
| 55 | const bridge = Object.assign((line: string) => ({ dataUsingEncoding: () => line }), { |
| 56 | getppid: () => { |
| 57 | if (!imports.has("unistd")) throw new Error("unistd bridge unavailable"); |
| 58 | return sleep.mock.calls.length < 2 ? 42 : 1; |
| 59 | }, |
| 60 | NSRunningApplication: { runningApplicationsWithBundleIdentifier: poll }, |
| 61 | NSFileHandle: { fileHandleWithStandardOutput: { writeData } }, |
| 62 | NSThread: { sleepForTimeInterval: sleep }, |
| 63 | NSUTF8StringEncoding: 4, |
| 64 | }); |
| 65 | NodeVM.runInNewContext(script + "\nrun();", { |
| 66 | ObjC: { import: (name: string) => imports.add(name) }, |
| 67 | $: bridge, |
| 68 | }); |
| 69 | expect(poll).toHaveBeenCalledTimes(2); |
| 70 | expect(writeData.mock.calls).toEqual([["null\n"]]); |
| 71 | }); |