Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
2.7 KB
Raw Blame
import assert from "node:assert/strict";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";

import { createUiControlServer } from "./ui-control-server.mjs";

test("UI control commands never activate the desktop window implicitly", async () => {
  const source = await readFile(new URL("./ui-control-server.mjs", import.meta.url), "utf8");

  assert.match(source, /webContents\.executeJavaScript/);
  assert.doesNotMatch(source, /\bwin\.(?:show|restore|focus)\(/);
  assert.match(source, /\/webmcp\/tools/);
  assert.match(source, /\/webmcp\/execute/);
  assert.ok(source.includes(
    "await executeWebMcpTool(await readJsonRequestBody(request), { signal: controller.signal })",
  ));
});

test("UI control failures are logged locally without exposing exception details", async () => {
  const userData = await mkdtemp(path.join(os.tmpdir(), "omnirush-ui-control-"));
  const failure = new Error("private renderer failure");
  const logged = [];
  const originalConsoleError = console.error;
  console.error = (...args) => logged.push(args);
  const server = createUiControlServer({
    app: { getPath: () => userData },
    appName: "OmniRush.ai",
    appIdentifier: "ai.omnirush.desktop",
    getWindow: async () => { throw failure; },
    browserTask: async () => { throw new Error("private website content"); },
    listWebMcpTools: () => ({ ok: false, error: "The built-in browser is not ready." }),
    executeWebMcpTool: () => ({ ok: false, error: "The built-in browser is not ready." }),
  });

  try {
    await server.start();
    const discovery = JSON.parse(await readFile(path.join(userData, "omnirush-ui-control.json"), "utf8"));
    const response = await fetch(`${discovery.baseUrl}/snapshot`, {
      headers: { Authorization: `Bearer ${discovery.token}` },
    });
    const payload = await response.json();

    assert.equal(response.status, 500);
    assert.deepEqual(payload, { ok: false, error: "OmniRush.ai UI control request failed." });
    assert.equal(logged[0]?.[0], "[ui-control] request failed");
    assert.equal(logged[0]?.[1], failure);
    assert.doesNotMatch(JSON.stringify(payload), /private renderer failure/);
    const browserResponse = await fetch(`${discovery.baseUrl}/browser/task`, {
      method: "POST", headers: { Authorization: `Bearer ${discovery.token}`, "Content-Type": "application/json" }, body: "{}",
    });
    assert.equal(browserResponse.status, 500);
    assert.doesNotMatch(JSON.stringify(await browserResponse.json()), /private website content/);
    assert.deepEqual(logged[1], ["[ui-control] request failed"]);
  } finally {
    await server.stop();
    console.error = originalConsoleError;
    await rm(userData, { recursive: true, force: true });
  }
});