122 lines · 8.3 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import type { DecisionRecord } from "@refs/protocol"; |
| 4 | import type { Actor } from "../contract/actor.ts"; |
| 5 | import { addressSteers } from "../decisions.ts"; |
| 6 | import { createPull, findPull, freshBranchName, listBranches, toPullDetail } from "../pulls.ts"; |
| 7 | import type { RepoRecord } from "../repos.ts"; |
| 8 | import { createTaskRecord } from "../tasks.ts"; |
| 9 | import { recordPushers, withPushers } from "../events.ts"; |
| 10 | import { d1, migrated } from "./d1.ts"; |
| 11 | |
| 12 | const agent: Actor = { |
| 13 | kind: "agent", |
| 14 | agent: { id: "claude", name: "Claude Code", runtime: "claude-code" } as never, |
| 15 | onBehalfOf: { id: "u1", name: "Aman" } as never, |
| 16 | sessionId: "s1", |
| 17 | grant: { repos: "all" } as never, |
| 18 | }; |
| 19 | const record = (id: string, supersedes: string[]): DecisionRecord => ({ decisionId: id, decision: "x", reason: ["y"], toReverse: "", alternativesConsidered: [], rejected: {}, assumptions: [], affectedComponents: [], evidence: [], supersedes }); |
| 20 | |
| 21 | test("a thread anchors to lines or to a decision, never both or neither", () => { |
| 22 | const sqlite = migrated(); |
| 23 | 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)"); |
| 24 | insert.run("a.ts", "additions", 1, "additions", 2, null); |
| 25 | insert.run(null, null, null, null, null, "DEC-A"); |
| 26 | assert.throws(() => insert.run("a.ts", "additions", 1, "additions", 2, "DEC-A")); |
| 27 | assert.throws(() => insert.run(null, null, null, null, null, null)); |
| 28 | }); |
| 29 | |
| 30 | test("a pushed decision that supersedes a steered one marks its open threads addressed", async () => { |
| 31 | const sqlite = migrated(); |
| 32 | 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)"); |
| 33 | const insert = sqlite.prepare("INSERT INTO review_threads (pull_id, decision_id, status, revision, code, created_at, updated_at) VALUES (?, ?, ?, 'r', 'c', 0, 0)"); |
| 34 | insert.run(1, "DEC-A", "open"); |
| 35 | insert.run(1, "DEC-A", "resolved"); |
| 36 | insert.run(1, "DEC-B", "open"); |
| 37 | insert.run(2, "DEC-A", "open"); |
| 38 | |
| 39 | await addressSteers(d1(sqlite), agent, { id: "repo" } as RepoRecord, [{ commit: "c0ffee", record: record("DEC-C", ["DEC-A"]) }]); |
| 40 | |
| 41 | const rows = sqlite.prepare("SELECT id, status, addressed_commit FROM review_threads ORDER BY id").all(); |
| 42 | assert.deepEqual( |
| 43 | rows.map((row) => ({ ...row })), |
| 44 | [ |
| 45 | { id: 1, status: "addressed", addressed_commit: "c0ffee" }, |
| 46 | { id: 2, status: "resolved", addressed_commit: null }, |
| 47 | { id: 3, status: "open", addressed_commit: null }, |
| 48 | { id: 4, status: "open", addressed_commit: null }, |
| 49 | ], |
| 50 | ); |
| 51 | const events = sqlite.prepare("SELECT kind, data FROM events").all(); |
| 52 | assert.deepEqual( |
| 53 | events.map((event) => ({ ...event })), |
| 54 | [{ kind: "pull.thread_addressed", data: JSON.stringify({ thread: 1, commit: "c0ffee" }) }], |
| 55 | ); |
| 56 | }); |
| 57 | |
| 58 | test("a branched pull request knows where it started, and its source lists it", async () => { |
| 59 | const sqlite = migrated(); |
| 60 | sqlite.exec("INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@example.com', 'Aman', 'x', 0)"); |
| 61 | sqlite.exec("INSERT INTO namespaces (id, slug, name, created_at) VALUES ('ns', 'refs', 'refs', 0)"); |
| 62 | sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'source', 'private', 'source', 'u1', 0)"); |
| 63 | const db = d1(sqlite); |
| 64 | const human: Actor = { kind: "human", person: { id: "u1", name: "Aman" } as never, via: "session" }; |
| 65 | const pull = { repoId: "repo", title: "Sessions in DOs", body: "", authorId: "u1", base: "main" }; |
| 66 | const number = async (created: Awaited<ReturnType<typeof createPull>>) => (created.kind === "created" ? created.number : assert.fail("the pull request was not created")); |
| 67 | const source = (await findPull(db, "repo", await number(await createPull(db, human, { ...pull, head: "tasks/7" }))))!; |
| 68 | const branched = await number(await createPull(db, human, { ...pull, title: "Sessions in D1", head: "tasks/8", branchedFrom: { pull: source.id, commit: "c0ffee" } })); |
| 69 | |
| 70 | assert.deepEqual(toPullDetail((await findPull(db, "repo", branched))!).branchedFrom, { number: source.number, title: "Sessions in DOs", commit: "c0ffee" }); |
| 71 | assert.equal(toPullDetail(source).branchedFrom, null); |
| 72 | assert.deepEqual((await listBranches(db, source)).map((row) => [row.number, row.branched_at]), [[branched, "c0ffee"]]); |
| 73 | 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); |
| 74 | assert.deepEqual(await createPull(db, human, { ...pull, head: "tasks/7" }), { kind: "already_open" }); |
| 75 | }); |
| 76 | |
| 77 | test("a new task gets its number, its opened event and its labels in one write", async () => { |
| 78 | const sqlite = migrated(); |
| 79 | sqlite.exec("INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@example.com', 'Aman', 'x', 0)"); |
| 80 | sqlite.exec("INSERT INTO namespaces (id, slug, name, created_at) VALUES ('ns', 'refs', 'refs', 0)"); |
| 81 | sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'source', 'private', 'source', 'u1', 0)"); |
| 82 | sqlite.exec("INSERT INTO labels (id, namespace_id, name, color, description, created_at) VALUES ('bug', 'ns', 'bug', 'red', '', 0)"); |
| 83 | const db = d1(sqlite); |
| 84 | const human: Actor = { kind: "human", person: { id: "u1", name: "Aman" } as never, via: "session" }; |
| 85 | const input = { repoId: "repo", title: "Fix it", body: "", authorId: "u1", assigneeId: null, labelIds: ["bug"] }; |
| 86 | await createTaskRecord(db, human, input); |
| 87 | const second = await createTaskRecord(db, human, input); |
| 88 | |
| 89 | assert.equal(second.number, 2); |
| 90 | 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); |
| 91 | assert.deepEqual(sqlite.prepare("SELECT label_id FROM task_labels WHERE task_id = ?").all(second.id).map((row) => row.label_id), ["bug"]); |
| 92 | }); |
| 93 | |
| 94 | test("a pull request and its commits name the agent that opened and pushed them", async () => { |
| 95 | const sqlite = migrated(); |
| 96 | sqlite.exec("INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@example.com', 'Aman', 'x', 0)"); |
| 97 | sqlite.exec("INSERT INTO namespaces (id, slug, name, created_at) VALUES ('ns', 'refs', 'refs', 0)"); |
| 98 | sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'source', 'private', 'source', 'u1', 0)"); |
| 99 | 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)"); |
| 100 | const db = d1(sqlite); |
| 101 | const byAgent: Actor = { ...agent, agent: { id: "s1", name: "claude-code", runtime: "claude-code" } }; |
| 102 | const human: Actor = { kind: "human", person: { id: "u1", name: "Aman" } as never, via: "session" }; |
| 103 | const opened = await createPull(db, byAgent, { repoId: "repo", title: "t", body: "", authorId: "u1", base: "main", head: "tasks/7" }); |
| 104 | const number = opened.kind === "created" ? opened.number : assert.fail("the pull request was not created"); |
| 105 | assert.equal(toPullDetail((await findPull(db, "repo", number))!).agent, "claude-code"); |
| 106 | |
| 107 | await db.batch(recordPushers(db, byAgent, "repo", ["a1"])); |
| 108 | await db.batch(recordPushers(db, human, "repo", ["a1", "b2"])); |
| 109 | assert.deepEqual(recordPushers(db, { kind: "system" }, "repo", ["c3"]), []); |
| 110 | const commits = await withPushers(db, "repo", [{ hash: "a1" }, { hash: "b2" }, { hash: "c3" }] as never[]); |
| 111 | assert.deepEqual( |
| 112 | commits.map((commit: { pushedBy?: { actor: { kind: string; name: string } } }) => commit.pushedBy?.actor), |
| 113 | [{ kind: "agent", id: "s1", name: "claude-code", runtime: "claude-code" }, { kind: "human", id: "u1", name: "Aman" }, undefined], |
| 114 | ); |
| 115 | }); |
| 116 | |
| 117 | test("a hand-off branch is named from the title, skipping taken names and the default branch", () => { |
| 118 | const taken = new Map([["fix-login", "c0ffee"]]); |
| 119 | assert.equal(freshBranchName("Fix: login!", taken, "main"), "fix-login-2"); |
| 120 | assert.equal(freshBranchName("main", new Map(), "main"), "main-2"); |
| 121 | assert.equal(freshBranchName("!!!", new Map(), "main"), "branch"); |
| 122 | }); |