RIT-ACT: dogfooded `npx @recoder.xyz/rithub init` live and found two real activation blockers. 1. Universal: provisionAgent created every starter repo with auto_init:true, so the server-authored README commit on main always rejects the CLI's own printed `git push -u origin main` as a non-fast-forward push. Switched to auto_init:false; verified against the live API (empty:true, a real local push lands as [new branch]). 2. Machine-dependent: the CLI hardcoded `main` in both its own `git init` and the printed push instruction, so any machine whose git still defaults to `master` hit "src refspec main does not match any" before even reaching bug 1. Normalized freshly-created repos to `main` via symbolic-ref and made the printed instruction report the real current branch. Added a real-git regression test that forces init.defaultBranch=master to prove it. Published @recoder.xyz/rithub@0.1.1 to npm, re-dogfooded live. Server-side fix (agentSignup.mjs) is committed but not yet deployed to railyard-box's pm2 rithub-mcp -- box load1 was climbing (10->16) from other lanes' concurrent work during this pass, so the restart was held rather than forced through a spike. Next session: rsync + pm2 restart once load1<8, then re-walk the init->commit->push flow end to end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
272 lines
11 KiB
JavaScript
272 lines
11 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, rmSync, existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { createHash } from "node:crypto";
|
|
import { execFileSync } from "node:child_process";
|
|
|
|
import { main } from "../src/cli.mjs";
|
|
|
|
function leadingZeroBits(buf) {
|
|
let n = 0;
|
|
for (const byte of buf) {
|
|
if (byte === 0) {
|
|
n += 8;
|
|
continue;
|
|
}
|
|
let b = byte;
|
|
while ((b & 0x80) === 0) {
|
|
n += 1;
|
|
b <<= 1;
|
|
}
|
|
break;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
function solve(challenge, bits) {
|
|
for (let i = 0; ; i++) {
|
|
const nonce = i.toString(36);
|
|
if (leadingZeroBits(createHash("sha256").update(`${challenge}:${nonce}`).digest()) >= bits) return nonce;
|
|
}
|
|
}
|
|
|
|
/** A fake Rithub agent-signup server, in-process (no real network): just enough for the CLI's three calls (challenge, signup, claim). */
|
|
function fakeServer({ bits = 8 } = {}) {
|
|
let issued = null;
|
|
let claimed = false;
|
|
const log = [];
|
|
return {
|
|
log,
|
|
fetchImpl: async (url, init) => {
|
|
const u = new URL(url);
|
|
log.push(`${init?.method ?? "GET"} ${u.pathname}`);
|
|
if (u.pathname === "/agent-signup/challenge") {
|
|
issued = { challenge: "deadbeef", bits };
|
|
return jsonResponse(200, issued);
|
|
}
|
|
if (u.pathname === "/agent-signup" && init?.method === "POST") {
|
|
const body = JSON.parse(init.body);
|
|
if (!issued || body.challenge !== issued.challenge) return jsonResponse(400, { error: "bad challenge" });
|
|
return jsonResponse(201, {
|
|
username: "agent-test123",
|
|
token: "tok_test123",
|
|
repo: "agent-test123/first-repo",
|
|
html_url: "https://rithub.dev/agent-test123/first-repo",
|
|
clone_url: "https://rithub.dev/agent-test123/first-repo.git",
|
|
git_remote: "https://agent-test123:tok_test123@rithub.dev/agent-test123/first-repo.git",
|
|
quota: { repos: 3 },
|
|
});
|
|
}
|
|
if (u.pathname === "/agent-signup/claim" && init?.method === "POST") {
|
|
const body = JSON.parse(init.body);
|
|
if (claimed) return jsonResponse(409, { error: "already claimed" });
|
|
claimed = true;
|
|
return jsonResponse(200, { username: "agent-test123", email: body.email, quota: "removed", next: "open https://rithub.dev/user/forgot_password" });
|
|
}
|
|
return jsonResponse(404, { error: "not found" });
|
|
},
|
|
};
|
|
}
|
|
|
|
function jsonResponse(status, body) {
|
|
return { status, text: async () => JSON.stringify(body) };
|
|
}
|
|
|
|
function fakeIo({ cwd, fetchImpl, gitState = { isRepo: false, remotes: [] } }) {
|
|
const out = [];
|
|
const err = [];
|
|
return {
|
|
io: {
|
|
env: {},
|
|
cwd,
|
|
out: (s) => out.push(s),
|
|
err: (s) => err.push(s),
|
|
now: () => Date.now(),
|
|
fetchImpl,
|
|
exists: (p) => existsSync(p),
|
|
readFile: (p) => (existsSync(p) ? readFileSync(p, "utf8") : null),
|
|
writeFile: (p, c) => {
|
|
mkdirSync(join(p, ".."), { recursive: true });
|
|
writeFileSync(p, c);
|
|
},
|
|
run: (command, args) => {
|
|
if (command !== "git") return { ok: false, output: "unexpected command" };
|
|
if (args[0] === "rev-parse") return gitState.isRepo ? { ok: true, output: "true\n" } : { ok: false, output: "not a repo" };
|
|
if (args[0] === "init") {
|
|
gitState.isRepo = true;
|
|
return { ok: true, output: "Initialized empty Git repository\n" };
|
|
}
|
|
if (args[0] === "remote" && args.length === 1) return { ok: true, output: gitState.remotes.map((r) => r.name).join("\n") };
|
|
if (args[0] === "remote" && args[1] === "add") {
|
|
gitState.remotes.push({ name: args[2], url: args[3] });
|
|
return { ok: true, output: "" };
|
|
}
|
|
if (args[0] === "remote" && args[1] === "set-url") {
|
|
const r = gitState.remotes.find((x) => x.name === args[2]);
|
|
if (r) r.url = args[3];
|
|
return { ok: true, output: "" };
|
|
}
|
|
return { ok: false, output: `unexpected git ${args.join(" ")}` };
|
|
},
|
|
},
|
|
out,
|
|
err,
|
|
gitState,
|
|
};
|
|
}
|
|
|
|
function tmpCwd() {
|
|
return mkdtempSync(join(tmpdir(), "rithub-cli-test-"));
|
|
}
|
|
|
|
test("rithub init: full run in a clean temp dir provisions an account and sets the git remote", async () => {
|
|
const cwd = tmpCwd();
|
|
const server = fakeServer();
|
|
const { io, out, gitState } = fakeIo({ cwd, fetchImpl: server.fetchImpl });
|
|
try {
|
|
const code = await main(["init"], io);
|
|
assert.equal(code, 0);
|
|
assert.deepEqual(server.log, ["GET /agent-signup/challenge", "POST /agent-signup"]);
|
|
assert.equal(gitState.isRepo, true);
|
|
assert.equal(gitState.remotes.length, 1);
|
|
assert.equal(gitState.remotes[0].name, "origin");
|
|
assert.equal(gitState.remotes[0].url, "https://agent-test123:tok_test123@rithub.dev/agent-test123/first-repo.git");
|
|
|
|
const state = JSON.parse(readFileSync(join(cwd, ".rithub/agent.json"), "utf8"));
|
|
assert.equal(state.username, "agent-test123");
|
|
assert.equal(state.token, "tok_test123");
|
|
|
|
const gitignore = readFileSync(join(cwd, ".gitignore"), "utf8");
|
|
assert.match(gitignore, /^\.rithub\/$/m);
|
|
|
|
assert.ok(out.some((line) => line.includes("agent-test123")));
|
|
assert.ok(out.some((line) => line.includes("rithub claim")));
|
|
} finally {
|
|
rmSync(cwd, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("rithub init: re-running in the same directory reuses the saved account instead of signing up again", async () => {
|
|
const cwd = tmpCwd();
|
|
const server = fakeServer();
|
|
const { io: io1, gitState } = fakeIo({ cwd, fetchImpl: server.fetchImpl });
|
|
await main(["init"], io1);
|
|
server.log.length = 0;
|
|
|
|
const { io: io2, out } = fakeIo({ cwd, fetchImpl: server.fetchImpl, gitState });
|
|
const code = await main(["init"], io2);
|
|
assert.equal(code, 0);
|
|
assert.deepEqual(server.log, []); // no network call at all on reuse
|
|
assert.ok(out.some((line) => line.includes("Reusing")));
|
|
rmSync(cwd, { recursive: true, force: true });
|
|
});
|
|
|
|
test("rithub claim: sends the stored token and the given email", async () => {
|
|
const cwd = tmpCwd();
|
|
const server = fakeServer();
|
|
const { io } = fakeIo({ cwd, fetchImpl: server.fetchImpl });
|
|
await main(["init"], io);
|
|
server.log.length = 0;
|
|
|
|
const { io: io2, out } = fakeIo({ cwd, fetchImpl: server.fetchImpl, gitState: { isRepo: true, remotes: [] } });
|
|
const code = await main(["claim", "--email", "founder@example.com"], io2);
|
|
assert.equal(code, 0);
|
|
assert.deepEqual(server.log, ["POST /agent-signup/claim"]);
|
|
assert.ok(out.some((line) => line.includes("founder@example.com")));
|
|
rmSync(cwd, { recursive: true, force: true });
|
|
});
|
|
|
|
test("rithub claim: without --email fails with a clear usage error, no network call", async () => {
|
|
const cwd = tmpCwd();
|
|
const server = fakeServer();
|
|
const { io } = fakeIo({ cwd, fetchImpl: server.fetchImpl });
|
|
await main(["init"], io);
|
|
server.log.length = 0;
|
|
|
|
const { io: io2, err } = fakeIo({ cwd, fetchImpl: server.fetchImpl, gitState: { isRepo: true, remotes: [] } });
|
|
const code = await main(["claim"], io2);
|
|
assert.equal(code, 1);
|
|
assert.deepEqual(server.log, []);
|
|
assert.ok(err.some((line) => line.includes("--email")));
|
|
rmSync(cwd, { recursive: true, force: true });
|
|
});
|
|
|
|
test("rithub with no command prints usage and exits 0", async () => {
|
|
const { io, err } = fakeIo({ cwd: tmpCwd(), fetchImpl: async () => jsonResponse(404, {}) });
|
|
const code = await main([], io);
|
|
assert.equal(code, 0);
|
|
assert.ok(err.length > 0);
|
|
});
|
|
|
|
test("an unknown option is a clear error, not a crash", async () => {
|
|
const { io, err } = fakeIo({ cwd: tmpCwd(), fetchImpl: async () => jsonResponse(404, {}) });
|
|
const code = await main(["init", "--bogus"], io);
|
|
assert.equal(code, 1);
|
|
assert.ok(err.some((line) => line.includes("--bogus")));
|
|
});
|
|
|
|
/** Real git (not the mocked io.run above) catches the exact friction found by
|
|
* dogfooding `npx @recoder.xyz/rithub init` for real: on a machine whose git
|
|
* still defaults new repos to `master` (git's own historical default --
|
|
* plenty of installs never set `init.defaultBranch=main`), the CLI used to
|
|
* tell the user to `git push -u origin main`, which fails with "src refspec
|
|
* main does not match any" because the repo it just created is on `master`. */
|
|
test("rithub init: push instruction matches the real branch even when git defaults to master", async () => {
|
|
const cwd = tmpCwd();
|
|
// Force this test's git calls to the historical `master` default,
|
|
// regardless of this machine's real global git config.
|
|
const fakeHome = mkdtempSync(join(tmpdir(), "rithub-cli-git-home-"));
|
|
const gitConfig = join(fakeHome, ".gitconfig");
|
|
writeFileSync(gitConfig, "[init]\n\tdefaultBranch = master\n[user]\n\tname = Test\n\temail = test@example.com\n");
|
|
const env = { ...process.env, HOME: fakeHome, GIT_CONFIG_GLOBAL: gitConfig, GIT_CONFIG_SYSTEM: "/dev/null" };
|
|
|
|
const server = fakeServer();
|
|
const out = [];
|
|
const io = {
|
|
env: {},
|
|
cwd,
|
|
out: (s) => out.push(s),
|
|
err: () => {},
|
|
now: () => Date.now(),
|
|
fetchImpl: server.fetchImpl,
|
|
exists: (p) => existsSync(p),
|
|
readFile: (p) => (existsSync(p) ? readFileSync(p, "utf8") : null),
|
|
writeFile: (p, c) => {
|
|
mkdirSync(join(p, ".."), { recursive: true });
|
|
writeFileSync(p, c);
|
|
},
|
|
run: (command, args, runCwd) => {
|
|
try {
|
|
const output = execFileSync(command, args, { cwd: runCwd ?? cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
|
|
return { ok: true, output };
|
|
} catch (err) {
|
|
return { ok: false, output: `${err.stdout ?? ""}${err.stderr ?? err.message ?? ""}` };
|
|
}
|
|
},
|
|
};
|
|
|
|
try {
|
|
const code = await main(["init"], io);
|
|
assert.equal(code, 0);
|
|
|
|
// Confirm the directory really would have defaulted to `master` without our fix.
|
|
const probeDir = mkdtempSync(join(tmpdir(), "rithub-cli-master-probe-"));
|
|
execFileSync("git", ["init"], { cwd: probeDir, env });
|
|
const probeBranch = execFileSync("git", ["branch", "--show-current"], { cwd: probeDir, encoding: "utf8", env }).trim();
|
|
assert.equal(probeBranch, "master", "this test's git must default to master to prove the fix");
|
|
rmSync(probeDir, { recursive: true, force: true });
|
|
|
|
const realBranch = execFileSync("git", ["branch", "--show-current"], { cwd, encoding: "utf8", env }).trim();
|
|
assert.equal(realBranch, "main", "rithub init must normalize a fresh repo to main, not inherit git's master default");
|
|
assert.ok(
|
|
out.some((line) => line.includes("git push -u origin main")),
|
|
`printed instruction must say the real branch (main), got: ${out.join(" | ")}`
|
|
);
|
|
} finally {
|
|
rmSync(cwd, { recursive: true, force: true });
|
|
rmSync(fakeHome, { recursive: true, force: true });
|
|
}
|
|
});
|