// The pure notification model (L5): qualifying transitions, exactly-once // dedupe, pre-existing/dismissed suppression, the app's exact wording and the // total permission/storage helpers. No DOM, no timer, no live server. import { test } from "node:test"; import assert from "node:assert/strict"; import { AttentionKind, attentionKey } from "../src/attention.js"; import { NOTIFICATION_DEFAULT, NOTIFICATION_DEFAULT_ASKED, NOTIFICATION_DENIED, NOTIFICATION_GRANTED, NOTIFICATION_INSECURE, NOTIFICATION_UNSUPPORTED, NOTIFY_STORAGE_KEY, alertBody, alertKey, alertTitle, createAlertLedger, decodeNotificationSettings, encodeNotificationSettings, notificationEnvironment, notificationPermissionState, } from "../src/notify.js"; const frame = (runId, status, extra = {}) => ({ run_id: runId, status, name: "release", title: "Ship it", ...extra, }); test("off suppresses every browser alert from summaries and pushed modes", () => { for (const status of ["waiting", "failed", "succeeded"]) { const ledger = createAlertLedger(); ledger.seed([{ id: 1, status: "running", notify_mode: "off" }]); assert.equal(ledger.observe(frame(1, status)), null); ledger.seed([{ id: 1, status: "running", notify_mode: "on" }]); assert.equal(ledger.observe(frame(1, status, { notify_mode: "off" })), null); } }); test("on adds final success once while auto retains attention-only semantics", () => { const ledger = createAlertLedger(); ledger.seed([{ id: 1, status: "running", notify_mode: "on" }, { id: 2, status: "running", notify_mode: "auto" }]); const alert = ledger.observe(frame(1, "succeeded")); assert.equal(alert.title, "A run succeeded"); assert.equal(alert.key, "1:Succeeded"); assert.equal(ledger.observe(frame(1, "succeeded")), null); assert.equal(ledger.observe(frame(2, "succeeded")), null); }); test("headlines are exactly the app's two, and unknown kinds are blank", () => { assert.equal(alertTitle(AttentionKind.Failed), "A run failed"); assert.equal(alertTitle(AttentionKind.Decision), "A run needs your decision"); assert.equal(alertTitle({ name: "Failed" }), "A run failed"); assert.equal(alertTitle({ name: "Other" }), ""); }); test("the alert identity is the attention identity", () => { assert.equal(alertKey(7, AttentionKind.Failed), attentionKey(7, AttentionKind.Failed)); assert.equal(alertKey(7, AttentionKind.Decision), "7:Decision"); }); test("alert body is the title then the pipeline, with the pipeline fallback", () => { assert.equal(alertBody({ label: "Ship it", pipeline: "release" }), "Ship it \u00b7 release"); assert.equal(alertBody({ title: " Ship it ", name: "release" }), "Ship it \u00b7 release"); assert.equal(alertBody({ title: " ", name: "release" }), "release"); assert.equal(alertBody({ label: "release", pipeline: "release" }), "release"); assert.equal(alertBody({ label: "Ship it", pipeline: "" }), "Ship it"); assert.equal(alertBody(frame(7, "failed")), "Ship it \u00b7 release"); }); test("queued -> failed raises one alert and duplicate frames never repeat it", () => { const ledger = createAlertLedger(); ledger.seed([{ id: 4, status: "queued", name: "release", title: "Ship it" }]); const alert = ledger.observe(frame(4, "failed")); assert.equal(alert.title, "A run failed"); assert.equal(alert.body, "Ship it \u00b7 release"); assert.equal(alert.key, "4:Failed"); assert.equal(ledger.observe(frame(4, "failed")), null); assert.equal(ledger.observe(frame(4, "failed")), null); }); test("running -> waiting raises the decision alert; unchanged waiting raises none", () => { const ledger = createAlertLedger(); ledger.seed([{ id: 9, status: "running", name: "release", title: "Ship it" }]); const alert = ledger.observe(frame(9, "waiting")); assert.equal(alert.title, "A run needs your decision"); assert.equal(alert.key, "9:Decision"); assert.equal(ledger.observe(frame(9, "waiting")), null); }); test("succeeded, canceled, interrupted and unknown statuses raise nothing", () => { for (const status of ["succeeded", "canceled", "interrupted", "mystery", ""]) { const ledger = createAlertLedger(); ledger.seed([{ id: 11, status: "running" }]); assert.equal(ledger.observe(frame(11, status)), null, `status ${status}`); } }); test("a first-seen failed/waiting run (a snapshot seed) raises nothing", () => { const ledger = createAlertLedger(); assert.equal(ledger.observe(frame(21, "failed")), null); assert.equal(ledger.observe(frame(22, "waiting")), null); }); test("a run already failed/waiting in the seed raises nothing", () => { const ledger = createAlertLedger(); ledger.seed([ { id: 31, status: "failed" }, { id: 32, status: "waiting" }, ]); assert.equal(ledger.observe(frame(31, "failed")), null); assert.equal(ledger.observe(frame(32, "waiting")), null); }); test("the subscribe snapshot is seeded, so a queued run that failed in the gap does not notify", () => { const ledger = createAlertLedger(); ledger.seed([{ id: 41, status: "queued" }]); ledger.beginSeed(); assert.equal(ledger.observe(frame(41, "failed")), null); ledger.endSeed(); assert.equal(ledger.observe(frame(41, "failed")), null); // A later real transition still raises exactly once. assert.equal(ledger.observe(frame(41, "running")), null); assert.equal(ledger.observe(frame(41, "failed")).key, "41:Failed"); }); test("leaving the qualifying state forgets the key for a genuinely new transition", () => { const ledger = createAlertLedger(); ledger.seed([{ id: 51, status: "queued" }]); assert.equal(ledger.observe(frame(51, "failed")).key, "51:Failed"); assert.equal(ledger.observe(frame(51, "running")), null); assert.equal(ledger.observe(frame(51, "failed")).key, "51:Failed"); // And a failed -> waiting hand-off raises the other kind once. assert.equal(ledger.observe(frame(51, "waiting")).key, "51:Decision"); assert.equal(ledger.observe(frame(51, "waiting")), null); }); test("a dismissed attention key notifies nothing, even on a real transition", () => { const ledger = createAlertLedger(); ledger.seed([{ id: 61, status: "queued" }]); const alert = ledger.observe(frame(61, "failed"), { dismissed: new Set([attentionKey(61, AttentionKind.Failed)]), }); assert.equal(alert, null); assert.deepEqual(ledger.notifiedKeys(), ["61:Failed"]); }); test("reset drops the status baseline but keeps the page session's dedupe", () => { const ledger = createAlertLedger(); ledger.seed([{ id: 71, status: "queued" }]); assert.equal(ledger.observe(frame(71, "failed")).key, "71:Failed"); ledger.reset(); // Re-seeding the fresh (still failed) list is not a transition. ledger.seed([{ id: 71, status: "failed" }]); assert.equal(ledger.observe(frame(71, "failed")), null); assert.deepEqual(ledger.notifiedKeys(), ["71:Failed"]); }); test("a malformed frame is ignored, never crashed on", () => { const ledger = createAlertLedger(); assert.equal(ledger.observe(null), null); assert.equal(ledger.observe({}), null); assert.equal(ledger.observe({ run_id: 0, status: "failed" }), null); }); test("permission wording is honest for every browser state", () => { assert.equal(notificationPermissionState({ supported: false }).body, NOTIFICATION_UNSUPPORTED); assert.equal( notificationPermissionState({ supported: true, secureContext: false }).body, NOTIFICATION_INSECURE, ); assert.equal(notificationPermissionState({ permission: "denied" }).body, NOTIFICATION_DENIED); assert.equal(notificationPermissionState({ permission: "granted" }).body, NOTIFICATION_GRANTED); assert.equal(notificationPermissionState({ asked: false }).body, NOTIFICATION_DEFAULT); assert.equal(notificationPermissionState({ asked: true }).body, NOTIFICATION_DEFAULT_ASKED); }); test("only a supported, secure default permission offers the request control", () => { const defaultView = notificationPermissionState({ asked: false }); assert.equal(defaultView.state, "default"); assert.equal(defaultView.canRequest, true); assert.equal(defaultView.actionLabel, "Enable notifications"); for (const options of [ { supported: false }, { supported: true, secureContext: false }, { permission: "granted" }, { permission: "denied" }, ]) { const view = notificationPermissionState(options); assert.equal(view.canRequest, false, JSON.stringify(options)); assert.equal(view.actionLabel, null, JSON.stringify(options)); } // Having been asked once does not remove the control while permission stays // default (the browser itself decides whether it may prompt again). assert.equal(notificationPermissionState({ asked: true }).canRequest, true); }); test("the asked flag round-trips and malformed storage decodes safely", () => { assert.equal(NOTIFY_STORAGE_KEY, "pipelines.web.notify.v1"); const stored = encodeNotificationSettings({ asked: true }); assert.deepEqual(JSON.parse(stored), { asked: true }); assert.equal(decodeNotificationSettings(stored).asked, true); assert.equal(decodeNotificationSettings(encodeNotificationSettings({ asked: false })).asked, false); for (const bad of ["", " ", "not json", "[]", "null", '"asked"', "{", '{"asked":"yes"}', "42"]) { assert.deepEqual(decodeNotificationSettings(bad), { asked: false }, bad); } assert.deepEqual(decodeNotificationSettings(null), { asked: false }); }); test("notificationEnvironment reads the browser facts without guessing", () => { const granted = notificationEnvironment({ Notification: Object.assign(function Notification() {}, { permission: "granted" }), isSecureContext: true, }); assert.deepEqual(granted, { supported: true, secureContext: true, permission: "granted" }); assert.deepEqual(notificationEnvironment({}), { supported: false, secureContext: false, permission: "default", }); const odd = notificationEnvironment({ Notification: Object.assign(function Notification() {}, { permission: "maybe" }), isSecureContext: false, }); assert.deepEqual(odd, { supported: true, secureContext: false, permission: "default" }); });