// Which alert instances the alerts page lists. Run by `npm test`, which Node // runs with its built-in test runner and type stripping, so the `.ts` module // is imported as it is. import assert from "node:assert/strict"; import { test } from "node:test"; import { activeSinceMs, alertKey, firingAlerts, listedAlerts, } from "../src/pages/alerts.ts"; const alert = (name, state, extra = {}) => ({ name, state, active_at: null, summary: null, labels: { instance: name }, ...extra, }); test("only instances in the Alerting state are listed", () => { const all = [ alert("a", "Alerting"), alert("b", "Pending"), alert("c", "NoData"), alert("d", "Error"), alert("e", "Alerting"), ]; assert.deepEqual( firingAlerts(all).map((a) => a.name), ["a", "e"], ); }); test("asking for the rest lists every instance, in order", () => { const all = [ alert("a", "Pending"), alert("b", "Alerting"), alert("c", "Error"), ]; assert.deepEqual( listedAlerts(all, true).map((a) => a.name), ["a", "b", "c"], ); assert.deepEqual( listedAlerts(all, false).map((a) => a.name), ["b"], ); }); test("nothing firing leaves the list empty, and the input is not changed", () => { const all = [alert("a", "Pending"), alert("b", "Error")]; assert.deepEqual(firingAlerts(all), []); assert.equal(all.length, 2); assert.deepEqual(firingAlerts([]), []); }); test("the state match is exact, so an unknown or lower-case state is left out", () => { assert.deepEqual(firingAlerts([alert("a", "alerting"), alert("b", "")]), []); }); test("an instance that never became active has no active-since time", () => { assert.equal(activeSinceMs(alert("a", "Alerting")), null); assert.equal( activeSinceMs( alert("a", "Alerting", { active_at: "0001-01-01T00:00:00Z" }), ), null, ); assert.equal( activeSinceMs(alert("a", "Alerting", { active_at: "not a date" })), null, ); assert.equal( activeSinceMs( alert("a", "Alerting", { active_at: "2026-10-18T08:00:00Z" }), ), Date.parse("2026-10-18T08:00:00Z"), ); }); test("instances of one rule get different keys, whatever the label order", () => { const a = alert("r", "Alerting", { labels: { x: "1", y: "2" } }); const b = alert("r", "Alerting", { labels: { y: "2", x: "1" } }); const c = alert("r", "Alerting", { labels: { x: "1", y: "3" } }); assert.equal(alertKey(a), alertKey(b)); assert.notEqual(alertKey(a), alertKey(c)); });