The desktop host publishes every engine event, from commands and from a
pump thread that calls Engine::pump_vfs every 100 ms, to control channel
subscribers and to the window as the Tauri event engine://event, in engine
order. ui/src/ipc.ts gets a typed onEngineEvent; its first subscription
calls engine_ready, and the host replays the current project and buffers,
so a project opened from the command line (lutra-desktop --control PORT
PATH) reaches the UI. project.openDialog returns Effect::PickFolder; the
host shows the native folder picker (tauri-plugin-dialog, licence checked)
and opens the choice, one picker at a time, cancel is not an error.
Capabilities shrink to event listen/unlisten.
Contract fix: the M1.5 project UI assumed shapes the engine does not use.
engineEvents.ts now translates engine events ({kind: projectOpened, ...})
and project.children results ({dir, entries, truncated}) into the UI types,
keeping role and truncated; the store maps listings through
toTreeEntries() and the project tests use the real engine shapes.
Verified in the running app on Windows 11: tree of a project given on the
command line, expanding src, a file created on disk appearing live, the
folder picker opening one dialog and opening the chosen folder.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.3 KiB
TypeScript
62 lines
2.3 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("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();
|
|
});
|
|
});
|