import assert from "node:assert/strict"; import { test } from "node:test"; import { cliApi } from "../cli.ts"; import { d1, migrated } from "./d1.ts"; test("the CLI's repository routes act as the person only for a sign-in with the git scope", async () => { const sqlite = migrated(); 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)"); sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('r1', 'ns', 'demo', 'private', 'demo', 'u1', 0)"); sqlite.exec("INSERT INTO pulls (id, repo_id, number, title, author_id, base_ref, head_ref, created_at, updated_at) VALUES (1, 'r1', 3, 'Fix login', 'u1', 'main', 'fix-login', 0, 0)"); const env = { DB: d1(sqlite), ADMIN_EMAILS: "" } as unknown as Env; const call = (path: string, scope: string[]) => cliApi.fetch(new Request(`https://refs.test${path}`), env, { waitUntil() {}, props: { userId: "u1" }, auth: { scope } } as unknown as ExecutionContext); assert.equal((await call("/api/cli/repos/refs/demo/pulls", ["agents"])).status, 403); const listed = await call("/api/cli/repos/refs/demo/pulls?state=open", ["agents", "git"]); assert.equal(listed.status, 200); assert.deepEqual(((await listed.json()) as { items: { number: number }[] }).items.map((pull) => pull.number), [3]); assert.equal((await call("/api/cli/repos/refs/missing/pulls", ["agents", "git"])).status, 404); assert.equal((await call("/api/cli/whoami", ["agents"])).status, 200); });