import assert from "node:assert/strict"; import { execFile, execFileSync } from "node:child_process"; import fs from "node:fs"; import http from "node:http"; import type net from "node:net"; import os from "node:os"; import path from "node:path"; import { test } from "node:test"; const CLI = new URL("../refs.mjs", import.meta.url).pathname; test("the gh-style commands read the repository from the clone, print JSON for agents, and never merge without a terminal", async (t) => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "refs-cmd-")); const clone = path.join(home, "clone"); const seen: string[] = []; const pull = { number: 4, title: "Fix login", status: "open", head: "fix-login", base: "main", author: { name: "Aman" }, agent: "claude-code", body: "", threads: 1, mergeability: { state: "clean" }, checks: [{ id: "run1", state: "failed", sha: "abcdef0", trigger: "pull_request", branch: "fix-login" }] }; const server = http.createServer(async (request, response) => { let body = ""; for await (const chunk of request) body += chunk; seen.push(`${request.method} ${request.url}${body ? ` ${body}` : ""}`); const answers: Record = { "GET /api/cli/repos/acme/web/pulls?state=open": { items: [pull], counts: { open: 1, closed: 0 } }, "GET /api/cli/repos/acme/web/pulls/4": pull, "POST /api/cli/repos/acme/web/tasks": { number: 9 }, "POST /api/cli/repos": { name: "acme/api", visibility: "private", cloneUrl: `${origin}/acme/api.git` }, }; const answer = answers[`${request.method} ${request.url}`]; response.writeHead(answer ? 200 : 404, { "content-type": "application/json" }).end(JSON.stringify(answer ?? { error: "nope" })); }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); const origin = `http://127.0.0.1:${(server.address() as net.AddressInfo).port}`; t.after(() => (server.close(), fs.rmSync(home, { recursive: true, force: true }))); fs.writeFileSync(path.join(home, "credentials.json"), JSON.stringify({ origin, accessToken: "oauth", expiresAt: Date.now() / 1000 + 3600 })); execFileSync("git", ["init", "-q", "-b", "fix-login", clone]); execFileSync("git", ["-C", clone, "remote", "add", "origin", `${origin}/acme/web.git`]); const refs = (...args: string[]) => new Promise<{ status: number; stdout: string; stderr: string }>((resolve) => { const child = execFile(process.execPath, [CLI, ...args], { cwd: clone, env: { ...process.env, REFS_HOME: home } }, (error, stdout, stderr) => resolve({ status: error ? Number(error.code) : 0, stdout, stderr })); child.stdin?.end(); }); const listed = (await refs("pr", "list", "--json")); assert.equal(listed.status, 0, listed.stderr); assert.equal(JSON.parse(listed.stdout).items[0].number, 4); const viewed = (await refs("pr", "view")); assert.equal(viewed.status, 0, viewed.stderr); assert.match(viewed.stdout, /#4 Fix login\nopen · fix-login → main · by Aman \(claude-code\)/); assert.match(viewed.stdout, /Checks: run run1 failed/); assert.equal((await refs("pr", "checks", "4")).status, 1); const created = (await refs("task", "create", "--title", "Add tests", "--body", "Cover the CLI")); assert.match(created.stdout, /Created task #9/); assert.ok(seen.includes('POST /api/cli/repos/acme/web/tasks {"title":"Add tests","body":"Cover the CLI"}')); const merged = (await refs("pr", "merge", "4")); assert.equal(merged.status, 1); assert.match(merged.stderr, /only a person can merge/); assert.ok(!seen.some((line) => line.includes("/merge"))); const other = (await refs("pr", "view", "7", "-R", "acme/api")); assert.equal(other.status, 1); assert.match(other.stderr, /nope/); const made = await refs("repo", "create", "acme/api", "--public"); assert.match(made.stdout, /Created private repository acme\/api/); assert.ok(seen.includes('POST /api/cli/repos {"owner":"acme","name":"api","description":"","visibility":"public","setUp":true}')); });