import assert from "node:assert/strict"; import { test } from "node:test"; import type { DecisionRecord } from "@refs/protocol"; import type { Actor } from "../contract/actor.ts"; import { addressSteers } from "../decisions.ts"; import { createPull, findPull, listBranches, toPullDetail } from "../pulls.ts"; import type { RepoRecord } from "../repos.ts"; import { createTaskRecord } from "../tasks.ts"; import { recordPushers, withPushers } from "../events.ts"; import { d1, migrated } from "./d1.ts"; const agent: Actor = { kind: "agent", agent: { id: "claude", name: "Claude Code", runtime: "claude-code" } as never, onBehalfOf: { id: "u1", name: "Aman" } as never, sessionId: "s1", grant: { repos: "all" } as never, }; const record = (id: string, supersedes: string[]): DecisionRecord => ({ decisionId: id, decision: "x", reason: ["y"], toReverse: "", alternativesConsidered: [], rejected: {}, assumptions: [], affectedComponents: [], evidence: [], supersedes }); test("a thread anchors to lines or to a decision, never both or neither", () => { const sqlite = migrated(); const insert = sqlite.prepare("INSERT INTO review_threads (pull_id, path, side, start_line, end_side, end_line, decision_id, revision, code, created_at, updated_at) VALUES (1, ?, ?, ?, ?, ?, ?, 'r', 'c', 0, 0)"); insert.run("a.ts", "additions", 1, "additions", 2, null); insert.run(null, null, null, null, null, "DEC-A"); assert.throws(() => insert.run("a.ts", "additions", 1, "additions", 2, "DEC-A")); assert.throws(() => insert.run(null, null, null, null, null, null)); }); test("a pushed decision that supersedes a steered one marks its open threads addressed", async () => { const sqlite = migrated(); sqlite.exec("INSERT INTO pulls (id, repo_id, number, title, author_id, base_ref, head_ref, created_at, updated_at) VALUES (1, 'repo', 1, 't', 'u1', 'main', 'tasks/7', 0, 0), (2, 'other', 1, 't', 'u1', 'main', 'tasks/7', 0, 0)"); const insert = sqlite.prepare("INSERT INTO review_threads (pull_id, decision_id, status, revision, code, created_at, updated_at) VALUES (?, ?, ?, 'r', 'c', 0, 0)"); insert.run(1, "DEC-A", "open"); insert.run(1, "DEC-A", "resolved"); insert.run(1, "DEC-B", "open"); insert.run(2, "DEC-A", "open"); await addressSteers(d1(sqlite), agent, { id: "repo" } as RepoRecord, [{ commit: "c0ffee", record: record("DEC-C", ["DEC-A"]) }]); const rows = sqlite.prepare("SELECT id, status, addressed_commit FROM review_threads ORDER BY id").all(); assert.deepEqual( rows.map((row) => ({ ...row })), [ { id: 1, status: "addressed", addressed_commit: "c0ffee" }, { id: 2, status: "resolved", addressed_commit: null }, { id: 3, status: "open", addressed_commit: null }, { id: 4, status: "open", addressed_commit: null }, ], ); const events = sqlite.prepare("SELECT kind, data FROM events").all(); assert.deepEqual( events.map((event) => ({ ...event })), [{ kind: "pull.thread_addressed", data: JSON.stringify({ thread: 1, commit: "c0ffee" }) }], ); }); test("a branched pull request knows where it started, and its source lists it", async () => { const sqlite = migrated(); sqlite.exec("INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@example.com', 'Aman', 'x', 0)"); sqlite.exec("INSERT INTO namespaces (id, slug, name, created_at) VALUES ('ns', 'refs', 'refs', 0)"); sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'source', 'private', 'source', 'u1', 0)"); const db = d1(sqlite); const human: Actor = { kind: "human", person: { id: "u1", name: "Aman" } as never, via: "session" }; const pull = { repoId: "repo", title: "Sessions in DOs", body: "", authorId: "u1", base: "main" }; const number = async (created: Awaited>) => (created.kind === "created" ? created.number : assert.fail("the pull request was not created")); const source = (await findPull(db, "repo", await number(await createPull(db, human, { ...pull, head: "tasks/7" }))))!; const branched = await number(await createPull(db, human, { ...pull, title: "Sessions in D1", head: "tasks/8", branchedFrom: { pull: source.id, commit: "c0ffee" } })); assert.deepEqual(toPullDetail((await findPull(db, "repo", branched))!).branchedFrom, { number: source.number, title: "Sessions in DOs", commit: "c0ffee" }); assert.equal(toPullDetail(source).branchedFrom, null); assert.deepEqual((await listBranches(db, source)).map((row) => [row.number, row.branched_at]), [[branched, "c0ffee"]]); assert.equal((sqlite.prepare("SELECT count(*) AS n FROM events WHERE stream = ? AND kind = 'pull.opened'").get(`pull:${source.id}`) as { n: number }).n, 1); assert.deepEqual(await createPull(db, human, { ...pull, head: "tasks/7" }), { kind: "already_open" }); }); test("a new task gets its number, its opened event and its labels in one write", async () => { const sqlite = migrated(); sqlite.exec("INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@example.com', 'Aman', 'x', 0)"); sqlite.exec("INSERT INTO namespaces (id, slug, name, created_at) VALUES ('ns', 'refs', 'refs', 0)"); sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'source', 'private', 'source', 'u1', 0)"); sqlite.exec("INSERT INTO labels (id, namespace_id, name, color, description, created_at) VALUES ('bug', 'ns', 'bug', 'red', '', 0)"); const db = d1(sqlite); const human: Actor = { kind: "human", person: { id: "u1", name: "Aman" } as never, via: "session" }; const input = { repoId: "repo", title: "Fix it", body: "", authorId: "u1", assigneeId: null, labelIds: ["bug"] }; await createTaskRecord(db, human, input); const second = await createTaskRecord(db, human, input); assert.equal(second.number, 2); assert.equal((sqlite.prepare("SELECT count(*) AS n FROM events WHERE stream = ? AND kind = 'task.opened'").get(`task:${second.id}`) as { n: number }).n, 1); assert.deepEqual(sqlite.prepare("SELECT label_id FROM task_labels WHERE task_id = ?").all(second.id).map((row) => row.label_id), ["bug"]); }); test("a pull request and its commits name the agent that opened and pushed them", async () => { const sqlite = migrated(); sqlite.exec("INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@example.com', 'Aman', 'x', 0)"); sqlite.exec("INSERT INTO namespaces (id, slug, name, created_at) VALUES ('ns', 'refs', 'refs', 0)"); sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'source', 'private', 'source', 'u1', 0)"); sqlite.exec("INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, created_at, expires_at) VALUES ('s1', 'h', 'u1', 'repo', 'claude-code', 0, 9)"); const db = d1(sqlite); const byAgent: Actor = { ...agent, agent: { id: "s1", name: "claude-code", runtime: "claude-code" } }; const human: Actor = { kind: "human", person: { id: "u1", name: "Aman" } as never, via: "session" }; const opened = await createPull(db, byAgent, { repoId: "repo", title: "t", body: "", authorId: "u1", base: "main", head: "tasks/7" }); const number = opened.kind === "created" ? opened.number : assert.fail("the pull request was not created"); assert.equal(toPullDetail((await findPull(db, "repo", number))!).agent, "claude-code"); await db.batch(recordPushers(db, byAgent, "repo", ["a1"])); await db.batch(recordPushers(db, human, "repo", ["a1", "b2"])); assert.deepEqual(recordPushers(db, { kind: "system" }, "repo", ["c3"]), []); const commits = await withPushers(db, "repo", [{ hash: "a1" }, { hash: "b2" }, { hash: "c3" }] as never[]); assert.deepEqual( commits.map((commit: { pushedBy?: { actor: { kind: string; name: string } } }) => commit.pushedBy?.actor), [{ kind: "agent", id: "s1", name: "claude-code", runtime: "claude-code" }, { kind: "human", id: "u1", name: "Aman" }, undefined], ); });