pr_01m47d24b0e6n91zwymwxg0vpx/apps/web/app/lib/status.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { |
| 5 | COMPONENTS, |
| 6 | type ComponentStatus, |
| 7 | type Incident, |
| 8 | classify, |
| 9 | componentStatus, |
| 10 | dotClass, |
| 11 | recentIncidents, |
| 12 | report, |
| 13 | summarize, |
| 14 | } from "./status.ts"; |
| 15 | |
| 16 | const info = (key: string) => COMPONENTS.find((c) => c.key === key)!; |
| 17 | const row = (key: string, state: ComponentStatus["state"]): ComponentStatus => ({ |
| 18 | ...componentStatus(info(key), null), |
| 19 | state, |
| 20 | }); |
| 21 | |
| 22 | test("a quick answer is up, a slow one degraded, a failure down", () => { |
| 23 | assert.deepEqual(classify({ ok: true, ms: 84.4 }), { state: "up", detail: "Answered in 84 ms" }); |
| 24 | assert.equal(classify({ ok: true, ms: 2400 }).state, "degraded"); |
| 25 | assert.deepEqual(classify({ ok: false, ms: 3000, error: "timed out" }), { state: "down", detail: "Failed: timed out" }); |
| 26 | assert.equal(classify({ ok: false, ms: 10 }).detail, "Failed: no answer"); |
| 27 | }); |
| 28 | |
| 29 | test("a part with no check is never shown as working", () => { |
| 30 | assert.deepEqual(classify(null), { state: "unmonitored", detail: "Not monitored yet" }); |
| 31 | const sandboxes = componentStatus(info("sandboxes"), null); |
| 32 | assert.equal(sandboxes.state, "unmonitored"); |
| 33 | assert.equal(sandboxes.latency_ms, null); |
| 34 | }); |
| 35 | |
| 36 | test("everything checked and answering is all clear, whatever is unmonitored", () => { |
| 37 | const overall = summarize([row("site", "up"), row("api", "up"), row("sandboxes", "unmonitored")]); |
| 38 | assert.deepEqual(overall, { state: "up", line: "All monitored systems are working" }); |
| 39 | }); |
| 40 | |
| 41 | test("a core part down is a major outage", () => { |
| 42 | const overall = summarize([row("site", "up"), row("git", "down"), row("docs", "up")]); |
| 43 | assert.equal(overall.state, "down"); |
| 44 | assert.equal(overall.line, "Major outage: Git and repositories"); |
| 45 | }); |
| 46 | |
| 47 | test("a non-core part down, or anything slow, is degraded", () => { |
| 48 | assert.equal(summarize([row("site", "up"), row("docs", "down")]).state, "degraded"); |
| 49 | assert.equal(summarize([row("site", "degraded"), row("docs", "up")]).state, "degraded"); |
| 50 | assert.equal( |
| 51 | summarize([row("site", "up"), row("docs", "down"), row("billing", "degraded")]).line, |
| 52 | "Some systems are having trouble: Documentation, Billing", |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | test("down outranks degraded", () => { |
| 57 | assert.equal(summarize([row("docs", "degraded"), row("api", "down")]).state, "down"); |
| 58 | }); |
| 59 | |
| 60 | const incident = (extra: Partial<Incident>): Incident => ({ |
| 61 | id: "inc_1", |
| 62 | title: "Pushes are slow", |
| 63 | impact: "degraded", |
| 64 | components: ["git"], |
| 65 | started_at: "2026-10-05T10:00:00Z", |
| 66 | resolved_at: null, |
| 67 | updates: [], |
| 68 | ...extra, |
| 69 | }); |
| 70 | |
| 71 | test("an open incident lowers the line even when checks pass", () => { |
| 72 | const overall = summarize([row("site", "up"), row("git", "up")], [incident({})]); |
| 73 | assert.deepEqual(overall, { state: "degraded", line: "Investigating: Pushes are slow" }); |
| 74 | assert.equal(summarize([row("site", "up")], [incident({ impact: "down" })]).state, "down"); |
| 75 | }); |
| 76 | |
| 77 | test("a resolved incident does not", () => { |
| 78 | const overall = summarize([row("site", "up")], [incident({ resolved_at: "2026-10-05T11:00:00Z" })]); |
| 79 | assert.equal(overall.state, "up"); |
| 80 | }); |
| 81 | |
| 82 | test("nothing checked and nothing open is unknown, not green", () => { |
| 83 | assert.equal(summarize([row("sandboxes", "unmonitored")]).state, "unknown"); |
| 84 | assert.equal(dotClass("unknown"), "bg-faint"); |
| 85 | }); |
| 86 | |
| 87 | test("old resolved incidents fall off the page; open ones never do", () => { |
| 88 | const now = new Date("2026-10-05T00:00:00Z"); |
| 89 | const list = [ |
| 90 | incident({ id: "old", resolved_at: "2026-06-01T00:00:00Z" }), |
| 91 | incident({ id: "recent", resolved_at: "2026-09-20T00:00:00Z" }), |
| 92 | incident({ id: "open", started_at: "2026-01-01T00:00:00Z" }), |
| 93 | ]; |
| 94 | assert.deepEqual( |
| 95 | recentIncidents(list, now).map((i) => i.id), |
| 96 | ["recent", "open"], |
| 97 | ); |
| 98 | }); |
| 99 | |
| 100 | test("the report covers every part, in order, with the overall line from the same data", () => { |
| 101 | const now = new Date("2026-10-05T12:00:00Z"); |
| 102 | const r = report({ site: { ok: true, ms: 40 }, api: { ok: false, ms: 3000, error: "timed out" } }, now, []); |
| 103 | assert.deepEqual( |
| 104 | r.components.map((c) => c.key), |
| 105 | COMPONENTS.map((c) => c.key), |
| 106 | ); |
| 107 | assert.equal(r.components.find((c) => c.key === "docs")?.state, "unmonitored"); |
| 108 | assert.equal(r.overall.state, "down"); |
| 109 | assert.equal(r.checked_at, "2026-10-05T12:00:00.000Z"); |
| 110 | }); |
| 111 | |
| 112 | test("each state has its own dot", () => { |
| 113 | assert.equal(dotClass("up"), "bg-accent"); |
| 114 | assert.equal(dotClass("degraded"), "bg-warn"); |
| 115 | assert.equal(dotClass("down"), "bg-danger"); |
| 116 | assert.equal(dotClass("unmonitored"), "bg-faint"); |
| 117 | assert.equal(dotClass(null), "bg-faint"); |
| 118 | }); |