Skip to content

Commit 5073bc6

Browse files
committed
test(desktop): exercise settings tracker parent exit
1 parent 0dc92f2 commit 5073bc6

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

‎apps/desktop/src/permissions/MacSettingsWindow.test.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as NodeEvents from "node:events";
2+
import * as NodeVM from "node:vm";
23
import { beforeEach, expect, it, vi } from "vite-plus/test";
34
import { watchMacSettingsWindow } from "./MacSettingsWindow.ts";
45

@@ -42,9 +43,29 @@ it("distinguishes unavailable tracking from a valid absent window", () => {
4243
expect(unavailable).toHaveBeenCalledTimes(2);
4344
stop();
4445
});
45-
it("stops the tracker once its parent exits", () => {
46+
it("polls while its parent lives and stops after reparenting to launchd", () => {
4647
watchMacSettingsWindow(vi.fn(), vi.fn())();
4748
const script = mocks.spawn.mock.lastCall?.[1][3];
48-
expect(script).toContain('ObjC.import("unistd");');
49-
expect(script).toContain("while ($.getppid() !== 1) {");
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"]]);
5071
});

0 commit comments

Comments
 (0)