107 lines · 6.3 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { agentActor } from "../identity.ts"; |
| 4 | import { Policy } from "../policy.ts"; |
| 5 | import { agentSessions, type Answer } from "../sessions.ts"; |
| 6 | import { d1, migrated } from "./d1.ts"; |
| 7 | |
| 8 | const ORIGIN = "https://refs.test"; |
| 9 | |
| 10 | function sessions(clientSession: string) { |
| 11 | return sessionsWithDb(clientSession).agent; |
| 12 | } |
| 13 | |
| 14 | function sessionsWithDb(clientSession: string) { |
| 15 | const sqlite = migrated(); |
| 16 | sqlite.exec("INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0); INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@refs.test', 'Aman', '', 0); INSERT INTO memberships (namespace_id, user_id, role, created_at) VALUES ('ns', 'u1', 'owner', 0)"); |
| 17 | sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('r1', 'ns', 'demo', 'private', 'demo', 'u1', 0)"); |
| 18 | sqlite.exec("INSERT INTO tasks (id, repo_id, number, title, author_id, created_at, updated_at) VALUES (1, 'r1', 7, 'Do it', 'u1', 0, 0)"); |
| 19 | return { sqlite, agent: agentSessions({ DB: d1(sqlite) } as Env, { waitUntil() {} }, "u1", "mcp-1", "claude-code", ORIGIN, clientSession) }; |
| 20 | } |
| 21 | |
| 22 | test("connect and take keep the token out of the text for the credential helper, and in it for clients without one", async () => { |
| 23 | for (const credentials of ["helper", undefined]) { |
| 24 | const agent = sessions(`client-${credentials}`); |
| 25 | const connected = (await agent.connect({ repo: "refs/demo", credentials })) as Answer; |
| 26 | const taken = await agent.take({ branch: "fix-login" }, credentials); |
| 27 | const released = await agent.release(); |
| 28 | |
| 29 | for (const answer of [connected, taken]) { |
| 30 | assert.match(answer.refs!.token!, /^refs_agent_/); |
| 31 | assert.equal(answer.refs!.remote, `${ORIGIN}/refs/demo.git`); |
| 32 | assert.equal(answer.text.includes(answer.refs!.token!), credentials === undefined); |
| 33 | } |
| 34 | assert.notEqual(taken.refs!.token, connected.refs!.token); |
| 35 | assert.match(released, /still pushes as this session/); |
| 36 | assert.equal(/refs_agent_|extraHeader/.test(connected.text + taken.text + released), credentials === undefined); |
| 37 | } |
| 38 | }); |
| 39 | |
| 40 | test("refs_take hands a session a named branch or a pull request's branch, and never the default branch", async () => { |
| 41 | const agent = sessions("client-take"); |
| 42 | await agent.connect({ repo: "refs/demo", credentials: "helper" }); |
| 43 | assert.match((await agent.take({ branch: "agent/fix-login" }, "helper")).text, /^You took branch agent\/fix-login\. You may push only agent\/fix-login\./); |
| 44 | assert.match(await agent.status(), /on branch agent\/fix-login\.$/); |
| 45 | assert.match(await agent.release(), /^Released branch agent\/fix-login\./); |
| 46 | assert.match(await agent.status(), /, idle\.$/); |
| 47 | |
| 48 | for (const [target, problem] of [ |
| 49 | [{ branch: "main" }, /default branch/], |
| 50 | [{ branch: "a..b" }, /not a valid branch name/], |
| 51 | [{ pull: 1, branch: "x" }, /exactly one/], |
| 52 | [{}, /exactly one/], |
| 53 | [{ pull: 9 }, /No pull request #9/], |
| 54 | ] as const) await assert.rejects(agent.take(target, "helper"), problem); |
| 55 | }); |
| 56 | |
| 57 | test("refs_take on a pull request takes its head branch", async () => { |
| 58 | for (const head of ["fix-login", "tasks/7"]) { |
| 59 | const { agent, sqlite } = sessionsWithDb(`client-pull-${head}`); |
| 60 | sqlite.prepare("INSERT INTO pulls (id, repo_id, number, title, author_id, base_ref, head_ref, created_at, updated_at) VALUES (1, 'r1', 1, 'Fix login', 'u1', 'main', ?, 0, 0)").run(head); |
| 61 | await agent.connect({ repo: "refs/demo", credentials: "helper" }); |
| 62 | assert.equal((await agent.take({ pull: 1 }, "helper")).text.split("\n")[0], `You took pull request #1: Fix login, on branch ${head}. You may push only ${head}.`); |
| 63 | assert.deepEqual({ ...sqlite.prepare("SELECT branch FROM agent_sessions").get() }, { branch: head }); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | test("an agent sees the pull requests of its branch, and no others, and reads tasks like its human", () => { |
| 68 | const row = { id: "s1", runtime: "claude-code", expires_at: 2 ** 31, repo_id: "r1", repo_name: "refs/demo", user_id: "u1", email: "a@refs.test", name: "Aman" }; |
| 69 | const onBranch = new Policy(agentActor({ ...row, branch: "fix-login" }), "owner"); |
| 70 | const idle = new Policy(agentActor({ ...row, branch: null }), "owner"); |
| 71 | assert.equal(onBranch.canOpenPull("refs/demo", { head: "fix-login" }), true); |
| 72 | assert.equal(onBranch.canOpenPull("refs/demo", { head: "other" }), false); |
| 73 | assert.equal(onBranch.canOpenPull("refs/other", { head: "fix-login" }), false); |
| 74 | assert.equal(idle.canOpenPull("refs/demo", { head: "fix-login" }), false); |
| 75 | assert.equal(idle.canOpenTask("refs/demo"), true); |
| 76 | assert.equal(idle.canOpenTask("refs/other"), false); |
| 77 | }); |
| 78 | |
| 79 | test("refs_open_pull needs a held branch and opens only from it", async () => { |
| 80 | const agent = sessions("client-pull"); |
| 81 | await agent.connect({ repo: "refs/demo" }); |
| 82 | await assert.rejects(agent.openPull("Fix it", "", undefined, undefined), /Take a branch/); |
| 83 | await agent.take({ branch: "fix-login" }); |
| 84 | await assert.rejects(agent.openPull("", "", undefined, undefined), /title must be/); |
| 85 | await assert.rejects(agent.openPull("Fix it", "", "main", "other"), /only open pull requests from your branch/); |
| 86 | }); |
| 87 | |
| 88 | test("two refs_events calls at the same time hand an update to only one of them", async () => { |
| 89 | const { sqlite, agent } = sessionsWithDb("racing"); |
| 90 | await agent.connect({ repo: "refs/demo" }); |
| 91 | const session = (sqlite.prepare("SELECT id FROM agent_sessions").get() as { id: string }).id; |
| 92 | sqlite.prepare("INSERT INTO events (stream, kind, data, actor_kind, actor_id, created_at) VALUES ('repo:r1', 'session.instructed', ?, 'human', 'u1', 0)").run(JSON.stringify({ session, body: "Stop and write tests." })); |
| 93 | |
| 94 | const answers = await Promise.all([agent.events(), agent.events()]); |
| 95 | |
| 96 | assert.equal(answers.filter((answer) => answer.includes("Stop and write tests.")).length, 1); |
| 97 | assert.doesNotMatch(await agent.events(), /Stop and write tests\./); |
| 98 | }); |
| 99 | |
| 100 | test("refs_connect names the session outside the text, both for a new session and for a reconnect", async () => { |
| 101 | const agent = sessions("reconnecting"); |
| 102 | const first = (await agent.connect({ repo: "refs/demo" })) as Answer; |
| 103 | const again = (await agent.connect({ repo: "refs/demo" })) as Answer; |
| 104 | assert.match(first.session!, /^[0-9a-f-]{36}$/); |
| 105 | assert.equal(again.session, first.session); |
| 106 | assert.equal(again.refs, undefined); |
| 107 | }); |