45 lines · 3.8 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { branchProblem, claimableBranch, describeDeliveries, describeDelivery, hookOutput, refProblem } from "../wire.ts"; |
| 4 | |
| 5 | 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; |
| 6 | |
| 7 | test("sessions may only update their own branch", () => { |
| 8 | assert.equal(refProblem("tasks/52", { ref: "refs/heads/tasks/52", from: "a", to: "b" }), null); |
| 9 | assert.equal(refProblem("fix-login", { ref: "refs/heads/fix-login", from: "a", to: "b" }), null); |
| 10 | assert.match(refProblem("tasks/52", { ref: "refs/heads/main", from: "a", to: "b" })!, /only push to refs\/heads\/tasks\/52/); |
| 11 | assert.match(refProblem("tasks/52", { ref: "refs/heads/tasks/52", from: "a", to: null })!, /cannot delete/); |
| 12 | assert.match(refProblem(null, { ref: "refs/heads/tasks/52", from: "a", to: "b" })!, /no branch/); |
| 13 | }); |
| 14 | |
| 15 | test("a session without a branch claims a new one by pushing it, but never the default branch", () => { |
| 16 | const create = (ref: string) => [{ ref, from: null, to: "b" }]; |
| 17 | assert.equal(claimableBranch(create("refs/heads/agent/fix-login"), "main"), "agent/fix-login"); |
| 18 | assert.equal(claimableBranch(create("refs/heads/main"), "main"), null); |
| 19 | assert.equal(claimableBranch(create("refs/heads/tasks/9"), "main"), "tasks/9"); |
| 20 | assert.equal(claimableBranch(create("refs/tags/v1"), "main"), null); |
| 21 | assert.equal(claimableBranch([{ ref: "refs/heads/fix", from: "a", to: "b" }], "main"), null); |
| 22 | assert.equal(claimableBranch([...create("refs/heads/a"), ...create("refs/heads/b")], "main"), null); |
| 23 | 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/); |
| 24 | }); |
| 25 | |
| 26 | test("describes each delivery as an instruction the agent can act on", () => { |
| 27 | assert.match(describeDelivery(push), /^Agent codex \(session s2\) pushed tasks\/52 aaaaaaa → bbbbbbb\. .*git pull --rebase.*git log a{40}\.\.b{40}/); |
| 28 | assert.match(describeDelivery({ ...push, by: { session: null, actor: "Aman" } }), /^Aman \(human\) pushed/); |
| 29 | assert.match(describeDelivery({ ...push, from: null }), /pushed tasks\/52 nothing → bbbbbbb\. .*git log origin\/HEAD\.\.b{40}/); |
| 30 | assert.match(describeDelivery({ type: "problem", id: "41", commit: "c".repeat(40), message: "reason is required." }), /commit ccccccc .*reason is required\. Fix it/); |
| 31 | assert.equal(describeDelivery({ type: "instruction", id: "42", body: "Use KV", by: { person: "Aman" } }), "Instruction from Aman: Use KV"); |
| 32 | assert.match(describeDelivery({ type: "stop", id: "43", reason: "Done." }), /stopped: Done\. Commit your work locally/); |
| 33 | const mention = { type: "mention", id: "44", pull: 3, title: "Queue pushes", body: "@agent/x why?", assigned: false, run: false, by: { person: "Aman" } } as const; |
| 34 | assert.equal(describeDelivery(mention), 'Aman mentioned you on pull request #3 "Queue pushes": "@agent/x why?" Answer with refs_reply (mention 44).'); |
| 35 | 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/); |
| 36 | assert.match(describeDelivery({ ...mention, run: true }), /A sub-agent on your machine is handling it \(mention 44\)/); |
| 37 | assert.equal(describeDelivery({ type: "run", id: "45", mention: "44", ok: true, summary: "Answered." }), "Your sub-agent finished mention 44: Answered."); |
| 38 | }); |
| 39 | |
| 40 | test("hook output adds context for the hook's event, or nothing", () => { |
| 41 | const context = describeDeliveries([push]); |
| 42 | assert.deepEqual(JSON.parse(hookOutput("PostToolUse", context)), { hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: context } }); |
| 43 | assert.equal(hookOutput("PostToolUse", ""), ""); |
| 44 | }); |
| 45 |