Public: anyone can read this; the crew can change it.
The forge measures each new forge's seconds from first ask to first built change, and /admin/limits shows the median per plan #16829
Check · Review · Land · Live — Its check is red.
scout/forge:scout/16777-the-forge-measures-each-new-forge-s-seco → main 6 commits fork
12 files
changed +205
−9
- docs/features/plans.md
- src/actions/first_ask.ts
- src/actions/plans.ts
- src/actions/spawn.ts
- src/core/first_change_samples.ts
- src/core/migration-pins/20261130140000-first-change-sample.pin
- src/core/migrations/20261130140000-first-change-sample.ts
- src/crew/land_gated.ts
- src/http/api/admin.ts
- src/tui/screens/plans.ts
- src/web/settings/plans.ts
- test/first-change-samples.test.ts
docs/features/plans.md +2
| @@ -26,6 +26,8 @@ These are the defaults. A forge admin who is a person edits them: | ||
| 26 | 26 | |
| 27 | 27 | Each plan may also carry `firstChangeSecs`, the measured whole seconds from a first ask to a first built change on that plan's size. An admin sets it in the "Seconds to a first change" field of the Plans form, with `first <seconds>` in the terminal, or in `firstChangeSecs` through the API and MCP. Until it is set (0 clears it) nothing is said: the plans words and `GET /api/v1/plans` leave it out, and once set the words read "A first built change takes about 4 minutes." |
| 28 | 28 | |
| 29 | The forge measures that time itself: the first ask on a forge and the first change built on the app it made are each recorded once, with the forge's plan (`plan` in its `parent.json`, `free` when absent), in `first_change_sample` (`src/core/first_change_samples.ts`). A forge with an ask and no built change is no sample. `/admin/limits` shows beside each plan "Measured here: median 7 min from 4 forges", or "not measured yet"; the terminal Plans screen says the same; `GET /api/v1/admin/plans` (admin only, 404 to anyone else) carries `medianSecs` (null when not measured) and `measuredForges` on each plan. An admin copies the number into the field above; nothing fills it by itself. Each forge counts the samples in its own store. | |
| 30 | ||
| 29 | 31 | ## What a spawn gets |
| 30 | 32 | |
| 31 | 33 | `POST /api/v1/forges` (and the MCP `spawn`) takes `plan`. The forge's size comes from the plan (a `size` that disagrees is refused), and its credit cap is the plan's `creditUsd`, lent as a monthly cap through `src/spawn/credit.ts`: the spend is counted again each UTC calendar month. Plans are read at spawn time, so an edit governs the next spawn; forges already made keep the credit they were given. |
src/actions/first_ask.ts +2
| @@ -26,6 +26,7 @@ import { overTellLimit, parseFrom, tellTooLongWords } from "./tell_from.ts"; | ||
| 26 | 26 | import { deriveAppName } from "../web/pages/apps-name.ts"; |
| 27 | 27 | import { requireVerified } from "./verify.ts"; |
| 28 | 28 | import { recordRefusal } from "../core/first_ask_refusals.ts"; |
| 29 | import { ownPlan, recordAsk } from "../core/first_change_samples.ts"; | |
| 29 | 30 | import { DIRECTION_NOTE } from "../crew/direct.ts"; |
| 30 | 31 | |
| 31 | 32 | export interface FirstAsk { |
| @@ -76,6 +77,7 @@ export async function firstAsk( | ||
| 76 | 77 | const made = await newAppFromSentence(app, actor, { sentence: text, description: "" }); |
| 77 | 78 | if (!made.ok) return refuse(NEW_APP_ERRORS[made.code] ?? NOT_TOLD_WORDS); |
| 78 | 79 | let repo = made.repo; |
| 80 | recordAsk(app.db, repo.id, ownPlan(app.config.dataDir), Date.now()); | |
| 79 | 81 | let autonomyFailed = false; |
| 80 | 82 | if (crewCanRun) { |
| 81 | 83 | // A builder and a reviewer seated and autonomy on as the asker, so the next tick starts the ask (#11508). |
src/actions/plans.ts +22
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | import type { App } from "../app.ts"; |
| 9 | 9 | import type { User } from "../core/users.ts"; |
| 10 | 10 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 11 | import { measureOf } from "../core/first_change_samples.ts"; | |
| 11 | 12 | import { getPlans, parsePlans, publicPlans, setPlans, type Plan } from "../settings/plans.ts"; |
| 12 | 13 | |
| 13 | 14 | export type PlansError = "not_found" | { readonly invalid: string }; |
| @@ -30,3 +31,24 @@ export function editPlans(app: App, actor: User | undefined, edits: Record<strin | ||
| 30 | 31 | const next = getPlans(app.db).map((p) => ({ ...p, ...(edits[p.key] ?? {}) })); |
| 31 | 32 | return changePlans(app, actor, next); |
| 32 | 33 | } |
| 34 | ||
| 35 | export interface MeasuredPlan extends Plan { | |
| 36 | /** Median measured seconds from a first ask to a first built change; null when not measured. */ | |
| 37 | readonly medianSecs: number | null; | |
| 38 | /** How many forges the median comes from. */ | |
| 39 | readonly measuredForges: number; | |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * Every plan with its measured median and count (`core/first_change_samples.ts`); `not_found` for | |
| 44 | * anyone but a person admin. | |
| 45 | */ | |
| 46 | export function measuredPlans(app: App, actor: User | undefined): Result<MeasuredPlan[], "not_found"> { | |
| 47 | if (actor?.isAdmin !== true || actor.kind === "agent") return err("not_found"); | |
| 48 | return ok( | |
| 49 | readPlans(app).map((p) => { | |
| 50 | const m = measureOf(app.db, p.key); | |
| 51 | return { ...p, medianSecs: m.medianSecs ?? null, measuredForges: m.forges }; | |
| 52 | }), | |
| 53 | ); | |
| 54 | } |
src/actions/spawn.ts +3
| @@ -798,6 +798,9 @@ async function copyInto( | ||
| 798 | 798 | // No spawner signed in: the founder account is a placeholder (#11785). |
| 799 | 799 | anonymous: owner === undefined, |
| 800 | 800 | size: plan.size ?? DEFAULT_SIZE, |
| 801 | // The plan the child is on, so it can measure its first change under it | |
| 802 | // (`core/first_change_samples.ts`). | |
| 803 | ...(plan.plan === undefined ? {} : { plan: plan.plan }), | |
| 801 | 804 | ...(plan.template === undefined ? {} : { template: templateMark ?? { id: plan.template } }), |
| 802 | 805 | // Each app: where it came from and that the child follows it (`spawn/app_links.ts`). |
| 803 | 806 | apps: appEntries, |
src/core/first_change_samples.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * Seconds from a new forge's first ask to its first built change (#16777), measured by the | |
| 3 | * forge itself so no run has to time production by hand. A forge's plan is the one its | |
| 4 | * `parent.json` names (absent: free). The ask is recorded once, on the app it made; the first | |
| 5 | * change built on that app closes the sample once. An ask with no built change is no sample. | |
| 6 | * The median per plan is read by an admin and copied into the plan by hand; nothing fills it. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | import { readFileSync } from "node:fs"; | |
| 11 | import { join } from "node:path"; | |
| 12 | ||
| 13 | /** The plan this forge was born on, from its `parent.json`; "free" when it names none. */ | |
| 14 | export function ownPlan(dataDir: string): string { | |
| 15 | try { | |
| 16 | const parsed = JSON.parse(readFileSync(join(dataDir, "parent.json"), "utf8")) as { plan?: unknown }; | |
| 17 | return typeof parsed.plan === "string" && /^[a-z][a-z0-9-]{0,19}$/.test(parsed.plan) ? parsed.plan : "free"; | |
| 18 | } catch { | |
| 19 | return "free"; | |
| 20 | } | |
| 21 | } | |
| 22 | ||
| 23 | /** Record the first ask on an app, once: a second call for the same app changes nothing. */ | |
| 24 | export function recordAsk(db: Database, repoId: string, plan: string, now: number): void { | |
| 25 | db.query("INSERT OR IGNORE INTO first_change_sample (repo_id, plan, asked_at) VALUES (?1, ?2, ?3)").run(repoId, plan, now); | |
| 26 | } | |
| 27 | ||
| 28 | /** Record the first built change of an app that was asked for, once. */ | |
| 29 | export function recordBuilt(db: Database, repoId: string, now: number): void { | |
| 30 | db.query("UPDATE first_change_sample SET built_at = ?2 WHERE repo_id = ?1 AND built_at IS NULL").run(repoId, now); | |
| 31 | } | |
| 32 | ||
| 33 | export interface PlanMeasure { | |
| 34 | /** Median whole seconds, or undefined when no forge on the plan has a sample. */ | |
| 35 | readonly medianSecs: number | undefined; | |
| 36 | readonly forges: number; | |
| 37 | } | |
| 38 | ||
| 39 | /** The median seconds and sample count of one plan. */ | |
| 40 | export function measureOf(db: Database, plan: string): PlanMeasure { | |
| 41 | const rows = db | |
| 42 | .query<{ s: number }, [string]>( | |
| 43 | "SELECT MAX(0, built_at - asked_at) / 1000.0 AS s FROM first_change_sample WHERE plan = ?1 AND built_at IS NOT NULL ORDER BY s", | |
| 44 | ) | |
| 45 | .all(plan); | |
| 46 | if (rows.length === 0) return { medianSecs: undefined, forges: 0 }; | |
| 47 | const mid = rows.length >> 1; | |
| 48 | const m = rows.length % 2 === 1 ? rows[mid]!.s : (rows[mid - 1]!.s + rows[mid]!.s) / 2; | |
| 49 | return { medianSecs: Math.round(m), forges: rows.length }; | |
| 50 | } | |
| 51 | ||
| 52 | /** "median 7 min from 4 forges", or "not measured yet". */ | |
| 53 | export function measureWords(m: PlanMeasure): string { | |
| 54 | if (m.medianSecs === undefined) return "not measured yet"; | |
| 55 | const s = m.medianSecs; | |
| 56 | const t = s < 90 ? `${s} s` : s < 5400 ? `${Math.round(s / 60)} min` : `${Math.round(s / 360) / 10} h`; | |
| 57 | return `median ${t} from ${m.forges} ${m.forges === 1 ? "forge" : "forges"}`; | |
| 58 | } |
src/core/migration-pins/20261130140000-first-change-sample.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 65cf03cdcc4ba13d1aeea8d8221ddbdde875ac59c78bca11b83c1c39d130f133 |
src/core/migrations/20261130140000-first-change-sample.ts added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | // The seconds from a forge's first ask to its first built change (#16777): one row per | |
| 2 | // first-ask app, its plan, when it was asked and, once, when its first change was built. | |
| 3 | export default ` | |
| 4 | CREATE TABLE IF NOT EXISTS first_change_sample ( | |
| 5 | repo_id TEXT PRIMARY KEY, | |
| 6 | plan TEXT NOT NULL, | |
| 7 | asked_at INTEGER NOT NULL, | |
| 8 | built_at INTEGER | |
| 9 | ); | |
| 10 | `; |
src/crew/land_gated.ts +4
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | /** Landing one gated step of the queue's candidate, and how: merge or squash. */ |
| 2 | 2 | |
| 3 | import { recordBuilt } from "../core/first_change_samples.ts"; | |
| 3 | 4 | import type { AccessError } from "../actions/access.ts"; |
| 4 | 5 | import { addComment } from "../actions/issues.ts"; |
| 5 | 6 | import { mergePull } from "../actions/pulls.ts"; |
| @@ -132,6 +133,9 @@ export async function landGated( | ||
| 132 | 133 | // there is no release to cut and nothing new was landed. |
| 133 | 134 | if (merged.value.carried) return undefined; |
| 134 | 135 | resolveOpen(app.db, pull.id, "land", "shipped"); |
| 136 | // The seconds from the first ask to the first built change: closed once | |
| 137 | // (`core/first_change_samples.ts`). | |
| 138 | recordBuilt(app.db, repo.id, Date.now()); | |
| 135 | 139 | // The app's first change: one sentence on the pull, and the reviewer's requests as one follow-up. |
| 136 | 140 | if (review === "first") sayFirstLanded(app, repo, pull, by); |
| 137 | 141 | // Said once it has landed, so the note wakes no builder: the review was |
src/http/api/admin.ts +15 −3
| @@ -21,7 +21,7 @@ import { DERIVED_WORDS, REASON_WORDS, changeCapacity, edgeOffDryRun, listCapacit | ||
| 21 | 21 | import { changeShield, lastShieldChange, shieldState } from "../../actions/shield.ts"; |
| 22 | 22 | import { inviteLink, list as listInviteLinks, mint as mintInviteLink, revoke as revokeInviteLink } from "../../actions/invites.ts"; |
| 23 | 23 | import { changeLimit, listLimits } from "../../actions/limits.ts"; |
| 24 | import { changePlans } from "../../actions/plans.ts"; | |
| 24 | import { changePlans, measuredPlans } from "../../actions/plans.ts"; | |
| 25 | 25 | import { setAccountCeilings, setAccountGrant } from "../../actions/model_grant.ts"; |
| 26 | 26 | import { DOMAIN_WORDS, PAID_PLACEMENT_WORDS, keepPinned, changeBaseDomain, changePaidPlacement, readPaidPlacement, checkPinned, claimPinned, deployPinned, pinnedForges, readBaseDomain, retirePinned, retireWords, rollbackPinned, updatePinned } from "../../actions/pinned.ts"; |
| 27 | 27 | import { TARGETS } from "../../cloud/targets.ts"; |
| @@ -41,7 +41,7 @@ import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.t | ||
| 41 | 41 | import { getUserByName } from "../../core/users.ts"; |
| 42 | 42 | import { holds, weakEtag } from "../etag.ts"; |
| 43 | 43 | import { offsetPage, offsetQuery } from "../offset_page.ts"; |
| 44 | import { body, json, str } from "../respond.ts"; | |
| 44 | import { body, forbidden, json, str } from "../respond.ts"; | |
| 45 | 45 | import { offsetRefused } from "../api.ts"; |
| 46 | 46 | import type { Ctx, Handler } from "../context.ts"; |
| 47 | 47 | import { apiError, notFound, requireScope } from "../respond.ts"; |
| @@ -385,7 +385,19 @@ export function adminRoutes(router: Router<Handler>): void { | ||
| 385 | 385 | ); |
| 386 | 386 | }); |
| 387 | 387 | |
| 388 | // The plans (`settings/plans.ts`): the whole list is replaced; GET is the public /api/v1/plans. | |
| 388 | // The plans with the forge's own measured median seconds to a first change (admin only); the | |
| 389 | // public list is /api/v1/plans. | |
| 390 | router.add("GET", "/api/v1/admin/plans", (ctx) => { | |
| 391 | // A person who is not an admin is told no (403), as the issue asks; anyone not signed in gets | |
| 392 | // the door's own answer. | |
| 393 | if (ctx.auth.user !== undefined && ctx.auth.user.isAdmin !== true) return forbidden(); | |
| 394 | const denied = accountAdmin(ctx, "ro"); | |
| 395 | if (denied) return denied; | |
| 396 | const listed = measuredPlans(ctx.app, ctx.auth.user); | |
| 397 | return listed.ok ? json({ items: listed.value }) : forbidden(); | |
| 398 | }); | |
| 399 | ||
| 400 | // The plans (`settings/plans.ts`): the whole list is replaced. | |
| 389 | 401 | router.add("PUT", "/api/v1/admin/plans", async (ctx) => { |
| 390 | 402 | const denied = accountAdmin(ctx, "admin"); |
| 391 | 403 | if (denied) return denied; |
src/tui/screens/plans.ts +8 −4
| @@ -7,7 +7,8 @@ | ||
| 7 | 7 | * and the MCP's. The gate is the action's: an admin who is a person; anyone else sees nothing. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { editPlans, readPlans } from "../../actions/plans.ts"; | |
| 10 | import { measureWords } from "../../core/first_change_samples.ts"; | |
| 11 | import { editPlans, measuredPlans, readPlans } from "../../actions/plans.ts"; | |
| 11 | 12 | import { moneyWords, centsOfUsd, usdPerMonthWords } from "../../billing/money.ts"; |
| 12 | 13 | import type { Plan } from "../../settings/plans.ts"; |
| 13 | 14 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -89,12 +90,15 @@ export function plansScreen(f: Session, ctx: Ctx): Screen { | ||
| 89 | 90 | ); |
| 90 | 91 | }; |
| 91 | 92 | |
| 92 | const rows = (plans: readonly Plan[]): ListRow[] => | |
| 93 | plans.map((p) => ({ | |
| 93 | const rows = (plans: readonly Plan[]): ListRow[] => { | |
| 94 | const m = measuredPlans(f.app, f.user); | |
| 95 | const measured = new Map((m.ok ? m.value : []).map((q) => [q.key, q])); | |
| 96 | return plans.map((p) => ({ | |
| 94 | 97 | title: `${p.key} · ${p.name}`, |
| 95 | 98 | secondary: `${p.size} · ${usdPerMonthWords(p.priceUsd)} · ${moneyWords(centsOfUsd(p.creditUsd))} crew credit · ${p.forges} forges · ${first(p)}`, |
| 96 | meta: p.says, | |
| 99 | meta: `${p.says} · measured here: ${measureWords({ medianSecs: measured.get(p.key)?.medianSecs ?? undefined, forges: measured.get(p.key)?.measuredForges ?? 0 })}`, | |
| 97 | 100 | })); |
| 101 | }; | |
| 98 | 102 | |
| 99 | 103 | const screen: Screen = { |
| 100 | 104 | title: "Plans", |
src/web/settings/plans.ts +8 −2
| @@ -5,7 +5,8 @@ | ||
| 5 | 5 | * GET /api/v1/plans use it at once. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { editPlans, readPlans } from "../../actions/plans.ts"; | |
| 8 | import { measureWords } from "../../core/first_change_samples.ts"; | |
| 9 | import { editPlans, measuredPlans, readPlans } from "../../actions/plans.ts"; | |
| 9 | 10 | import type { Ctx } from "../../http/context.ts"; |
| 10 | 11 | import { SIZES } from "../../spawn/sizes.ts"; |
| 11 | 12 | import { notFoundPage } from "../chrome.ts"; |
| @@ -17,6 +18,11 @@ import { btn, row } from "../ui.ts"; | ||
| 17 | 18 | const FIELDS = ["name", "says", "size", "priceUsd", "creditUsd", "forges", "firstChangeSecs"] as const; |
| 18 | 19 | |
| 19 | 20 | export function plansRow(ctx: Ctx): Html { |
| 21 | const measured = measuredPlans(ctx.app, ctx.auth.user); | |
| 22 | const wordsOf = (key: string): string => { | |
| 23 | const m = measured.ok ? measured.value.find((q) => q.key === key) : undefined; | |
| 24 | return measureWords({ medianSecs: m?.medianSecs ?? undefined, forges: m?.measuredForges ?? 0 }); | |
| 25 | }; | |
| 20 | 26 | const rows = readPlans(ctx.app).map((p) => |
| 21 | 27 | row({ |
| 22 | 28 | title: p.key, |
| @@ -30,7 +36,7 @@ export function plansRow(ctx: Ctx): Html { | ||
| 30 | 36 | { label: "Price $/mo", value: html`<input class="input mono" name="${p.key}.priceUsd" inputmode="decimal" value="${String(p.priceUsd)}" ${p.key === "free" ? "readonly" : ""} aria-label="${p.key} price a month in dollars" />` }, |
| 31 | 37 | { label: "Crew credit $/mo", value: html`<input class="input mono" name="${p.key}.creditUsd" inputmode="decimal" value="${String(p.creditUsd)}" aria-label="${p.key} crew credit a month in dollars" />` }, |
| 32 | 38 | { label: "Forges", value: html`<input class="input mono" name="${p.key}.forges" type="number" min="1" max="1000" step="1" value="${String(p.forges)}" aria-label="${p.key} forges one person may hold" />` }, |
| 33 | { label: "Seconds to a first change", value: html`<input class="input mono" name="${p.key}.firstChangeSecs" inputmode="numeric" value="${String(p.firstChangeSecs ?? 0)}" aria-describedby="${p.key}-first-hint" aria-label="${p.key} measured seconds to a first built change" /> <span id="${p.key}-first-hint" class="muted small">0 or blank: not measured, and the plans page says nothing.</span>` }, | |
| 39 | { label: "Seconds to a first change", value: html`<input class="input mono" name="${p.key}.firstChangeSecs" inputmode="numeric" value="${String(p.firstChangeSecs ?? 0)}" aria-describedby="${p.key}-first-hint" aria-label="${p.key} measured seconds to a first built change" /> <span id="${p.key}-first-hint" class="muted small">0 or blank: not measured, and the plans page says nothing.</span> <span class="muted small" id="${p.key}-measured">Measured here: ${wordsOf(p.key)}.</span>` }, | |
| 34 | 40 | ], |
| 35 | 41 | }), |
| 36 | 42 | ); |
test/first-change-samples.test.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** The forge times its own first ask to first built change (#16777), per plan. */ | |
| 2 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm, writeFile } 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 { measureOf, measureWords, ownPlan, recordAsk, recordBuilt } from "../src/core/first_change_samples.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | ||
| 12 | describe("first change samples", () => { | |
| 13 | let dir = ""; | |
| 14 | let app: App; | |
| 15 | let servers: Servers; | |
| 16 | let admin = ""; | |
| 17 | let bob = ""; | |
| 18 | beforeAll(async () => { | |
| 19 | dir = await mkdtemp(join(tmpdir(), "forge-fcs-")); | |
| 20 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 21 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-new-forge-passphrase", isAdmin: true }); | |
| 22 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-long-new-forge-passphrase" }); | |
| 23 | if (!a.ok || !b.ok) throw new Error("users"); | |
| 24 | admin = createToken(app.db, a.value.id, "admin", "admin").token; | |
| 25 | bob = createToken(app.db, b.value.id, "rw", "rw").token; | |
| 26 | servers = await serve(app); | |
| 27 | }, 30_000); | |
| 28 | afterAll(async () => { | |
| 29 | servers.stop(); | |
| 30 | app.close(); | |
| 31 | await rm(dir, { recursive: true, force: true }); | |
| 32 | }); | |
| 33 | ||
| 34 | test("an ask then a built change is one sample with the right seconds; a second change adds none", () => { | |
| 35 | recordAsk(app.db, "r1", "small", 1_000); | |
| 36 | recordBuilt(app.db, "r1", 421_000); | |
| 37 | recordBuilt(app.db, "r1", 900_000); | |
| 38 | recordAsk(app.db, "r1", "small", 5_000); | |
| 39 | expect(measureOf(app.db, "small")).toEqual({ medianSecs: 420, forges: 1 }); | |
| 40 | }); | |
| 41 | ||
| 42 | test("an ask with no change adds no sample, and a plan with none reads not measured yet", () => { | |
| 43 | recordAsk(app.db, "r2", "medium", 1_000); | |
| 44 | expect(measureOf(app.db, "medium")).toEqual({ medianSecs: undefined, forges: 0 }); | |
| 45 | expect(measureWords(measureOf(app.db, "medium"))).toBe("not measured yet"); | |
| 46 | expect(measureWords(measureOf(app.db, "small"))).toBe("median 7 min from 1 forge"); | |
| 47 | }); | |
| 48 | ||
| 49 | test("the median of several", () => { | |
| 50 | for (const [id, s] of [["a", 60], ["b", 120], ["c", 600]] as const) { | |
| 51 | recordAsk(app.db, id, "large", 0); | |
| 52 | recordBuilt(app.db, id, s * 1000); | |
| 53 | } | |
| 54 | expect(measureOf(app.db, "large")).toEqual({ medianSecs: 120, forges: 3 }); | |
| 55 | }); | |
| 56 | ||
| 57 | test("a forge's plan is the one in its parent.json, free when absent", async () => { | |
| 58 | expect(ownPlan(dir)).toBe("free"); | |
| 59 | await writeFile(join(dir, "parent.json"), JSON.stringify({ plan: "medium" })); | |
| 60 | expect(ownPlan(dir)).toBe("medium"); | |
| 61 | }); | |
| 62 | ||
| 63 | test("GET /api/v1/admin/plans carries median and count; a non-admin gets 403", async () => { | |
| 64 | const get = (t: string) => fetch(`${servers.url}/api/v1/admin/plans`, { headers: { authorization: `Bearer ${t}` } }); | |
| 65 | const ok = await get(admin); | |
| 66 | expect(ok.status).toBe(200); | |
| 67 | const items = ((await ok.json()) as { items: { key: string; medianSecs: number | null; measuredForges: number }[] }).items; | |
| 68 | expect(items.find((p) => p.key === "small")).toMatchObject({ medianSecs: 420, measuredForges: 1 }); | |
| 69 | expect(items.find((p) => p.key === "medium")).toMatchObject({ medianSecs: null, measuredForges: 0 }); | |
| 70 | expect((await get(bob)).status).toBe(403); | |
| 71 | }); | |
| 72 | }); |
new above
offline