Skip to content
refs
46 lines · 4.0 KBRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { branchProblem, claimableBranch, describeDeliveries, describeDelivery, hookOutput, refProblem } from "../wire.ts";
4
5const 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" }, base: false } as const;
6
7test("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
15test("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
26test("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, ref: "refs/heads/main", base: true }), /moved main aaaaaaa → bbbbbbb, the branch your work merges into\. .*git fetch origin.*git merge origin\/main/);
30 assert.match(describeDelivery({ ...push, from: null }), /pushed tasks\/52 nothing → bbbbbbb\. .*git log origin\/HEAD\.\.b{40}/);
31 assert.match(describeDelivery({ type: "problem", id: "41", commit: "c".repeat(40), message: "reason is required." }), /commit ccccccc .*reason is required\. Fix it/);
32 assert.equal(describeDelivery({ type: "instruction", id: "42", body: "Use KV", by: { person: "Aman" } }), "Instruction from Aman: Use KV");
33 assert.match(describeDelivery({ type: "stop", id: "43", reason: "Done." }), /stopped: Done\. Commit your work locally/);
34 const mention = { type: "mention", id: "44", pull: 3, title: "Queue pushes", body: "@agent/x why?", assigned: false, run: false, by: { person: "Aman" } } as const;
35 assert.equal(describeDelivery(mention), 'Aman mentioned you on pull request #3 "Queue pushes": "@agent/x why?" Answer with refs_reply (mention 44).');
36 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/);
37 assert.match(describeDelivery({ ...mention, run: true }), /A sub-agent on your machine is handling it \(mention 44\)/);
38 assert.equal(describeDelivery({ type: "run", id: "45", mention: "44", ok: true, summary: "Answered." }), "Your sub-agent finished mention 44: Answered.");
39});
40
41test("hook output adds context for the hook's event, or nothing", () => {
42 const context = describeDeliveries([push]);
43 assert.deepEqual(JSON.parse(hookOutput("PostToolUse", context)), { hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: context } });
44 assert.equal(hookOutput("PostToolUse", ""), "");
45});
46