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>
This commit is contained in:
parent
69130d1efb
commit
dd98636de4
23 changed files with 751 additions and 99 deletions
62
ui/tests/ipcEvents.test.ts
Normal file
62
ui/tests/ipcEvents.test.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
* 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();
|
||||
});
|
||||
});
|
||||
|
|
@ -11,12 +11,26 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|||
const invoke = vi.hoisted(() => vi.fn());
|
||||
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
|
||||
|
||||
import { emitEngineEvent, type TreeEntry } from "../../src/components/project/engineEvents";
|
||||
import { emitEngineEvent } from "../../src/components/project/engineEvents";
|
||||
import ProjectToolWindow from "../../src/components/project/ProjectToolWindow.vue";
|
||||
import { useProjectStore } from "../../src/stores/project";
|
||||
|
||||
/** An entry as the engine's `project.children` returns it. */
|
||||
interface EngineEntry {
|
||||
/** Display name. */
|
||||
name: string;
|
||||
/** Project-relative path. */
|
||||
path: string;
|
||||
/** Engine kind. */
|
||||
kind: "directory" | "file" | "other";
|
||||
/** Matched by an ignore rule. */
|
||||
ignored: boolean;
|
||||
/** Lutra's role for the entry. */
|
||||
role: "normal" | "library" | "excluded";
|
||||
}
|
||||
|
||||
/** In-memory folder listings by directory, as `project.children` returns them. */
|
||||
let folder: Record<string, TreeEntry[]>;
|
||||
let folder: Record<string, EngineEntry[]>;
|
||||
|
||||
/**
|
||||
* Builds an entry.
|
||||
|
|
@ -25,8 +39,10 @@ let folder: Record<string, TreeEntry[]>;
|
|||
* @param kind - entry kind.
|
||||
* @param ignored - ignore flag.
|
||||
*/
|
||||
function entry(path: string, kind: "dir" | "file", ignored = false): TreeEntry {
|
||||
return { name: path.split("/").pop() ?? path, path, kind, ignored };
|
||||
function entry(path: string, kind: "dir" | "file", ignored = false): EngineEntry {
|
||||
const engineKind = kind === "dir" ? "directory" : "file";
|
||||
const role = path === "node_modules" ? "excluded" : path === "vendor" ? "library" : "normal";
|
||||
return { name: path.split("/").pop() ?? path, path, kind: engineKind, ignored, role };
|
||||
}
|
||||
|
||||
/** Mock engine: answers `project.children`, accepts other commands. */
|
||||
|
|
@ -38,7 +54,7 @@ function engine(command: string, args: { command: string; params: { dir?: string
|
|||
const dir = args.params.dir ?? "";
|
||||
const entries = folder[dir];
|
||||
return entries
|
||||
? Promise.resolve(entries)
|
||||
? Promise.resolve({ dir, entries, truncated: false })
|
||||
: Promise.reject(Object.assign(new Error(`cannot list ${dir}`), { code: "not_found" }));
|
||||
}
|
||||
return Promise.resolve(null);
|
||||
|
|
@ -47,7 +63,7 @@ function engine(command: string, args: { command: string; params: { dir?: string
|
|||
/** Mounts the tool window and opens the mock project through an event. */
|
||||
async function mountOpen(): Promise<VueWrapper> {
|
||||
const wrapper = mount(ProjectToolWindow, { attachTo: document.body });
|
||||
emitEngineEvent({ type: "ProjectOpened", root: { path: "/tmp/demo", name: "demo" } });
|
||||
emitEngineEvent({ kind: "projectOpened", root: "/tmp/demo", name: "demo" });
|
||||
await flushPromises();
|
||||
return wrapper;
|
||||
}
|
||||
|
|
@ -183,7 +199,7 @@ describe("ProjectToolWindow", () => {
|
|||
await press(wrapper, "ArrowRight");
|
||||
expect(selectedPath(wrapper)).toBe("src/Controller");
|
||||
folder.src = [entry("src/App.php", "file"), entry("src/Kernel.php", "file")];
|
||||
emitEngineEvent({ type: "TreeChanged", dir: "src" });
|
||||
emitEngineEvent({ kind: "treeChanged", dir: "src" });
|
||||
await flushPromises();
|
||||
expect(paths(wrapper)).toEqual(["src", "src/App.php", "src/Kernel.php", "vendor", "node_modules", "index.php"]);
|
||||
expect(selectedPath(wrapper)).toBeNull();
|
||||
|
|
@ -194,7 +210,7 @@ describe("ProjectToolWindow", () => {
|
|||
|
||||
it("ignores TreeChanged for directories never loaded", async () => {
|
||||
wrapper = await mountOpen();
|
||||
emitEngineEvent({ type: "TreeChanged", dir: "vendor" });
|
||||
emitEngineEvent({ kind: "treeChanged", dir: "vendor" });
|
||||
await flushPromises();
|
||||
expect(childrenCalls()).toEqual([""]);
|
||||
});
|
||||
|
|
@ -211,7 +227,7 @@ describe("ProjectToolWindow", () => {
|
|||
it("keeps old entries when a refresh fails", async () => {
|
||||
wrapper = await mountOpen();
|
||||
Reflect.deleteProperty(folder, "");
|
||||
emitEngineEvent({ type: "TreeChanged", dir: "" });
|
||||
emitEngineEvent({ kind: "treeChanged", dir: "" });
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-test=tree-error]").text()).toBe("cannot list");
|
||||
expect(paths(wrapper)).toEqual(["src", "vendor", "node_modules", "index.php"]);
|
||||
|
|
@ -219,12 +235,12 @@ describe("ProjectToolWindow", () => {
|
|||
|
||||
it("returns to the empty state on ProjectClosed and unsubscribes on unmount", async () => {
|
||||
wrapper = await mountOpen();
|
||||
emitEngineEvent({ type: "ProjectClosed" });
|
||||
emitEngineEvent({ kind: "projectClosed" });
|
||||
await flushPromises();
|
||||
expect(wrapper.find("[data-test=project-empty]").exists()).toBe(true);
|
||||
wrapper.unmount();
|
||||
wrapper = null;
|
||||
emitEngineEvent({ type: "ProjectOpened", root: { path: "/tmp/other", name: "other" } });
|
||||
emitEngineEvent({ kind: "projectOpened", root: "/tmp/other", name: "other" });
|
||||
expect(useProjectStore().root).toBeNull();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
105
ui/tests/project/engineEvents.test.ts
Normal file
105
ui/tests/project/engineEvents.test.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
/**
|
||||
* 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");
|
||||
});
|
||||
});
|
||||
|
|
@ -23,9 +23,9 @@ describe("project store", () => {
|
|||
const store = useProjectStore();
|
||||
store.handleEvent({ type: "ProjectOpened", root: { path: "/p", name: "p" } });
|
||||
const second = store.loadChildren("");
|
||||
resolvers[1]?.([{ name: "new.php", path: "new.php", kind: "file", ignored: false }]);
|
||||
resolvers[1]?.({ dir: "", entries: [{ name: "new.php", path: "new.php", kind: "file", ignored: false, role: "normal" }], truncated: false });
|
||||
await second;
|
||||
resolvers[0]?.([{ name: "old.php", path: "old.php", kind: "file", ignored: false }]);
|
||||
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"]);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue