Lutra/ui/tests/ipc.test.ts
danny 20d1d28c32 M3.5: pending outcomes for commands that wait for the language server
Step 1 of M3.5: the shared mechanism. M3.4 and M3.5 build on it.

- Engine: `Outcome.pending: Option<Pending>`. `Pending::new(rx, timeout,
  finish)`; `Pending::wait()` is called without the engine lock and
  returns at the answer, a dropped sender or the deadline (timeout + 1 s).
  `Engine::resolve(answered)` then runs `finish(state, reply)` inside the
  guard, under the lock. `run_waiting(lock, cmd, params, publish)` does
  all three steps for hosts.
- Hosts: `Host::run`, `testkit::EngineBackend` and the control test
  backend use `run_waiting`.
- New IPC `engine_execute_waiting` (`#[tauri::command(async)]`, off the
  main thread) with `runWaiting()` in `ui/src/ipc.ts`. Only `lsp.*`
  requests use it.

Ordering guarantee:
- `engine_execute` stays synchronous on the main thread, so ordinary
  commands (`buffer.edit`) still run in call order.
- The order towards the server is fixed under the engine lock:
  `buffer.edit` queues its didChange on the session's FIFO queue before
  it returns, and `lsp::submit` queues the request on the same queue.
  A request therefore sees every edit whose `buffer.edit` returned
  first, and the UI awaits the sync's `idle()` before sending one.

Failures:
- A stall is caught by the request timeout, with the deadline as
  backstop.
- On restart, close, quit or crash the request is dropped or resolved
  as stopped, so the waiter gets an error, never a hang.

Tests: `crates/engine/tests/pending.rs` (answer, lock free while
waiting, dropped sender, deadline, resolve, lsp.restart while a
request is pending) and Vitest for `runWaiting`. Gate: 13 steps,
321 s, Vitest 443 + 1 skipped.

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

68 lines
2.5 KiB
TypeScript

/**
* Tests for the engine bridge: results and every kind of rejection map to
* a `RunResult` instead of a rejected promise.
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
const invoke = vi.hoisted(() => vi.fn());
vi.mock("@tauri-apps/api/core", () => ({ invoke }));
import { listCommands, run, runWaiting, toEngineError } from "../src/ipc";
describe("run", () => {
beforeEach(() => {
invoke.mockReset();
});
it("passes command and params to the shell", async () => {
invoke.mockResolvedValue({ name: "Lutra" });
const result = await run("app.about", { verbose: true });
expect(invoke).toHaveBeenCalledWith("engine_execute", {
command: "app.about",
params: { verbose: true },
});
expect(result).toEqual({ ok: true, value: { name: "Lutra" } });
});
it("returns engine errors instead of rejecting", async () => {
invoke.mockRejectedValue({ code: "unknown_command", message: "no such command: x" });
const result = await run("x");
expect(result).toEqual({
ok: false,
error: { code: "unknown_command", message: "no such command: x" },
});
});
it("wraps unexpected rejections", async () => {
invoke.mockRejectedValue(new Error("window.__TAURI_INTERNALS__ is undefined"));
const result = await run("app.about");
expect(result.ok).toBe(false);
expect(result.ok ? null : result.error.code).toBe("ipc");
});
it("lists commands", async () => {
invoke.mockResolvedValue([]);
expect(await listCommands()).toEqual({ ok: true, value: [] });
expect(invoke).toHaveBeenCalledWith("engine_list_commands");
});
it("runs waiting commands through the async shell command", async () => {
invoke.mockResolvedValue({ locations: [] });
const result = await runWaiting("lsp.definition", { id: 1 });
expect(result).toEqual({ ok: true, value: { locations: [] } });
expect(invoke).toHaveBeenCalledWith("engine_execute_waiting", {
command: "lsp.definition",
params: { id: 1 },
});
invoke.mockRejectedValue({ code: "failed", message: "the PHP language server is not running" });
const failed = await runWaiting("lsp.definition", { id: 1 });
expect(failed.ok ? null : failed.error.code).toBe("failed");
});
});
describe("toEngineError", () => {
it("handles strings and malformed objects", () => {
expect(toEngineError("boom")).toEqual({ code: "ipc", message: "boom" });
expect(toEngineError({ code: 1, message: "x" }).code).toBe("ipc");
});
});