import assert from "node:assert/strict" import { spawnSync } from "node:child_process" import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { dirname, join } from "node:path" import test from "node:test" /** * A commit hook sets GIT_INDEX_FILE and GIT_DIR for its children. A test that * creates its own repository has to start from a clean slate or git reads the * hook's index instead of the temporary one - which fails only when the suite * is run from the hook, and only for the tests that touch a real worktree. */ function gitEnv() { const env = { ...process.env } for (const key of ["GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES"]) delete env[key] return env } import { fileURLToPath } from "node:url" import { writeTrack } from "../src/driver/track.mjs" const ROOT = dirname(dirname(fileURLToPath(import.meta.url))) const PLAN = `# Answer the question ## Tasks - [ ] write the answer - files: answer.txt - change: write 42 into answer.txt - verify: test -f answer.txt - status: pending ` /** * A stand-in for the real CLI. It answers `--version`, and emits the event * stream shape the driver parses - not prose - so the test exercises the same * path a real opencode answer takes. */ function fakeOpencode() { const bin = mkdtempSync(join(tmpdir(), "oc-bin-")) const script = join(bin, "opencode") writeFileSync(script, `#!/usr/bin/env node const fs = require("node:fs") const path = require("node:path") const argv = process.argv.slice(2) if (argv[0] === "--version") { console.log("1.99.0"); process.exit(0) } const plan = ${JSON.stringify(PLAN)} const role = argv[argv.indexOf("--agent") + 1] const dir = argv[argv.indexOf("--dir") + 1] if (role === "plan") { if (process.env.PLAN_CALL_LOG) fs.appendFileSync(process.env.PLAN_CALL_LOG, "x") console.log(JSON.stringify({ type: "text", part: { text: plan } })) process.exit(0) } fs.writeFileSync(path.join(dir, "answer.txt"), "42\\n") console.log(JSON.stringify({ type: "text", part: { text: "done" } })) `) chmodSync(script, 0o755) return bin } function repo(bin) { const dir = mkdtempSync(join(tmpdir(), "oc-cli-")) const git = (...a) => spawnSync("git", ["-C", dir, ...a], { encoding: "utf8", env: gitEnv() }) git("init", "-q") git("config", "user.email", "t@example.com") git("config", "user.name", "T") writeFileSync(join(dir, "README.md"), "start\n") git("add", "README.md") git("commit", "-q", "-m", "start") return { dir, env: { ...process.env, PATH: `${bin}:${process.env.PATH}` } } } const cli = (...args) => [join(ROOT, "bin", "oc-harness.mjs"), ...args] test("plan writes a task file and refuses to overwrite it", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) const first = spawnSync("node", cli("plan", "answer the question"), { cwd: dir, encoding: "utf8", env }) assert.equal(first.status, 0, first.stderr) assert.match(readFileSync(join(dir, ".space", "tasks.md"), "utf8"), /write the answer/) const second = spawnSync("node", cli("plan", "again"), { cwd: dir, encoding: "utf8", env }) assert.equal(second.status, 2) assert.match(second.stderr, /already exists/) }) test("work --json --print-verify prints JSON, not a bare command list", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) mkdirSync(join(dir, ".space"), { recursive: true }) writeFileSync(join(dir, ".space", "tasks.md"), PLAN) const r = spawnSync("node", cli("work", "--no-strict", "--json", "--print-verify"), { cwd: dir, encoding: "utf8", env }) assert.equal(r.status, 0, r.stderr) const report = JSON.parse(r.stdout) assert.equal(report.ok, true) assert.equal(report.code, 0) assert.equal(report.phase, "print-verify") assert.deepEqual(report.commands, ["test -f answer.txt"]) assert.equal(existsSync(join(dir, "answer.txt")), false, "printing verify must not run the loop") const text = spawnSync("node", cli("work", "--no-strict", "--print-verify"), { cwd: dir, encoding: "utf8", env }) assert.equal(text.status, 0, text.stderr) assert.match(text.stdout, /test -f answer\.txt\n$/) assert.doesNotMatch(text.stdout, /"commands"/) }) test("run refuses an untrusted task file on a non-tty", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) mkdirSync(join(dir, ".space"), { recursive: true }) writeFileSync(join(dir, ".space", "tasks.md"), PLAN) const r = spawnSync("node", cli("run"), { cwd: dir, encoding: "utf8", env }) assert.equal(r.status, 2) assert.match(r.stderr, /--trust-verify/) assert.equal(existsSync(join(dir, "answer.txt")), false, "the agent must not run before trust") }) test("--print-verify shows the commands without running them", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) mkdirSync(join(dir, ".space"), { recursive: true }) writeFileSync(join(dir, ".space", "tasks.md"), PLAN) const r = spawnSync("node", cli("run", "--print-verify"), { cwd: dir, encoding: "utf8", env }) assert.equal(r.status, 0) assert.equal(r.stdout.trim(), "test -f answer.txt") }) test("--trust-verify runs the loop and --json emits one object", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) mkdirSync(join(dir, ".space"), { recursive: true }) writeFileSync(join(dir, ".space", "tasks.md"), PLAN) const r = spawnSync("node", cli("run", "--trust-verify", "--json", "--max-rounds", "3"), { cwd: dir, encoding: "utf8", env, timeout: 60_000, }) assert.equal(r.status, 0, r.stderr) const report = JSON.parse(r.stdout) assert.equal(report.ok, true) assert.equal(report.summary.done, 1) assert.equal(existsSync(join(dir, ".space", "run.json")), true) }) test("work --plan-only plans without a git repository", () => { const bin = fakeOpencode() const dir = mkdtempSync(join(tmpdir(), "oc-cli-nogit-")) const env = { ...process.env, PATH: `${bin}:${process.env.PATH}` } const r = spawnSync("node", cli("work", "--no-strict", "answer the question", "--plan-only"), { cwd: dir, encoding: "utf8", env, timeout: 60_000 }) assert.equal(r.status, 0, r.stderr) assert.match(readFileSync(join(dir, ".space", "tasks.md"), "utf8"), /write the answer/) }) test("work --replan replaces a valid plan once and runs the replacement", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) mkdirSync(join(dir, ".space"), { recursive: true }) writeFileSync(join(dir, ".space", "tasks.md"), PLAN.replace("write 42 into answer.txt", "write 41 into answer.txt")) const log = join(mkdtempSync(join(tmpdir(), "oc-plan-log-")), "calls") const r = spawnSync("node", cli("work", "--no-strict", "answer the question", "--replan", "--trust-verify", "--json", "--max-rounds", "3"), { cwd: dir, encoding: "utf8", env: { ...env, PLAN_CALL_LOG: log }, timeout: 60_000, }) assert.equal(r.status, 0, r.stderr) const report = JSON.parse(r.stdout) assert.equal(report.ok, true) // The prepare call replans; the run call must not ask the planner for a // second plan, or the tasks whose verify commands were trusted would be // replaced underneath the run. assert.equal(readFileSync(log, "utf8"), "x", "the planner must run exactly once") assert.match(readFileSync(join(dir, ".space", "tasks.md"), "utf8"), /write 42/) assert.equal(readFileSync(join(dir, "answer.txt"), "utf8").trim(), "42") }) test("status reads the run record and prints it as JSON", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) mkdirSync(join(dir, ".space"), { recursive: true }) writeFileSync(join(dir, ".space", "tasks.md"), PLAN) spawnSync("node", cli("run", "--trust-verify"), { cwd: dir, encoding: "utf8", env, timeout: 60_000 }) const r = spawnSync("node", cli("status", "--json"), { cwd: dir, encoding: "utf8", env }) assert.equal(r.status, 0, r.stderr) const report = JSON.parse(r.stdout) assert.equal(report.ok, true) assert.equal(report.run.status, "done") }) test("strict accepts the track acceptance command as its final verify", () => { const bin = fakeOpencode() const { dir, env } = repo(bin) mkdirSync(join(dir, ".space"), { recursive: true }) writeTrack(dir, { schema_version: 1, template: { id: "blank", version: "1.0.0" }, vars: {}, acceptance: `node -e "process.exit(0)"`, required_sections: [], trace_required: false, milestone: null, readiness: { charter_ready: true, research_ready: true, design_ready: true, plan_ready: false }, updated_at: new Date().toISOString(), }) const r = spawnSync( "node", cli("work", "--strict", "--review", "--final-review", "--worktree", "--commit", "--plan-only", "answer the question"), { cwd: dir, encoding: "utf8", env, timeout: 60_000 }, ) assert.equal(r.status, 0, r.stderr) assert.doesNotMatch(r.stderr, /--strict requires --final-verify/) })