Public: anyone can read this; the crew can change it.
A maintenance finding can be quieted for a day from /maintenance, and it comes back on its own unless it is fixed #14585
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14557-a-maintenance-finding-can-be-quieted-for → main 7 commits fork 2 threads · 2 unresolved
12 files
changed +526
−13
docs/features/maintenance.md +8
| @@ -159,3 +159,11 @@ of the slowest file's seconds; it changes nothing. Only the last 512 KB of a log | ||
| 159 | 159 | log was cut the finding says earlier files are missing; where the log has no per-test times, or no |
| 160 | 160 | finished check is stored, the run says so and names nothing it did not read. It shows as the `slow-tests` |
| 161 | 161 | row on `/maintenance`. Test: `test/maintain-slow-tests.test.ts`. |
| 162 | ||
| 163 | ## Quiet a finding for a day (#14557) | |
| 164 | ||
| 165 | Each row of the stale-pull, merged-branch and preview-data cards on `/maintenance` has a small "Quiet for a day" button: a | |
| 166 | same-origin POST for a forge admin who is a person, receipted in the maintenance runs under its own duty name, `quieted-findings`, so no card's duty looks as if it ran | |
| 167 | (`maintenance_quiet` holds the row). The undo is on the Quieted row, not on the receipt or the flash. The finding leaves the card and is listed under "Quieted" with the time it is back and a "Bring back" button | |
| 168 | that undoes it, also receipted. Nothing expires by job: after 24 hours the row is simply in the past, and the finding | |
| 169 | shows again if it still holds. The terminal's maintenance screen does the same for an admin who is a person: `z` quiets a numbered finding, `b` brings a numbered quieted one back, through the same `quiet()`/`unquiet()`. Tests: `test/maintenance-quiet.test.ts`, `test/tui-maintenance-quiet.test.ts`. |
src/core/migration-pins/20261120100000-maintenance-quiet.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | f1832e37e2af7e742f02204c07b96571d3dd9869426bbddaa1568b654db0d0ef |
src/core/migrations/20261120100000-maintenance-quiet.ts added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | // A maintenance finding quieted for a day (#14557): one row per finding (its kind and key), the | |
| 2 | // time it is back, who quieted it and the words it showed. The row is only read against the clock, | |
| 3 | // so expiry needs no job. Additive. | |
| 4 | export default ` | |
| 5 | CREATE TABLE IF NOT EXISTS maintenance_quiet ( | |
| 6 | kind TEXT NOT NULL, | |
| 7 | key TEXT NOT NULL, | |
| 8 | label TEXT NOT NULL, | |
| 9 | until TEXT NOT NULL, | |
| 10 | set_by TEXT NOT NULL, | |
| 11 | created_at TEXT NOT NULL, | |
| 12 | PRIMARY KEY (kind, key) | |
| 13 | ); | |
| 14 | `; |
src/http/server.ts +3 −1
| @@ -45,7 +45,7 @@ import { contributePage } from "../web/pages/contribute.ts"; | ||
| 45 | 45 | import { pipelinePage } from "../web/pages/pipeline.ts"; |
| 46 | 46 | import { adminDataPage, appDataCsv, appDataPage, dataPage } from "../web/pages/data.ts"; |
| 47 | 47 | import { fleetAct, fleetReceipt, fleetUndo } from "../web/pages/fleet-ops.ts"; |
| 48 | import { maintenanceDuty, maintenanceDismiss, maintenancePage, maintenancePrepare, maintenanceResume, staleAction, mergedAction, previewAction } from "../web/pages/maintenance.ts"; | |
| 48 | import { maintenanceDuty, maintenanceDismiss, maintenanceQuiet, maintenanceUnquiet, maintenancePage, maintenancePrepare, maintenanceResume, staleAction, mergedAction, previewAction } from "../web/pages/maintenance.ts"; | |
| 49 | 49 | import { maintenanceBudget } from "../web/pages/maintenance_budgets.ts"; |
| 50 | 50 | import { nightlyOutputPage, resiliencePage } from "../web/pages/resilience.ts"; |
| 51 | 51 | import { agentStartSet, billingLinksSet, limitsPage, limitsSet, quotasSet, signupSet, inviteMint, inviteRevoke } from "../web/pages/admin-limits.ts"; |
| @@ -1170,6 +1170,8 @@ export function buildRouter(): Router<Handler> { | ||
| 1170 | 1170 | router.add("POST", "/maintenance/budget", maintenanceBudget); |
| 1171 | 1171 | router.add("POST", "/maintenance/duties/:name", maintenanceDuty); |
| 1172 | 1172 | router.add("POST", "/maintenance/dismiss", maintenanceDismiss); |
| 1173 | router.add("POST", "/maintenance/quiet", maintenanceQuiet); | |
| 1174 | router.add("POST", "/maintenance/unquiet", maintenanceUnquiet); | |
| 1173 | 1175 | router.add("POST", "/maintenance/stale/:id/:what", staleAction); |
| 1174 | 1176 | router.add("POST", "/maintenance/branches/delete", mergedAction); |
| 1175 | 1177 | router.add("POST", "/maintenance/previews/remove", previewAction); |
src/maintenance/quiet.ts added +73
| @@ -0,0 +1,73 @@ | ||
| 1 | /** | |
| 2 | * A finding quieted for a day (#14557): the owner clears a known finding off /maintenance without | |
| 3 | * losing it. Rows only, keyed by the kind of finding and its key; a finding is hidden while its | |
| 4 | * `until` is in the future and is simply back after, if it still holds. Each change is receipted | |
| 5 | * in `maintenance_run`. Plain data in, plain data out. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { record } from "../maintain/runs.ts"; | |
| 10 | ||
| 11 | export const QUIET_MS = 24 * 3_600_000; | |
| 12 | /** The receipts carry their own duty name, so no card's duty looks as if it ran. */ | |
| 13 | export const RECEIPT_DUTY = "quieted-findings"; | |
| 14 | /** The finding cards that can be quieted. */ | |
| 15 | export const QUIET_KINDS = ["stale-pulls", "merged-branches", "preview-dirs"] as const; | |
| 16 | export type QuietKind = (typeof QUIET_KINDS)[number]; | |
| 17 | ||
| 18 | export interface Quieted { | |
| 19 | readonly kind: QuietKind; | |
| 20 | readonly key: string; | |
| 21 | readonly label: string; | |
| 22 | readonly until: string; | |
| 23 | readonly setBy: string; | |
| 24 | } | |
| 25 | ||
| 26 | export const isKind = (v: unknown): v is QuietKind => typeof v === "string" && (QUIET_KINDS as readonly string[]).includes(v); | |
| 27 | ||
| 28 | /** `YYYY-MM-DD HH:MM UTC`, the page's clock. */ | |
| 29 | export const when = (iso: string): string => `${iso.slice(0, 10)} ${iso.slice(11, 16)} UTC`; | |
| 30 | ||
| 31 | /** Every finding quieted now, soonest back first. */ | |
| 32 | export function quieted(db: Database, now: Date = new Date()): Quieted[] { | |
| 33 | return db | |
| 34 | .query<{ kind: QuietKind; key: string; label: string; until: string; set_by: string }, [string]>( | |
| 35 | "SELECT kind, key, label, until, set_by FROM maintenance_quiet WHERE until > ?1 ORDER BY until, kind, key", | |
| 36 | ) | |
| 37 | .all(now.toISOString()) | |
| 38 | .map((r) => ({ kind: r.kind, key: r.key, label: r.label, until: r.until, setBy: r.set_by })); | |
| 39 | } | |
| 40 | ||
| 41 | /** The keys quieted now for one kind. */ | |
| 42 | export function quietKeys(all: readonly Quieted[], kind: QuietKind): Set<string> { | |
| 43 | return new Set(all.filter((q) => q.kind === kind).map((q) => q.key)); | |
| 44 | } | |
| 45 | ||
| 46 | export type QuietResult = { ok: true; says: string } | { ok: false; says: string }; | |
| 47 | ||
| 48 | /** | |
| 49 | * Quiet one finding for a day; `label` is what it said. The caller has checked the finding is live. | |
| 50 | */ | |
| 51 | export function quiet(db: Database, kind: unknown, key: unknown, label: string, by: string, now: Date = new Date()): QuietResult { | |
| 52 | if (!isKind(kind)) return { ok: false, says: "No such kind of finding." }; | |
| 53 | if (typeof key !== "string" || key === "" || key.length > 400) return { ok: false, says: "No such finding." }; | |
| 54 | const until = new Date(now.getTime() + QUIET_MS).toISOString(); | |
| 55 | const shown = label.slice(0, 200); | |
| 56 | db.query( | |
| 57 | `INSERT INTO maintenance_quiet (kind, key, label, until, set_by, created_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6) | |
| 58 | ON CONFLICT(kind, key) DO UPDATE SET label = ?3, until = ?4, set_by = ?5, created_at = ?6`, | |
| 59 | ).run(kind, key, shown, until, by, now.toISOString()); | |
| 60 | const says = `quieted ${shown} until ${when(until)} (pressed by ${by}); it comes back then if it still holds; bring it back sooner from Quieted, not from this receipt`; | |
| 61 | record(db, { duty: RECEIPT_DUTY, started: now, finished: now, findings: [], actions: [says], outcome: "ok" }); | |
| 62 | return { ok: true, says: says[0]!.toUpperCase() + says.slice(1) + "." }; | |
| 63 | } | |
| 64 | ||
| 65 | /** Bring a quieted finding back now. */ | |
| 66 | export function unquiet(db: Database, kind: unknown, key: unknown, by: string, now: Date = new Date()): QuietResult { | |
| 67 | if (!isKind(kind) || typeof key !== "string") return { ok: false, says: "No such finding." }; | |
| 68 | const row = db.query<{ label: string }, [string, string, string]>("DELETE FROM maintenance_quiet WHERE kind = ?1 AND key = ?2 AND until > ?3 RETURNING label").get(kind, key, now.toISOString()); | |
| 69 | if (row === null) return { ok: false, says: "That finding is not quieted." }; | |
| 70 | const says = `brought back ${row.label} (pressed by ${by})`; | |
| 71 | record(db, { duty: RECEIPT_DUTY, started: now, finished: now, findings: [], actions: [says], outcome: "ok" }); | |
| 72 | return { ok: true, says: `Brought back ${row.label}.` }; | |
| 73 | } |
src/maintenance/quiet_live.ts added +34
| @@ -0,0 +1,34 @@ | ||
| 1 | /** | |
| 2 | * The findings on the three cards that can be quieted (#14557), as plain rows: the one place that | |
| 3 | * says which findings are live and what each is called, for the page and the terminal alike. | |
| 4 | */ | |
| 5 | ||
| 6 | import type { App } from "../app.ts"; | |
| 7 | import { lineOf, mergedBranches } from "../maintain/duties/merged_branches.ts"; | |
| 8 | import { previewDirs, previewLine } from "../maintain/previews.ts"; | |
| 9 | import { stalePulls } from "../maintain/stale_pulls.ts"; | |
| 10 | import { quietKeys, type Quieted, type QuietKind } from "./quiet.ts"; | |
| 11 | ||
| 12 | export interface Live { | |
| 13 | readonly kind: QuietKind; | |
| 14 | readonly key: string; | |
| 15 | readonly label: string; | |
| 16 | } | |
| 17 | ||
| 18 | /** Every finding on the three cards now, not counting what is quieted. */ | |
| 19 | export async function liveFindings(app: App, now: Date, hidden: readonly Quieted[]): Promise<Live[]> { | |
| 20 | const skip = (kind: QuietKind): Set<string> => quietKeys(hidden, kind); | |
| 21 | const pulls = skip("stale-pulls"); | |
| 22 | const branches = skip("merged-branches"); | |
| 23 | const previews = skip("preview-dirs"); | |
| 24 | return [ | |
| 25 | ...stalePulls(app.db, now).filter((p) => !pulls.has(p.id)).map((p): Live => ({ kind: "stale-pulls", key: p.id, label: `${p.repo}#${String(p.number)}` })), | |
| 26 | ...(await mergedBranches(app, now)).filter((b) => !branches.has(lineOf(b))).map((b): Live => ({ kind: "merged-branches", key: lineOf(b), label: `${b.repo}:${b.branch}` })), | |
| 27 | ...previewDirs(app, now).filter((p) => !previews.has(previewLine(p))).map((p): Live => ({ kind: "preview-dirs", key: previewLine(p), label: previewLine(p) })), | |
| 28 | ]; | |
| 29 | } | |
| 30 | ||
| 31 | /** The label of one finding that is live now, or undefined. */ | |
| 32 | export async function liveLabel(app: App, kind: QuietKind, key: string, now: Date = new Date()): Promise<string | undefined> { | |
| 33 | return (await liveFindings(app, now, [])).find((l) => l.kind === kind && l.key === key)?.label; | |
| 34 | } |
src/perf/page_budgets/maintenance.ts +3 −3
| @@ -2,6 +2,6 @@ import type { PageBudget } from "../page_budget.ts"; | ||
| 2 | 2 | |
| 3 | 3 | // The admin board of duties (#13388): walked as the admin, stored rows only. Each duty reads its |
| 4 | 4 | // own state and runs, so the count moves with the number of duties and background ticks; an N+1 |
| 5 | // over the duties would still show. One query reads every dismissal (#13967); one grouped read of | |
| 6 | // the degraded findings (#14577). | |
| 7 | export const budget: PageBudget = { page: "maintenance", pattern: "/maintenance", medianMs: 50, statements: 82 }; | |
| 5 | // over the duties would still show. One query reads every dismissal (#13967), one every quieted | |
| 6 | // finding (#14557); one grouped read of the degraded findings (#14577). | |
| 7 | export const budget: PageBudget = { page: "maintenance", pattern: "/maintenance", medianMs: 50, statements: 83 }; |
src/tui/screens/maintenance.ts +57 −2
| @@ -18,6 +18,8 @@ import type { Session } from "../session.ts"; | ||
| 18 | 18 | import { wrap } from "../term/width.ts"; |
| 19 | 19 | import { dialog } from "../ui/widgets.ts"; |
| 20 | 20 | import { confirmScreen, later, promptScreen, runAsync } from "./common.ts"; |
| 21 | import { quiet as quietFinding, quieted, unquiet, when } from "../../maintenance/quiet.ts"; | |
| 22 | import { liveFindings, type Live } from "../../maintenance/quiet_live.ts"; | |
| 21 | 23 | import type { PaletteEntry } from "./entries.ts"; |
| 22 | 24 | |
| 23 | 25 | const WIDTH = 72; |
| @@ -51,7 +53,18 @@ export function maintenanceLines(f: Session, standby?: Standby): string[] { | ||
| 51 | 53 | ...loud.flatMap((d) => wrap(filedText(f.app.db, d, f.user), INNER)), |
| 52 | 54 | ...(quiet.length === 0 ? [] : wrap(`${quietLine(quiet.length, loud.length === 0)}: ${quiet.map((d) => d.name).join(", ")}`, INNER)), |
| 53 | 55 | ]; |
| 54 | return [...wrap(s.says, INNER), "", ...facts, ...checks, "", ...dutyLines, ...findingLines(f.app.db).flatMap((l) => wrap(l, INNER))]; | |
| 56 | // Findings quieted (#14557): numbered, so `b` can bring one back; when each is back. | |
| 57 | const hidden = quieted(f.app.db).flatMap((q, i) => wrap(`${String(i + 1)}. quieted until ${when(q.until)}: ${q.label}`, INNER)); | |
| 58 | return [ | |
| 59 | ...wrap(s.says, INNER), | |
| 60 | "", | |
| 61 | ...facts, | |
| 62 | ...checks, | |
| 63 | "", | |
| 64 | ...dutyLines, | |
| 65 | ...findingLines(f.app.db).flatMap((l) => wrap(l, INNER)), | |
| 66 | ...(hidden.length === 0 ? [] : ["", ...hidden]), | |
| 67 | ]; | |
| 55 | 68 | } |
| 56 | 69 | |
| 57 | 70 | export function maintenanceScreen(f: Session, ctx: Ctx): Screen { |
| @@ -65,6 +78,14 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 65 | 78 | }); |
| 66 | 79 | learnStandby(); |
| 67 | 80 | let note = ""; |
| 81 | // The findings on the three cards, numbered for `z`; read again after each press. | |
| 82 | let live: Live[] = []; | |
| 83 | const learnLive = (): void => | |
| 84 | runAsync(ctx, async () => { | |
| 85 | live = await liveFindings(f.app, new Date(), quieted(f.app.db)); | |
| 86 | ctx.invalidate(); | |
| 87 | }); | |
| 88 | learnLive(); | |
| 68 | 89 | const say = (text: string): void => { |
| 69 | 90 | note = text; |
| 70 | 91 | ctx.invalidate(); |
| @@ -121,6 +142,33 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 121 | 142 | }, |
| 122 | 143 | }); |
| 123 | 144 | |
| 145 | /** Ask for a number from 1 to `count`, then go on with the one chosen. */ | |
| 146 | const askNumber = (title: string, label: string, count: number, then: (i: number) => void): Screen => | |
| 147 | promptScreen(ctx, screen, { | |
| 148 | title, | |
| 149 | label, | |
| 150 | hint: "Enter does it.", | |
| 151 | onSubmit: (v) => { | |
| 152 | const n = Number(v.trim()); | |
| 153 | if (!Number.isInteger(n) || n < 1 || n > count) return `A number from 1 to ${String(count)}.`; | |
| 154 | then(n - 1); | |
| 155 | return undefined; | |
| 156 | }, | |
| 157 | }); | |
| 158 | ||
| 159 | const doQuiet = (l: Live): void => { | |
| 160 | const r = quietFinding(f.app.db, l.kind, l.key, l.label, f.user?.username ?? ""); | |
| 161 | live = live.filter((x) => x !== l); | |
| 162 | say(r.says); | |
| 163 | }; | |
| 164 | const doBringBack = (i: number): void => { | |
| 165 | const q = quieted(f.app.db)[i]; | |
| 166 | if (q === undefined) return say("That finding is not quieted."); | |
| 167 | const r = unquiet(f.app.db, q.kind, q.key, f.user?.username ?? ""); | |
| 168 | say(r.says); | |
| 169 | learnLive(); | |
| 170 | }; | |
| 171 | ||
| 124 | 172 | const screen: Screen = { |
| 125 | 173 | title: "Maintenance", |
| 126 | 174 | get keys() { |
| @@ -131,13 +179,18 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 131 | 179 | : idle |
| 132 | 180 | ? [binding(["p"], "prepare for maintenance")] |
| 133 | 181 | : [binding(["c"], "check again, retake the backup"), binding(["e"], "end maintenance")]), |
| 182 | ...(!mayAct() || busy ? [] : [...(live.length > 0 ? [binding(["z"], "quiet a finding for a day")] : []), ...(quieted(f.app.db).length > 0 ? [binding(["b"], "bring a quieted finding back")] : [])]), | |
| 134 | 183 | binding(["enter", "esc", "q"], "close"), |
| 135 | 184 | ]; |
| 136 | 185 | }, |
| 137 | 186 | render(area, s) { |
| 138 | 187 | const buttons = dialog(s, area, ctx.theme.c, { |
| 139 | 188 | title: "Maintenance", |
| 140 | body: [...(note === "" ? [] : [...wrap(note, INNER), ""]), ...maintenanceLines(f, standby)], | |
| 189 | body: [ | |
| 190 | ...(note === "" ? [] : [...wrap(note, INNER), ""]), | |
| 191 | ...maintenanceLines(f, standby), | |
| 192 | ...(live.length === 0 ? [] : ["", "Findings (z quiets one):", ...live.flatMap((l, i) => wrap(`${String(i + 1)}. ${l.label} (${l.kind})`, INNER))]), | |
| 193 | ], | |
| 141 | 194 | actions: [{ label: "Close" }], |
| 142 | 195 | selected: 0, |
| 143 | 196 | width: WIDTH, |
| @@ -152,6 +205,8 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 152 | 205 | else if (e.key === "p" && idle) ctx.push(askPrepare()); |
| 153 | 206 | else if (e.key === "c" && !idle) doPrepare(""); |
| 154 | 207 | else if (e.key === "e" && !idle) ctx.push(askEnd()); |
| 208 | else if (e.key === "z" && live.length > 0) ctx.push(askNumber("Quiet a finding", "Number of the finding, quieted for a day", live.length, (i) => live[i] && doQuiet(live[i]!))); | |
| 209 | else if (e.key === "b" && quieted(f.app.db).length > 0) ctx.push(askNumber("Bring a finding back", "Number under quieted", quieted(f.app.db).length, doBringBack)); | |
| 155 | 210 | return true; |
| 156 | 211 | }, |
| 157 | 212 | }; |
src/web/pages/maintenance.ts +58 −6
| @@ -22,6 +22,8 @@ import { badge, btn, disclosure, field } from "../ui.ts"; | ||
| 22 | 22 | import { STALE_DAYS, STALE_WORDS, closeStale, nudge, stalePulls } from "../../maintain/stale_pulls.ts"; |
| 23 | 23 | import { PREVIEW_DAYS, PREVIEW_WORDS, previewDirs, previewLine, removePreviewDirs, sizeWords, totalWords, type PreviewDir } from "../../maintain/previews.ts"; |
| 24 | 24 | import { MERGED_DAYS, MERGED_WORDS, deleteMerged, lineOf, mergedBranches, type MergedBranch } from "../../maintain/duties/merged_branches.ts"; |
| 25 | import { isKind, quiet, quieted, quietKeys, unquiet, when, type QuietKind, type Quieted } from "../../maintenance/quiet.ts"; | |
| 26 | import { liveLabel } from "../../maintenance/quiet_live.ts"; | |
| 25 | 27 | import { writePauseFold } from "./maintenance_pause.ts"; |
| 26 | 28 | import { budgetRows } from "./maintenance_budgets.ts"; |
| 27 | 29 | import { statusRow } from "./security.ts"; |
| @@ -83,9 +85,10 @@ export async function maintenancePage(ctx: Ctx): Promise<Response> { | ||
| 83 | 85 | const { quiet, loud } = splitDuties(ran); |
| 84 | 86 | const dismissed = dismissals(ctx.app.db); |
| 85 | 87 | const states = dutyStates(ctx.app.db); |
| 86 | const mergedPending = await mergedBranches(ctx.app, new Date()); | |
| 87 | const previewPending = previewDirs(ctx.app, new Date()); | |
| 88 | const stalePending = stalePulls(ctx.app.db, new Date()); | |
| 88 | const hidden = quieted(ctx.app.db); | |
| 89 | const mergedPending = (await mergedBranches(ctx.app, new Date())).filter((b) => !quietKeys(hidden, "merged-branches").has(lineOf(b))); | |
| 90 | const previewPending = previewDirs(ctx.app, new Date()).filter((p) => !quietKeys(hidden, "preview-dirs").has(previewLine(p))); | |
| 91 | const stalePending = stalePulls(ctx.app.db, new Date()).filter((p) => !quietKeys(hidden, "stale-pulls").has(p.id)); | |
| 89 | 92 | const stale = stalePending.length; |
| 90 | 93 | const overdue = ran.filter((d) => d.overdue).length; |
| 91 | 94 | const dismissable = states.filter((d) => !d.paused && dismissed.get(d.name)?.active !== true).map((d) => d.name); |
| @@ -128,6 +131,9 @@ export async function maintenancePage(ctx: Ctx): Promise<Response> { | ||
| 128 | 131 | ${previewPending.length > 0 |
| 129 | 132 | ? fold("preview-dirs", `Preview data of pulls closed over ${String(PREVIEW_DAYS)} days ago`, `${String(previewPending.length)}, ${totalWords(previewPending)}`, true, previewRows(ctx.auth.user!.kind !== "agent", previewPending)) |
| 130 | 133 | : null} |
| 134 | ${hidden.length > 0 | |
| 135 | ? fold("maintenance-quieted", "Quieted", String(hidden.length), false, quietedRows(ctx.auth.user!.kind !== "agent", hidden)) | |
| 136 | : null} | |
| 131 | 137 | ${idle |
| 132 | 138 | ? fold("maintenance-prepare", "Prepare for a power-off", `${String(ABOUT_TO_STOP_ROWS.length)} things stop`, false, |
| 133 | 139 | html`<p id="maintenance-standby" class="muted">${s.standby.says}</p> |
| @@ -206,6 +212,52 @@ export async function maintenanceDismiss(ctx: Ctx): Promise<Response> { | ||
| 206 | 212 | return flashTo(ctx, "/maintenance", r.ok ? { message: r.says } : { tone: "danger", message: r.says }); |
| 207 | 213 | } |
| 208 | 214 | |
| 215 | /** The button on a finding's row: quiet it for a day. */ | |
outdated
Principles 3, 6 and 7: one outline button on every row of three cards makes the rows busy. Use the same quiet button style as Ask and Close. |
||
| 216 | const quietForm = (kind: QuietKind, key: string): Html => | |
| 217 | html`<form method="post" action="/maintenance/quiet" class="inline"><input type="hidden" name="kind" value="${kind}"><input type="hidden" name="key" value="${key}">${btn("Quiet for a day", { variant: "ghost", size: "sm" })}</form>`; | |
| 218 | ||
| 219 | /** What is quieted, each row with when it is back and the button that brings it back now. */ | |
| 220 | function quietedRows(withActions: boolean, rows: readonly Quieted[]): Html { | |
| 221 | return html`<dl id="maintenance-quieted-list" class="status-rows">${rows.map( | |
| 222 | (q) => html`<div class="status-row" data-quieted="${q.kind}:${q.key}"><dt>${q.label}</dt> | |
| 223 | <dd>quieted until ${when(q.until)} <span class="muted">by ${q.setBy}</span> | |
| 224 | ${withActions ? html`<form method="post" action="/maintenance/unquiet" class="inline"><input type="hidden" name="kind" value="${q.kind}"><input type="hidden" name="key" value="${q.key}">${btn("Bring back", { variant: "ghost", size: "sm" })}</form>` : null}</dd></div>`, | |
| 225 | )}</dl>`; | |
| 226 | } | |
| 227 | ||
| 228 | /** Who may press: a forge admin who is a person, the gate the finding cards' buttons have. */ | |
| 229 | function mayQuiet(ctx: Ctx): Response | undefined { | |
| 230 | const user = ctx.auth.user; | |
| 231 | if (user === undefined) return signInFirst(ctx); | |
| 232 | if (!user.isAdmin) return notFoundPage(ctx); | |
| 233 | if (user.kind === "agent") return flashTo(ctx, "/maintenance", { tone: "danger", message: "Only a person may quiet a finding." }); | |
| 234 | return undefined; | |
| 235 | } | |
| 236 | ||
| 237 | /** POST /maintenance/quiet (fields kind, key): hide one live finding for 24 hours, receipted. */ | |
| 238 | export async function maintenanceQuiet(ctx: Ctx): Promise<Response> { | |
| 239 | const denied = mayQuiet(ctx); | |
| 240 | if (denied) return denied; | |
| 241 | const form = await readForm(ctx, "admin"); | |
| 242 | if (form instanceof Response) return form; | |
| 243 | const kind = formField(form, "kind"); | |
| 244 | const key = formField(form, "key"); | |
| 245 | const label = isKind(kind) && key.length > 0 && key.length <= 400 ? await liveLabel(ctx.app, kind, key) : undefined; | |
| 246 | if (label === undefined) return flashTo(ctx, "/maintenance", { tone: "danger", message: "That finding is gone." }); | |
| 247 | const r = quiet(ctx.app.db, kind, key, label, ctx.auth.user!.username); | |
| 248 | return flashTo(ctx, "/maintenance", r.ok ? { message: r.says } : { tone: "danger", message: r.says }); | |
| 249 | } | |
| 250 | ||
| 251 | /** POST /maintenance/unquiet (fields kind, key): undo a quiet now, receipted. */ | |
| 252 | export async function maintenanceUnquiet(ctx: Ctx): Promise<Response> { | |
| 253 | const denied = mayQuiet(ctx); | |
| 254 | if (denied) return denied; | |
| 255 | const form = await readForm(ctx, "admin"); | |
| 256 | if (form instanceof Response) return form; | |
| 257 | const r = unquiet(ctx.app.db, formField(form, "kind"), formField(form, "key"), ctx.auth.user!.username); | |
| 258 | return flashTo(ctx, "/maintenance", r.ok ? { message: r.says } : { tone: "danger", message: r.says }); | |
| 259 | } | |
| 260 | ||
| 209 | 261 | /** The stale pulls as rows with their age; the buttons only for a forge admin who is a person. */ |
| 210 | 262 | function staleRows(withActions: boolean, pulls: ReturnType<typeof stalePulls>): Html { |
| 211 | 263 | const form = (id: string, what: "nudge" | "close", label: string): Html => |
| @@ -214,7 +266,7 @@ function staleRows(withActions: boolean, pulls: ReturnType<typeof stalePulls>): | ||
| 214 | 266 | return html`<dl class="status-rows">${pulls.map( |
| 215 | 267 | (p) => html`<div class="status-row" data-pull="${p.repo}#${String(p.number)}"><dt><a href="/${p.repo}/pulls/${String(p.number)}">${p.repo}#${String(p.number)}</a></dt> |
| 216 | 268 | <dd>${p.title} <span class="muted">${String(p.ageDays)} days</span> |
| 217 | ${withActions ? html`${form(p.id, "nudge", "Ask")}${p.byAgent ? form(p.id, "close", "Close") : null}` : null}</dd></div>`, | |
| 269 | ${withActions ? html`${form(p.id, "nudge", "Ask")}${p.byAgent ? form(p.id, "close", "Close") : null}${quietForm("stale-pulls", p.id)}` : null}</dd></div>`, | |
| 218 | 270 | )}</dl>`; |
| 219 | 271 | } |
| 220 | 272 | |
| @@ -245,7 +297,7 @@ export async function staleAction(ctx: Ctx): Promise<Response> { | ||
| 245 | 297 | */ |
| 246 | 298 | function mergedRows(withActions: boolean, found: readonly MergedBranch[]): Html { |
| 247 | 299 | return html`<dl class="status-rows">${found.map( |
| 248 | (b) => html`<div class="status-row" data-branch="${b.repo}:${b.branch}"><dt>${b.repo}:${b.branch}</dt><dd><code>${b.head.slice(0, 12)}</code> <span class="muted">merged ${b.mergedAt.slice(0, 10)}</span></dd></div>`, | |
| 300 | (b) => html`<div class="status-row" data-branch="${b.repo}:${b.branch}"><dt>${b.repo}:${b.branch}</dt><dd><code>${b.head.slice(0, 12)}</code> <span class="muted">merged ${b.mergedAt.slice(0, 10)}</span>${withActions ? quietForm("merged-branches", lineOf(b)) : null}</dd></div>`, | |
| 249 | 301 | )}</dl> |
| 250 | 302 | ${withActions |
| 251 | 303 | ? html`<form method="post" action="/maintenance/branches/delete" class="stack"> |
| @@ -278,7 +330,7 @@ export async function mergedAction(ctx: Ctx): Promise<Response> { | ||
| 278 | 330 | */ |
| 279 | 331 | function previewRows(withActions: boolean, found: readonly PreviewDir[]): Html { |
| 280 | 332 | return html`<dl class="status-rows">${found.map( |
| 281 | (p) => html`<div class="status-row" data-preview="${previewLine(p)}"><dt>${previewLine(p)}</dt><dd>${sizeWords(p.bytes)} <span class="muted">closed ${p.closedAt.slice(0, 10)}</span></dd></div>`, | |
| 333 | (p) => html`<div class="status-row" data-preview="${previewLine(p)}"><dt>${previewLine(p)}</dt><dd>${sizeWords(p.bytes)} <span class="muted">closed ${p.closedAt.slice(0, 10)}</span>${withActions ? quietForm("preview-dirs", previewLine(p)) : null}</dd></div>`, | |
| 282 | 334 | )}</dl> |
| 283 | 335 | ${withActions |
| 284 | 336 | ? html`<form method="post" action="/maintenance/previews/remove" class="stack"> |
test/maintenance-quiet.test.ts added +182
| @@ -0,0 +1,182 @@ | ||
| 1 | /** A finding quieted for a day (#14557): hidden, listed, back after 24 h, undone, and gated. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdirSync, writeFileSync } from "node:fs"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createBranch } from "../src/actions/branches.ts"; | |
| 8 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 9 | import { appsRoot } from "../src/apps/data.ts"; | |
| 10 | import { resolveOid } from "../src/git/merge.ts"; | |
| 11 | import { repoDir } from "../src/git/repos.ts"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { createRepo, type Repo } from "../src/core/repos.ts"; | |
| 14 | import { createUser, type User } from "../src/core/users.ts"; | |
| 15 | import { id } from "../src/kernel/id.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | import { QUIET_MS, quiet, quieted, unquiet } from "../src/maintenance/quiet.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | let dir = "", app: App, servers: Servers, admin: User, person: User, repo: Repo, seq = 0; | |
| 22 | let adminCookie = "", personCookie = "", agentCookie = "", appId = ""; | |
| 23 | ||
| 24 | async function user(name: string, isAdmin: boolean): Promise<User> { | |
| 25 | const made = await createUser(app.db, { username: name, email: `${name}@forge.test`, password: "a-good-passphrase", origin: "seed", trusted: true, ...(isAdmin ? { isAdmin: true } : {}) }); | |
| 26 | if (!made.ok) throw new Error(name); | |
| 27 | return made.value; | |
| 28 | } | |
| 29 | async function login(username: string): Promise<string> { | |
| 30 | const res = await fetch(`${servers.url}/signin`, { | |
| 31 | method: "POST", | |
| 32 | redirect: "manual", | |
| 33 | headers: { "content-type": "application/x-www-form-urlencoded", origin: servers.url }, | |
| 34 | body: new URLSearchParams({ username, password: "a-good-passphrase" }), | |
| 35 | }); | |
| 36 | return (res.headers.get("set-cookie") ?? "").split(";")[0] ?? ""; | |
| 37 | } | |
| 38 | function stalePull(author: User): { id: string; n: number } { | |
| 39 | const issue = id(); | |
| 40 | const n = ++seq; | |
| 41 | const at = new Date(Date.now() - 30 * 86_400_000).toISOString(); | |
| 42 | app.db.query("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at) VALUES (?1, ?2, ?3, 'pull', ?4, '', ?5, 'open', ?6, ?6)").run(issue, repo.id, n, `Findable ${n}`, author.id, at); | |
| 43 | app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id) VALUES (?1, 'main', ?2, ?3, ?3, ?4)").run(issue, `b/${n}`, "0".repeat(40), repo.id); | |
| 44 | return { id: issue, n }; | |
| 45 | } | |
| 46 | const post = (path: string, cookie: string, fields: Record<string, string>, origin = servers.url): Promise<Response> => | |
| 47 | fetch(`${servers.url}${path}`, { method: "POST", redirect: "manual", headers: { "content-type": "application/x-www-form-urlencoded", origin, cookie }, body: new URLSearchParams(fields) }); | |
| 48 | const view = async (): Promise<string> => (await fetch(`${servers.url}/maintenance`, { headers: { cookie: adminCookie } })).text(); | |
| 49 | ||
| 50 | beforeAll(async () => { | |
| 51 | dir = await mkdtemp(join(tmpdir(), "forge-quiet-")); | |
| 52 | app = createApp({ dataDir: dir, port: 0 }); | |
| 53 | servers = await serve(app); | |
| 54 | admin = await user("boss", true); | |
| 55 | person = await user("pat", false); | |
| 56 | const made = await createRepo(app.db, app.reposRoot, { ownerId: admin.id, name: "r", visibility: "public", description: "r" }); | |
| 57 | if (!made.ok) throw new Error("repo"); | |
| 58 | repo = made.value; | |
| 59 | const robo = await user("robo", true); | |
| 60 | app.db.query("UPDATE user SET is_agent = 1 WHERE id = ?1").run(robo.id); | |
| 61 | appId = id().toLowerCase().replace(/[^0-9a-z]/g, ""); | |
| 62 | app.db.query("INSERT INTO app (id, repo_id, slug, access) VALUES (?1, ?2, 'r', 'public')").run(appId, repo.id); | |
| 63 | const seeded = await commitToBranch(app, admin, repo, { message: "seed", files: [{ path: "a.txt", content: "a\n" }] }); | |
| 64 | if (!seeded.ok) throw new Error("seed"); | |
| 65 | agentCookie = await login("robo"); | |
| 66 | adminCookie = await login("boss"); | |
| 67 | personCookie = await login("pat"); | |
| 68 | }); | |
| 69 | afterAll(async () => { | |
| 70 | servers.stop(); | |
| 71 | app.close(); | |
| 72 | await rm(dir, { recursive: true, force: true }); | |
| 73 | }); | |
| 74 | ||
| 75 | test("quiet hides the finding, lists it under Quieted with its time, and the receipt is recorded; bring back undoes it", async () => { | |
| 76 | const p = stalePull(person); | |
| 77 | expect(await view()).toContain(`data-pull="boss/r#${p.n}"`); | |
| 78 | const r = await post("/maintenance/quiet", adminCookie, { kind: "stale-pulls", key: p.id }); | |
| 79 | expect(r.status).toBe(303); | |
| 80 | const after = await view(); | |
| 81 | expect(after).not.toContain(`data-pull="boss/r#${p.n}"`); | |
| 82 | expect(after).toContain(`data-quieted="stale-pulls:${p.id}"`); | |
| 83 | expect(after).toContain("quieted until"); | |
| 84 | const run = app.db.query<{ actions: string }, []>("SELECT actions FROM maintenance_run WHERE duty = 'quieted-findings' ORDER BY id DESC LIMIT 1").get(); | |
| 85 | expect(run?.actions).toContain("quieted boss/r#"); | |
| 86 | await post("/maintenance/unquiet", adminCookie, { kind: "stale-pulls", key: p.id }); | |
| 87 | const back = await view(); | |
| 88 | expect(back).toContain(`data-pull="boss/r#${p.n}"`); | |
| 89 | expect(back).not.toContain("data-quieted="); | |
| 90 | }); | |
| 91 | ||
| 92 | test("after 24 hours it is back if it still holds, and gone from the list", () => { | |
| 93 | const t0 = new Date("2026-11-20T10:00:00Z"); | |
| 94 | expect(quiet(app.db, "stale-pulls", "k-expiry", "x#1", "boss", t0).ok).toBe(true); | |
| 95 | expect(quieted(app.db, new Date(t0.getTime() + QUIET_MS - 1)).some((q) => q.key === "k-expiry")).toBe(true); | |
| 96 | expect(quieted(app.db, new Date(t0.getTime() + QUIET_MS)).some((q) => q.key === "k-expiry")).toBe(false); | |
| 97 | expect(unquiet(app.db, "stale-pulls", "k-expiry", "boss", new Date(t0.getTime() + QUIET_MS)).ok).toBe(false); | |
| 98 | }); | |
| 99 | ||
| 100 | test("a cross-origin POST is refused and quiets nothing", async () => { | |
| 101 | const p = stalePull(person); | |
| 102 | const r = await post("/maintenance/quiet", adminCookie, { kind: "stale-pulls", key: p.id }, "https://evil.example"); | |
| 103 | expect(r.status).toBeGreaterThanOrEqual(400); | |
| 104 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(false); | |
| 105 | }); | |
| 106 | ||
| 107 | test("a visitor and a signed-in non-admin are refused", async () => { | |
| 108 | const p = stalePull(person); | |
| 109 | const anon = await post("/maintenance/quiet", "", { kind: "stale-pulls", key: p.id }); | |
| 110 | expect(anon.status).not.toBe(200); | |
| 111 | const pat = await post("/maintenance/quiet", personCookie, { kind: "stale-pulls", key: p.id }); | |
| 112 | expect([403, 404]).toContain(pat.status); | |
| 113 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(false); | |
| 114 | }); | |
| 115 | ||
| 116 | test("an unknown kind or a finding that is not live is refused", async () => { | |
| 117 | const bad = await post("/maintenance/quiet", adminCookie, { kind: "nope", key: "x" }); | |
| 118 | expect(bad.status).toBe(303); | |
| 119 | expect(quieted(app.db).some((q) => q.key === "x")).toBe(false); | |
| 120 | const gone = await post("/maintenance/quiet", adminCookie, { kind: "stale-pulls", key: "no-such-pull" }); | |
| 121 | expect(gone.status).toBe(303); | |
| 122 | expect(quieted(app.db).some((q) => q.key === "no-such-pull")).toBe(false); | |
| 123 | }); | |
| 124 | ||
| 125 | test("the receipt has its own duty name and leaves every card's duty state alone", () => { | |
| 126 | const t0 = new Date("2026-11-20T10:00:00Z"); | |
| 127 | const before = app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM maintenance_run WHERE duty IN ('stale-pulls', 'merged-branches', 'preview-dirs')").get()!.n; | |
| 128 | quiet(app.db, "merged-branches", "k-own", "x:y", "boss", t0); | |
| 129 | unquiet(app.db, "merged-branches", "k-own", "boss", t0); | |
| 130 | const after = app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM maintenance_run WHERE duty IN ('stale-pulls', 'merged-branches', 'preview-dirs')").get()!.n; | |
| 131 | expect(after).toBe(before); | |
| 132 | const own = app.db.query<{ outcome: string }, []>("SELECT outcome FROM maintenance_run WHERE duty = 'quieted-findings'").all(); | |
| 133 | expect(own.length).toBeGreaterThanOrEqual(2); | |
| 134 | }); | |
| 135 | ||
| 136 | test("unquiet is refused cross-origin, to a visitor and a non-admin; an agent is refused on both routes", async () => { | |
| 137 | const p = stalePull(person); | |
| 138 | await post("/maintenance/quiet", adminCookie, { kind: "stale-pulls", key: p.id }); | |
| 139 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(true); | |
| 140 | const fields = { kind: "stale-pulls", key: p.id }; | |
| 141 | expect((await post("/maintenance/unquiet", adminCookie, fields, "https://evil.example")).status).toBeGreaterThanOrEqual(400); | |
| 142 | expect((await post("/maintenance/unquiet", "", fields)).status).not.toBe(200); | |
| 143 | expect([403, 404]).toContain((await post("/maintenance/unquiet", personCookie, fields)).status); | |
| 144 | await post("/maintenance/unquiet", agentCookie, fields); | |
| 145 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(true); | |
| 146 | const q2 = stalePull(person); | |
| 147 | await post("/maintenance/quiet", agentCookie, { kind: "stale-pulls", key: q2.id }); | |
| 148 | expect(quieted(app.db).some((q) => q.key === q2.id)).toBe(false); | |
| 149 | await post("/maintenance/unquiet", adminCookie, fields); | |
| 150 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(false); | |
| 151 | }); | |
| 152 | ||
| 153 | test("a merged branch and a preview directory can be quieted and come back", async () => { | |
| 154 | const made = await createBranch(app, admin, repo, { name: "done" }); | |
| 155 | if (!made.ok) throw new Error("branch"); | |
| 156 | const head = (await resolveOid(repoDir(app.reposRoot, repo.ownerName, repo.name), "refs/heads/done"))!; | |
| 157 | const at = new Date(Date.now() - 10 * 86_400_000).toISOString(); | |
| 158 | const mi = id(); | |
| 159 | app.db.query("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at) VALUES (?1, ?2, ?3, 'pull', 'P', '', ?4, 'closed', ?5, ?5)").run(mi, repo.id, ++seq, admin.id, at); | |
| 160 | app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, merged_at) VALUES (?1, 'main', 'done', ?2, ?2, ?3, ?4)").run(mi, head, repo.id, at); | |
| 161 | const n = ++seq; | |
| 162 | app.db.query("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at, closed_at) VALUES (?1, ?2, ?3, 'pull', 'P', '', ?4, 'closed', ?5, ?5, ?5)").run(id(), repo.id, n, admin.id, at); | |
| 163 | mkdirSync(join(appsRoot(dir), appId, `pull-${n}`, "data"), { recursive: true }); | |
| 164 | writeFileSync(join(appsRoot(dir), appId, `pull-${n}`, "data", "db"), "x"); | |
| 165 | ||
| 166 | const mkey = `boss/r:done@${head}`, pkey = `boss/r#${n}`; | |
| 167 | const page = await view(); | |
| 168 | expect(page).toContain('data-branch="boss/r:done"'); | |
| 169 | expect(page).toContain(`data-preview="${pkey}"`); | |
| 170 | expect((await post("/maintenance/quiet", adminCookie, { kind: "merged-branches", key: mkey })).status).toBe(303); | |
| 171 | expect((await post("/maintenance/quiet", adminCookie, { kind: "preview-dirs", key: pkey })).status).toBe(303); | |
| 172 | const hidden = await view(); | |
| 173 | expect(hidden).not.toContain('data-branch="boss/r:done"'); | |
| 174 | expect(hidden).not.toContain(`data-preview="${pkey}"`); | |
| 175 | expect(hidden).toContain(`data-quieted="merged-branches:${mkey}"`); | |
| 176 | expect(hidden).toContain(`data-quieted="preview-dirs:${pkey}"`); | |
| 177 | await post("/maintenance/unquiet", adminCookie, { kind: "merged-branches", key: mkey }); | |
| 178 | await post("/maintenance/unquiet", adminCookie, { kind: "preview-dirs", key: pkey }); | |
| 179 | const back = await view(); | |
| 180 | expect(back).toContain('data-branch="boss/r:done"'); | |
| 181 | expect(back).toContain(`data-preview="${pkey}"`); | |
| 182 | }); |
test/pages-budget.test.ts +1 −1
| @@ -59,7 +59,7 @@ const HEAVIER: Record<string, { statements?: number; git?: number }> = { | ||
| 59 | 59 | "/api/v1/asks": { statements: 61 }, // +1: the stopped apps someone asked for, one read of the viewer's archived repositories (#12277) |
| 60 | 60 | "/board": { statements: 72 }, // +1: the forge-copy read for the tell box (#12037); +1: the vitals read the database's growth (#12025); +6: the money line (#510) — one settings read, then the month's and the 30 days' meter, storage and ledger reads; constant in the number of apps |
| 61 | 61 | "/settings/capacity": { statements: 70 }, |
| 62 | "/maintenance": { statements: 82 }, // +1: the findings fold (#14577); +5: slow-tests (#13966); +3: untested-sources (#14125) and the dismissal query (#13967); +4: flaky-tests (#13937), red apps (#13938), chapter stall (#14057), dead-doc-paths (#13977); +3: stale-branches (#14141) — each reads its own state and runs | |
| 62 | "/maintenance": { statements: 83 }, // +1: quieted findings (#14557); +1: the findings fold (#14577); +5: slow-tests (#13966); +3: untested-sources (#14125) and the dismissal query (#13967); +4: flaky-tests (#13937), red apps (#13938), chapter stall (#14057), dead-doc-paths (#13977); +3: stale-branches (#14141) — each reads its own state and runs | |
| 63 | 63 | "/api/v1/maintenance/duties": { statements: 66 }, // the same duties as JSON; slow-tests (#13966) and stale-branches (#14141) each add 3 |
| 64 | 64 | }; |
| 65 | 65 |
test/tui-maintenance-quiet.test.ts added +92
| @@ -0,0 +1,92 @@ | ||
| 1 | /** | |
| 2 | * The terminal's maintenance screen quiets and brings back a finding through quiet()/unquiet() | |
| 3 | * (#14557). | |
| 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, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/core/repos.ts"; | |
| 11 | import { createUser, type User } from "../src/core/users.ts"; | |
| 12 | import { id } from "../src/kernel/id.ts"; | |
| 13 | import { quieted } from "../src/maintenance/quiet.ts"; | |
| 14 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 15 | import { maintenanceScreen } from "../src/tui/screens/maintenance.ts"; | |
| 16 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | import { settleApp, settleUntil } from "./helpers/tui.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | let dir = "", app: App, admin: User, person: User, seq = 0; | |
| 22 | ||
| 23 | const must = (r: { ok: boolean; value?: unknown }): User => { | |
| 24 | if (!r.ok) throw new Error("user"); | |
| 25 | return r.value as User; | |
| 26 | }; | |
| 27 | function stalePull(repoId: string, author: User): { id: string; n: number } { | |
| 28 | const issue = id(); | |
| 29 | const n = ++seq; | |
| 30 | const at = new Date(Date.now() - 30 * 86_400_000).toISOString(); | |
| 31 | app.db.query("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at) VALUES (?1, ?2, ?3, 'pull', ?4, '', ?5, 'open', ?6, ?6)").run(issue, repoId, n, `Findable ${n}`, author.id, at); | |
| 32 | app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id) VALUES (?1, 'main', ?2, ?3, ?3, ?4)").run(issue, `b/${n}`, "0".repeat(40), repoId); | |
| 33 | return { id: issue, n }; | |
| 34 | } | |
| 35 | async function open(user: User) { | |
| 36 | const terminal = headlessTerminal(120, 40); | |
| 37 | const { app: tui, session } = createForgeApp(app, { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user }, { terminal, exitHooks: false }); | |
| 38 | tui.start(); | |
| 39 | tui.ctx.push(maintenanceScreen(session, tui.ctx)); | |
| 40 | await settleUntil(tui, "Maintenance"); | |
| 41 | return { tui, terminal, text: () => tui.screen.toText(), press: async (keys: string) => { terminal.feed(keys); await settleApp(tui); } }; | |
| 42 | } | |
| 43 | ||
| 44 | beforeAll(async () => { | |
| 45 | dir = await mkdtemp(join(tmpdir(), "forge-tui-quiet-")); | |
| 46 | app = createApp({ dataDir: dir, port: 0 }); | |
| 47 | const mk = async (username: string, extra: object = {}): Promise<User> => | |
| 48 | must(await createUser(app.db, { username, email: `${username}@f.dev`, password: "a-good-passphrase", origin: "seed", trusted: true, ...extra })); | |
| 49 | admin = await mk("boss", { isAdmin: true }); | |
| 50 | person = await mk("pat"); | |
| 51 | const made = await createRepo(app.db, app.reposRoot, { ownerId: admin.id, name: "r", visibility: "public", description: "r" }); | |
| 52 | if (!made.ok) throw new Error("repo"); | |
| 53 | }); | |
| 54 | afterAll(async () => { | |
| 55 | app.close(); | |
| 56 | await rm(dir, { recursive: true, force: true }); | |
| 57 | }); | |
| 58 | ||
| 59 | test("an admin lists the findings, quiets one with z and brings it back with b; the receipt is the page's", async () => { | |
| 60 | const repoId = app.db.query<{ id: string }, []>("SELECT id FROM repo LIMIT 1").get()!.id; | |
| 61 | const p = stalePull(repoId, person); | |
| 62 | const s = await open(admin); | |
| 63 | await settleUntil(s.tui, `boss/r#${p.n}`); | |
| 64 | expect(s.text()).toContain("z quiets one"); | |
| 65 | await s.press("z"); | |
| 66 | expect(s.text()).toContain("Quiet a finding"); | |
| 67 | await s.press("1"); | |
| 68 | await s.press("\r"); | |
| 69 | await settleUntil(s.tui, "quieted until"); | |
| 70 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(true); | |
| 71 | const receipt = app.db.query<{ actions: string }, []>("SELECT actions FROM maintenance_run WHERE duty = 'quieted-findings' ORDER BY id DESC LIMIT 1").get(); | |
| 72 | expect(receipt?.actions).toContain("pressed by boss"); | |
| 73 | await s.press("b"); | |
| 74 | await s.press("1"); | |
| 75 | await s.press("\r"); | |
| 76 | await settleUntil(s.tui, `1. boss/r#${p.n}`); | |
| 77 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(false); | |
| 78 | s.tui.stop?.(); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a non-admin and an agent are offered nothing and quiet nothing", async () => { | |
| 82 | const repoId = app.db.query<{ id: string }, []>("SELECT id FROM repo LIMIT 1").get()!.id; | |
| 83 | const p = stalePull(repoId, person); | |
| 84 | for (const who of [person, { ...admin, kind: "agent" } as User]) { | |
| 85 | const s = await open(who); | |
| 86 | await s.press("z"); | |
| 87 | await s.press("1"); | |
| 88 | await s.press("\r"); | |
| 89 | expect(quieted(app.db).some((q) => q.key === p.id)).toBe(false); | |
| 90 | s.tui.stop?.(); | |
| 91 | } | |
| 92 | }); |
new above
offline
Both doors: the screen is read-only. Add quiet and bring-back here, with a test, so the terminal does what the page does.