Skip to content

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

3.1 KB
Raw Blame
import { randomBytes } from "node:crypto";
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";

import { launchVerificationServer, type VerificationServer } from "../scripts/control-omb.ts";

describe("Reload Settings in the desktop app's menu", () => {
  let fixture: VerificationServer;
  const owner = randomBytes(32).toString("base64url");
  const SECRET = "sk-e2e-never-shown-77";
  const file = () => join(fixture.info.dataDir, "config.json");
  const saved = () => { try { return JSON.parse(readFileSync(file(), "utf8")) as Record<string, unknown>; } catch { return {}; } };
  const call = async (method: string, path: string, headers: Record<string, string> = {}) => {
    const response = await fetch(`${fixture.info.url}${path}`, { method, headers: { "x-openmausbot-desktop-owner": owner, ...headers } });
    return { status: response.status, body: await response.json().catch(() => ({})) as Record<string, any> };
  };

  beforeAll(async () => {
    const realFetch = globalThis.fetch;
    globalThis.fetch = (input, init = {}) => realFetch(input, { ...init, headers: { ...Object.fromEntries(new Headers(init.headers)), "x-openmausbot-desktop-owner": owner } });
    try {
      fixture = await launchVerificationServer({ ...process.env, OMB_TEST_DESKTOP_OWNER_TOKEN: owner });
    } finally {
      globalThis.fetch = realFetch;
    }
  });
  afterAll(async () => {
    await fixture?.close();
  });

  it("puts a hand edit in use, keeps the settings on a broken file, and answers only the app's own main process", async () => {
    writeFileSync(file(), JSON.stringify({ ...saved(), everyBotRules: "Answer in one line.", rooms: { turnTimeoutMinutes: 42 } }, null, 2));
    const reloaded = await call("POST", "/api/config/reload");
    expect(reloaded.status).toBe(200);
    expect(reloaded.body).toMatchObject({ ok: true, restart: [] });
    expect(reloaded.body.applied).toEqual(expect.arrayContaining(["everyBotRules", "rooms"]));
    expect((await call("GET", "/api/config")).body).toMatchObject({ everyBotRules: "Answer in one line.", rooms: { turnTimeoutMinutes: 42 } });

    const good = readFileSync(file(), "utf8");
    writeFileSync(file(), good.replace('"everyBotRules"', `"anthropic": { "key": "${SECRET}" }\n  "everyBotRules"`));
    const broken = await call("POST", "/api/config/reload");
    expect(broken.status).toBe(422);
    expect(broken.body).toMatchObject({ ok: false, code: "json" });
    expect(broken.body.error).toMatch(/^config\.json line \d+, column \d+: /);
    expect(JSON.stringify(broken.body)).not.toContain(SECRET);
    expect((await call("GET", "/api/config")).body).toMatchObject({ everyBotRules: "Answer in one line.", rooms: { turnTimeoutMinutes: 42 } });

    writeFileSync(file(), good);
    // The app's own page sends the owner capability too, but as a browser: a page may not reload.
    expect((await call("POST", "/api/config/reload", { origin: fixture.info.url, "sec-fetch-site": "same-origin" })).status).toBe(403);
    expect((await call("POST", "/api/config/reload", { "x-openmausbot-desktop-owner": "" })).status).toBe(403);
  });
});