Lutra/ui/tests/project/store.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

51 lines
2.1 KiB
TypeScript

/**
* Tests for the project store on its own: stale listings are dropped and
* events of unknown kinds are ignored.
*/
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 type { EngineEvent } from "../../src/components/project/engineEvents";
import { parentOf, useProjectStore } from "../../src/stores/project";
describe("project store", () => {
beforeEach(() => {
setActivePinia(createPinia());
invoke.mockReset();
});
it("drops a listing that was superseded by a newer request", async () => {
const resolvers: ((value: unknown) => void)[] = [];
invoke.mockImplementation(() => new Promise((resolve) => resolvers.push(resolve)));
const store = useProjectStore();
store.handleEvent({ type: "ProjectOpened", root: { path: "/p", name: "p" } });
const second = store.loadChildren("");
resolvers[1]?.({ dir: "", entries: [{ name: "new.php", path: "new.php", kind: "file", ignored: false, role: "normal" }], truncated: false });
await second;
resolvers[0]?.({ dir: "", entries: [{ name: "old.php", path: "old.php", kind: "file", ignored: false, role: "normal" }], truncated: false });
await Promise.resolve();
expect(store.rows.map((row) => (row.kind === "entry" ? row.entry.path : row.kind))).toEqual(["new.php"]);
});
it("shows a loading row while the root lists", () => {
invoke.mockImplementation(() => new Promise(() => undefined));
const store = useProjectStore();
store.handleEvent({ type: "ProjectOpened", root: { path: "/p", name: "p" } });
expect(store.rows).toEqual([{ kind: "loading", dir: "", depth: 0 }]);
});
it("ignores events it does not handle", () => {
const store = useProjectStore();
store.handleEvent({ type: "BufferOpened" } as unknown as EngineEvent);
expect(store.root).toBeNull();
expect(invoke).not.toHaveBeenCalled();
});
it("computes parents", () => {
expect(parentOf("a/b/c.php")).toBe("a/b");
expect(parentOf("c.php")).toBe("");
});
});