Public: anyone can read this; the crew can change it.
A maintenance finding shows who last touched it #17015
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16187-a-maintenance-finding-shows-who-last-tou → main 0 commits fork 7 threads · 7 unresolved
- docs/features/maintenance-findings.md
- src/core/account_purge.ts
- src/core/migration-pins/20261201130000-finding-touched.pin
- src/core/migrations/20261201130000-finding-touched.ts
- src/generated/migrations.ts
- src/maintenance/findings.ts
- src/tui/screens/maintenance.ts
- src/web/pages/maintenance.ts
- test/account-purge.test.ts
- test/maintenance-autoclose.test.ts
- test/maintenance-finding-age.test.ts
- test/maintenance-findings.test.ts
- test/maintenance-json.test.ts
docs/features/maintenance-findings.md +5
| @@ -12,6 +12,11 @@ held (#14577), so a cause that lasted a month is one line, not thirty. | ||
| 12 | 12 | still kept: a cause that cleared and opened again keeps the age of its first kept span, beside |
| 13 | 13 | "; still open", which ends the row while the newest span is open. The fold opens when any row |
| 14 | 14 | is open. |
| 15 | - **Who last touched it (#16187).** Each row ends with "last touched by ada 2 h ago", or | |
| 16 | "untouched" while nobody has acted. Reopening an auto-closed finding records the person; the | |
| 17 | tick's own close records "maintenance tick". Two columns on `degraded_span` | |
| 18 | (`touched_by`, `touched_at`); there is no dismiss action yet, so those two are the actions | |
| 19 | that exist. | |
| 15 | 20 | - **Bounded.** The 20 newest causes, listed oldest first so the longest-standing leads, then "And |
| 16 | 21 | N more". One grouped read of `degraded_span` (`src/maintenance/findings.ts`), whose closed spans |
| 17 | 22 | are pruned, so the first paint costs one statement. |
src/core/account_purge.ts +4
| @@ -59,6 +59,7 @@ export const COVERED: Readonly<Record<string, Coverage>> = { | ||
| 59 | 59 | "config_change.by": FORMER, |
| 60 | 60 | "crew_device.added_by": FORMER, |
| 61 | 61 | "crew_device.granted_by": FORMER, |
| 62 | "degraded_span.touched_by": { do: "none", why: "a username, not an id: `purgeAccount` rewrites it to the placeholder's name" }, | |
| 62 | 63 | "repo_review.by": FORMER, |
| 63 | 64 | "review_request.requested_by": FORMER, |
| 64 | 65 | "review_request.reviewer_id": FORMER, |
| @@ -367,6 +368,9 @@ export function purgeAccount(db: Database, userId: string, now: Date): PurgeOutc | ||
| 367 | 368 | `${JSON.stringify(name).slice(0, -1)}/`, `${JSON.stringify(FORMER_NAME).slice(0, -1)}/`, |
| 368 | 369 | JSON.stringify(userId), JSON.stringify(FORMER_ID), |
| 369 | 370 | ).changes; |
| 371 | // `degraded_span.touched_by` holds a username (or the tick's name): the finding keeps the | |
| 372 | // touch, as the placeholder's. | |
| 373 | if (name !== "") kept += db.query("UPDATE degraded_span SET touched_by = ?2 WHERE touched_by = ?1").run(name, FORMER_NAME).changes; | |
| 370 | 374 | // `entitlement.payer` is an address, not an id: an ended plan forgets who paid. |
| 371 | 375 | const email = db.query<{ email: string }, [string]>("SELECT email FROM user WHERE id = ?1").get(userId)?.email ?? ""; |
| 372 | 376 | if (email !== "") kept += db.query("UPDATE entitlement SET payer = '' WHERE payer = ?1 COLLATE NOCASE").run(email).changes; |
src/core/migration-pins/20261201130000-finding-touched.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 461643df3330f0bdad0c36fa5105221fd0b93c831dfab8b2e7011621e84546f6 |
src/core/migrations/20261201130000-finding-touched.ts added +6
| @@ -0,0 +1,6 @@ | ||
| 1 | // A finding names who last acted on it (#16187, maintenance/findings.ts): a person who reopened | |
| 2 | // it, or the maintenance tick that closed it. Null while nobody has. | |
| 3 | export default ` | |
| 4 | ALTER TABLE degraded_span ADD COLUMN touched_by TEXT; | |
| 5 | ALTER TABLE degraded_span ADD COLUMN touched_at INTEGER; | |
| 6 | `; |
src/generated/migrations.ts +4 −2
| @@ -445,7 +445,8 @@ import m440 from "../core/migrations/20261201000000-games.ts"; | ||
| 445 | 445 | import m441 from "../core/migrations/20261201090000-app-vital-member.ts"; |
| 446 | 446 | import m442 from "../core/migrations/20261201120000-first-change-forge.ts"; |
| 447 | 447 | import m443 from "../core/migrations/20261201121000-repo-check-slots.ts"; |
| 448 | import m444 from "../core/migrations/20261201130000-game-box-boot.ts"; | |
| 448 | import m444 from "../core/migrations/20261201130000-finding-touched.ts"; | |
| 449 | import m445 from "../core/migrations/20261201130000-game-box-boot.ts"; | |
| 449 | 450 | |
| 450 | 451 | /** Each migration file's name and its default export, in name order. */ |
| 451 | 452 | export const MIGRATION_MODULES: readonly (readonly [string, unknown])[] = [ |
| @@ -893,5 +894,6 @@ export const MIGRATION_MODULES: readonly (readonly [string, unknown])[] = [ | ||
| 893 | 894 | ["20261201090000-app-vital-member.ts", m441], |
| 894 | 895 | ["20261201120000-first-change-forge.ts", m442], |
| 895 | 896 | ["20261201121000-repo-check-slots.ts", m443], |
| 896 | ["20261201130000-game-box-boot.ts", m444], | |
| 897 | ["20261201130000-finding-touched.ts", m444], | |
| 898 | ["20261201130000-game-box-boot.ts", m445], | |
| 897 | 899 | ]; |
src/maintenance/findings.ts +46 −18
| @@ -21,8 +21,14 @@ export interface FindingRow { | ||
| 21 | 21 | readonly openedAt: number; |
| 22 | 22 | /** When the tick closed it for want of action (epoch ms), or null while it stands. */ |
| 23 | 23 | readonly autoClosedAt: number | null; |
| 24 | /** Who last acted on it (a person, or the tick that closed it) and when; null when nobody has. */ | |
| 25 | readonly touchedBy: string | null; | |
| 26 | readonly touchedAt: number | null; | |
| 24 | 27 | } |
| 25 | 28 | |
| 29 | /** The name the tick's own sweep is recorded under. */ | |
| 30 | export const TICK_ACTOR = "maintenance tick"; | |
| 31 | ||
| 26 | 32 | /** Days a finding may stand with no action before the tick closes it. */ |
| 27 | 33 | export const AUTO_CLOSE_DAYS = 14; |
| 28 | 34 | /** The note an auto-closed finding carries. */ |
| @@ -46,29 +52,40 @@ export interface Findings { | ||
| 46 | 52 | */ |
| 47 | 53 | export function findingRows(db: Database, cap = FINDING_ROWS): Findings { |
| 48 | 54 | const { got, more } = latestSpans(db, cap); |
| 49 | const rows = oldestFirst(got).map((r) => ({ cause: r.cause, latest: r.sentence, open: r.closed_at === null, openedAt: r.first, autoClosedAt: r.swept_at })); | |
| 55 | const rows = oldestFirst(got).map((r) => ({ cause: r.cause, latest: r.sentence, open: r.closed_at === null, openedAt: r.first, autoClosedAt: r.swept_at, touchedBy: r.touched_by, touchedAt: r.touched_at })); | |
| 50 | 56 | return { rows, autoClosed: autoClosedCauses(db).length, more }; |
| 51 | 57 | } |
| 52 | 58 | |
| 53 | interface SpanRow { cause: string; sentence: string; href: string; closed_at: number | null; swept_at: number | null; first: number; total: number } | |
| 59 | interface SpanRow { cause: string; sentence: string; href: string; closed_at: number | null; swept_at: number | null; touched_by: string | null; touched_at: number | null; first: number; total: number } | |
| 54 | 60 | |
| 55 | 61 | /** Oldest first-seen leads; ties keep the query's order. */ |
| 56 | 62 | const oldestFirst = (rows: SpanRow[]): SpanRow[] => [...rows].sort((a, b) => a.first - b.first); |
| 57 | 63 | |
| 64 | /** "3 d ago", "just now": how long before `now` a moment was. */ | |
| 65 | function agoWords(at: number, now: number): string { | |
| 66 | const ms = Math.max(0, now - at); | |
| 67 | if (ms < 60_000) return "just now"; | |
| 68 | if (ms < 3_600_000) return `${String(Math.floor(ms / 60_000))} m ago`; | |
| 69 | if (ms < DAY_MS) return `${String(Math.floor(ms / 3_600_000))} h ago`; | |
| 70 | return `${String(Math.floor(ms / DAY_MS))} d ago`; | |
| 71 | } | |
| 72 | ||
| 58 | 73 | /** "opened 3 d ago": the age of a finding against the clock the caller passes in. */ |
| 59 | export function openedWords(openedAt: number, now: number): string { | |
| 60 | const ms = Math.max(0, now - openedAt); | |
| 61 | if (ms < 60_000) return "opened just now"; | |
| 62 | if (ms < 3_600_000) return `opened ${String(Math.floor(ms / 60_000))} m ago`; | |
| 63 | if (ms < DAY_MS) return `opened ${String(Math.floor(ms / 3_600_000))} h ago`; | |
| 64 | return `opened ${String(Math.floor(ms / DAY_MS))} d ago`; | |
| 74 | export const openedWords = (openedAt: number, now: number): string => `opened ${agoWords(openedAt, now)}`; | |
| 75 | ||
| 76 | /** "last touched by ada 2 d ago", or "untouched" while nobody has acted on it. */ | |
| 77 | export function touchedWords(by: string | null, at: number | null, now: number): string { | |
| 78 | if (by === null || at === null) return "untouched"; | |
| 79 | return `last touched by ${by} ${agoWords(at, now)}`; | |
| 65 | 80 | } |
| 66 | 81 | |
| 67 | 82 | /** The one query behind every reading of the findings: the board, the terminal and the JSON. */ |
| 68 | 83 | function latestSpans(db: Database, cap: number): { got: SpanRow[]; more: number } { |
| 69 | 84 | const got = db |
| 70 | 85 | .query<SpanRow, [number]>( |
| 71 | `SELECT s.cause, s.sentence, s.href, s.closed_at, s.swept_at, g.first, COUNT(*) OVER () AS total | |
| 86 | `SELECT s.cause, s.sentence, s.href, s.closed_at, s.swept_at, g.first, | |
| 87 | (SELECT t.touched_by FROM degraded_span t WHERE t.cause = s.cause AND t.touched_at IS NOT NULL ORDER BY t.touched_at DESC, t.id DESC LIMIT 1) AS touched_by, | |
| 88 | (SELECT MAX(t.touched_at) FROM degraded_span t WHERE t.cause = s.cause) AS touched_at, COUNT(*) OVER () AS total | |
| 72 | 89 | FROM degraded_span s |
| 73 | 90 | JOIN (SELECT cause, MAX(id) AS last, MIN(opened_at) AS first FROM degraded_span GROUP BY cause) g ON s.id = g.last |
| 74 | 91 | ORDER BY s.opened_at DESC, s.id DESC LIMIT ?1`, |
| @@ -83,12 +100,12 @@ function latestSpans(db: Database, cap: number): { got: SpanRow[]; more: number | ||
| 83 | 100 | */ |
| 84 | 101 | export function autoClose(db: Database, now: number): string[] { |
| 85 | 102 | return db |
| 86 | .query<{ cause: string }, [number, number]>( | |
| 87 | `UPDATE degraded_span SET swept_at = ?1 | |
| 103 | .query<{ cause: string }, [number, number, string]>( | |
| 104 | `UPDATE degraded_span SET swept_at = ?1, touched_by = ?3, touched_at = ?1 | |
| 88 | 105 | WHERE closed_at IS NULL AND swept_at IS NULL AND COALESCE(clock_at, opened_at) <= ?2 |
| 89 | 106 | RETURNING cause`, |
| 90 | 107 | ) |
| 91 | .all(now, now - AUTO_CLOSE_DAYS * DAY_MS) | |
| 108 | .all(now, now - AUTO_CLOSE_DAYS * DAY_MS, TICK_ACTOR) | |
| 92 | 109 | .map((r) => r.cause); |
| 93 | 110 | } |
| 94 | 111 | |
| @@ -104,13 +121,14 @@ export function autoClosedCauses(db: Database): string[] { | ||
| 104 | 121 | } |
| 105 | 122 | |
| 106 | 123 | /** Reopen an auto-closed finding: it stands again and its 14 days begin now. */ |
| 107 | export function reopenFinding(db: Database, cause: string, now: number): boolean { | |
| 124 | export function reopenFinding(db: Database, cause: string, now: number, actor: string): boolean { | |
| 125 | if (actor.trim() === "") return false; | |
| 108 | 126 | return ( |
| 109 | 127 | db |
| 110 | .query<{ id: number }, [string, number]>( | |
| 111 | "UPDATE degraded_span SET swept_at = NULL, clock_at = ?2 WHERE cause = ?1 AND swept_at IS NOT NULL RETURNING id", | |
| 128 | .query<{ id: number }, [string, number, string]>( | |
| 129 | "UPDATE degraded_span SET swept_at = NULL, clock_at = ?2, touched_by = ?3, touched_at = ?2 WHERE cause = ?1 AND swept_at IS NOT NULL RETURNING id", | |
| 112 | 130 | ) |
| 113 | .all(cause, now).length > 0 | |
| 131 | .all(cause, now, actor).length > 0 | |
| 114 | 132 | ); |
| 115 | 133 | } |
| 116 | 134 | |
| @@ -129,6 +147,10 @@ export interface FindingJson { | ||
| 129 | 147 | readonly action: string; |
| 130 | 148 | /** ISO time the cause was first seen. */ |
| 131 | 149 | readonly openedAt: string; |
| 150 | /** Who last acted on it, or null when nobody has. */ | |
| 151 | readonly touchedBy: string | null; | |
| 152 | /** ISO time of that action, or null. */ | |
| 153 | readonly touchedAt: string | null; | |
| 132 | 154 | } |
| 133 | 155 | |
| 134 | 156 | /** The same rows as `findingRows`, as the plain data `/maintenance.json` answers. */ |
| @@ -140,13 +162,19 @@ export function findingsJson(db: Database): { findings: FindingJson[] } { | ||
| 140 | 162 | cause: r.sentence, |
| 141 | 163 | action: r.href, |
| 142 | 164 | openedAt: new Date(r.first).toISOString(), |
| 165 | touchedBy: r.touched_by, | |
| 166 | touchedAt: r.touched_at === null ? null : new Date(r.touched_at).toISOString(), | |
| 143 | 167 | })), |
| 144 | 168 | }; |
| 145 | 169 | } |
| 146 | 170 | |
| 147 | /** The words a row ends on, board and terminal alike: its age, then "still open" while it is. */ | |
| 171 | /** | |
| 172 | * The words a row ends on, board and terminal alike: its age, then "still open" while it is, then | |
| 173 | * who last touched it, or "untouched". When the tick closed this row, its "closed" clause already | |
The touch is dropped whenever |
||
| 174 | * says the touch, so it is not said twice; a tick touch on an older span still shows. | |
| 175 | */ | |
| 148 | 176 | export const rowWords = (r: FindingRow, now: number): string => |
| 149 | `${openedWords(r.openedAt, now)}${r.autoClosedAt !== null ? `; closed ${closedDate(r.autoClosedAt)}: ${AUTO_CLOSE_NOTE}` : r.open ? "; still open" : ""}`; | |
| 177 | `${openedWords(r.openedAt, now)}${r.autoClosedAt !== null ? `; closed ${closedDate(r.autoClosedAt)}: ${AUTO_CLOSE_NOTE}` : r.open ? "; still open" : ""}${r.autoClosedAt !== null && r.touchedBy === TICK_ACTOR ? "" : `; ${touchedWords(r.touchedBy, r.touchedAt, now)}`}`; | |
| 150 | 178 | |
| 151 | 179 | /** The same rows as terminal lines, led by a blank line; nothing when none. */ |
| 152 | 180 | export function findingLines(db: Database, now: number = Date.now()): string[] { |
src/tui/screens/maintenance.ts +1 −1
| @@ -199,7 +199,7 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 199 | 199 | */ |
| 200 | 200 | const doReopen = (i: number): void => { |
| 201 | 201 | const cause = autoClosedCauses(f.app.db)[i]; |
| 202 | say(cause !== undefined && reopenFinding(f.app.db, cause, Date.now()) ? `Reopened ${cause}.` : "That finding is not closed."); | |
| 202 | say(cause !== undefined && reopenFinding(f.app.db, cause, Date.now(), f.user?.username ?? "") ? `Reopened ${cause}.` : "That finding is not closed."); | |
| 203 | 203 | }; |
| 204 | 204 | |
| 205 | 205 | /** |
src/web/pages/maintenance.ts +1 −1
| @@ -335,7 +335,7 @@ export async function maintenanceReopen(ctx: Ctx): Promise<Response> { | ||
| 335 | 335 | const form = await readForm(ctx, "admin"); |
| 336 | 336 | if (form instanceof Response) return form; |
| 337 | 337 | const cause = formField(form, "cause"); |
| 338 | const ok = cause.length > 0 && cause.length <= 200 && reopenFinding(ctx.app.db, cause, Date.now()); | |
| 338 | const ok = cause.length > 0 && cause.length <= 200 && reopenFinding(ctx.app.db, cause, Date.now(), ctx.auth.user!.username); | |
| 339 | 339 | return flashTo(ctx, "/maintenance", ok ? { message: `Reopened ${cause}.` } : { tone: "danger", message: "That finding is not closed." }); |
| 340 | 340 | } |
| 341 | 341 |
test/account-purge.test.ts +13
| @@ -217,6 +217,19 @@ describe("what stays names nobody", () => { | ||
| 217 | 217 | }); |
| 218 | 218 | }); |
| 219 | 219 | |
| 220 | describe("a finding's toucher", () => { | |
| 221 | test("a purged person's name on a maintenance finding becomes the placeholder's; the tick's stays", async () => { | |
| 222 | const dana = await make("dana"); | |
| 223 | app.db.run("INSERT INTO degraded_span (cause, sentence, href, opened_at, touched_by, touched_at) VALUES ('a', 'x', '/system', 1, 'dana', 5), ('b', 'x', '/system', 1, 'maintenance tick', 5), ('c', 'x', '/system', 1, 'danae', 5)"); | |
| 224 | expect(requestClose(app.db, dana.id, T0)).toBe(true); | |
| 225 | await purgeDue(app, at(7)); | |
| 226 | const by = (c: string) => app.db.query<{ t: string }, [string]>("SELECT touched_by AS t FROM degraded_span WHERE cause = ?1").get(c)!.t; | |
| 227 | expect(by("a")).toBe(FORMER_NAME); | |
| 228 | expect(by("b")).toBe("maintenance tick"); | |
| 229 | expect(by("c")).toBe("danae"); | |
| 230 | }); | |
| 231 | }); | |
| 232 | ||
| 220 | 233 | describe("every column that names a user is covered", () => { |
| 221 | 234 | test("by its cascade or by a row of COVERED", () => { |
| 222 | 235 | // A new table that holds a user id with no ON DELETE CASCADE / SET NULL |
test/maintenance-autoclose.test.ts +61 −4
| @@ -8,7 +8,7 @@ import { tmpdir } from "node:os"; | ||
| 8 | 8 | import { join } from "node:path"; |
| 9 | 9 | import { createApp, type App } from "../src/app.ts"; |
| 10 | 10 | import { serve, type Servers } from "../src/http/server.ts"; |
| 11 | import { AUTO_CLOSE_NOTE, autoClose, closedDate, findingLines, findingRows, reopenFinding } from "../src/maintenance/findings.ts"; | |
| 11 | import { AUTO_CLOSE_NOTE, TICK_ACTOR, autoClose, closedDate, findingLines, findingRows, reopenFinding, rowWords } from "../src/maintenance/findings.ts"; | |
| 12 | 12 | import { adminCookie } from "./helpers/admin.ts"; |
| 13 | 13 | import { quietLogs } from "./helpers/globals.ts"; |
| 14 | 14 | |
| @@ -18,7 +18,7 @@ const T0 = Date.parse("2026-11-01T12:00:00.000Z"); | ||
| 18 | 18 | |
| 19 | 19 | function seed(): Database { |
| 20 | 20 | const db = new Database(":memory:"); |
| 21 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER)"); | |
| 21 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER, touched_by TEXT, touched_at INTEGER)"); | |
| 22 | 22 | return db; |
| 23 | 23 | } |
| 24 | 24 | const add = (db: Database, cause: string, at: number, closedAt: number | null = null): void => { |
| @@ -42,8 +42,8 @@ test("a reopened finding stands again and its 14 days begin at the reopening", ( | ||
| 42 | 42 | const db = seed(); |
| 43 | 43 | add(db, "disk-free", T0); |
| 44 | 44 | autoClose(db, T0 + 14 * DAY); |
| 45 | expect(reopenFinding(db, "disk-free", T0 + 16 * DAY)).toBe(true); | |
| 46 | expect(reopenFinding(db, "disk-free", T0 + 16 * DAY)).toBe(false); | |
| 45 | expect(reopenFinding(db, "disk-free", T0 + 16 * DAY, "ada")).toBe(true); | |
| 46 | expect(reopenFinding(db, "disk-free", T0 + 16 * DAY, "ada")).toBe(false); | |
| 47 | 47 | expect(findingRows(db).autoClosed).toBe(0); |
| 48 | 48 | expect(autoClose(db, T0 + 29 * DAY)).toEqual([]); // day 13 after reopening |
| 49 | 49 | expect(autoClose(db, T0 + 30 * DAY)).toEqual(["disk-free"]); |
| @@ -84,3 +84,60 @@ test("the page shows the closing with its date and a Reopen button; the summary | ||
| 84 | 84 | expect(res.status).toBe(303); |
| 85 | 85 | expect(findingRows(app.db).autoClosed).toBe(0); |
| 86 | 86 | }); |
| 87 | ||
| 88 | test("the sweep records the tick as the toucher, at the sweep's time, and the row does not say it twice", () => { | |
| 89 | const db = seed(); | |
| 90 | add(db, "disk-free", T0); | |
| 91 | autoClose(db, T0 + 14 * DAY); | |
| 92 | const r = findingRows(db).rows[0]!; | |
| 93 | expect(r.touchedBy).toBe(TICK_ACTOR); | |
| 94 | expect(r.touchedAt).toBe(T0 + 14 * DAY); | |
| 95 | expect(rowWords(r, T0 + 15 * DAY)).not.toContain("last touched"); | |
| 96 | expect(rowWords(r, T0 + 15 * DAY)).toContain("closed 2026-11-15"); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a tick touch on a swept older span still shows when a newer span of the cause stands open", () => { | |
| 100 | const db = seed(); | |
| 101 | add(db, "disk-free", T0); | |
| 102 | autoClose(db, T0 + 14 * DAY); | |
| 103 | add(db, "disk-free", T0 + 16 * DAY); | |
| 104 | const r = findingRows(db).rows[0]!; | |
| 105 | expect(r.autoClosedAt).toBeNull(); | |
| 106 | expect(r.open).toBe(true); | |
| 107 | expect(rowWords(r, T0 + 17 * DAY)).toBe(`opened 17 d ago; still open; last touched by ${TICK_ACTOR} 3 d ago`); | |
| 108 | }); | |
| 109 | ||
| 110 | test("a person's touch reads its age in the same words as the opening: just now, m, h, d", () => { | |
| 111 | const db = seed(); | |
| 112 | add(db, "disk-free", T0); | |
| 113 | autoClose(db, T0 + 14 * DAY); | |
| 114 | reopenFinding(db, "disk-free", T0 + 15 * DAY, "ada"); | |
| 115 | const r = findingRows(db).rows[0]!; | |
| 116 | expect(rowWords(r, T0 + 15 * DAY)).toContain("; last touched by ada just now"); | |
| 117 | expect(rowWords(r, T0 + 15 * DAY + 5 * 60_000)).toContain("; last touched by ada 5 m ago"); | |
| 118 | expect(rowWords(r, T0 + 15 * DAY + 3 * 3_600_000)).toContain("; last touched by ada 3 h ago"); | |
| 119 | expect(rowWords(r, T0 + 17 * DAY)).toContain("; last touched by ada 2 d ago"); | |
| 120 | }); | |
| 121 | ||
| 122 | test("a reopening with no one to name is refused and leaves the finding closed", () => { | |
| 123 | const db = seed(); | |
| 124 | add(db, "disk-free", T0); | |
| 125 | autoClose(db, T0 + 14 * DAY); | |
| 126 | expect(reopenFinding(db, "disk-free", T0 + 15 * DAY, "")).toBe(false); | |
| 127 | expect(reopenFinding(db, "disk-free", T0 + 15 * DAY, " ")).toBe(false); | |
| 128 | expect(findingRows(db).autoClosed).toBe(1); | |
| 129 | }); | |
| 130 | ||
| 131 | test("the page's Reopen records the signed-in username as the toucher", async () => { | |
| 132 | add(app.db, "load-high", Date.now() - 20 * DAY); | |
| 133 | autoClose(app.db, Date.now()); | |
| 134 | const res = await fetch(`${servers.url}/maintenance/findings/reopen`, { | |
| 135 | method: "POST", redirect: "manual", | |
| 136 | headers: { cookie, origin: servers.url, "content-type": "application/x-www-form-urlencoded" }, | |
| 137 | body: new URLSearchParams({ cause: "load-high" }), | |
| 138 | }); | |
| 139 | expect(res.status).toBe(303); | |
| 140 | const row = findingRows(app.db).rows.find((x) => x.cause === "load-high")!; | |
| 141 | expect(row.touchedBy).toBe("boss"); | |
| 142 | expect(row.touchedAt).not.toBeNull(); | |
| 143 | }); |
test/maintenance-finding-age.test.ts +21 −5
| @@ -2,14 +2,14 @@ | ||
| 2 | 2 | import { expect, test } from "bun:test"; |
| 3 | 3 | import { Database } from "bun:sqlite"; |
| 4 | 4 | import { findingsFold } from "../src/web/pages/maintenance.ts"; |
| 5 | import { findingLines, findingRows, findingsJson, openedWords } from "../src/maintenance/findings.ts"; | |
| 5 | import { findingLines, findingRows, findingsJson, openedWords, reopenFinding } from "../src/maintenance/findings.ts"; | |
| 6 | 6 | |
| 7 | 7 | const DAY = 86_400_000; |
| 8 | 8 | const NOW = Date.parse("2026-11-23T12:00:00.000Z"); |
| 9 | 9 | |
| 10 | 10 | function seed(): Database { |
| 11 | 11 | const db = new Database(":memory:"); |
| 12 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER)"); | |
| 12 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER, touched_by TEXT, touched_at INTEGER)"); | |
| 13 | 13 | const ins = db.query("INSERT INTO degraded_span (cause, sentence, href, opened_at, closed_at) VALUES (?1, 'x', '/system', ?2, NULL)"); |
| 14 | 14 | ins.run("newer", NOW - 2 * 3_600_000); |
| 15 | 15 | ins.run("oldest", NOW - 3 * DAY); |
| @@ -27,7 +27,7 @@ test("ages read in minutes, hours and days against the clock given", () => { | ||
| 27 | 27 | test("findings list oldest first, in the rows, the terminal and the JSON", () => { |
| 28 | 28 | const db = seed(); |
| 29 | 29 | expect(findingRows(db).rows.map((r) => r.cause)).toEqual(["oldest", "middle", "newer"]); |
| 30 | expect(findingLines(db, NOW)[2]).toBe("oldest: x (opened 3 d ago; still open)"); | |
| 30 | expect(findingLines(db, NOW)[2]).toBe("oldest: x (opened 3 d ago; still open; untouched)"); | |
| 31 | 31 | const json = findingsJson(db).findings; |
| 32 | 32 | expect(json.map((f) => f.duty)).toEqual(["oldest", "middle", "newer"]); |
| 33 | 33 | expect(json[0]?.openedAt).toBe(new Date(NOW - 3 * DAY).toISOString()); |
| @@ -45,12 +45,28 @@ test("the board's Findings fold says the age in each row, oldest first, against | ||
| 45 | 45 | |
| 46 | 46 | test("a cause that cleared and opened again keeps the age of its first kept span", () => { |
| 47 | 47 | const db = new Database(":memory:"); |
| 48 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER)"); | |
| 48 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER, touched_by TEXT, touched_at INTEGER)"); | |
| 49 | 49 | const ins = db.query("INSERT INTO degraded_span (cause, sentence, href, opened_at, closed_at) VALUES ('disk', ?1, '/system', ?2, ?3)"); |
| 50 | 50 | ins.run("first", NOW - 5 * DAY, NOW - 4 * DAY); |
| 51 | 51 | ins.run("again", NOW - 3_600_000, null); |
| 52 | 52 | const [row] = findingRows(db).rows; |
| 53 | 53 | expect(row?.openedAt).toBe(NOW - 5 * DAY); |
| 54 | expect(findingLines(db, NOW)[2]).toBe("disk: again (opened 5 d ago; still open)"); | |
| 54 | expect(findingLines(db, NOW)[2]).toBe("disk: again (opened 5 d ago; still open; untouched)"); | |
| 55 | 55 | expect(findingsJson(db).findings[0]?.openedAt).toBe(new Date(NOW - 5 * DAY).toISOString()); |
| 56 | 56 | }); |
| 57 | ||
| 58 | test("a row names who last touched it and when, or says untouched (#16187)", () => { | |
| 59 | const db = seed(); | |
| 60 | db.run("UPDATE degraded_span SET swept_at = ?1, touched_by = 'maintenance tick', touched_at = ?1 WHERE cause = 'oldest'", [NOW - DAY]); | |
| 61 | expect(reopenFinding(db, "oldest", NOW - 2 * 3_600_000, "ada")).toBe(true); | |
| 62 | const rows = findingRows(db).rows; | |
| 63 | expect(rows.find((r) => r.cause === "oldest")?.touchedBy).toBe("ada"); | |
| 64 | expect(rows.find((r) => r.cause === "middle")?.touchedBy).toBeNull(); | |
| 65 | const lines = findingLines(db, NOW); | |
| 66 | expect(lines.find((l) => l.startsWith("oldest:"))).toContain("last touched by ada 2 h ago"); | |
| 67 | expect(lines.find((l) => l.startsWith("middle:"))).toContain("untouched"); | |
| 68 | expect(String(findingsFold(db, NOW))).toContain("last touched by ada 2 h ago"); | |
| 69 | const json = findingsJson(db).findings; | |
| 70 | expect(json.find((f) => f.duty === "oldest")).toMatchObject({ touchedBy: "ada", touchedAt: new Date(NOW - 2 * 3_600_000).toISOString() }); | |
| 71 | expect(json.find((f) => f.duty === "middle")).toMatchObject({ touchedBy: null, touchedAt: null }); | |
| 72 | }); |
test/maintenance-findings.test.ts +4 −4
| @@ -8,7 +8,7 @@ const T0 = Date.parse("2026-11-01T12:00:00.000Z"); | ||
| 8 | 8 | |
| 9 | 9 | function seed(): Database { |
| 10 | 10 | const db = new Database(":memory:"); |
| 11 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER)"); | |
| 11 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER, touched_by TEXT, touched_at INTEGER)"); | |
| 12 | 12 | return db; |
| 13 | 13 | } |
| 14 | 14 | const add = (db: Database, cause: string, sentence: string, at: number, closed: boolean): void => { |
| @@ -25,8 +25,8 @@ test("three days of one finding are one row with the latest reading; another fin | ||
| 25 | 25 | const { rows } = findingRows(db); |
| 26 | 26 | expect(rows.length).toBe(2); |
| 27 | 27 | const mem = rows.find((r) => r.cause === "memory-growth")!; |
| 28 | expect(mem).toEqual({ cause: "memory-growth", latest: "50 MiB", open: true, openedAt: T0, autoClosedAt: null }); | |
| 29 | expect(rows.find((r) => r.cause === "disk-free")).toEqual({ cause: "disk-free", latest: "5 GiB", open: false, openedAt: T0 + DAY, autoClosedAt: null }); | |
| 28 | expect(mem).toEqual({ cause: "memory-growth", latest: "50 MiB", open: true, openedAt: T0, autoClosedAt: null, touchedBy: null, touchedAt: null }); | |
| 29 | expect(rows.find((r) => r.cause === "disk-free")).toEqual({ cause: "disk-free", latest: "5 GiB", open: false, openedAt: T0 + DAY, autoClosedAt: null, touchedBy: null, touchedAt: null }); | |
| 30 | 30 | }); |
| 31 | 31 | |
| 32 | 32 | test("rows are capped and the rest counted; the terminal says the same", () => { |
| @@ -42,5 +42,5 @@ test("rows are capped and the rest counted; the terminal says the same", () => { | ||
| 42 | 42 | test("an open cause says so once, in its row", () => { |
| 43 | 43 | const db = seed(); |
| 44 | 44 | add(db, "disk-free", "1 GiB", T0, false); |
| 45 | expect(findingLines(db, T0)).toContain("disk-free: 1 GiB (opened just now; still open)"); | |
| 45 | expect(findingLines(db, T0)).toContain("disk-free: 1 GiB (opened just now; still open; untouched)"); | |
| 46 | 46 | }); |
test/maintenance-json.test.ts +2 −2
| @@ -36,8 +36,8 @@ afterAll(async () => { | ||
| 36 | 36 | |
| 37 | 37 | test("the JSON carries the findings the page lists, from the same module", async () => { |
| 38 | 38 | const want = [ |
| 39 | { duty: "disk-free", severity: "degraded", cause: "5 GiB left", action: "/system", openedAt: new Date(1_000).toISOString() }, | |
| 40 | { duty: "memory-growth", severity: "cleared", cause: "40 MiB", action: "/resilience", openedAt: new Date(2_000).toISOString() }, | |
| 39 | { duty: "disk-free", severity: "degraded", cause: "5 GiB left", action: "/system", openedAt: new Date(1_000).toISOString(), touchedBy: null, touchedAt: null }, | |
| 40 | { duty: "memory-growth", severity: "cleared", cause: "40 MiB", action: "/resilience", openedAt: new Date(2_000).toISOString(), touchedBy: null, touchedAt: null }, | |
| 41 | 41 | ]; |
| 42 | 42 | for (const res of [ |
| 43 | 43 | await fetch(`${servers.url}/maintenance.json`, { headers: { cookie } }), |
The column is written with a username, not a user id, so FORMER will not scrub it. Store the id or cover it as a username column, and add a purge test. Also keep the alphabetical order.