51 lines · 3.5 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import type { Actor } from "../contract/actor.ts"; |
| 4 | import { workKey } from "../analytics.ts"; |
| 5 | import { deliverMentions, onPull } from "../mentions.ts"; |
| 6 | import { d1, migrated } from "./d1.ts"; |
| 7 | |
| 8 | const SESSION = "11111111-2222-3333-4444-555555555555"; |
| 9 | |
| 10 | function setup() { |
| 11 | const sqlite = migrated(); |
| 12 | const at = Math.floor(Date.now() / 1000); |
| 13 | 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', 'x', 0); INSERT INTO memberships (namespace_id, user_id, role, created_at) VALUES ('ns', 'u1', 'owner', 0)`); |
| 14 | sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'refs', 'private', 'refs', 'u1', 0)"); |
| 15 | sqlite.prepare("INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, created_at, expires_at, mcp_session) VALUES (?, 'h', 'u1', 'repo', 'codex', ?, ?, 'mcp')").run(SESSION, at, at + 3600); |
| 16 | const env = { DB: d1(sqlite), POSTHOG_ENABLED: "true", POSTHOG_PROJECT_TOKEN: "project-token", POSTHOG_HOST: "https://us.i.posthog.com" } as unknown as Env; |
| 17 | const pending: Promise<unknown>[] = []; |
| 18 | const ctx = { waitUntil: (promise: Promise<unknown>) => void pending.push(promise) }; |
| 19 | return { env, ctx, pending }; |
| 20 | } |
| 21 | |
| 22 | const pull = onPull({ repo_id: "repo", number: 2, title: "Private title", head_ref: "private-branch" }); |
| 23 | const human: Actor = { kind: "human", person: { id: "u1", name: "Aman", email: "a@x" }, via: "session" }; |
| 24 | |
| 25 | test("a person directing a live agent is captured once per mention, without the mention text", async (t) => { |
| 26 | const payloads: { event: string; distinct_id: string; properties: Record<string, unknown> }[] = []; |
| 27 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 28 | payloads.push(JSON.parse(String(init.body))); |
| 29 | return new Response(null, { status: 204 }); |
| 30 | }); |
| 31 | const { env, ctx, pending } = setup(); |
| 32 | await deliverMentions(env, ctx, human, pull, `@agent/${SESSION} private instruction`, 3, { thread: 5 }); |
| 33 | await deliverMentions(env, ctx, human, pull, "no agent named here", 4); |
| 34 | await Promise.all(pending); |
| 35 | assert.equal(payloads.length, 1); |
| 36 | const { event, distinct_id, properties } = payloads[0]; |
| 37 | assert.deepEqual({ event, distinct_id }, { event: "agent.directed", distinct_id: "human:u1" }); |
| 38 | assert.deepEqual({ channel: properties.channel, assigned: properties.assigned, task_number: properties.task_number, $groups: properties.$groups }, { channel: "thread", assigned: false, task_number: undefined, $groups: { workspace: "ns" } }); |
| 39 | assert.equal(properties.work_key, await workKey("repo", "private-branch")); |
| 40 | assert.match(String(properties.work_key), /^[0-9a-f]{16}$/); |
| 41 | assert.doesNotMatch(JSON.stringify(payloads), /private instruction|Private title|private-branch|Aman|a@x/); |
| 42 | }); |
| 43 | |
| 44 | test("an agent mentioning another agent is not human direction", async (t) => { |
| 45 | const fetch = t.mock.method(globalThis, "fetch", async () => new Response(null, { status: 204 })); |
| 46 | const { env, ctx, pending } = setup(); |
| 47 | const agent: Actor = { kind: "agent", agent: { id: "other", name: "codex", runtime: "codex" }, onBehalfOf: human.person, sessionId: "other", grant: { repos: "all", org: "none" } }; |
| 48 | await deliverMentions(env, ctx, agent, pull, `@agent/${SESSION} look`, null); |
| 49 | await Promise.all(pending); |
| 50 | assert.equal(fetch.mock.callCount(), 0); |
| 51 | }); |