|
|
@@ -0,0 +1,87 @@ |
| 1 |
|
| 2 |
import { afterAll, beforeAll, describe, 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 |
import { promote } from "./helpers/members.ts"; |
| 11 |
|
| 12 |
describe("MCP bugs", () => { |
| 13 |
let dir: string; |
| 14 |
let app: App; |
| 15 |
let servers: Servers; |
| 16 |
let adminTok: string; |
| 17 |
let guestTok: string; |
| 18 |
let id = 1; |
| 19 |
let n = 0; |
| 20 |
|
| 21 |
const call = async (token: string) => { |
| 22 |
const res = await fetch(`${servers.url}/mcp`, { |
| 23 |
method: "POST", |
| 24 |
headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, |
| 25 |
body: JSON.stringify({ jsonrpc: "2.0", id: id++, method: "tools/call", params: { name: "bugs", arguments: {} } }), |
| 26 |
}); |
| 27 |
const body: any = await res.json(); |
| 28 |
return JSON.parse(body.result.content[0].text); |
| 29 |
}; |
| 30 |
|
| 31 |
const bug = (repo: string, hoursOld: number, high: boolean) => { |
| 32 |
n++; |
| 33 |
const iid = `i${n}`; |
| 34 |
const at = new Date(Date.now() - hoursOld * 3_600_000).toISOString(); |
| 35 |
app.db.run( |
| 36 |
"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)", |
| 37 |
[iid, repo, n, at], |
| 38 |
); |
| 39 |
for (const l of high ? ["bug", "high"] : ["bug"]) app.db.run("INSERT INTO issue_label (issue_id, label_id) VALUES (?1, ?2)", [iid, `${repo}-${l}`]); |
| 40 |
}; |
| 41 |
|
| 42 |
beforeAll(async () => { |
| 43 |
dir = await mkdtemp(join(tmpdir(), "forge-mcp-bugs-")); |
| 44 |
app = createApp({ dataDir: dir, port: 0 }); |
| 45 |
servers = await serve(app); |
| 46 |
const mk = async (username: string, first = false) => { |
| 47 |
const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first }); |
| 48 |
if (!u.ok) throw new Error(u.error); |
| 49 |
promote(app.db, u.value); |
| 50 |
return u.value; |
| 51 |
}; |
| 52 |
const alice = await mk("alice", true); |
| 53 |
const bob = await mk("bob"); |
| 54 |
adminTok = createToken(app.db, alice.id, "ro", "ro").token; |
| 55 |
guestTok = createToken(app.db, bob.id, "ro", "ro").token; |
| 56 |
for (const [name, vis] of [["open", "public"], ["secret", "private"]] as const) { |
| 57 |
app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES (?1, ?2, ?1, ?3)", [name, alice.id, vis]); |
| 58 |
for (const l of ["bug", "high"]) app.db.run("INSERT INTO label (id, repo_id, name) VALUES (?1, ?2, ?3)", [`${name}-${l}`, name, l]); |
| 59 |
} |
| 60 |
bug("open", 5, true); |
| 61 |
bug("open", 1, false); |
| 62 |
bug("secret", 60, false); |
| 63 |
}, 30_000); |
| 64 |
|
| 65 |
afterAll(async () => { |
| 66 |
servers.stop(); |
| 67 |
app.close(); |
| 68 |
await rm(dir, { recursive: true, force: true }); |
| 69 |
}); |
| 70 |
|
| 71 |
test("an admin sees every repository, and an overdue high bug is named", async () => { |
| 72 |
const got = await call(adminTok); |
| 73 |
expect(got.open).toBe(3); |
| 74 |
expect(got.overdue).toBe(2); |
| 75 |
const high = got.bugs.find((b: any) => b.repo === "alice/open" && b.high); |
| 76 |
expect(high).toMatchObject({ number: 1, overdue: true }); |
| 77 |
expect(Date.parse(high.dueAt) - Date.parse(high.createdAt)).toBe(4 * 3_600_000); |
| 78 |
expect(got.bugs[0].repo).toBe("alice/secret"); |
| 79 |
}); |
| 80 |
|
| 81 |
test("a guest does not see a private repository's bug", async () => { |
| 82 |
const got = await call(guestTok); |
| 83 |
expect(got.open).toBe(2); |
| 84 |
expect(got.overdue).toBe(1); |
| 85 |
expect(JSON.stringify(got)).not.toContain("secret"); |
| 86 |
}); |
| 87 |
}); |