Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
6.2 KB
Raw Blame
import { afterEach, beforeEach, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";

import { BOX_HEADER } from "../model-modes/model-modes.mjs";
import { BOX_NOTE, OFFLINE_NOTE, keyOf } from "../box/plugin-lib.js";
import plugin from "./omnirush-box-next.js";

type Hook = (event: any) => Promise<void>;
let dir = "";
let savedHome: string | undefined;

beforeEach(() => {
  dir = mkdtempSync(path.join(tmpdir(), "box-next-"));
  savedHome = process.env.OMNIRUSH_BOX_HOME;
  process.env.OMNIRUSH_BOX_HOME = path.join(dir, "box");
});
afterEach(() => {
  if (savedHome === undefined) delete process.env.OMNIRUSH_BOX_HOME;
  else process.env.OMNIRUSH_BOX_HOME = savedHome;
  rmSync(dir, { recursive: true, force: true });
});

/** A project folder whose box the desktop lists (on) or not, with its running record. */
function project(on: boolean): { root: string; key: string } {
  const root = path.join(dir, "proj");
  mkdirSync(root, { recursive: true });
  const key = keyOf(root);
  mkdirSync(path.join(dir, "box", "boxes"), { recursive: true });
  writeFileSync(path.join(dir, "box", "boxes", `${key}.json`), JSON.stringify({ state: "running", uid: 0 }));
  writeFileSync(path.join(dir, "box", "desktop-projects.json"), JSON.stringify({ keys: on ? [key] : [], pkglog_dir: path.join(dir, "pkglog") }));
  return { root, key };
}

/** The 2.x plugin set up the way the engine does it: one instance per project folder. */
async function engine(root: string) {
  const hooks = new Map<string, Hook>();
  const hook = async (name: string, callback: Hook) => { hooks.set(name, callback); };
  await plugin.setup({
    location: { directory: root },
    session: { get: async () => ({ data: {} }), hook } as any,
    shell: { hook: (name: string, callback: Hook) => hook(`shell:${name}`, callback) } as any,
    tool: { hook: (name: string, callback: Hook) => hook(`tool:${name}`, callback) } as any,
  });
  return hooks;
}

test("a box chat's shell calls carry its box key and chat, its requests say box, the prompt names the sandbox", async () => {
  const { root, key } = project(true);
  const hooks = await engine(root);
  await hooks.get("tool:execute.before")!({ tool: "shell", id: "call_1", sessionID: "ses_a", input: { command: "hostname" } });
  const shell = { command: "hostname", cwd: root, env: { PATH: "/usr/bin" } as Record<string, string | undefined> };
  await hooks.get("shell:create.before")!(shell);
  // boxsh (the engine's shell) runs the command in the box named by OMNIRUSH_BOX_KEY.
  expect(shell.env).toMatchObject({ OMNIRUSH_BOX_KEY: key, OMNIRUSH_CALL: "call_1", OMNIRUSH_SESSION: "ses_a", PATH: "/usr/bin" });
  const request = { sessionID: "ses_a", headers: {} as Record<string, string> };
  await hooks.get("model.request")!(request);
  expect(request.headers[BOX_HEADER]).toBe("1");
  const context = { sessionID: "ses_a", system: ["Platform: darwin", { type: "text", text: "rules" }] as unknown[] };
  await hooks.get("context")!(context);
  expect(context.system[0]).toBe("Platform: linux");
  expect(context.system[1]).toEqual({ type: "text", text: "rules" });
  expect(JSON.stringify(context.system.at(-1))).toContain("Sandbox: shell commands run in a Linux");
  // File tools stay in the project folder: the rest is the box's.
  await expect(hooks.get("tool:execute.before")!({ tool: "read", id: "call_2", sessionID: "ses_a", input: { filePath: "/etc/hostname" } })).rejects.toThrow(/outside the project folder/);
});

test("farm runs: the prompt gets the offline line only (no box note); other runs never get it", async () => {
  const { root } = project(true);
  const hooks = await engine(root);
  const saved = process.env.OMNIRUSH_FARM;
  const savedRun = process.env.OMNIRUSH_FARM_RUN;
  try {
    // A hackathon event's stock mode (no farm-run marker): stock, as before: no box note, no offline line.
    process.env.OMNIRUSH_FARM = "1";
    delete process.env.OMNIRUSH_FARM_RUN;
    const event = { sessionID: "ses_e", system: ["Platform: darwin"] as unknown[] };
    await hooks.get("context")!(event);
    expect(event.system).toEqual(["Platform: linux"]);
    process.env.OMNIRUSH_FARM_RUN = "1";
    for (const on of ["1", "true", "on", "yes"]) {
      process.env.OMNIRUSH_FARM = on;
      const farm = { sessionID: "ses_f", system: ["Platform: darwin"] as unknown[] };
      await hooks.get("context")!(farm);
      expect(farm.system).toEqual(["Platform: linux", { type: "text", text: OFFLINE_NOTE }]);
    }
    expect(OFFLINE_NOTE).toBe("This session is offline: the shell cannot reach the internet, except package installs (npm/pnpm/yarn, pip/uv, cargo, go, composer). Mock external services and APIs instead of calling them.");
    for (const off of [undefined, "0"]) {
      if (off === undefined) delete process.env.OMNIRUSH_FARM;
      else process.env.OMNIRUSH_FARM = off;
      const normal = { sessionID: "ses_n", system: ["Platform: darwin"] as unknown[] };
      await hooks.get("context")!(normal);
      expect(normal.system).toEqual(["Platform: linux", { type: "text", text: BOX_NOTE(root, 0, root, undefined) }]);
      expect(JSON.stringify(normal.system)).not.toContain("offline");
    }
  } finally {
    if (saved === undefined) delete process.env.OMNIRUSH_FARM;
    else process.env.OMNIRUSH_FARM = saved;
    if (savedRun === undefined) delete process.env.OMNIRUSH_FARM_RUN;
    else process.env.OMNIRUSH_FARM_RUN = savedRun;
  }
});

test("a project whose box is off is left alone: no box key, no box header, no note", async () => {
  const { root } = project(false);
  const hooks = await engine(root);
  await hooks.get("tool:execute.before")!({ tool: "shell", id: "call_1", sessionID: "ses_b", input: { command: "hostname" } });
  const shell = { command: "hostname", cwd: root, env: {} as Record<string, string | undefined> };
  await hooks.get("shell:create.before")!(shell);
  expect(shell.env.OMNIRUSH_BOX_KEY).toBeUndefined();
  const request = { sessionID: "ses_b", headers: {} as Record<string, string> };
  await hooks.get("model.request")!(request);
  expect(request.headers[BOX_HEADER]).toBeUndefined();
  const context = { sessionID: "ses_b", system: ["Platform: darwin"] as unknown[] };
  await hooks.get("context")!(context);
  expect(context.system).toEqual(["Platform: darwin"]);
});