|
|
@@ -0,0 +1,77 @@ |
| 1 |
|
| 2 |
import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 3 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 4 |
import { tmpdir } from "node:os"; |
| 5 |
import { join } from "node:path"; |
| 6 |
import { createApp, type App } from "../src/app.ts"; |
| 7 |
import { createToken } from "../src/core/tokens.ts"; |
| 8 |
import { createUser } from "../src/core/users.ts"; |
| 9 |
import { serve, type Servers } from "../src/http/server.ts"; |
| 10 |
|
| 11 |
let dir: string; |
| 12 |
let app: App; |
| 13 |
let servers: Servers; |
| 14 |
let adminTok: string; |
| 15 |
let guestTok: string; |
| 16 |
let n = 0; |
| 17 |
|
| 18 |
const call = async (token: string): Promise<any> => { |
| 19 |
const res = await fetch(`${servers.url}/mcp`, { |
| 20 |
method: "POST", |
| 21 |
headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, |
| 22 |
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "bugs", arguments: {} } }), |
| 23 |
}); |
| 24 |
const body: any = await res.json(); |
| 25 |
return JSON.parse(body.result.content[0].text); |
| 26 |
}; |
| 27 |
|
| 28 |
function bug(repo: string, hoursOld: number, high: boolean): void { |
| 29 |
n++; |
| 30 |
const created = new Date(Date.now() - hoursOld * 3_600_000).toISOString(); |
| 31 |
app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, author_id, state, created_at) VALUES (?1, ?2, ?3, 'issue', 't', (SELECT id FROM user LIMIT 1), 'open', ?4)", [`i${n}`, repo, n, created]); |
| 32 |
app.db.run("INSERT INTO issue_label (issue_id, label_id) VALUES (?1, ?2)", [`i${n}`, `${repo}-bug`]); |
| 33 |
if (high) app.db.run("INSERT INTO issue_label (issue_id, label_id) VALUES (?1, ?2)", [`i${n}`, `${repo}-high`]); |
| 34 |
} |
| 35 |
|
| 36 |
beforeAll(async () => { |
| 37 |
dir = await mkdtemp(join(tmpdir(), "forge-mcp-bugs-")); |
| 38 |
app = createApp({ dataDir: dir, port: 0 }); |
| 39 |
servers = await serve(app); |
| 40 |
const mk = async (username: string, first: boolean) => { |
| 41 |
const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first }); |
| 42 |
if (!u.ok) throw new Error(u.error); |
| 43 |
return u.value; |
| 44 |
}; |
| 45 |
const alice = await mk("alice", true); |
| 46 |
const carol = await mk("carol", false); |
| 47 |
adminTok = createToken(app.db, alice.id, "ro", "ro").token; |
| 48 |
guestTok = createToken(app.db, carol.id, "ro", "ro").token; |
| 49 |
for (const [id, vis] of [["pub", "public"], ["priv", "private"]] as const) { |
| 50 |
app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES (?1, ?2, ?1, ?3)", [id, alice.id, vis]); |
| 51 |
for (const l of ["bug", "high"]) app.db.run("INSERT INTO label (id, repo_id, name) VALUES (?1, ?2, ?3)", [`${id}-${l}`, id, l]); |
| 52 |
} |
| 53 |
bug("pub", 5, true); |
| 54 |
bug("pub", 1, false); |
| 55 |
bug("priv", 10, true); |
| 56 |
}, 30_000); |
| 57 |
|
| 58 |
afterAll(async () => { |
| 59 |
servers.stop(); |
| 60 |
app.close(); |
| 61 |
await rm(dir, { recursive: true, force: true }); |
| 62 |
}); |
| 63 |
|
| 64 |
test("an admin sees every bug, and the overdue high one is named", async () => { |
| 65 |
const r = await call(adminTok); |
| 66 |
expect(r).toMatchObject({ open: 3, overdue: 2 }); |
| 67 |
expect(r.bugs[0]).toMatchObject({ repo: "alice/priv", priority: "high", overdue: true }); |
| 68 |
expect(r.says).toContain("alice/priv#3"); |
| 69 |
}); |
| 70 |
|
| 71 |
test("a guest sees public repositories only", async () => { |
| 72 |
const r = await call(guestTok); |
| 73 |
expect(r).toMatchObject({ open: 2, overdue: 1 }); |
| 74 |
expect(r.bugs.map((b: any) => b.repo)).toEqual(["alice/pub", "alice/pub"]); |
| 75 |
expect(JSON.stringify(r)).not.toContain("priv"); |
| 76 |
expect(r.says).toContain("alice/pub#1"); |
| 77 |
}); |