import assert from "node:assert/strict"; import { test } from "node:test"; import { branchProblem, claimableBranch, describeDeliveries, describeDelivery, hookOutput, refProblem } from "../wire.ts"; const push = { type: "event", id: "40", event: "repo.pushed", ref: "refs/heads/tasks/52", from: "a".repeat(40), to: "b".repeat(40), by: { session: "s2", actor: "codex" } } as const; test("sessions may only update their own branch", () => { assert.equal(refProblem("tasks/52", { ref: "refs/heads/tasks/52", from: "a", to: "b" }), null); assert.equal(refProblem("fix-login", { ref: "refs/heads/fix-login", from: "a", to: "b" }), null); assert.match(refProblem("tasks/52", { ref: "refs/heads/main", from: "a", to: "b" })!, /only push to refs\/heads\/tasks\/52/); assert.match(refProblem("tasks/52", { ref: "refs/heads/tasks/52", from: "a", to: null })!, /cannot delete/); assert.match(refProblem(null, { ref: "refs/heads/tasks/52", from: "a", to: "b" })!, /no branch/); }); test("a session without a branch claims a new one by pushing it, but never the default branch", () => { const create = (ref: string) => [{ ref, from: null, to: "b" }]; assert.equal(claimableBranch(create("refs/heads/agent/fix-login"), "main"), "agent/fix-login"); assert.equal(claimableBranch(create("refs/heads/main"), "main"), null); assert.equal(claimableBranch(create("refs/heads/tasks/9"), "main"), "tasks/9"); assert.equal(claimableBranch(create("refs/tags/v1"), "main"), null); assert.equal(claimableBranch([{ ref: "refs/heads/fix", from: "a", to: "b" }], "main"), null); assert.equal(claimableBranch([...create("refs/heads/a"), ...create("refs/heads/b")], "main"), null); for (const bad of ["../x", "a..b", "x.lock", "a//b", "/a", "a/", "a b", "-x", "a/.b", "HEAD"]) assert.match(branchProblem(bad, "main")!, /not a valid branch name/); }); test("describes each delivery as an instruction the agent can act on", () => { assert.match(describeDelivery(push), /^Agent codex \(session s2\) pushed tasks\/52 aaaaaaa → bbbbbbb\. .*git pull --rebase.*git log a{40}\.\.b{40}/); assert.match(describeDelivery({ ...push, by: { session: null, actor: "Aman" } }), /^Aman \(human\) pushed/); assert.match(describeDelivery({ ...push, from: null }), /pushed tasks\/52 nothing → bbbbbbb\. .*git log origin\/HEAD\.\.b{40}/); assert.match(describeDelivery({ type: "problem", id: "41", commit: "c".repeat(40), message: "reason is required." }), /commit ccccccc .*reason is required\. Fix it/); assert.equal(describeDelivery({ type: "instruction", id: "42", body: "Use KV", by: { person: "Aman" } }), "Instruction from Aman: Use KV"); assert.match(describeDelivery({ type: "stop", id: "43", reason: "Done." }), /stopped: Done\. Commit your work locally/); const mention = { type: "mention", id: "44", pull: 3, title: "Queue pushes", body: "@agent/x why?", assigned: false, run: false, by: { person: "Aman" } } as const; assert.equal(describeDelivery(mention), 'Aman mentioned you on pull request #3 "Queue pushes": "@agent/x why?" Answer with refs_reply (mention 44).'); assert.match(describeDelivery({ ...mention, assigned: true }), /^Aman assigned you pull request #3 "Queue pushes" .*refs_take with pull 3.*push to its branch.*refs_reply \(mention 44\).*refs_release/); assert.match(describeDelivery({ ...mention, run: true }), /A sub-agent on your machine is handling it \(mention 44\)/); assert.equal(describeDelivery({ type: "run", id: "45", mention: "44", ok: true, summary: "Answered." }), "Your sub-agent finished mention 44: Answered."); }); test("hook output adds context for the hook's event, or nothing", () => { const context = describeDeliveries([push]); assert.deepEqual(JSON.parse(hookOutput("PostToolUse", context)), { hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: context } }); assert.equal(hookOutput("PostToolUse", ""), ""); });