1import { assert, beforeEach, it, vi } from "vite-plus/test";
2
3const { byPid, list, on, postMessage } = vi.hoisted(() => ({
4 byPid: vi.fn(),
5 list: vi.fn(),
6 on: vi.fn(),
7 postMessage: vi.fn(),
8}));
9
10vi.mock("node:worker_threads", () => ({ parentPort: { on, postMessage } }));
11vi.mock("@crowecawcaw/xa11y", () => ({ App: { byPid, list } }));
12
13beforeEach(() => {
14 vi.resetModules();
15 vi.resetAllMocks();
16});
17
18it.each(["lookup", "children"])("falls back to enumeration after a failed %s", async (failure) => {
19 const error = new Error("Unavailable");
20 if (failure === "lookup") byPid.mockRejectedValue(error);
21 else byPid.mockResolvedValue({ children: () => Promise.reject(error) });
22 const bounds = { x: 0, y: 0, width: 800, height: 600 };
23 const focus = vi.fn().mockResolvedValue(undefined);
24 list.mockResolvedValue([{ pid: 42, asElement: () => ({ name: "T3", bounds, focus }) }]);
25 await import("./WindowsForegroundFocusWorker.ts");
26 await vi.dynamicImportSettled();
27 const result = Promise.withResolvers<unknown>();
28 postMessage.mockImplementation(result.resolve);
29 on.mock.calls[0]![1]({
30 type: "focus",
31 requestId: 1,
32 target: { windowId: 1, processId: 42, title: "T3", bounds, contentBounds: bounds },
33 });
34 assert.deepEqual(await result.promise, { type: "result", requestId: 1, focused: true });
35 assert.lengthOf(focus.mock.calls, 1);
36});