Public: anyone can read this; the crew can change it.
Each bet's row says what it bought: the runs and pulls its spend paid for #13957
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13940-each-bet-s-row-says-what-it-bought-the-r → main 6 commits fork 3 threads · 3 unresolved
docs/design/areas/bets.md +2
| @@ -37,3 +37,5 @@ class. `test/bets-card-fit.test.ts` fails if the bound is removed. | ||
| 37 | 37 | ## Rows, not boxes (#9859) |
| 38 | 38 | |
| 39 | 39 | The list on `/:owner/:name/bets` is one `row()` per bet in a `ul.list.setting-list[data-bets]`, no `card()`. A proposed bet's row carries its share and next action in the meta; its journeys (three, the rest in a "N more" `<details>`), issue count and actions sit in the row's `after` slot. `data-bet-card` is now on the row's `<li>` (the bounds above still scope to it). The one CSS change: `[data-bets]` joins `[data-origins]` in giving `.row-after` the full row width. A finished bet's "This is done. Try it." is a `settings-block` heading and a line. No new class. |
| 40 | ||
| 41 | - `.row-links`: the value of a row fact (`dl.row-facts`) that is links, as a bet's Runs and Pulls; each link is a 44px-tall target and each names what it opens ("Run 2 ($0.25)"). |
docs/features/bets.md +1
| @@ -8,6 +8,7 @@ A bet is a name, an appetite (a share of the week), what done looks like in a fe | ||
| 8 | 8 | - **Back.** The owner (a person who administers the repo, never an agent) backs one with a share. At most 5 run, and their shares stay within 100%. |
| 9 | 9 | - **The wallet.** Backing is refused, with one sentence, "Estimate $X, balance $Y", when the bet's estimate is above what is free of the owner's model grant; an estimate equal to it starts. A form shows the sentence as a flash on the bet; a JSON client gets 422. The estimate is the bet's budget (below) at the dollars a builder second has cost this repository's runs over 30 days, and the wallet is the grant's remainder less what the bets already running have yet to spend. With no grant or no history nothing is refused. The planner's own starts skip a bet the wallet cannot cover. The sentence ends with the way out: lower the share, or ask a site admin to raise the grant (Settings, Accounts). The web page, MCP `bet` and terminal show the same sentence. |
| 10 | 10 | - **Before you press.** A proposed bet's card (and its row in the terminal) prints `Estimate $X of $Y left`, the estimate beside the wallet the refusal would judge by. When the estimate passes the wallet the line adds `short by $Z`, the *Back it* button is disabled, and the reason sits beside it. With no grant or no history there is no line. The line is on proposed cards only: a stopped bet re-backed from its page shows no estimate first, though the refusal still judges it. |
| 11 | - **What it bought.** Each bet's row on the page, and in the terminal, says what its spend paid for: `Spent $X on N runs` (on the page the newest five are linked, with `and M more`) and the pulls those runs built or that are scopes themselves, or `No spend yet.` The dollars come from the usage ledger rows whose issue is one of the bet's scopes. Only an admin sees it, with the rest of the plan. | |
| 11 | 12 | - **Picks.** Once a repository has ever had a bet backed, builders pick only scopes of a running bet, issues the owner asked for or raised, and small fixes, polish included (polish held to its share of the week, see polish-share.md). An issue in no bet does not start by itself. A repository with no bet picks as before. |
| 12 | 13 | - **Appetite.** The budget is share × builder slots × one week of builder time, counted from the runs on the bet's scopes since it was backed. At the budget the bet stops: no extension. Its left line defaults to the open scopes; the lead rewrites it in one line; the owner re-backs, cuts or ends it (ending as done is for a stopped bet only; a running bet finishes when every scope closes). |
| 13 | 14 | - **Scopes** read *figuring out* (no run yet), *executing*, or *raised hand* (nothing moved for two days), or *done*. |
src/core/bets.ts +46
| @@ -209,3 +209,49 @@ export function costRate(db: Database, repoId: string, now: Date): number | unde | ||
| 209 | 209 | .get(repoId, since); |
| 210 | 210 | return row !== null && row.ms > 0 && row.cost > 0 ? row.cost / (row.ms / 1000) : undefined; |
| 211 | 211 | } |
| 212 | ||
| 213 | export interface BetReceipt { | |
| 214 | /** Dollars the bet's scopes cost, from the usage ledger. */ | |
| 215 | readonly usd: number; | |
| 216 | /** The runs that spent it, newest first. */ | |
| 217 | readonly runs: readonly { readonly runId: string; readonly usd: number }[]; | |
| 218 | /** The pulls those runs built, or that are scopes themselves, newest first. */ | |
| 219 | readonly pulls: readonly { readonly number: number; readonly title: string }[]; | |
| 220 | } | |
| 221 | ||
| 222 | /** | |
| 223 | * What bets bought: the usage rows whose issue is one of a bet's scopes (the ledger carries the | |
| 224 | * run's issue and `bet_issue` says which bet holds it, so no row needs a bet id of its own), the | |
| 225 | * runs they belong to and the pulls those runs built. Two queries for any number of bets, so a | |
| 226 | * list costs what one row costs; every id asked for has an entry. | |
| 227 | */ | |
| 228 | export function betReceipts(db: Database, betIds: readonly string[]): Map<string, BetReceipt> { | |
| 229 | const out = new Map<string, { usd: number; runs: { runId: string; usd: number }[]; pulls: { number: number; title: string }[] }>(); | |
| 230 | for (const id of betIds) out.set(id, { usd: 0, runs: [], pulls: [] }); | |
| 231 | if (betIds.length === 0) return out; | |
| 232 | const marks = betIds.map((_, i) => `?${i + 1}`).join(","); | |
| 233 | const rows = db | |
| 234 | .query<{ bet_id: string; run_id: string; cost_usd: number }, string[]>( | |
| 235 | `SELECT bi.bet_id, u.run_id, u.cost_usd FROM usage u JOIN bet_issue bi ON bi.issue_id = u.issue_id | |
| 236 | WHERE bi.bet_id IN (${marks}) ORDER BY u.at DESC, u.run_id DESC`, | |
| 237 | ) | |
| 238 | .all(...betIds); | |
| 239 | for (const r of rows) { | |
| 240 | const e = out.get(r.bet_id)!; | |
| 241 | e.usd += r.cost_usd; | |
| 242 | e.runs.push({ runId: r.run_id, usd: r.cost_usd }); | |
| 243 | } | |
| 244 | const pulls = db | |
| 245 | .query<{ bet_id: string; number: number; title: string }, string[]>( | |
| 246 | `SELECT DISTINCT bi.bet_id, p.number, p.title FROM bet_issue bi, issue p JOIN pull pl ON pl.issue_id = p.id | |
| 247 | WHERE bi.bet_id IN (${marks}) | |
| 248 | AND (p.id = bi.issue_id | |
| 249 | OR EXISTS (SELECT 1 FROM usage u JOIN run r ON r.id = u.run_id | |
| 250 | WHERE u.issue_id IN (SELECT issue_id FROM bet_issue WHERE bet_id = bi.bet_id) | |
| 251 | AND r.repo_id = p.repo_id AND r.branch = pl.head_branch)) | |
| 252 | ORDER BY p.number DESC`, | |
| 253 | ) | |
| 254 | .all(...betIds); | |
| 255 | for (const p of pulls) out.get(p.bet_id)!.pulls.push({ number: p.number, title: p.title }); | |
| 256 | return out; | |
| 257 | } |
src/tui/screens/bets.ts +22 −3
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | import { backBet, betEstimate, capBet, endBet, giveVerdict, proposeBets, stopBet, writeLeft, type BetError } from "../../actions/bets.ts"; |
| 12 | 12 | import { estimateLine } from "../../crew/bets.ts"; |
| 13 | 13 | import { money } from "../../crew/bet_cap.ts"; |
| 14 | import { type Bet, listBets } from "../../core/bets.ts"; | |
| 14 | import { type Bet, betReceipts, type BetReceipt, listBets } from "../../core/bets.ts"; | |
| 15 | 15 | import { canAdmin, getRepoById, type Repo } from "../../core/repos.ts"; |
| 16 | 16 | import type { Result } from "../../kernel/result.ts"; |
| 17 | 17 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -41,6 +41,17 @@ function administered(f: Session): Repo[] { | ||
| 41 | 41 | }); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | const SHOWN_PULLS = 3; | |
| 45 | ||
| 46 | /** What a bet bought, for the end of its row: the page's Spent and Pulls facts in one phrase. */ | |
| 47 | export function receiptLine(r: BetReceipt | undefined): string { | |
| 48 | if (r === undefined || r.runs.length === 0) return " · no spend yet"; | |
| 49 | const shown = r.pulls.slice(0, SHOWN_PULLS).map((p) => `#${p.number}`).join(", "); | |
| 50 | const more = r.pulls.length - SHOWN_PULLS; | |
| 51 | const pulls = r.pulls.length === 0 ? "no pulls yet" : `pulls ${shown}${more > 0 ? ` and ${more} more` : ""}`; | |
| 52 | return ` · spent $${r.usd.toFixed(2)} on ${r.runs.length} ${r.runs.length === 1 ? "run" : "runs"}, ${pulls}`; | |
| 53 | } | |
| 54 | ||
| 44 | 55 | export function betsScreen(f: Session, ctx: Ctx): Screen { |
| 45 | 56 | const state = new ListState(); |
| 46 | 57 | let note = ""; |
| @@ -54,8 +65,11 @@ export function betsScreen(f: Session, ctx: Ctx): Screen { | ||
| 54 | 65 | const e = betEstimate(f.app, repo, bet.appetite, running, new Date()); |
| 55 | 66 | return e === null ? "" : estimateLine(e); |
| 56 | 67 | }; |
| 68 | // Computed once per load and after a change, not per draw. | |
| 69 | let receipts: Map<string, BetReceipt> | undefined; | |
| 57 | 70 | const say = (text: string): void => { |
| 58 | 71 | note = text; |
| 72 | receipts = undefined; | |
| 59 | 73 | ctx.invalidate(); |
| 60 | 74 | }; |
| 61 | 75 | const settle = (r: Result<Bet, { error: BetError; message: string }>, what: string): string | undefined => { |
| @@ -70,7 +84,10 @@ export function betsScreen(f: Session, ctx: Ctx): Screen { | ||
| 70 | 84 | binding(["c"], "cut"), binding(["e"], "end"), binding(["l"], "what is left"), binding(["d"], "feels done"), binding(["n"], "not yet"), |
| 71 | 85 | ], |
| 72 | 86 | async load(signal) { |
| 73 | live(f, signal, { where: (e) => e.kind.startsWith("bet.") }, () => ctx.invalidate()); | |
| 87 | live(f, signal, { where: (e) => e.kind.startsWith("bet.") }, () => { | |
| 88 | receipts = undefined; | |
| 89 | ctx.invalidate(); | |
| 90 | }); | |
| 74 | 91 | }, |
| 75 | 92 | render(area, s) { |
| 76 | 93 | const c = ctx.theme.c; |
| @@ -86,10 +103,12 @@ export function betsScreen(f: Session, ctx: Ctx): Screen { | ||
| 86 | 103 | ? sel.bet.leftLine || sel.bet.journeys.split("\n")[0] || sel.bet.name |
| 87 | 104 | : "No bets. Press p to have the planner propose some."; |
| 88 | 105 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 106 | receipts ??= betReceipts(f.app.db, all.map((l) => l.bet.id)); | |
| 107 | const bought = (bet: Bet): string => receiptLine(receipts?.get(bet.id)); | |
| 89 | 108 | const rows: ListRow[] = all.map(({ repo, bet }) => ({ |
| 90 | 109 | title: bet.name, |
| 91 | 110 | ...(bet.state === "stopped" ? { tone: "danger" as const } : {}), |
| 92 | secondary: `${repo.name} · ${bet.state} · ${bet.appetite}% of the week${bet.capUsd === null ? "" : ` · cap ${money(bet.capUsd)}/day`}${bet.backedBy === "planner" ? " · not backed by the owner" : ""}`, | |
| 111 | secondary: `${repo.name} · ${bet.state} · ${bet.appetite}% of the week${bet.capUsd === null ? "" : ` · cap ${money(bet.capUsd)}/day`}${bet.backedBy === "planner" ? " · not backed by the owner" : ""}${bought(bet)}`, | |
| 93 | 112 | meta: bet.state === "done" && bet.verdict === "" ? "try it, then d or n" : bet.state === "proposed" ? estimateOf(repo, bet) : "", |
| 94 | 113 | })); |
| 95 | 114 | drawList(s, list!, c, state, rows); |
src/web/pages/bets.ts +24 −3
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | import { |
| 11 | 11 | backBet, betEstimate, capBet, endBet, giveVerdict, oneLine, proposeBets, stopBet, writeLeft, |
| 12 | 12 | } from "../../actions/bets.ts"; |
| 13 | import { type Bet, betRuns, betScopes, betSpentSince, getBet, listBets } from "../../core/bets.ts"; | |
| 13 | import { type Bet, betReceipts, type BetReceipt, betRuns, betScopes, betSpentSince, getBet, listBets } from "../../core/bets.ts"; | |
| 14 | 14 | import { getUserById } from "../../core/users.ts"; |
| 15 | 15 | import { canAdmin, type Repo } from "../../core/repos.ts"; |
| 16 | 16 | import { capStanding, dayStart, money } from "../../crew/bet_cap.ts"; |
| @@ -46,7 +46,26 @@ function useWords(ctx: Ctx, repo: Repo, b: Bet, now: Date): string { | ||
| 46 | 46 | return `${days(spent)} of ${days(budgetSeconds(b.appetite, repo.autonomySlots))} builder time`; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | function betRow(ctx: Ctx, repo: Repo, b: Bet, now: Date, owner: boolean): Html { | |
| 49 | const SHOWN_RUNS = 5; | |
| 50 | const dollars = (usd: number): string => `$${usd.toFixed(2)}`; | |
| 51 | ||
| 52 | /** | |
| 53 | * What the bet's spend bought, as facts under its row (Spent, Pulls): each run and pull a link | |
| 54 | * that names what it opens. It sits beside the row's link, not in it, so the links do not nest. | |
| 55 | */ | |
| 56 | function receipt(r: BetReceipt, repo: Repo): Html { | |
| 57 | if (r.runs.length === 0) return html`<dl class="row-facts" data-bet-receipt><div><dt>Spent</dt><dd>Nothing yet</dd></div></dl>`; | |
| 58 | const more = r.runs.length - SHOWN_RUNS; | |
outdated
Principle 7 (rows over boxes: a fact is a dl row) and 2 (no notes): this hand-rolled |
||
| 59 | const runs = r.runs.slice(0, SHOWN_RUNS).map((x, i) => html`<a href="${repoBase(repo)}/runs/${x.runId}">Run ${r.runs.length - i} (${dollars(x.usd)})</a>`); | |
| 60 | const pulls = r.pulls.map((x) => html`<a href="${repoBase(repo)}/pulls/${x.number}">Pull #${x.number}</a>`); | |
| 61 | return html`<dl class="row-facts" data-bet-receipt> | |
| 62 | <div><dt>Spent</dt><dd>${dollars(r.usd)} on ${r.runs.length} ${r.runs.length === 1 ? "run" : "runs"}</dd></div> | |
| 63 | <div><dt>Runs</dt><dd class="row-links">${runs}${more > 0 ? ` and ${more} more` : ""}</dd></div> | |
| 64 | <div><dt>Pulls</dt><dd class="row-links">${pulls.length === 0 ? "None yet" : pulls}</dd></div> | |
| 65 | </dl>`; | |
| 66 | } | |
| 67 | ||
| 68 | function betRow(ctx: Ctx, repo: Repo, b: Bet, now: Date, owner: boolean, receipts: ReadonlyMap<string, BetReceipt>): Html { | |
| 50 | 69 | // The plan — shares, what is left — is for those who administer; a reader sees names and states. |
| 51 | 70 | if (!owner) return row({ href: betHref(repo, b.id), |
| 52 | 71 | nav: true, title: html`${b.name}`, trail: badge(STATE_WORDS[b.state]) }); |
| @@ -58,6 +77,7 @@ function betRow(ctx: Ctx, repo: Repo, b: Bet, now: Date, owner: boolean): Html { | ||
| 58 | 77 | nav: true, |
| 59 | 78 | title: html`${b.name}`, |
| 60 | 79 | meta: html`${meta}`, |
| 80 | after: receipt(receipts.get(b.id)!, repo), | |
| 61 | 81 | trail: badge(`${STATE_WORDS[b.state]} · ${b.appetite}% of the week`, { tone: b.state === "backed" ? "success" : b.state === "stopped" ? "attn" : "default" }), |
| 62 | 82 | }); |
| 63 | 83 | } |
| @@ -154,11 +174,12 @@ export function betsPage(ctx: Ctx): Response { | ||
| 154 | 174 | const running = backed.length; |
| 155 | 175 | const proposed = owner ? bets.filter((b) => b.state === "proposed") : []; |
| 156 | 176 | const rest = owner ? bets.filter((b) => b.state !== "proposed") : bets; |
| 177 | const receipts = owner ? betReceipts(ctx.app.db, rest.map((b) => b.id)) : new Map<string, BetReceipt>(); | |
| 157 | 178 | const body = html`${repoHeader(ctx, repo, "issues")} |
| 158 | 179 | ${pageHead("Bets", { sub: `A few named wholes, each with a share of the week. ${running} of ${MAX_BACKED} running. Builders pick only from running bets, the owner's own asks and small fixes.` })} |
| 159 | 180 | ${bets.length === 0 |
| 160 | 181 | ? html`<p class="muted">No bets yet. The planner proposes them from the open work.</p>` |
| 161 | : html`${rows([...(owner ? proposed.map((b) => proposedRow(ctx, repo, b, backed, now)) : []), ...rest.map((b) => betRow(ctx, repo, b, now, owner))])}`} | |
| 182 | : html`${rows([...(owner ? proposed.map((b) => proposedRow(ctx, repo, b, backed, now)) : []), ...rest.map((b) => betRow(ctx, repo, b, now, owner, receipts))])}`} | |
| 162 | 183 | ${owner |
| 163 | 184 | ? html`<form method="post" action="${repoBase(repo)}/bets/propose" class="mt">${btn("Ask the planner to propose bets", { variant: "secondary" })}</form>` |
| 164 | 185 | : null} |
src/web/style/components.ts +3
| @@ -558,6 +558,9 @@ a.row:hover .row-title { color: var(--accent); } | ||
| 558 | 558 | .row-facts > div { display: flex; gap: 6px; min-width: 0; } |
| 559 | 559 | .row-facts dt { font-weight: 550; } |
| 560 | 560 | .row-facts dd { margin: 0; min-width: 0; overflow-wrap: anywhere; } |
| 561 | /* A fact whose value is links (a bet's runs and pulls): each a thumb-sized target. */ | |
| 562 | .row-facts .row-links { display: flex; flex-wrap: wrap; gap: 0 12px; } | |
| 563 | .row-facts .row-links a { display: inline-flex; align-items: center; min-height: 44px; } | |
| 561 | 564 | .row-meta .avatar { width: 16px; height: 16px; font-size: var(--fs-min); vertical-align: -0.28em; } |
| 562 | 565 | .row-meta .byline-name { margin-left: 4px; } |
| 563 | 566 | .row-lead { color: var(--faint); display: inline-flex; flex: none; } |
test/bets-receipt.test.ts added +111
| @@ -0,0 +1,111 @@ | ||
| 1 | /** | |
| 2 | * A bet's row says what it bought (#13940): the runs and pulls whose usage | |
| 3 | * ledger rows belong to its scopes, linked; a bet with no spend says so. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, 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, type App } from "../src/app.ts"; | |
| 10 | import { openIssue } from "../src/actions/issues.ts"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { betReceipts, insertBet, markBacked } from "../src/core/bets.ts"; | |
| 13 | import type { Issue } from "../src/core/issues.ts"; | |
| 14 | import type { Repo } from "../src/core/repos.ts"; | |
| 15 | import { createRun } from "../src/core/runs.ts"; | |
| 16 | import { recordUsage } from "../src/core/usage.ts"; | |
| 17 | import { createUser } from "../src/core/users.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | ||
| 20 | const NOW = new Date("2026-11-03T10:00:00Z"); | |
| 21 | let dir: string; | |
| 22 | let app: App; | |
| 23 | let servers: Servers; | |
| 24 | let repo: Repo; | |
| 25 | let cookie: string; | |
| 26 | let aliceUser: Awaited<ReturnType<typeof createUser>> extends infer R ? (R extends { ok: true; value: infer V } ? V : never) : never; | |
| 27 | ||
| 28 | const open = (title: string): Issue => { | |
| 29 | const made = openIssue(app, aliceUser, repo, { title }); | |
| 30 | if (!made.ok) throw new Error(String(made.error)); | |
| 31 | return made.value; | |
| 32 | }; | |
| 33 | const spend = (issue: Issue | null, costUsd: number, at: string, branch = ""): string => { | |
| 34 | const run = createRun(app.db, { repoId: repo.id, kind: "agent", issueId: issue?.id, branch }); | |
| 35 | recordUsage(app.db, { | |
| 36 | runId: run.id, repoId: repo.id, issueId: issue?.id ?? null, agentId: null, subscriptionId: null, | |
| 37 | tokensIn: 0, tokensOut: 0, costUsd, wallMs: 1000, outcome: "ok", at, | |
| 38 | }); | |
| 39 | return run.id; | |
| 40 | }; | |
| 41 | const bet = (name: string, issueIds: string[]) => { | |
| 42 | const b = insertBet(app.db, { repoId: repo.id, name, journeys: "", appetite: 10, leadId: null, issueIds }, NOW); | |
| 43 | markBacked(app.db, b.id, 10, "owner", NOW); | |
| 44 | return b; | |
| 45 | }; | |
| 46 | const list = async () => (await fetch(`${servers.url}/alice/notes/bets`, { headers: { cookie } })).text(); | |
| 47 | /** The receipt line of the row named `name`. */ | |
| 48 | const receiptOf = (page: string, name: string): string => { | |
| 49 | const at = page.indexOf(`>${name}<`); | |
| 50 | const from = page.indexOf("data-bet-receipt", at); | |
| 51 | return page.slice(from, page.indexOf("</dl>", from)).replace(/\s+/g, " "); | |
| 52 | }; | |
| 53 | ||
| 54 | beforeAll(async () => { | |
| 55 | dir = await mkdtemp(join(tmpdir(), "forge-bets-receipt-")); | |
| 56 | app = createApp({ dataDir: dir, port: 0 }); | |
| 57 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 58 | if (!a.ok) throw new Error(a.error); | |
| 59 | aliceUser = a.value; | |
| 60 | const made = await createRepo(app, aliceUser, { name: "notes", visibility: "public" }); | |
| 61 | if (!made.ok) throw new Error(String(made.error)); | |
| 62 | repo = made.value; | |
| 63 | servers = await serve(app); | |
| 64 | const r = await fetch(`${servers.url}/signin`, { | |
| 65 | method: "POST", redirect: "manual", | |
| 66 | headers: { "content-type": "application/x-www-form-urlencoded", origin: servers.url }, | |
| 67 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 68 | }); | |
| 69 | cookie = (r.headers.get("set-cookie") ?? "").split(";")[0] ?? ""; | |
| 70 | }); | |
| 71 | ||
| 72 | afterAll(async () => { | |
| 73 | servers.stop(); | |
| 74 | app.close(); | |
| 75 | await rm(dir, { recursive: true, force: true }); | |
| 76 | }); | |
| 77 | ||
| 78 | describe("a bet's receipt", () => { | |
| 79 | test("joins the ledger to the bet's scopes: its runs and the pulls they built, and no other bet's", async () => { | |
| 80 | const scope = open("Receipt scope"); | |
| 81 | const other = open("Someone else's scope"); | |
| 82 | const pull = open("Receipt pull"); | |
| 83 | app.db.query("UPDATE issue SET kind = 'pull' WHERE id = ?1").run(pull.id); | |
| 84 | app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'crew/receipt', 'a', 'b')").run(pull.id); | |
| 85 | const r1 = spend(scope, 0.5, "2026-11-03T11:00:00Z", "crew/receipt"); | |
| 86 | const r2 = spend(scope, 0.25, "2026-11-03T12:00:00Z", "crew/receipt"); | |
| 87 | spend(other, 9, "2026-11-03T12:00:00Z"); | |
| 88 | spend(null, 7, "2026-11-03T12:00:00Z"); | |
| 89 | const b = bet("Receipted", [scope.id]); | |
| 90 | ||
| 91 | const got = betReceipts(app.db, [b.id]).get(b.id)!; | |
| 92 | expect(got.usd).toBeCloseTo(0.75); | |
| 93 | expect(got.runs.map((r) => r.runId)).toEqual([r2, r1]); | |
| 94 | expect(got.pulls.map((p) => p.number)).toEqual([pull.number]); | |
| 95 | ||
| 96 | const line = receiptOf(await list(), "Receipted"); | |
| 97 | expect(line).toContain("Spent</dt><dd>$0.75 on 2 runs"); | |
| 98 | expect(line).toContain("Run 2 ($0.25)"); | |
| 99 | expect(line).toContain(`Pull #${pull.number}</a>`); | |
| 100 | expect(line).toContain(`href="/alice/notes/runs/${r1}"`); | |
| 101 | expect(line).toContain(`href="/alice/notes/runs/${r2}"`); | |
| 102 | expect(line).toContain(`href="/alice/notes/pulls/${pull.number}"`); | |
| 103 | expect(line).not.toContain("$9"); | |
| 104 | }); | |
| 105 | ||
| 106 | test("a bet with no spend says so", async () => { | |
| 107 | const b = bet("Quiet", [open("Quiet scope").id]); | |
| 108 | expect(betReceipts(app.db, [b.id]).get(b.id)!).toEqual({ usd: 0, runs: [], pulls: [] }); | |
| 109 | expect(receiptOf(await list(), "Quiet")).toContain("Nothing yet"); | |
| 110 | }); | |
| 111 | }); |
test/tui-bets-estimate.test.ts +22
| @@ -6,6 +6,7 @@ import { afterAll, beforeAll, expect, test } from "bun:test"; | ||
| 6 | 6 | import { mkdtemp, rm } from "node:fs/promises"; |
| 7 | 7 | import { tmpdir } from "node:os"; |
| 8 | 8 | import { join } from "node:path"; |
| 9 | import { openIssue } from "../src/actions/issues.ts"; | |
| 9 | 10 | import { createRepo } from "../src/actions/repos.ts"; |
| 10 | 11 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 12 | import { insertBet } from "../src/core/bets.ts"; |
| @@ -82,3 +83,24 @@ test("short: the row adds by how much", async () => { | ||
| 82 | 83 | const text = await betsText(); |
| 83 | 84 | expect(text).toContain(`Estimate $${estimate.toFixed(2)} of $5.00 left: short by $${(estimate - 5).toFixed(2)}`); |
| 84 | 85 | }); |
| 86 | ||
outdated
Only the no-spend row is tested in the terminal. Add a test where a bet has spend and assert |
||
| 87 | test("a bet that spent nothing says so on its row (#13940)", async () => { | |
| 88 | expect(await betsText()).toContain("no spend yet"); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a bet with spend says what it bought: dollars, runs and pulls (#13940)", async () => { | |
| 92 | const repo = alice && forge.db.query<{ id: string }, []>("SELECT id FROM repo WHERE name = 'wallet'").get()!; | |
| 93 | const issue = openIssue(forge, alice, getRepoById(forge.db, repo.id)!, { title: "Scope with spend" }); | |
| 94 | if (!issue.ok) throw new Error("issue"); | |
| 95 | const b = insertBet(forge.db, { repoId: repo.id, name: "Spender", journeys: "one", appetite: 10, leadId: null, issueIds: [issue.value.id] }, new Date()); | |
| 96 | void b; | |
| 97 | recordUsage(forge.db, { | |
| 98 | runId: createRun(forge.db, { repoId: repo.id, kind: "agent", issueId: issue.value.id, branch: "crew/spender" }).id, repoId: repo.id, | |
| 99 | issueId: issue.value.id, agentId: null, subscriptionId: null, | |
| 100 | tokensIn: 0, tokensOut: 0, costUsd: 0.5, wallMs: 1000, outcome: "ok", at: new Date().toISOString(), | |
| 101 | }); | |
| 102 | forge.db.query("UPDATE issue SET kind = 'pull' WHERE id = ?1").run(issue.value.id); | |
| 103 | forge.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'crew/spender', 'a', 'b')").run(issue.value.id); | |
| 104 | const text = await betsText(); | |
| 105 | expect(text).toContain(`spent $0.50 on 1 run, pulls #${issue.value.number}`); | |
| 106 | }); |
This does two SQL queries per bet on every redraw. Compute once per list load, not per draw. Also the secondary line grows long with the pull list; truncate with
and N morelike the page.