Public: anyone can read this; the crew can change it.
A repository's page says this month's spend in one quiet line, always, linked to its wallet #14582
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14556-a-repository-s-page-says-this-month-s-sp → main 6 commits fork
5 files
changed +110
−13
src/core/wallet.ts +18 −4
| @@ -32,6 +32,20 @@ export function setMonthCap(db: Database, repoId: string, usd: number | undefine | ||
| 32 | 32 | return true; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | /** | |
| 36 | * The cap and this month's spend in one statement, for a page that wants both (#14556). The cap is | |
| 37 | * undefined when none is set. | |
| 38 | */ | |
| 39 | export function walletStanding(db: Database, repoId: string, now: Date): { cap: number | undefined; spent: number } { | |
| 40 | const row = db | |
| 41 | .query<{ cap: number | null; spent: number }, [string, string]>( | |
| 42 | `SELECT (SELECT month_cap_usd FROM repo_wallet WHERE repo_id = ?1) AS cap, | |
| 43 | (SELECT COALESCE(SUM(cost_usd), 0) FROM usage WHERE repo_id = ?1 AND at >= ?2) AS spent`, | |
| 44 | ) | |
| 45 | .get(repoId, monthStart(now)); | |
| 46 | return { cap: row?.cap ?? undefined, spent: row?.spent ?? 0 }; | |
| 47 | } | |
| 48 | ||
| 35 | 49 | /** Dollars the repository's runs have cost this UTC month, from the ledger. */ |
| 36 | 50 | export function monthSpent(db: Database, repoId: string, now: Date): number { |
| 37 | 51 | return ( |
| @@ -54,9 +68,9 @@ export function dollars(usd: number): string { | ||
| 54 | 68 | * "Wallet: $X left of $Y" while the balance is above nothing and under WALLET_LOW_SHARE of the |
| 55 | 69 | * cap; undefined otherwise (no cap, plenty left, or already reached, which has its own sentence). |
| 56 | 70 | */ |
| 57 | export function lowWallet(db: Database, repoId: string, now: Date, cap: number | undefined = monthCap(db, repoId)): string | undefined { | |
| 71 | export function lowWallet(db: Database, repoId: string, now: Date, cap: number | undefined = monthCap(db, repoId), spentAlready?: number): string | undefined { | |
| 58 | 72 | if (cap === undefined) return undefined; |
| 59 | const left = cap - monthSpent(db, repoId, now); | |
| 73 | const left = cap - (spentAlready ?? monthSpent(db, repoId, now)); | |
| 60 | 74 | if (left <= 0 || left >= cap * WALLET_LOW_SHARE) return undefined; |
| 61 | 75 | return `Wallet: ${dollars(left)} left of ${dollars(cap)}`; |
| 62 | 76 | } |
| @@ -68,9 +82,9 @@ export const resetDate = (now: Date): string => new Date(Date.UTC(now.getUTCFull | ||
| 68 | 82 | * The one sentence a refused run, and the repository page, say; undefined while spend is under the |
| 69 | 83 | * cap or there is none. A caller that has read the cap passes it, so it is not read twice. |
| 70 | 84 | */ |
| 71 | export function capReached(db: Database, repoId: string, now: Date, cap: number | undefined = monthCap(db, repoId)): string | undefined { | |
| 85 | export function capReached(db: Database, repoId: string, now: Date, cap: number | undefined = monthCap(db, repoId), spentAlready?: number): string | undefined { | |
| 72 | 86 | if (cap === undefined) return undefined; |
| 73 | const spent = monthSpent(db, repoId, now); | |
| 87 | const spent = spentAlready ?? monthSpent(db, repoId, now); | |
| 74 | 88 | return spent < cap ? undefined : balanceWords(cap, spent, now); |
| 75 | 89 | } |
| 76 | 90 |
src/web/pages/repo.ts +10 −7
| @@ -29,7 +29,7 @@ import { storageLimitMessage } from "../../core/storage.ts"; | ||
| 29 | 29 | import { canRead, canWrite, listForks, type Repo } from "../../core/repos.ts"; |
| 30 | 30 | import { reportsClosed } from "../../core/reports.ts"; |
| 31 | 31 | import { publicRepoLd } from "../structured_data.ts"; |
| 32 | import { capReached, lowWallet, monthCap } from "../../core/wallet.ts"; | |
| 32 | import { capReached, lowWallet, money, walletStanding } from "../../core/wallet.ts"; | |
| 33 | 33 | import { takesOutside } from "../../actions/contribute_token.ts"; |
| 34 | 34 | import { FORKS_PER_DAY, forkRepo, repositoryLimitMessage } from "../../actions/repos.ts"; |
| 35 | 35 | import { |
| @@ -1449,15 +1449,18 @@ function movedLine(ctx: Ctx, repo: Repo): Html | null { | ||
| 1449 | 1449 | } |
| 1450 | 1450 | |
| 1451 | 1451 | /** |
| 1452 | * One plain line, for an admin, when the repository's monthly cap is reached (#13309); no banner. | |
| 1452 | * One plain line for the owner and admins (#13309, #14556): the cap or low-balance sentence when | |
| 1453 | * one applies, else "$N.NN this month" in muted text. Nothing for anyone else. The month total is | |
| 1454 | * read once, one indexed query; no banner. | |
| 1453 | 1455 | */ |
| 1454 | 1456 | function walletLine(ctx: Ctx, repo: Repo): Html | null { |
| 1455 | // The cheap case first: a repository with no cap row costs one primary-key read, no access check. | |
| 1456 | const cap = monthCap(ctx.app.db, repo.id); | |
| 1457 | if (cap === undefined || !canAdmin(ctx.app.db, repo, ctx.auth.user)) return null; | |
| 1457 | if (!canAdmin(ctx.app.db, repo, ctx.auth.user)) return null; | |
| 1458 | 1458 | const now = new Date(); |
| 1459 | const said = capReached(ctx.app.db, repo.id, now, cap) ?? lowWallet(ctx.app.db, repo.id, now, cap); | |
| 1460 | if (said === undefined) return null; | |
| 1459 | const { cap, spent } = walletStanding(ctx.app.db, repo.id, now); | |
| 1460 | // No cap: skip both, whose default would read the cap row again when passed undefined. | |
| 1461 | const said = | |
| 1462 | (cap === undefined ? undefined : (capReached(ctx.app.db, repo.id, now, cap, spent) ?? lowWallet(ctx.app.db, repo.id, now, cap, spent))) ?? | |
| 1463 | `${money(spent)} this month`; | |
| 1461 | 1464 | return html`<p class="muted small mt" id="wallet-line">${said} <a href="${repoBase(repo)}/wallet">Wallet</a></p>`; |
| 1462 | 1465 | } |
| 1463 | 1466 |
test/repo-wallet-line.test.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | /** | |
| 2 | * The repository page's one muted spend line (#14556): plain, beaten by the cap message, hidden | |
| 3 | * from a visitor. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createUser } from "../src/core/users.ts"; | |
| 12 | import { createRun } from "../src/core/runs.ts"; | |
| 13 | import { setMonthCap } from "../src/core/wallet.ts"; | |
| 14 | import { serve } from "../src/http/server.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | let dir = ""; | |
| 20 | let app: ReturnType<typeof createApp>; | |
| 21 | let servers: Awaited<ReturnType<typeof serve>>; | |
| 22 | let repoId = ""; | |
| 23 | let ownerId = ""; | |
| 24 | let cookie = ""; | |
| 25 | ||
| 26 | beforeAll(async () => { | |
| 27 | dir = await mkdtemp(join(tmpdir(), "forge-wallet-month-")); | |
| 28 | app = createApp({ dataDir: dir, port: 0 }); | |
| 29 | servers = await serve(app); | |
| 30 | const made: any = await createUser(app.db, { | |
| 31 | origin: "seed", trusted: true, username: "trevato", email: "[email protected]", | |
| 32 | password: "a-good-passphrase", adminIfFirst: true, | |
| 33 | }); | |
| 34 | const user = made.value ?? made; | |
| 35 | ownerId = user.id; | |
| 36 | const res = await fetch(servers.url + "/signin", { | |
| 37 | method: "POST", redirect: "manual", | |
| 38 | headers: { "content-type": "application/x-www-form-urlencoded", origin: servers.url }, | |
| 39 | body: new URLSearchParams({ username: "trevato", password: "a-good-passphrase" }), | |
| 40 | }); | |
| 41 | cookie = (res.headers.get("set-cookie") ?? "").split(";")[0] ?? ""; | |
| 42 | const r: any = await createRepo(app, user, { name: "garden", visibility: "public" }); | |
| 43 | repoId = (r.value ?? r).id; | |
| 44 | }); | |
| 45 | afterAll(async () => { | |
| 46 | servers.stop(); | |
| 47 | app.close(); | |
| 48 | await rm(dir, { recursive: true, force: true }); | |
| 49 | }); | |
| 50 | ||
| 51 | const page = async (as: string) => | |
| 52 | (await fetch(servers.url + "/trevato/garden", { redirect: "manual", headers: as ? { cookie: as } : {} })).text(); | |
| 53 | const spend = (usd: number) => { | |
| 54 | const run = createRun(app.db, { repoId, kind: "agent" }); | |
| 55 | app.db.query(`INSERT INTO usage (run_id, repo_id, kind, cost_usd, outcome, at) VALUES (?1, ?2, 'build', ?3, 'ok', ?4)`).run(run.id, repoId, usd, new Date().toISOString()); | |
| 56 | }; | |
| 57 | ||
| 58 | test("no spend: the owner reads $0.00 this month, linked to the wallet; a visitor reads nothing", async () => { | |
| 59 | const owner = await page(cookie); | |
| 60 | expect(owner).toContain("$0.00 this month"); | |
| 61 | expect(owner).toContain('href="/trevato/garden/wallet"'); | |
| 62 | const visitor = await page(""); | |
| 63 | expect(visitor).not.toContain("this month"); | |
| 64 | expect(visitor).not.toContain("wallet-line"); | |
| 65 | }); | |
| 66 | ||
| 67 | test("spend with no cap reads the total to the cent", async () => { | |
| 68 | spend(1.25); | |
| 69 | spend(2); | |
| 70 | expect(await page(cookie)).toContain("$3.25 this month"); | |
| 71 | }); | |
| 72 | ||
| 73 | test("the cap message wins when the cap is reached", async () => { | |
| 74 | setMonthCap(app.db, repoId, 3, ownerId, new Date()); | |
| 75 | const owner = await page(cookie); | |
| 76 | expect(owner).toContain("monthly cap of $3 is reached"); | |
| 77 | expect(owner).not.toContain("$3.25 this month"); | |
| 78 | expect(owner.match(/id="wallet-line"/g)?.length).toBe(1); | |
| 79 | }); |
test/repo-wallet-routes.test.ts +2 −1
| @@ -107,7 +107,8 @@ test("a valid POST flashes, writes the cap and a wallet.cap event; a bad amount | ||
| 107 | 107 | |
| 108 | 108 | test("the repository page shows the line to an admin once the cap is reached, to no one else", async () => { |
| 109 | 109 | const quiet = await get("/trevato/garden", "trevato"); |
| 110 | expect(quiet.body).not.toContain("wallet-line"); | |
| 110 | expect(quiet.body).toContain("$0.00 this month"); | |
| 111 | expect(quiet.body).not.toContain("monthly cap"); | |
| 111 | 112 | spend(7); |
| 112 | 113 | const admin = await get("/trevato/garden", "trevato"); |
| 113 | 114 | expect(admin.body).toContain('id="wallet-line"'); |
test/wallet-low-line.test.ts +1 −1
| @@ -58,7 +58,7 @@ test("20% or more left says nothing; under 20% says it once, in the page and in | ||
| 58 | 58 | setMonthCap(app.db, repoId, 10, ownerId, new Date()); |
| 59 | 59 | spend(8); |
| 60 | 60 | expect(lowWallet(app.db, repoId, new Date())).toBeUndefined(); |
| 61 | expect(await get("/trevato/garden")).not.toContain("wallet-line"); | |
| 61 | expect(await get("/trevato/garden")).not.toContain("left of"); | |
| 62 | 62 | expect(await get("/trevato/garden/next")).not.toContain("wallet-row"); |
| 63 | 63 | |
| 64 | 64 | spend(0.5); |
new above
offline