Skip to content
refs
History
70 lines · 3.9 KBRaw
1import assert from "node:assert/strict";
2import { execFile, execFileSync } from "node:child_process";
3import fs from "node:fs";
4import http from "node:http";
5import type net from "node:net";
6import os from "node:os";
7import path from "node:path";
8import { test } from "node:test";
9
10const CLI = new URL("../refs.mjs", import.meta.url).pathname;
11
12test("the gh-style commands read the repository from the clone, print JSON for agents, and never merge without a terminal", async (t) => {
13 const home = fs.mkdtempSync(path.join(os.tmpdir(), "refs-cmd-"));
14 const clone = path.join(home, "clone");
15 const seen: string[] = [];
16 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" }] };
17 const server = http.createServer(async (request, response) => {
18 let body = "";
19 for await (const chunk of request) body += chunk;
20 seen.push(`${request.method} ${request.url}${body ? ` ${body}` : ""}`);
21 const answers: Record<string, unknown> = {
22 "GET /api/cli/repos/acme/web/pulls?state=open": { items: [pull], counts: { open: 1, closed: 0 } },
23 "GET /api/cli/repos/acme/web/pulls/4": pull,
24 "POST /api/cli/repos/acme/web/tasks": { number: 9 },
25 "POST /api/cli/repos": { name: "acme/api", visibility: "private", cloneUrl: `${origin}/acme/api.git` },
26 };
27 const answer = answers[`${request.method} ${request.url}`];
28 response.writeHead(answer ? 200 : 404, { "content-type": "application/json" }).end(JSON.stringify(answer ?? { error: "nope" }));
29 });
30 await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
31 const origin = `http://127.0.0.1:${(server.address() as net.AddressInfo).port}`;
32 t.after(() => (server.close(), fs.rmSync(home, { recursive: true, force: true })));
33
34 fs.writeFileSync(path.join(home, "credentials.json"), JSON.stringify({ origin, accessToken: "oauth", expiresAt: Date.now() / 1000 + 3600 }));
35 execFileSync("git", ["init", "-q", "-b", "fix-login", clone]);
36 execFileSync("git", ["-C", clone, "remote", "add", "origin", `${origin}/acme/web.git`]);
37 const refs = (...args: string[]) =>
38 new Promise<{ status: number; stdout: string; stderr: string }>((resolve) => {
39 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 }));
40 child.stdin?.end();
41 });
42
43 const listed = (await refs("pr", "list", "--json"));
44 assert.equal(listed.status, 0, listed.stderr);
45 assert.equal(JSON.parse(listed.stdout).items[0].number, 4);
46
47 const viewed = (await refs("pr", "view"));
48 assert.equal(viewed.status, 0, viewed.stderr);
49 assert.match(viewed.stdout, /#4 Fix login\nopen · fix-login → main · by Aman \(claude-code\)/);
50 assert.match(viewed.stdout, /Checks: run run1 failed/);
51
52 assert.equal((await refs("pr", "checks", "4")).status, 1);
53
54 const created = (await refs("task", "create", "--title", "Add tests", "--body", "Cover the CLI"));
55 assert.match(created.stdout, /Created task #9/);
56 assert.ok(seen.includes('POST /api/cli/repos/acme/web/tasks {"title":"Add tests","body":"Cover the CLI"}'));
57
58 const merged = (await refs("pr", "merge", "4"));
59 assert.equal(merged.status, 1);
60 assert.match(merged.stderr, /only a person can merge/);
61 assert.ok(!seen.some((line) => line.includes("/merge")));
62
63 const other = (await refs("pr", "view", "7", "-R", "acme/api"));
64 assert.equal(other.status, 1);
65 assert.match(other.stderr, /nope/);
66
67 const made = await refs("repo", "create", "acme/api", "--public");
68 assert.match(made.stdout, /Created private repository acme\/api/);
69 assert.ok(seen.includes('POST /api/cli/repos {"owner":"acme","name":"api","description":"","visibility":"public","setUp":true}'));
70});