← Files Codex ReplayARCHIVED FILE

worker/tests/worker-policy.test.mjs

2.42 KB · Nov 20, 2026 · 06:32 UTC

↓ Download file

See the change to this file →

import assert from "node:assert/strict";
import { mkdtemp, writeFile, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { readRuntimeConfig, replayConfig, runtimeEnvironment } from "../src/worker-policy.mjs";

// Exercise the JSON-RPC launch gate, including unsupported/failed discovery.
for (const [label, result, allowed] of [
  ["unmanaged runtime", { requirements: null }, true],
  ["managed network runtime", { requirements: { network: { enabled: true, allowLocalBinding: true } } }, true],
  ["incomplete requirements response", {}, false],
]) {
  test(`configuration gate: ${label}`, async () => {
    const root = await mkdtemp(path.join(os.tmpdir(), "replay-policy-"));
    try {
      const target = path.join(root, "cli.mjs");
      await writeFile(target, `#!${process.execPath}\nimport { createInterface } from 'node:readline';
for await (const line of createInterface({input:process.stdin})) {
 const m=JSON.parse(line);
 if (m.id) console.log(JSON.stringify({id:m.id,result:m.method==='config/read'?{config:{model:'fixture'}}:m.method==='configRequirements/read'?${JSON.stringify(result)}:{}}));
}`, { mode: 0o700 });
      const read = readRuntimeConfig(target, runtimeEnvironment(), root);
      if (allowed) assert.equal((await read).model, "fixture");
      else await assert.rejects(read, /Cannot read/);
    } finally { await rm(root, { recursive: true, force: true }); }
  });
}

test("worker removes parent secrets and overrides inherited shell/MCP capabilities", () => {
  assert.deepEqual(runtimeEnvironment({ PATH: "/bin", CODEX_API_KEY: "runtime-auth", SECRET: "canary" }),
    { PATH: "/bin", CODEX_API_KEY: "runtime-auth" });
  const config = replayConfig({ shell_environment_policy: { set: { SECRET: "canary", HOME: "/real-home" } },
    mcp_servers: { "fixture.with.dots": { command: "touch" }, remote: { url: "https://example.invalid" } } }, "/workspace", "/workspace/.replay-tmp");
  assert.equal(config.shell_environment_policy.inherit, "none");
  assert.equal(config.shell_environment_policy.set.SECRET, "");
  assert.equal(config.shell_environment_policy.set.HOME, "/workspace/.replay-tmp");
  assert.equal(config.mcp_servers["fixture.with.dots"].enabled, false);
  assert.equal(config.mcp_servers["fixture.with.dots"].command, "");
  assert.equal(config.mcp_servers.remote.enabled, false);
  assert.equal(config.sandbox_workspace_write.network_access, false);
});

SHA-256: 19fd3ebbee5b79eadc862309ef818d420db78b193783c5b1fc2b55bb09563a7c