Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

2.1 KB
Raw Blame
// The real server, started as the desktop app starts it, behind a parent port that delivers as Electron's does: what the
// app posted while the server was starting is handed over once, to the listeners attached by then
// (server/testing/desktop-parent.mjs). The app's owner capability is such a message, and its own window is turned away
// to the pairing screen if the server never hears it.
import { randomBytes } from "node:crypto";
import { afterAll, beforeAll, expect, it } from "vitest";
import { launchVerificationServer } from "../scripts/control-omb.ts";

const owner = randomBytes(32).toString("base64url");
const realFetch = globalThis.fetch;
let fixture: Awaited<ReturnType<typeof launchVerificationServer>>;

beforeAll(async () => {
  // The launcher's own first requests are the app's, so they carry its capability, as its window's do.
  globalThis.fetch = (input, init = {}) => realFetch(input, { ...init, headers: { ...Object.fromEntries(new Headers(init.headers)), "x-openmausbot-desktop-owner": owner } });
  fixture = await launchVerificationServer({ ...process.env, OMB_TEST_DESKTOP_OWNER_TOKEN: owner });
}, 90_000);
afterAll(async () => {
  globalThis.fetch = realFetch;
  await fixture?.close();
});

it("hears the owner capability the app posted before the server's own receiver was attached", async () => {
  const { bots } = await (await realFetch(`${fixture.info.url}/api/bots?messages=0`)).json() as { bots: Array<{ id: string }> };
  const rename = (name: string, capability?: string) => realFetch(`${fixture.info.url}/api/bots/${bots[0].id}/profile`, {
    method: "PATCH",
    headers: { "content-type": "application/json", origin: fixture.info.url, ...(capability ? { "x-openmausbot-desktop-owner": capability } : {}) },
    body: JSON.stringify({ name }),
  });
  // Posted once, as the server starts, and never again here: only its arrival lets the app's own window make a change.
  expect((await rename("Heard", owner)).status).toBe(200);
  expect((await rename("No capability")).status).toBe(403);
  expect((await rename("Another capability", randomBytes(32).toString("base64url"))).status).toBe(403);
}, 60_000);