Lutra/ui/tests/ipcEvents.test.ts
danny f41df518b5 M1.12: notifications as toasts
The host publishes picker, command-line and pump errors as notification
events (Engine::notify, same id sequence) instead of stderr, keeps
notifications and watchFailed published before engine_ready and replays
them after the state replay, so startup warnings (corrupt settings,
journal entries) reach the UI. ipc.ts announces engine_ready once all
subscriptions started together are registered.

Frontend: notifications store and NotificationStack (bottom right, info
auto-dismissed after 5 s, warnings/errors stay, close button and Escape,
at most 5 visible with "+n more", role status/alert); watchFailed maps to
a warning toast; failed commands without their own UI raise an error
toast. Catalog keys notification.settings.*, project.*, vfs.*, watch.*,
command.* plus the runtime allow-list. Docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 16:20:45 +02:00

95 lines
3.4 KiB
TypeScript

/**
* 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("announces readiness only after every subscription started together is registered", async () => {
const resolvers: ((fn: () => void) => void)[] = [];
listen.mockImplementation(
() =>
new Promise<() => void>((resolve) => {
resolvers.push(resolve);
}),
);
const { onEngineEvent } = await import("../src/ipc");
onEngineEvent(() => undefined);
const stopSecond = onEngineEvent(() => undefined);
resolvers[0]?.(() => undefined);
await flushPromises();
expect(invoke).not.toHaveBeenCalled();
stopSecond();
resolvers[1]?.(() => undefined);
await flushPromises();
expect(invoke).toHaveBeenCalledTimes(1);
expect(invoke).toHaveBeenCalledWith("engine_ready");
});
it("does not announce readiness without a live subscription", async () => {
listen.mockResolvedValue(() => undefined);
const { onEngineEvent } = await import("../src/ipc");
const stop = onEngineEvent(() => undefined);
stop();
await flushPromises();
expect(invoke).not.toHaveBeenCalled();
onEngineEvent(() => undefined);
await flushPromises();
expect(invoke).toHaveBeenCalledTimes(1);
});
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();
});
});