Lutra/ui/tests/project/engineEvents.test.ts
danny dd98636de4 M1.4: desktop bridge: engine events to the window, folder picker, live project tree
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>
2026-10-15 06:41:49 +02:00

105 lines
4.4 KiB
TypeScript

/**
* Tests for the adapter between the engine contract and the project UI:
* `project.children` results and engine events are translated without
* losing kind, ignore flag, role or truncation.
*/
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
const invoke = vi.hoisted(() => vi.fn());
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
import {
emitEngineEvent,
onEngineEvent,
toProjectEvent,
toTreeEntries,
type EngineEvent,
} from "../../src/components/project/engineEvents";
import { useProjectStore } from "../../src/stores/project";
describe("toTreeEntries", () => {
it("maps kinds, ignore flags and roles", () => {
const listing = toTreeEntries({
dir: "",
entries: [
{ name: "src", path: "src", kind: "directory", ignored: false, role: "normal" },
{ name: "vendor", path: "vendor", kind: "directory", ignored: false, role: "library" },
{ name: "node_modules", path: "node_modules", kind: "directory", ignored: false, role: "excluded" },
{ name: "a.log", path: "a.log", kind: "file", ignored: true, role: "normal" },
{ name: "dangling", path: "dangling", kind: "other", ignored: false, role: "normal" },
],
truncated: true,
});
expect(listing).toEqual({
truncated: true,
entries: [
{ name: "src", path: "src", kind: "dir", ignored: false, role: "normal", excluded: false, special: false },
{ name: "vendor", path: "vendor", kind: "dir", ignored: false, role: "library", excluded: false, special: false },
{ name: "node_modules", path: "node_modules", kind: "dir", ignored: false, role: "excluded", excluded: true, special: false },
{ name: "a.log", path: "a.log", kind: "file", ignored: true, role: "normal", excluded: false, special: false },
{ name: "dangling", path: "dangling", kind: "file", ignored: false, role: "normal", excluded: false, special: true },
],
});
});
it("skips malformed entries and rejects results without entries", () => {
expect(toTreeEntries({ entries: [{ name: "x" }, 3, null, { name: "a", path: "a", kind: "file" }] })).toEqual({
truncated: false,
entries: [{ name: "a", path: "a", kind: "file", ignored: false, role: "normal", excluded: false, special: false }],
});
expect(toTreeEntries([])).toBeNull();
expect(toTreeEntries(null)).toBeNull();
expect(toTreeEntries({ entries: "no" })).toBeNull();
});
});
describe("toProjectEvent", () => {
it("translates project events and ignores the rest", () => {
expect(toProjectEvent({ kind: "projectOpened", root: "/p/demo", name: "demo" })).toEqual({
type: "ProjectOpened",
root: { path: "/p/demo", name: "demo" },
});
expect(toProjectEvent({ kind: "projectClosed" })).toEqual({ type: "ProjectClosed" });
expect(toProjectEvent({ kind: "treeChanged", dir: "src" })).toEqual({ type: "TreeChanged", dir: "src" });
expect(toProjectEvent({ kind: "bufferOpened", id: 1, path: "a.php" })).toBeNull();
});
it("delivers only translated events to project handlers", () => {
const seen: EngineEvent[] = [];
const stop = onEngineEvent((event) => seen.push(event));
emitEngineEvent({ kind: "dirtyChanged", id: 1, dirty: true });
emitEngineEvent({ kind: "treeChanged", dir: "" });
stop();
emitEngineEvent({ kind: "treeChanged", dir: "late" });
expect(seen).toEqual([{ type: "TreeChanged", dir: "" }]);
});
});
describe("project store with the engine result", () => {
beforeEach(() => {
setActivePinia(createPinia());
invoke.mockReset();
});
it("keeps truncation and role from project.children", async () => {
invoke.mockResolvedValue({
dir: "",
entries: [{ name: "vendor", path: "vendor", kind: "directory", ignored: false, role: "library" }],
truncated: true,
});
const store = useProjectStore();
store.handleEvent({ type: "ProjectOpened", root: { path: "/p", name: "p" } });
await store.loadChildren("");
expect(store.dirs[""]?.truncated).toBe(true);
expect(store.dirs[""]?.entries?.[0]?.role).toBe("library");
});
it("reports an unexpected result as an error instead of throwing", async () => {
invoke.mockResolvedValue(["old", "shape"]);
const store = useProjectStore();
store.handleEvent({ type: "ProjectOpened", root: { path: "/p", name: "p" } });
await store.loadChildren("");
expect(store.dirs[""]?.error?.code).toBe("ipc");
});
});