/** * Tests for the engine event subscription in `ipc.ts`: payloads reach the * handler, the shell is told once that the frontend listens, and a failed * subscription never rejects. */ import { flushPromises } from "@vue/test-utils"; import { beforeEach, describe, expect, it, vi } from "vitest"; const invoke = vi.hoisted(() => vi.fn()); const listen = vi.hoisted(() => vi.fn()); vi.mock("@tauri-apps/api/core", () => ({ invoke })); vi.mock("@tauri-apps/api/event", () => ({ listen })); describe("onEngineEvent", () => { beforeEach(() => { vi.resetModules(); invoke.mockReset(); listen.mockReset(); invoke.mockResolvedValue(null); }); it("forwards payloads, announces readiness once and unlistens", async () => { const unlisten = vi.fn(); const callbacks: ((event: { payload: unknown }) => void)[] = []; listen.mockImplementation((_name: string, callback: (event: { payload: unknown }) => void) => { callbacks.push(callback); return Promise.resolve(unlisten); }); const { onEngineEvent, ENGINE_EVENT } = await import("../src/ipc"); const seen: unknown[] = []; const stop = onEngineEvent((event) => seen.push(event)); const stop2 = onEngineEvent(() => undefined); await flushPromises(); expect(listen).toHaveBeenCalledWith(ENGINE_EVENT, expect.any(Function)); expect(invoke).toHaveBeenCalledTimes(1); expect(invoke).toHaveBeenCalledWith("engine_ready"); callbacks[0]?.({ payload: { kind: "treeChanged", dir: "src" } }); expect(seen).toEqual([{ kind: "treeChanged", dir: "src" }]); stop(); stop2(); expect(unlisten).toHaveBeenCalledTimes(2); }); it("unlistens a subscription that was cancelled before it started", async () => { const unlisten = vi.fn(); listen.mockResolvedValue(unlisten); const { onEngineEvent } = await import("../src/ipc"); const stop = onEngineEvent(() => undefined); stop(); await flushPromises(); expect(unlisten).toHaveBeenCalledTimes(1); }); it("swallows a failing subscription outside the shell", async () => { listen.mockRejectedValue(new Error("no Tauri")); const { onEngineEvent } = await import("../src/ipc"); const stop = onEngineEvent(() => undefined); await flushPromises(); stop(); expect(invoke).not.toHaveBeenCalled(); }); });