Open History to see the last commit
1import * as NodeEvents from "node:events";
2import { beforeEach, expect, it, vi } from "vite-plus/test";
3import { watchMacSettingsWindow } from "./MacSettingsWindow.ts";
4
5const mocks = vi.hoisted(() => ({ spawn: vi.fn() }));
6vi.mock("node:child_process", () => ({ spawn: mocks.spawn }));
7let child: NodeEvents.EventEmitter & {
8 stdout: NodeEvents.EventEmitter & { setEncoding: ReturnType<typeof vi.fn> };
9 kill: ReturnType<typeof vi.fn>;
10};
11beforeEach(() => {
12 child = Object.assign(new NodeEvents.EventEmitter(), {
13 stdout: Object.assign(new NodeEvents.EventEmitter(), { setEncoding: vi.fn() }),
14 kill: vi.fn(),
15 });
16 mocks.spawn.mockReturnValue(child);
17});
18it("decodes partial updates and stops the one watcher process on disposal", () => {
19 const changed = vi.fn();
20 const unavailable = vi.fn();
21 const stop = watchMacSettingsWindow(changed, unavailable);
22 const state = { x: 10, y: 20, width: 723, height: 719, frontmost: true };
23 const line = JSON.stringify(state);
24 child.stdout.emit("data", line.slice(0, 8));
25 expect(changed).not.toHaveBeenCalled();
26 child.stdout.emit("data", line.slice(8) + "\nnull\n");
27 expect(changed.mock.calls).toEqual([[state], [null]]);
28 stop();
29 expect(child.kill).toHaveBeenCalledOnce();
30 child.emit("exit", 0);
31 expect(changed).toHaveBeenCalledTimes(2);
32});
33it("distinguishes unavailable tracking from a valid absent window", () => {
34 const changed = vi.fn();
35 const unavailable = vi.fn();
36 const stop = watchMacSettingsWindow(changed, unavailable);
37 child.stdout.emit("data", '{"x":"bad"}\n');
38 expect(changed).not.toHaveBeenCalled();
39 expect(unavailable).toHaveBeenCalledOnce();
40 child.emit("error", new Error("spawn failed"));
41 expect(changed).not.toHaveBeenCalled();
42 expect(unavailable).toHaveBeenCalledTimes(2);
43 stop();
44});