Public: anyone can read this; the crew can change it.
The v1 proof reads production's schedules with no account: one public line says the oldest overdue schedule, and the probe passes on it #12515
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12512-the-v1-proof-reads-production-s-schedule → main 5 commits fork
10 files
changed +196
−85
docs/features/v1-proven.md +7 −7
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | # Is v1.0.0 proven? |
| 2 | 2 | |
| 3 | 3 | sh |
| 4 | bun scripts/v1-proven.ts https://your-forge.example [--schedules owner/name] | |
| 4 | bun scripts/v1-proven.ts https://your-forge.example | |
| 5 | 5 | |
| 6 | 6 | |
| 7 | 7 | One read-only probe (GET only) for each item on the v1 list, one line each: |
| @@ -11,12 +11,12 @@ One read-only probe (GET only) for each item on the v1 list, one line each: | ||
| 11 | 11 | - `unproven`: no public read can show the claim, so it is not counted as a pass. |
| 12 | 12 | Today: #11140 (the free plan sleeps rather than is deleted), #11141 (CPU |
| 13 | 13 | slowing, the first change landing, lapsed-payment sleep: only the gated page |
| 14 | is seen) and #11595 when the `--schedules` repository (default | |
| 15 | `trevato/forge`) is not readable to a stranger. Point `--schedules` at a | |
| 16 | public repository to prove it. | |
| 14 | is seen), and #11595 only against a forge whose health answer predates | |
| 15 | #12512. | |
| 17 | 16 | |
| 18 | 17 | Exit 0 only when every probe passes; 1 when any fails; 3 when none fails but |
| 19 | 18 | some are unproven; 2 on bad usage. Owner-only pages are probed as "there and |
| 20 | gated". The schedule probe compares the last run with the period inferred from | |
| 21 | the last run and the next one: the API exposes the cron, not the period. It | |
| 22 | reports and never tags; `scripts/cut-release.ts` does that. | |
| 19 | gated". The schedule probe reads `schedules` on the public `GET /api/v1/health`: | |
| 20 | the forge-wide count of unpaused schedules more than five minutes past due and | |
| 21 | the age of the oldest, counts only, naming no app or repository. It passes at | |
| 22 | zero and fails naming the oldest one's age. It reports and never tags; `scripts/cut-release.ts` does that. |
scripts/v1-proven.ts +26 −44
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /** |
| 2 | * `bun scripts/v1-proven.ts <base-url> [--schedules owner/name]` (#12297): | |
| 2 | * `bun scripts/v1-proven.ts <base-url>` (#12297): | |
| 3 | 3 | * says whether v1.0.0 is proven on a running forge. One read-only probe per |
| 4 | 4 | * item on the v1 list, one line each: `pass`, `fail` or `unproven` with the |
| 5 | 5 | * reason. Exit 0 only when every probe passes, 1 when any fails, 3 when none |
| @@ -15,11 +15,7 @@ export interface Probe { | ||
| 15 | 15 | readonly issue: number; |
| 16 | 16 | readonly item: string; |
| 17 | 17 | /** A string is a pass; `unproven(why)` is "seen what a stranger can, not the claim". */ |
| 18 | readonly run: (get: Get, opts: Options) => Promise<string | Unproven>; | |
| 19 | } | |
| 20 | export interface Options { | |
| 21 | readonly schedules: string; | |
| 22 | readonly now: number; | |
| 18 | readonly run: (get: Get) => Promise<string | Unproven>; | |
| 23 | 19 | } |
| 24 | 20 | export class Unproven { |
| 25 | 21 | constructor(readonly why: string) {} |
| @@ -164,29 +160,15 @@ export const probes: readonly Probe[] = [ | ||
| 164 | 160 | }, |
| 165 | 161 | { |
| 166 | 162 | issue: 11595, |
| 167 | item: "a schedule's last run is under its period", | |
| 168 | run: async (get, opts) => { | |
| 169 | const path = `/api/v1/repos/${opts.schedules}/schedules`; | |
| 170 | const got = await get(path); | |
| 171 | if (got.status === 401 || got.status === 403 || got.status === 404) | |
| 172 | return unproven(`${path} answers ${got.status} to a stranger: ${opts.schedules} is not publicly readable; point --schedules at a public repository`); | |
| 173 | expect(got.status === 200, `${path} answers ${got.status}, not 200`); | |
| 174 | const items = (JSON.parse(got.text) as { items?: Row[] }).items ?? []; | |
| 175 | expect(items.length > 0, `${opts.schedules} declares no schedule`); | |
| 176 | const late: string[] = []; | |
| 177 | for (const s of items) { | |
| 178 | if (s.paused) continue; | |
| 179 | if (!s.last) { | |
| 180 | late.push(`${s.name} never ran`); | |
| 181 | continue; | |
| 182 | } | |
| 183 | if (s.last.status !== "ok") late.push(`${s.name} last run ${s.last.status}`); | |
| 184 | else if (s.nextAt !== null && opts.now > s.nextAt + 5 * 60_000) late.push(`${s.name} is past its next run`); | |
| 185 | else if (s.nextAt !== null && opts.now - s.last.at > s.nextAt - s.last.at + 5 * 60_000) | |
| 186 | late.push(`${s.name} last ran longer ago than its period`); | |
| 187 | } | |
| 188 | expect(late.length === 0, late.join("; ")); | |
| 189 | return `${items.length} schedule(s), each last run within its period`; | |
| 163 | item: "no schedule is past its period", | |
| 164 | // The forge-wide count on the public health answer (#12512): a stranger reads it, no repository named. | |
| 165 | run: async (get) => { | |
| 166 | const got = await bodyOk(get, "/api/v1/health"); | |
| 167 | const s = (JSON.parse(got.text) as { schedules?: { overdue?: unknown; oldestSeconds?: unknown } }).schedules; | |
| 168 | if (s === undefined || typeof s.overdue !== "number" || typeof s.oldestSeconds !== "number") | |
| 169 | return unproven("/api/v1/health carries no schedules count: this forge predates #12512"); | |
| 170 | expect(s.overdue === 0, `${s.overdue} schedule(s) overdue, the oldest by ${ageWords(s.oldestSeconds)}`); | |
| 171 | return "/api/v1/health: no schedule is overdue"; | |
| 190 | 172 | }, |
| 191 | 173 | }, |
| 192 | 174 | { |
| @@ -206,17 +188,20 @@ export const probes: readonly Probe[] = [ | ||
| 206 | 188 | }, |
| 207 | 189 | ]; |
| 208 | 190 | |
| 209 | interface Row { | |
| 210 | name: string; | |
| 211 | paused: boolean; | |
| 212 | nextAt: number | null; | |
| 213 | last: { at: number; status: string } | null; | |
| 191 | /** `3 h 12 min`, `45 s`: an age as a person reads it. */ | |
| 192 | export function ageWords(seconds: number): string { | |
| 193 | const s = Math.max(0, Math.floor(seconds)); | |
| 194 | if (s < 60) return `${s} s`; | |
| 195 | const m = Math.floor(s / 60); | |
| 196 | if (m < 60) return `${m} min`; | |
| 197 | const h = Math.floor(m / 60); | |
| 198 | if (h < 48) return `${h} h ${m % 60} min`; | |
| 199 | return `${Math.floor(h / 24)} d ${h % 24} h`; | |
| 214 | 200 | } |
| 215 | 201 | |
| 216 | 202 | /** Runs every probe against `base` with GET only; a probe that throws is a fail. */ |
| 217 | export async function prove(base: string, fetcher: Fetch, opts: Partial<Options> = {}): Promise<Result[]> { | |
| 203 | export async function prove(base: string, fetcher: Fetch): Promise<Result[]> { | |
| 218 | 204 | const root = base.replace(/\/+$/, ""); |
| 219 | const o: Options = { schedules: opts.schedules ?? "trevato/forge", now: opts.now ?? Date.now() }; | |
| 220 | 205 | const get: Get = async (path) => { |
| 221 | 206 | const res = await fetcher(root + path, { method: "GET", redirect: "manual", signal: AbortSignal.timeout(15_000) }); |
| 222 | 207 | return { status: res.status, headers: res.headers, text: await res.text(), location: res.headers.get("location") }; |
| @@ -224,7 +209,7 @@ export async function prove(base: string, fetcher: Fetch, opts: Partial<Options> | ||
| 224 | 209 | const out: Result[] = []; |
| 225 | 210 | for (const p of probes) { |
| 226 | 211 | try { |
| 227 | const r = await p.run(get, o); | |
| 212 | const r = await p.run(get); | |
| 228 | 213 | out.push(r instanceof Unproven ? { issue: p.issue, item: p.item, status: "unproven", detail: r.why } : { issue: p.issue, item: p.item, status: "pass", detail: r }); |
| 229 | 214 | } catch (e) { |
| 230 | 215 | out.push({ issue: p.issue, item: p.item, status: "fail", detail: e instanceof Fail ? e.message : `probe error: ${String(e)}` }); |
| @@ -236,15 +221,12 @@ export async function prove(base: string, fetcher: Fetch, opts: Partial<Options> | ||
| 236 | 221 | export const line = (r: Result): string => `${r.status} #${r.issue} ${r.item}: ${r.detail}`; |
| 237 | 222 | |
| 238 | 223 | if (import.meta.main) { |
| 239 | const args = process.argv.slice(2); | |
| 240 | const i = args.indexOf("--schedules"); | |
| 241 | const schedules = i >= 0 ? args.splice(i, 2)[1] : undefined; | |
| 242 | const base = args[0]; | |
| 243 | if (!base || !/^https?:\/\/[^/\s]+/.test(base) || (schedules !== undefined && !/^[\w.-]+\/[\w.-]+$/.test(schedules))) { | |
| 244 | console.error("usage: bun scripts/v1-proven.ts <base-url> [--schedules owner/name]"); | |
| 224 | const base = process.argv[2]; | |
| 225 | if (!base || !/^https?:\/\/[^/\s]+/.test(base) || process.argv.length > 3) { | |
| 226 | console.error("usage: bun scripts/v1-proven.ts <base-url>"); | |
| 245 | 227 | process.exit(2); |
| 246 | 228 | } |
| 247 | const results = await prove(base, fetch, schedules === undefined ? {} : { schedules }); | |
| 229 | const results = await prove(base, fetch); | |
| 248 | 230 | for (const r of results) console.log(line(r)); |
| 249 | 231 | const bad = results.filter((r) => r.status === "fail").length; |
| 250 | 232 | const open = results.filter((r) => r.status === "unproven").length; |
src/actions/health.ts +23 −1
| @@ -3,7 +3,9 @@ | ||
| 3 | 3 | * so anyone may ask; what it is told depends on who it is (#2917): |
| 4 | 4 | * |
| 5 | 5 | * - a stranger learns whether the forge is well and whether it is draining, |
| 6 | * and nothing of the platform's incidents or rollout. It is also told the | |
| 6 | * how many schedules are overdue and by how long the worst one is (counts | |
| 7 | * only, no app or repository: #12512), and nothing of the platform's | |
| 8 | * incidents or rollout. It is also told the | |
| 7 | 9 | * version and commit for one release more — a running forge's swap reads |
| 8 | 10 | * its successor's commit without a probe (`release/swap.ts` before #2917); |
| 9 | 11 | * once that forge is gone the stranger's answer is `ok` alone. |
| @@ -22,6 +24,7 @@ import type { App } from "../app.ts"; | ||
| 22 | 24 | import { appsRuntime } from "../apps/runtime.ts"; |
| 23 | 25 | import { listApps } from "../core/apps.ts"; |
| 24 | 26 | import { ledgerNeed } from "../core/ledger.ts"; |
| 27 | import { overdueSchedules } from "../core/timers.ts"; | |
| 25 | 28 | import type { User } from "../core/users.ts"; |
| 26 | 29 | import { forgeHealth, rolloutLine, type ForgeHealth } from "../kernel/guard.ts"; |
| 27 | 30 | import { |
| @@ -110,6 +113,25 @@ export function withHeavyAskers(v: Vitals, now: number = Date.now()): Vitals { | ||
| 110 | 113 | */ |
| 111 | 114 | export type StrangerHealth = Pick<ForgeHealth, "ok" | "version" | "commit" | "draining">; |
| 112 | 115 | |
| 116 | /** The forge's overdue schedules as counts, on the health answer (#12512). */ | |
| 117 | export interface ScheduleHealth { | |
| 118 | readonly schedules: { readonly overdue: number; readonly oldestSeconds: number }; | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * The health answer the API and MCP `health` give: `readHealth` plus the overdue schedules. | |
| 123 | * Only this answer pays for the schedule read; the asks, home and forge view that read | |
| 124 | * health for its standing do not (#12512). | |
| 125 | */ | |
| 126 | export function healthAnswer( | |
| 127 | app: App, | |
| 128 | actor: User | undefined, | |
| 129 | probe = false, | |
| 130 | now: number = Date.now(), | |
| 131 | ): ReturnType<typeof readHealth> & ScheduleHealth { | |
| 132 | return { ...readHealth(app, actor, probe), schedules: overdueSchedules(app.db, now) }; | |
| 133 | } | |
| 134 | ||
| 113 | 135 | export function readHealth( |
| 114 | 136 | app: App, |
| 115 | 137 | actor: User | undefined, |
src/contract/openapi/routes/getApiV1Health.ts +3 −1
| @@ -3,7 +3,9 @@ import type { Route } from "../shared.ts"; | ||
| 3 | 3 | export const route: Route = { |
| 4 | 4 | method: "get", |
| 5 | 5 | path: "/api/v1/health", |
| 6 | summary: "How the forge is, uncached; anyone may ask, a stranger is told little, the release probe and a site admin the whole of it", | |
| 6 | summary: | |
| 7 | "How the forge is, uncached; anyone may ask, a stranger is told little, the release probe and a site admin the whole of it. Every answer carries schedules {overdue, oldestSeconds}: how many unpaused schedules are more than five minutes past due and the age in seconds of the most overdue (0 when none), counts only, naming no app or repository", | |
| 7 | 8 | auth: false, |
| 8 | 9 | role: "anonymous", |
| 10 | reply: { ok: "boolean", draining: "boolean", schedules: "object" }, | |
| 9 | 11 | }; |
src/core/timers.ts +21
| @@ -102,6 +102,27 @@ export function dueTimers(db: Database, now: number, limit = 50): Timer[] { | ||
| 102 | 102 | return db.query<Row, [number, number]>("SELECT * FROM timer WHERE paused = 0 AND due_at <= ?1 ORDER BY due_at, id LIMIT ?2").all(now, limit).map(toTimer); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | /** | |
| 106 | * How long a schedule may sit past its due instant before it counts as overdue: the firing | |
| 107 | * loop's slack. | |
| 108 | */ | |
| 109 | export const OVERDUE_AFTER_MS = 5 * 60_000; | |
| 110 | ||
| 111 | /** | |
| 112 | * The forge's overdue schedules, counts only (#12512): how many unpaused schedules are due more | |
| 113 | * than `OVERDUE_AFTER_MS` ago, and the age in whole seconds of the most overdue (0 when none). | |
| 114 | * No app, repository or name: a stranger may read it from the health answer. | |
| 115 | */ | |
| 116 | export function overdueSchedules(db: Database, now: number): { overdue: number; oldestSeconds: number } { | |
| 117 | const r = db | |
| 118 | .query<{ n: number; oldest: number | null }, [number]>( | |
| 119 | "SELECT COUNT(*) AS n, MIN(due_at) AS oldest FROM timer WHERE kind = 'schedule' AND paused = 0 AND due_at < ?1", | |
| 120 | ) | |
| 121 | .get(now - OVERDUE_AFTER_MS); | |
| 122 | const n = r?.n ?? 0; | |
| 123 | return { overdue: n, oldestSeconds: n === 0 || r?.oldest == null ? 0 : Math.floor((now - r.oldest) / 1000) }; | |
| 124 | } | |
| 125 | ||
| 105 | 126 | export function getTimer(db: Database, id: number): Timer | undefined { |
| 106 | 127 | const r = db.query<Row, [number]>("SELECT * FROM timer WHERE id = ?1").get(id); |
| 107 | 128 | return r === null ? undefined : toTimer(r); |
src/http/api.ts +2 −2
| @@ -57,7 +57,7 @@ import { streamJson, thoughtStream } from "../core/thought_stream.ts"; | ||
| 57 | 57 | import { PROVE_FIRST } from "../actions/verify_words.ts"; |
| 58 | 58 | import { REPORT_LINK_TITLE } from "../actions/report_words.ts"; |
| 59 | 59 | import { editMessage } from "../actions/edit_words.ts"; |
| 60 | import { readHealth } from "../actions/health.ts"; | |
| 60 | import { healthAnswer } from "../actions/health.ts"; | |
| 61 | 61 | import { isProbe } from "../kernel/probe.ts"; |
| 62 | 62 | import { PRIVATE_ADDRESS_REFUSED } from "../kernel/address_words.ts"; |
| 63 | 63 | import { forgeState, PARENT_HEADER } from "../spawn/clients.ts"; |
| @@ -3509,7 +3509,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3509 | 3509 | router.add("GET", "/api/v1/health", (ctx) => { |
| 3510 | 3510 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 3511 | 3511 | const body = timePhase(ctx.request, "health", () => |
| 3512 | readHealth(ctx.app, ctx.auth.user, isProbe(ctx.request, ctx.app.config.dataDir)), | |
| 3512 | healthAnswer(ctx.app, ctx.auth.user, isProbe(ctx.request, ctx.app.config.dataDir)), | |
| 3513 | 3513 | ); |
| 3514 | 3514 | return timePhase(ctx.request, "render", () => |
| 3515 | 3515 | json(body, 200, { "cache-control": "no-store", vary: "authorization, cookie, x-forge-probe" }), |
src/http/mcp/tools/health.ts +2 −2
| @@ -1,4 +1,4 @@ | ||
| 1 | import { readHealth } from "../../../actions/health.ts"; | |
| 1 | import { healthAnswer } from "../../../actions/health.ts"; | |
| 2 | 2 | import { good, type Tool } from "../shared.ts"; |
| 3 | 3 | |
| 4 | 4 | export const tool: Tool = { |
| @@ -8,5 +8,5 @@ export const tool: Tool = { | ||
| 8 | 8 | "How this forge is: version, rollout, incidents. ok means serving, broken act now, todo would like done.", |
| 9 | 9 | inputSchema: { type: "object", properties: {} }, |
| 10 | 10 | writes: false, |
| 11 | run: (ctx, user) => good(readHealth(ctx.app, user)), | |
| 11 | run: (ctx, user) => good(healthAnswer(ctx.app, user)), | |
| 12 | 12 | }; |
test/health-schedules.test.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** | |
| 2 | * The health answer's overdue-schedule count (#12512): the v1 proof's #11595 probe reads it with | |
| 3 | * no account, so a stranger sees it, as counts only, never an app, repository or schedule name. | |
| 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 { parseSchedules } from "../src/apps/schedule_spec.ts"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createApp as createAppRecord } from "../src/core/apps.ts"; | |
| 12 | import { createRepo } from "../src/core/repos.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { OVERDUE_AFTER_MS, overdueSchedules, setPaused, scheduleTimer, syncSchedules } from "../src/core/timers.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | ||
| 17 | let dataDir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let ownerId = ""; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | dataDir = await mkdtemp(join(tmpdir(), "forge-health-schedules-")); | |
| 24 | app = createApp({ dataDir, port: 0, disk: () => ({ freeBytes: 95, totalBytes: 100 }) }); | |
| 25 | servers = await serve(app); | |
| 26 | const owner = await createUser(app.db, { origin: "seed", trusted: true, username: "secret-owner", email: "[email protected]", password: "a-good-passphrase" }); | |
| 27 | if (!owner.ok) throw new Error(owner.error); | |
| 28 | ownerId = owner.value.id; | |
| 29 | }); | |
| 30 | afterAll(async () => { | |
| 31 | servers.stop(); | |
| 32 | app.close(); | |
| 33 | await rm(dataDir, { recursive: true, force: true }); | |
| 34 | }); | |
| 35 | ||
| 36 | /** | |
| 37 | * A repository `repo-of-<slug>` with one app and one every-minute schedule, armed as if synced | |
| 38 | * at `at`. | |
| 39 | */ | |
| 40 | async function arm(appSlug: string, name: string, at: number): Promise<string> { | |
| 41 | const repo = await createRepo(app.db, app.reposRoot, { ownerId, name: `repo-of-${appSlug}` }); | |
| 42 | if (!repo.ok) throw new Error(repo.error); | |
| 43 | const made = createAppRecord(app.db, { repoId: repo.value.id, slug: appSlug, access: "private" }); | |
| 44 | if (!made.ok) throw new Error(made.error); | |
| 45 | const specs = parseSchedules([{ name, cron: "* * * * *", path: "/jobs/run" }]); | |
| 46 | if (!specs.ok) throw new Error(specs.error); | |
| 47 | syncSchedules(app.db, made.value.id, "production", specs.value, "c1", at); | |
| 48 | return made.value.id; | |
| 49 | } | |
| 50 | ||
| 51 | const strangerHealth = async () => { | |
| 52 | const res = await fetch(servers.url + "/api/v1/health"); | |
| 53 | expect(res.status).toBe(200); | |
| 54 | expect(res.headers.get("cache-control")).toBe("no-store"); | |
| 55 | return res.text(); | |
| 56 | }; | |
| 57 | ||
| 58 | test("no schedule: none overdue", async () => { | |
| 59 | expect(overdueSchedules(app.db, Date.now())).toEqual({ overdue: 0, oldestSeconds: 0 }); | |
| 60 | expect(JSON.parse(await strangerHealth()).schedules).toEqual({ overdue: 0, oldestSeconds: 0 }); | |
| 61 | }); | |
| 62 | ||
| 63 | test("a schedule due inside the loop's slack is not overdue; one past it is, with its age", async () => { | |
| 64 | const now = Date.now(); | |
| 65 | // Armed an hour ago, every minute: due 59 minutes ago, and never fired. | |
| 66 | const appId = await arm("secret-paper", "secret-nightly", now - 3_600_000); | |
| 67 | const due = scheduleTimer(app.db, appId, "secret-nightly", "production")!.dueAt; | |
| 68 | expect(overdueSchedules(app.db, due + OVERDUE_AFTER_MS - 1).overdue).toBe(0); | |
| 69 | const read = overdueSchedules(app.db, now); | |
| 70 | expect(read.overdue).toBe(1); | |
| 71 | expect(read.oldestSeconds).toBe(Math.floor((now - due) / 1000)); | |
| 72 | ||
| 73 | const text = await strangerHealth(); | |
| 74 | const body = JSON.parse(text) as { schedules: { overdue: number; oldestSeconds: number } }; | |
| 75 | expect(Object.keys(body).sort()).toEqual(["commit", "draining", "ok", "schedules", "version"]); | |
| 76 | expect(body.schedules.overdue).toBe(1); | |
| 77 | expect(body.schedules.oldestSeconds).toBeGreaterThanOrEqual(read.oldestSeconds); | |
| 78 | // Counts only: nothing names whose schedule it is. | |
| 79 | for (const word of ["secret-paper", "secret-nightly", "repo-of-secret-paper", "secret-owner", appId, "/jobs/run"]) expect(text).not.toContain(word); | |
| 80 | }); | |
| 81 | ||
| 82 | test("a paused schedule is not overdue", async () => { | |
| 83 | const now = Date.now(); | |
| 84 | const appId = await arm("paused-paper", "weekly", now - 7_200_000); | |
| 85 | const before = overdueSchedules(app.db, now).overdue; | |
| 86 | setPaused(app.db, scheduleTimer(app.db, appId, "weekly", "production")!.id, true, now); | |
| 87 | expect(overdueSchedules(app.db, now).overdue).toBe(before - 1); | |
| 88 | }); |
test/v1-proven.test.ts +22 −26
| @@ -1,7 +1,6 @@ | ||
| 1 | 1 | import { describe, expect, test } from "bun:test"; |
| 2 | import { line, probes, prove, type Fetch } from "../scripts/v1-proven.ts"; | |
| 2 | import { ageWords, line, probes, prove, type Fetch } from "../scripts/v1-proven.ts"; | |
| 3 | 3 | |
| 4 | const NOW = Date.parse("2026-11-14T00:00:00Z"); | |
| 5 | 4 | const HEADERS = { |
| 6 | 5 | "content-security-policy": "default-src 'none'", |
| 7 | 6 | "x-content-type-options": "nosniff", |
| @@ -11,8 +10,8 @@ const HEADERS = { | ||
| 11 | 10 | "cross-origin-opener-policy": "same-origin", |
| 12 | 11 | }; |
| 13 | 12 | const ld = JSON.stringify({ offers: [{ description: "A small forge that sleeps when idle" }, { description: "Small, awake" }] }); |
| 14 | const sched = (last: unknown, nextAt = NOW + 3_600_000) => | |
| 15 | JSON.stringify({ items: [{ name: "nightly", paused: false, nextAt, last }] }); | |
| 13 | const health = (schedules?: unknown) => | |
| 14 | Response.json({ ok: true, version: "1.0.0", commit: "058b6cf", draining: false, ...(schedules === undefined ? {} : { schedules }) }); | |
| 16 | 15 | |
| 17 | 16 | type Over = Record<string, () => Response>; |
| 18 | 17 | function stub(over: Over = {}, seen: string[] = []): Fetch { |
| @@ -26,8 +25,7 @@ function stub(over: Over = {}, seen: string[] = []): Fetch { | ||
| 26 | 25 | new Response(`<form method="post" action="/pricing/order"></form><script type="application/ld+json">${ld}</script>`), |
| 27 | 26 | "/settings/billing": () => new Response(null, { status: 303, headers: { location: "/signin?next=%2Fsettings%2Fbilling" } }), |
| 28 | 27 | "/settings/crew": () => new Response(null, { status: 303, headers: { location: "/signin?next=%2Fsettings%2Fcrew" } }), |
| 29 | "/api/v1/repos/trevato/forge/schedules": () => | |
| 30 | new Response(sched({ at: NOW - 3_600_000, status: "ok" })), | |
| 28 | "/api/v1/health": () => health({ overdue: 0, oldestSeconds: 0 }), | |
| 31 | 29 | ...over, |
| 32 | 30 | }; |
| 33 | 31 | return async (url, init) => { |
| @@ -39,7 +37,7 @@ function stub(over: Over = {}, seen: string[] = []): Fetch { | ||
| 39 | 37 | |
| 40 | 38 | describe("v1-proven", () => { |
| 41 | 39 | test("every list item has a probe, and all pass on a healthy forge", async () => { |
| 42 | const results = await prove("https://f.test/", stub(), { now: NOW }); | |
| 40 | const results = await prove("https://f.test/", stub()); | |
| 43 | 41 | expect(results.map((r) => r.issue).sort()).toEqual( |
| 44 | 42 | [11541, 11539, 11158, 11573, 10304, 11507, 11138, 11140, 11141, 11515, 11595, 10441].sort(), |
| 45 | 43 | ); |
| @@ -54,7 +52,7 @@ describe("v1-proven", () => { | ||
| 54 | 52 | |
| 55 | 53 | test("only reads: every request is a GET", async () => { |
| 56 | 54 | const seen: string[] = []; |
| 57 | await prove("https://f.test", stub({}, seen), { now: NOW }); | |
| 55 | await prove("https://f.test", stub({}, seen)); | |
| 58 | 56 | expect(seen.length).toBeGreaterThan(10); |
| 59 | 57 | expect(seen.every((s) => s.startsWith("GET "))).toBe(true); |
| 60 | 58 | }); |
| @@ -71,33 +69,37 @@ describe("v1-proven", () => { | ||
| 71 | 69 | [11140, { "/pricing": () => new Response('<form method="post" action="/pricing/order"></form><script type="application/ld+json">{"offers":[]}</script>') }, [11140]], |
| 72 | 70 | [11141, { "/settings/billing": () => new Response("x", { status: 404 }) }, [11141]], |
| 73 | 71 | [11515, { "/settings/crew": () => new Response("x", { status: 500 }) }, [11515]], |
| 74 | [11595, { "/api/v1/repos/trevato/forge/schedules": () => new Response(sched({ at: NOW - 3 * 86_400_000, status: "ok" }, NOW - 86_400_000)) }, [11595]], | |
| 72 | [11595, { "/api/v1/health": () => health({ overdue: 2, oldestSeconds: 86_400 }) }, [11595]], | |
| 75 | 73 | [10441, { "/healthz": () => new Response("ok") }, [10441]], |
| 76 | 74 | ]; |
| 77 | 75 | for (const [issue, over, expected] of breaks) |
| 78 | 76 | test(`#${issue} fails, and only it, when its evidence is gone`, async () => { |
| 79 | const results = await prove("https://f.test", stub(over), { now: NOW }); | |
| 77 | const results = await prove("https://f.test", stub(over)); | |
| 80 | 78 | const failed = results.filter((r) => r.status === "fail").map((r) => r.issue); |
| 81 | 79 | expect(failed.sort()).toEqual([...expected].sort()); |
| 82 | 80 | expect(results.find((r) => r.issue === issue)?.detail.length).toBeGreaterThan(0); |
| 83 | 81 | expect(line(results.find((r) => r.issue === issue)!)).toStartWith(`fail #${issue}`); |
| 84 | 82 | }); |
| 85 | 83 | |
| 86 | test("a failed last run, a never-run schedule and a network error are fails", async () => { | |
| 87 | const dead = await prove("https://f.test", stub({ "/api/v1/repos/trevato/forge/schedules": () => new Response("no", { status: 404 }) }), { now: NOW }); | |
| 88 | expect(dead.find((x) => x.issue === 11595)?.status).toBe("unproven"); | |
| 89 | expect(line(dead.find((x) => x.issue === 11595)!)).toStartWith("unproven #11595"); | |
| 90 | for (const body of [sched({ at: NOW - 1000, status: "failed" }), sched(null)]) { | |
| 91 | const r = await prove("https://f.test", stub({ "/api/v1/repos/trevato/forge/schedules": () => new Response(body) }), { now: NOW }); | |
| 92 | expect(r.find((x) => x.issue === 11595)?.status).toBe("fail"); | |
| 84 | test("the schedule probe fails naming the oldest age, is unproven without the field, and a network error is a fail", async () => { | |
| 85 | const late = (await prove("https://f.test", stub({ "/api/v1/health": () => health({ overdue: 2, oldestSeconds: 3 * 3600 + 720 }) }))).find((x) => x.issue === 11595)!; | |
| 86 | expect(line(late)).toBe("fail #11595 no schedule is past its period: 2 schedule(s) overdue, the oldest by 3 h 12 min"); | |
| 87 | for (const old of [health(), health({ overdue: "none" })]) { | |
| 88 | const r = (await prove("https://f.test", stub({ "/api/v1/health": () => old }))).find((x) => x.issue === 11595)!; | |
| 89 | expect(r.status).toBe("unproven"); | |
| 90 | expect(line(r)).toStartWith("unproven #11595"); | |
| 93 | 91 | } |
| 94 | 92 | const down: Fetch = async () => { |
| 95 | 93 | throw new Error("refused"); |
| 96 | 94 | }; |
| 97 | const all = await prove("https://f.test", down, { now: NOW }); | |
| 95 | const all = await prove("https://f.test", down); | |
| 98 | 96 | expect(all.every((r) => r.status === "fail")).toBe(true); |
| 99 | 97 | }); |
| 100 | 98 | |
| 99 | test("an age reads in the unit a person would use", () => { | |
| 100 | expect([ageWords(45), ageWords(600), ageWords(3 * 3600 + 720), ageWords(3 * 86_400 + 7200)]).toEqual(["45 s", "10 min", "3 h 12 min", "3 d 2 h"]); | |
| 101 | }); | |
| 102 | ||
| 101 | 103 | const run = async (answer: Fetch) => { |
| 102 | 104 | const server = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: (req) => answer(req.url) }); |
| 103 | 105 | try { |
| @@ -117,14 +119,8 @@ describe("v1-proven", () => { | ||
| 117 | 119 | }); |
| 118 | 120 | |
| 119 | 121 | test("the script exits 3, not 0, when nothing fails but some items are unproven", async () => { |
| 120 | // The script reads the real clock, so the schedule is fresh; | |
| 121 | // only the two no-public-read items are unproven. | |
| 122 | const { code, out } = await run( | |
| 123 | stub({ | |
| 124 | "/api/v1/repos/trevato/forge/schedules": () => | |
| 125 | new Response(JSON.stringify({ items: [{ name: "nightly", paused: false, nextAt: Date.now() + 3_600_000, last: { at: Date.now() - 3_600_000, status: "ok" } }] })), | |
| 126 | }), | |
| 127 | ); | |
| 122 | // Only the two no-public-read items are unproven. | |
| 123 | const { code, out } = await run(stub()); | |
| 128 | 124 | expect(out).not.toContain("fail #"); |
| 129 | 125 | expect(code).toBe(3); |
| 130 | 126 | expect(out).toContain("unproven #11140"); |
test/vitals.test.ts +2 −2
| @@ -95,7 +95,7 @@ test("a site admin with a read-only token gets the shape; others get the short o | ||
| 95 | 95 | [ |
| 96 | 96 | "accessLog", |
| 97 | 97 | "apps", "at", "broken", "checkedAt", "commit", "degraded", "disk", "draining", "errors", "flakes", |
| 98 | "incidents", "lane", "memory", "needs", "ok", "queue", "rollout", "stale", "stuck", | |
| 98 | "incidents", "lane", "memory", "needs", "ok", "queue", "rollout", "schedules", "stale", "stuck", | |
| 99 | 99 | "todo", "version", |
| 100 | 100 | ].sort(), |
| 101 | 101 | ); |
| @@ -113,7 +113,7 @@ test("a site admin with a read-only token gets the shape; others get the short o | ||
| 113 | 113 | expect(short["rollout"]).toBeDefined(); |
| 114 | 114 | // A stranger is told whether it is well, and no more (#2917). |
| 115 | 115 | const stranger = (await (await health()).json()) as Record<string, unknown>; |
| 116 | expect(Object.keys(stranger).sort()).toEqual(["commit", "draining", "ok", "version"]); | |
| 116 | expect(Object.keys(stranger).sort()).toEqual(["commit", "draining", "ok", "schedules", "version"]); | |
| 117 | 117 | expect((await fetch(servers.url + "/healthz")).status).toBe(200); |
| 118 | 118 | }); |
| 119 | 119 |
new above
offline