import assert from "node:assert/strict"; import { test } from "node:test"; import { framesFor, type DeliverableRow } from "../deliveries.ts"; const me = { session: "s1", branchRef: "refs/heads/tasks/52", baseRef: "refs/heads/main" }; const idle = { session: "s1", branchRef: null, baseRef: "refs/heads/main" }; const push = (session: string | null, ref = "refs/heads/tasks/52"): DeliverableRow => ({ id: 10, kind: "repo.pushed", data: JSON.stringify({ updates: [{ ref, from: "a", to: "b" }] }), session_id: session, actor: session ? "codex" : "Aman", }); test("delivers pushes to the current task branch by others, never the session's own", () => { assert.deepEqual(framesFor(push("s2"), me), [{ type: "event", id: "10", event: "repo.pushed", ref: "refs/heads/tasks/52", from: "a", to: "b", by: { session: "s2", actor: "codex" }, base: false }]); const human = framesFor(push(null), me)[0]; assert.deepEqual(human.type === "event" && human.by, { session: null, actor: "Aman" }); assert.deepEqual(framesFor(push("s1"), me), []); assert.deepEqual(framesFor(push("s2", "refs/heads/other"), me), []); }); test("tells a working session when the default branch moves, so it rebases onto merged work", () => { assert.deepEqual(framesFor(push("s2", "refs/heads/main"), me), [{ type: "event", id: "10", event: "repo.pushed", ref: "refs/heads/main", from: "a", to: "b", by: { session: "s2", actor: "codex" }, base: true }]); assert.equal(framesFor(push(null, "refs/heads/main"), me).length, 1); assert.deepEqual(framesFor(push("s1", "refs/heads/main"), me), []); assert.deepEqual(framesFor(push("s2", "refs/heads/main"), idle), []); }); test("an idle session receives no pushes", () => { assert.deepEqual(framesFor(push("s2"), idle), []); }); test("delivers problems only to the session that pushed the commit", () => { const problem: DeliverableRow = { id: 11, kind: "session.problem", data: JSON.stringify({ commit: "c3", message: "reason is required." }), session_id: "s1", actor: "claude-code" }; assert.deepEqual(framesFor(problem, me), [{ type: "problem", id: "11", commit: "c3", message: "reason is required." }]); assert.deepEqual(framesFor({ ...problem, session_id: "s2" }, me), []); }); test("delivers instructions and stop only to the session they address, idle or not", () => { const instructed: DeliverableRow = { id: 12, kind: "session.instructed", data: JSON.stringify({ session: "s1", body: "Use KV" }), session_id: null, actor: "Aman" }; assert.deepEqual(framesFor(instructed, idle), [{ type: "instruction", id: "12", body: "Use KV", by: { person: "Aman" } }]); assert.deepEqual(framesFor(instructed, { ...me, session: "s2" }), []); const ended: DeliverableRow = { id: 13, kind: "session.ended", data: JSON.stringify({ session: "s1", outcome: "finished", reason: "Stopped by a human." }), session_id: null, actor: "Aman" }; assert.deepEqual(framesFor(ended, idle), [{ type: "stop", id: "13", reason: "Stopped by a human." }]); assert.deepEqual(framesFor(ended, { ...me, session: "s2" }), []); }); test("delivers mentions to the session they name", () => { const mentioned: DeliverableRow = { id: 14, kind: "agent.mentioned", data: JSON.stringify({ session: "s1", pull: 7, title: "Fix login", comment: 99, body: "@agent/s1 look", assigned: true }), session_id: null, actor: "Aman" }; assert.deepEqual(framesFor(mentioned, idle), [{ type: "mention", id: "14", pull: 7, title: "Fix login", body: "@agent/s1 look", assigned: true, run: false, by: { person: "Aman" } }]); assert.deepEqual(framesFor(mentioned, { ...me, session: "s2" }), []); }); test("tells the parent session when its sub-agent finishes", () => { const finished: DeliverableRow = { id: 15, kind: "agent.run_finished", data: JSON.stringify({ session: "s1", run: "s9", mention: "14", ok: true, summary: "Answered." }), session_id: "s9", actor: "claude-code" }; assert.deepEqual(framesFor(finished, idle), [{ type: "run", id: "15", mention: "14", ok: true, summary: "Answered." }]); assert.deepEqual(framesFor(finished, { ...me, session: "s2" }), []); const own: DeliverableRow = { ...finished, data: JSON.stringify({ session: "s1", run: "s1", mention: "14", ok: true, summary: "Answered." }), session_id: "s1" }; assert.deepEqual(framesFor(own, idle), []); });