73 lines · 5.2 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { listAgents, openHostedAgents, resolveAgent } from "../agents.ts"; |
| 4 | import { failure } from "../models.ts"; |
| 5 | import type { Policy } from "../policy.ts"; |
| 6 | import { d1, migrated } from "./d1.ts"; |
| 7 | |
| 8 | const KEY = "sk-ant-api03-secret-value-1234"; |
| 9 | const admin = { actor: { kind: "human", person: { id: "u1", name: "Aman", email: "a@x" }, via: "session" }, person: { id: "u1", name: "Aman", email: "a@x" } } as unknown as Policy; |
| 10 | const input = { handle: "security", description: "Finds security holes.", instructions: "Look for injection.", provider: "", model: "", key: "" }; |
| 11 | |
| 12 | function setup(status = 200) { |
| 13 | const sqlite = migrated(); |
| 14 | 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@x', 'Aman', '', 0); 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')`); |
| 15 | const sent: { provider: string; headers: Record<string, string>; query: Record<string, unknown> }[] = []; |
| 16 | const AI = { |
| 17 | gateway: () => ({ |
| 18 | run: async (request: (typeof sent)[number]) => { |
| 19 | sent.push(request); |
| 20 | return new Response(status === 200 ? JSON.stringify({ choices: [{ message: { content: "OK" } }] }) : JSON.stringify({ error: { message: `Incorrect API key provided: ${KEY}` } }), { status }); |
| 21 | }, |
| 22 | }), |
| 23 | }; |
| 24 | const env = { DB: d1(sqlite), AI, CI_SECRETS_KEY: btoa(String.fromCharCode(...new Uint8Array(32).fill(7))) } as unknown as Env; |
| 25 | return { env, sqlite, sent }; |
| 26 | } |
| 27 | |
| 28 | test("a repository agent replaces the shared and built-in agent of the same handle", async () => { |
| 29 | const { env } = setup(); |
| 30 | assert.equal((await openHostedAgents(env, admin, "ns", null, true).save({ ...input, handle: "reviewer", instructions: "Shared review rules." })).ok, true); |
| 31 | assert.equal((await openHostedAgents(env, admin, "ns", "r1", true).save({ ...input, handle: "reviewer", instructions: "Repo review rules." })).ok, true); |
| 32 | const list = (await listAgents(env.DB, "ns", "r1")).filter((agent) => agent.handle === "reviewer"); |
| 33 | assert.deepEqual(list.map((agent) => [agent.scope, agent.handle, agent.replaced]), [["repo", "reviewer", false], ["namespace", "reviewer", true], ["builtin", "reviewer", true]]); |
| 34 | assert.equal((await resolveAgent(env, "ns", "r1", "reviewer"))?.instructions, "Repo review rules."); |
| 35 | assert.equal((await resolveAgent(env, "ns", "other", "reviewer"))?.instructions, "Shared review rules."); |
| 36 | assert.equal(await resolveAgent(env, "ns", "r1", "nobody"), null); |
| 37 | }); |
| 38 | |
| 39 | test("a working key is tested, sealed, kept on a later edit, and opened for runs", async () => { |
| 40 | const { env, sqlite, sent } = setup(); |
| 41 | const agents = openHostedAgents(env, admin, "ns", "r1", true); |
| 42 | assert.equal((await agents.save({ ...input, provider: "anthropic", model: " claude-sonnet-5-5 ", key: KEY })).ok, true); |
| 43 | assert.equal(sent[0].headers.authorization, `Bearer ${KEY}`); |
| 44 | assert.ok(!JSON.stringify(sqlite.prepare("SELECT * FROM hosted_agents").all()).includes(KEY)); |
| 45 | assert.equal((await agents.save({ ...input, description: "Changed.", provider: "anthropic", model: "claude-opus-5-5", key: "" })).ok, true); |
| 46 | assert.deepEqual((await resolveAgent(env, "ns", "r1", "security"))?.choice, { provider: "anthropic", model: "claude-opus-5-5", key: KEY }); |
| 47 | assert.deepEqual((await listAgents(env.DB, "ns", "r1"))[0].model, { provider: "anthropic", model: "claude-opus-5-5", keyHint: "1234" }); |
| 48 | }); |
| 49 | |
| 50 | test("a rejected key is not stored and the error never shows the key", async () => { |
| 51 | const { env, sqlite } = setup(400); |
| 52 | const outcome = await openHostedAgents(env, admin, "ns", "r1", true).save({ ...input, provider: "openai", model: "gpt-5.5", key: KEY }); |
| 53 | assert.ok(!outcome.ok && !outcome.problem.message.includes(KEY)); |
| 54 | assert.equal(sqlite.prepare("SELECT count(*) AS n FROM hosted_agents").get()!.n, 0); |
| 55 | }); |
| 56 | |
| 57 | test("only a person with edit rights changes agents, and bad input is refused before any request", async () => { |
| 58 | const { env, sent } = setup(); |
| 59 | assert.equal((await openHostedAgents(env, admin, "ns", "r1", false).save(input)).ok, false); |
| 60 | const agent = { ...admin, actor: { kind: "agent" } } as unknown as Policy; |
| 61 | assert.equal((await openHostedAgents(env, agent, "ns", "r1", true).save(input)).ok, false); |
| 62 | const agents = openHostedAgents(env, admin, "ns", "r1", true); |
| 63 | for (const bad of [{ handle: "Bad Name" }, { handle: "all" }, { instructions: " " }, { provider: "mistral", model: "x", key: KEY }, { provider: "openai", model: "has space", key: KEY }, { provider: "openai", model: "gpt-5.5", key: "" }]) { |
| 64 | assert.equal((await agents.save({ ...input, ...bad })).ok, false, JSON.stringify(bad)); |
| 65 | } |
| 66 | assert.equal(sent.length, 0); |
| 67 | }); |
| 68 | |
| 69 | test("provider errors name the problem without the key", () => { |
| 70 | const choice = { provider: "openai", model: "gpt-5.5", key: KEY } as const; |
| 71 | assert.equal(failure(choice, 401, KEY), "OpenAI rejected the API key."); |
| 72 | assert.equal(failure(choice, 404, JSON.stringify({ error: { message: `model not found for ${KEY} and sk-proj-otherkey99` } })), "OpenAI returned 404: model not found for … and …"); |
| 73 | }); |