|
1 | 1 | import * as NodeEvents from "node:events"; |
| 2 | +import * as NodeVM from "node:vm"; |
2 | 3 | import { beforeEach, expect, it, vi } from "vite-plus/test"; |
3 | 4 | import { watchMacSettingsWindow } from "./MacSettingsWindow.ts"; |
4 | 5 |
|
@@ -42,3 +43,29 @@ it("distinguishes unavailable tracking from a valid absent window", () => { |
42 | 43 | expect(unavailable).toHaveBeenCalledTimes(2); |
43 | 44 | stop(); |
44 | 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 | +}); |
0 commit comments