78 lines · 5.9 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { buildGraph } from "../code-index/graph.ts"; |
| 4 | import type { ChangeSet } from "../contract/views.ts"; |
| 5 | import { BUILTIN_AGENTS, mentionedHandles } from "../agents.ts"; |
| 6 | import { changedSymbols, consumeDeadAgentRuns, listAgentRuns } from "../agent-runs.ts"; |
| 7 | import { d1, migrated } from "./d1.ts"; |
| 8 | |
| 9 | test("an agent mention counts only outside code and as a whole handle", () => { |
| 10 | assert.deepEqual(mentionedHandles("@reviewer please look, and @security too"), ["reviewer", "security"]); |
| 11 | assert.deepEqual(mentionedHandles("On pull request #3:\n\nthanks, @reviewer."), ["reviewer"]); |
| 12 | assert.deepEqual(mentionedHandles("see `@reviewer` in the docs"), []); |
| 13 | assert.deepEqual(mentionedHandles("```\n@reviewer\n```"), []); |
| 14 | assert.deepEqual(mentionedHandles("mail ops@reviewer.dev"), []); |
| 15 | assert.deepEqual(mentionedHandles("@reviewer/x and @agent/abc and @all"), []); |
| 16 | }); |
| 17 | |
| 18 | test("each built-in agent has a handle that a mention finds", () => { |
| 19 | for (const agent of BUILTIN_AGENTS) assert.deepEqual(mentionedHandles(`@${agent.handle} look`), [agent.handle]); |
| 20 | assert.equal(new Set(BUILTIN_AGENTS.map((agent) => agent.handle)).size, BUILTIN_AGENTS.length); |
| 21 | }); |
| 22 | |
| 23 | test("the changed declarations list each touched symbol with how often it is used", () => { |
| 24 | const lib = ["export function total(items: number[]) {", " return items.reduce((a, b) => a + b, 0);", "}", "", "export function unused() {", " return 1;", "}"].join("\n"); |
| 25 | const app = ['import { total } from "./lib.ts";', "export function run() {", " return total([1, 2]);", "}"].join("\n"); |
| 26 | const graph = buildGraph("abc", [{ path: "lib.ts", text: lib }, { path: "app.ts", text: app }], [], 0); |
| 27 | const before = lib.replace("a + b", "b + a"); |
| 28 | const changes: ChangeSet = { |
| 29 | base: "base", |
| 30 | head: "abc", |
| 31 | commits: [], |
| 32 | files: [{ path: "lib.ts", status: "modified", kind: "text", additions: 1, deletions: 1, before, after: lib, truncated: false }], |
| 33 | additions: 1, |
| 34 | deletions: 1, |
| 35 | truncated: false, |
| 36 | }; |
| 37 | const found = changedSymbols(changes, graph); |
| 38 | assert.equal(found.length, 1); |
| 39 | assert.match(found[0], /^lib\.ts#total \(function, exported\) lib\.ts:1-3 · used 1 times$/); |
| 40 | }); |
| 41 | |
| 42 | test("the run list shows who started each run, where it stands, and the steps of a working run", async () => { |
| 43 | const sqlite = migrated(); |
| 44 | sqlite.exec(`INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0); |
| 45 | INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@x', 'Aman', '', 0); |
| 46 | INSERT INTO repos (id, namespace_id, name, artifacts_name, default_branch, visibility, description, created_at, creator_id) VALUES ('r1', 'ns', 'source', 'a', 'main', 'private', '', 0, 'u1'); |
| 47 | INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, status, created_at, expires_at, last_seen_at) VALUES ('s1', 'h1', 'u1', 'r1', 'refs:reviewer', 'running', 10, 99999, 10), ('s2', 'h2', 'u1', 'r1', 'refs:security', 'finished', 5, 99999, 5), ('s3', 'h3', 'u1', 'r1', 'claude-code', 'running', 1, 99999, 1); |
| 48 | UPDATE agent_sessions SET ended_at = 8 WHERE id = 's2'; |
| 49 | INSERT INTO events (stream, kind, data, actor_kind, actor_id, session_id, created_at) VALUES |
| 50 | ('repo:r1', 'agent.mentioned', '{"session":"s1","pull":2,"comment":7}', 'human', 'u1', NULL, 10), |
| 51 | ('repo:r1', 'agent.mentioned', '{"session":"s2","pull":3,"comment":null}', 'human', 'u1', NULL, 5), |
| 52 | ('repo:r1', 'agent.step', '{"session":"s1","step":1,"thinking":"Read the diff first.","tools":["read_file(a.ts)"]}', 'agent', 's1', 's1', 11), |
| 53 | ('repo:r1', 'agent.run_finished', '{"session":"s2","ok":true,"summary":"No real problem."}', 'agent', 's2', 's2', 8);`); |
| 54 | const runs = await listAgentRuns(d1(sqlite), "r1", null); |
| 55 | assert.deepEqual(runs.map((run) => [run.handle, run.state, run.pull, run.via, run.summary]), [["reviewer", "working", 2, "mention", null], ["security", "answered", 3, "ci", "No real problem."]]); |
| 56 | assert.deepEqual(runs[0].steps, [{ step: 1, at: 11, thinking: "Read the diff first.", tools: ["read_file(a.ts)"] }]); |
| 57 | assert.equal(runs[1].steps, null); |
| 58 | assert.deepEqual((await listAgentRuns(d1(sqlite), "r1", "s2"))[1].steps, []); |
| 59 | }); |
| 60 | |
| 61 | test("a dead run is marked failed once, and a run that already ended stays as it is", async () => { |
| 62 | const sqlite = migrated(); |
| 63 | sqlite.exec(`INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0); |
| 64 | INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@x', 'Aman', '', 0); |
| 65 | INSERT INTO repos (id, namespace_id, name, artifacts_name, default_branch, visibility, description, created_at, creator_id) VALUES ('r1', 'ns', 'source', 'a', 'main', 'private', '', 0, 'u1'); |
| 66 | INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, status, created_at, expires_at, last_seen_at) VALUES ('s1', 'h1', 'u1', 'r1', 'refs:reviewer', 'running', 10, 99999, 10), ('s2', 'h2', 'u1', 'r1', 'refs:reviewer', 'finished', 10, 99999, 10); |
| 67 | UPDATE agent_sessions SET ended_at = 20 WHERE id = 's2';`); |
| 68 | const acked: string[] = []; |
| 69 | const batch = (session: string) => ({ messages: [{ body: { session, mention: 5 }, ack: () => acked.push(session) }] }) as unknown as MessageBatch<{ session: string; mention: number }>; |
| 70 | const env = { DB: d1(sqlite) } as unknown as Env; |
| 71 | await consumeDeadAgentRuns(batch("s1"), env); |
| 72 | await consumeDeadAgentRuns(batch("s1"), env); |
| 73 | await consumeDeadAgentRuns(batch("s2"), env); |
| 74 | assert.deepEqual(acked, ["s1", "s1", "s2"]); |
| 75 | assert.equal((sqlite.prepare("SELECT status FROM agent_sessions WHERE id = 's1'").get() as { status: string }).status, "failed"); |
| 76 | const finished = sqlite.prepare("SELECT session_id, json_extract(data, '$.ok') AS ok, json_extract(data, '$.mention') AS mention FROM events WHERE kind = 'agent.run_finished'").all(); |
| 77 | assert.deepEqual(finished.map((row) => ({ ...row })), [{ session_id: "s1", ok: 0, mention: "5" }]); |
| 78 | }); |