Public: anyone can read this; the crew can change it.
A test pins a person's spend grant and role amount: a bad amount is refused, and agents and admins take no grant #7196
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/7103-a-test-pins-a-person-s-spend-grant-and-r → main 1 commit fork
1 file
changed +107
−0
test/grant-amounts.test.ts added +107
| @@ -0,0 +1,107 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { createAgent } from "../src/actions/crew.ts"; | |
| 7 | import { setAccountGrant } from "../src/actions/model_grant.ts"; | |
| 8 | import { setRole } from "../src/actions/roles.ts"; | |
| 9 | import { signUp } from "../src/actions/users.ts"; | |
| 10 | import { GRANT_MAX_USD, getModelGrant } from "../src/core/model_grant.ts"; | |
| 11 | import { createOrg } from "../src/core/orgs.ts"; | |
| 12 | import { getUserByName, type User } from "../src/core/users.ts"; | |
| 13 | import { fixtureSlot } from "./helpers/slot.ts"; | |
| 14 | import { proveAll } from "./helpers/verified.ts"; | |
| 15 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 16 | ||
| 17 | describe("a spend grant: what amount is kept, who takes one", () => { | |
| 18 | let dataDir: string; | |
| 19 | let app: App; | |
| 20 | let admin: User; | |
| 21 | let pat: User; | |
| 22 | const BAD = [-1, 0, 0.001, NaN, Infinity, -Infinity, GRANT_MAX_USD + 0.01]; | |
| 23 | ||
| 24 | beforeAll(async () => { | |
| 25 | dataDir = await mkdtemp(join(tmpdir(), "forge-grant-amounts-")); | |
| 26 | app = createApp({ dataDir, port: 0, url: "https://forge.example", edgeToken: "e" }); | |
| 27 | const a = await seedAdmin(app, { username: "olive", email: "[email protected]", password: "a-long-passphrase" }); | |
| 28 | if (!a.ok) throw new Error(a.error); | |
| 29 | admin = a.value; | |
| 30 | const b = await seedAdmin(app, { username: "other", email: "[email protected]", password: "a-long-passphrase" }); | |
| 31 | if (!b.ok) throw new Error(b.error); | |
| 32 | const s = await signUp( | |
| 33 | app, | |
| 34 | { username: "pat", email: "[email protected]", password: "a-long-passphrase" }, | |
| 35 | { slot: fixtureSlot(app) }, | |
| 36 | ); | |
| 37 | if (!s.ok) throw new Error(s.error); | |
| 38 | proveAll(app); | |
| 39 | pat = getUserByName(app.db, "pat")!; | |
| 40 | }); | |
| 41 | afterAll(async () => { | |
| 42 | app.close(); | |
| 43 | await rm(dataDir, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | const stored = (u: User) => getModelGrant(app.db, u.id)?.usdPerMonth; | |
| 47 | ||
| 48 | test("setAccountGrant keeps a valid amount, refuses a bad one, and null clears", () => { | |
| 49 | const set = setAccountGrant(app, admin, "pat", 12.345); | |
| 50 | expect(set.ok).toBe(true); | |
| 51 | expect(stored(pat)).toBe(12.35); | |
| 52 | for (const usd of BAD) { | |
| 53 | const r = setAccountGrant(app, admin, "pat", usd); | |
| 54 | expect(r).toEqual({ ok: false, error: "invalid_amount" }); | |
| 55 | expect(stored(pat)).toBe(12.35); | |
| 56 | } | |
| 57 | expect(setAccountGrant(app, admin, "pat", GRANT_MAX_USD).ok).toBe(true); | |
| 58 | expect(stored(pat)).toBe(GRANT_MAX_USD); | |
| 59 | expect(setAccountGrant(app, admin, "pat", 0.01).ok).toBe(true); | |
| 60 | expect(stored(pat)).toBe(0.01); | |
| 61 | expect(setAccountGrant(app, admin, "pat", null).ok).toBe(true); | |
| 62 | expect(stored(pat)).toBeUndefined(); | |
| 63 | }); | |
| 64 | ||
| 65 | test("setRole to member refuses the same amounts and leaves the person a guest with no grant", () => { | |
| 66 | for (const usd of BAD) { | |
| 67 | const r = setRole(app, admin, "pat", "member", usd); | |
| 68 | expect(r).toEqual({ ok: false, error: "invalid_amount" }); | |
| 69 | expect(stored(pat)).toBeUndefined(); | |
| 70 | } | |
| 71 | const ok = setRole(app, admin, "pat", "member", 7.5); | |
| 72 | expect(ok).toEqual({ ok: true, value: { username: "pat", role: "member", usdPerMonth: 7.5 } }); | |
| 73 | expect(stored(pat)).toBe(7.5); | |
| 74 | }); | |
| 75 | ||
| 76 | test("a refused role amount leaves an existing grant unchanged", () => { | |
| 77 | setRole(app, admin, "pat", "guest"); | |
| 78 | setAccountGrant(app, admin, "pat", 3); | |
| 79 | for (const usd of BAD) { | |
| 80 | expect(setRole(app, admin, "pat", "member", usd)).toEqual({ ok: false, error: "invalid_amount" }); | |
| 81 | expect(stored(pat)).toBe(3); | |
| 82 | } | |
| 83 | }); | |
| 84 | ||
| 85 | test("an agent or an admin takes no grant, and an org is not found", async () => { | |
| 86 | const bot = await createAgent(app, admin, { username: "bot" }); | |
| 87 | if (!bot.ok) throw new Error(String(bot.error)); | |
| 88 | const org = createOrg(app.db, "patco", pat.id); | |
| 89 | if (!org.ok) throw new Error(String(org.error)); | |
| 90 | for (const name of ["bot", "other", "olive"]) { | |
| 91 | const user = getUserByName(app.db, name)!; | |
| 92 | expect(setAccountGrant(app, admin, name, 5)).toEqual({ ok: false, error: "not_allowed" }); | |
| 93 | expect(setRole(app, admin, name, "member", 5)).toEqual({ ok: false, error: "not_allowed" }); | |
| 94 | expect(stored(user)).toBeUndefined(); | |
| 95 | } | |
| 96 | expect(setAccountGrant(app, admin, "patco", 5)).toEqual({ ok: false, error: "not_found" }); | |
| 97 | expect(setRole(app, admin, "patco", "member", 5)).toEqual({ ok: false, error: "not_found" }); | |
| 98 | expect(stored(getUserByName(app.db, "patco")!)).toBeUndefined(); | |
| 99 | expect(setAccountGrant(app, admin, "nobody", 5)).toEqual({ ok: false, error: "not_found" }); | |
| 100 | }); | |
| 101 | ||
| 102 | test("only an admin sets a grant", () => { | |
| 103 | expect(setAccountGrant(app, pat, "pat", 5)).toEqual({ ok: false, error: "not_found" }); | |
| 104 | expect(setAccountGrant(app, undefined, "pat", 5)).toEqual({ ok: false, error: "not_found" }); | |
| 105 | expect(stored(pat)).toBe(3); | |
| 106 | }); | |
| 107 | }); |
new above
offline