web: attention / "Needs you" surface — bell, sheet, dismissals (R15, R2)

This commit is contained in:
2026-10-19 04:52:34 +01:00
parent 9555bfe58f
commit f3bc6ed126
6 changed files with 1107 additions and 5 deletions
+284
View File
@@ -0,0 +1,284 @@
// Pure unit tests for the attention / "Needs you" model (R15): qualification,
// identity, wording, ordering and tie-breaks, dismissal filtering, the
// Clear-all snapshot semantics and the localStorage encoding. No server, no DOM.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
ATTENTION_STORAGE_KEY,
AttentionKind,
attentionBadgeLabel,
attentionDetail,
attentionDismiss,
attentionDismissAll,
attentionDismissalSnapshot,
attentionEntries,
attentionKey,
attentionKindOf,
attentionRowDescription,
decodeAttentionDismissals,
encodeAttentionDismissals,
} from "../src/attention.js";
/** A run summary with every field the attention model reads. */
function run(overrides = {}) {
return {
id: 1,
name: "Plan and land",
title: null,
status: "failed",
created_at: "2026-10-19T10:00:00Z",
finished_at: "2026-10-19T10:05:00Z",
diagnostic: "",
...overrides,
};
}
// ---------------------------------------------------------------------------
// Qualification
// ---------------------------------------------------------------------------
test("only waiting and failed qualify; every other status never produces an entry", () => {
assert.equal(attentionKindOf("waiting"), AttentionKind.Decision);
assert.equal(attentionKindOf("failed"), AttentionKind.Failed);
for (const status of [
"queued",
"running",
"succeeded",
"canceled",
"cancelled",
"interrupted",
"active",
"blocked",
"skipped",
"unknown",
"",
" ",
null,
undefined,
"FAILED?",
]) {
assert.equal(attentionKindOf(status), null, `status ${JSON.stringify(status)} should not qualify`);
}
});
test("qualification trims and lowercases the server's own words", () => {
assert.equal(attentionKindOf(" Waiting "), AttentionKind.Decision);
assert.equal(attentionKindOf("Failed\n"), AttentionKind.Failed);
assert.equal(attentionKindOf("WAITING"), AttentionKind.Decision);
});
test("ranks are Decision 0 then Failed 1", () => {
assert.equal(AttentionKind.Decision.rank, 0);
assert.equal(AttentionKind.Failed.rank, 1);
});
// ---------------------------------------------------------------------------
// Identity and wording
// ---------------------------------------------------------------------------
test("identity is exactly {runId}:{kind}", () => {
assert.equal(attentionKey(440, AttentionKind.Failed), "440:Failed");
assert.equal(attentionKey(12, AttentionKind.Decision), "12:Decision");
});
test("detail is the fixed decision line and the trimmed diagnostic else Run failed", () => {
assert.equal(attentionDetail(AttentionKind.Decision, run()), "Waiting for your decision");
assert.equal(
attentionDetail(AttentionKind.Failed, run({ diagnostic: " boom " })),
"boom",
);
assert.equal(attentionDetail(AttentionKind.Failed, run({ diagnostic: " " })), "Run failed");
assert.equal(attentionDetail(AttentionKind.Failed, run({ diagnostic: "" })), "Run failed");
assert.equal(attentionDetail(AttentionKind.Failed, run({ diagnostic: null })), "Run failed");
});
test("label is the trimmed title else the pipeline name", () => {
const [titled] = attentionEntries([run({ title: " My run " })], new Set());
assert.equal(titled.label, "My run");
assert.equal(titled.pipeline, "Plan and land");
const [untitled] = attentionEntries([run({ title: " " })], new Set());
assert.equal(untitled.label, "Plan and land");
const [missing] = attentionEntries([run({ title: null })], new Set());
assert.equal(missing.label, "Plan and land");
});
test("row description is exactly {pipeline} · {detail}", () => {
const [entry] = attentionEntries([run({ title: "T", diagnostic: "failed hard" })], new Set());
assert.equal(attentionRowDescription(entry), "Plan and land · failed hard");
});
test("badge label is bare Notifications when empty, else the count", () => {
assert.equal(attentionBadgeLabel(0), "Notifications");
assert.equal(attentionBadgeLabel(-3), "Notifications");
assert.equal(attentionBadgeLabel(1), "Notifications, 1 needing attention");
assert.equal(attentionBadgeLabel(171), "Notifications, 171 needing attention");
});
// ---------------------------------------------------------------------------
// Ordering
// ---------------------------------------------------------------------------
test("orders by kind rank, then recency descending, then run id descending", () => {
const runs = [
run({ id: 10, status: "failed", finished_at: "2026-10-19T10:00:00Z" }),
run({ id: 11, status: "waiting", finished_at: "2026-10-19T09:00:00Z" }),
run({ id: 12, status: "failed", finished_at: "2026-10-19T11:00:00Z" }),
run({ id: 13, status: "waiting", finished_at: "2026-10-19T08:00:00Z" }),
];
const entries = attentionEntries(runs, new Set());
assert.deepEqual(
entries.map((entry) => entry.runId),
[11, 13, 12, 10],
);
});
test("recency falls back to created_at when finished_at is blank", () => {
const runs = [
run({ id: 1, status: "failed", finished_at: "", created_at: "2026-10-19T12:00:00Z" }),
run({ id: 2, status: "failed", finished_at: "2026-10-19T11:00:00Z" }),
];
const entries = attentionEntries(runs, new Set());
assert.deepEqual(entries.map((entry) => entry.runId), [1, 2]);
});
test("equal kind and recency break ties by run id descending and stay stable", () => {
const runs = [
run({ id: 3, status: "failed", finished_at: "2026-10-19T10:00:00Z" }),
run({ id: 9, status: "failed", finished_at: "2026-10-19T10:00:00Z" }),
run({ id: 5, status: "failed", finished_at: "2026-10-19T10:00:00Z" }),
];
const first = attentionEntries(runs, new Set()).map((entry) => entry.runId);
const second = attentionEntries([...runs].reverse(), new Set()).map((entry) => entry.runId);
assert.deepEqual(first, [9, 5, 3]);
assert.deepEqual(second, [9, 5, 3]);
});
test("non-qualifying runs are dropped entirely", () => {
const runs = [
run({ id: 1, status: "succeeded" }),
run({ id: 2, status: "running" }),
run({ id: 3, status: "failed" }),
run({ id: 4, status: "queued" }),
];
assert.deepEqual(
attentionEntries(runs, new Set()).map((entry) => entry.runId),
[3],
);
assert.deepEqual(attentionEntries(null, new Set()), []);
});
// ---------------------------------------------------------------------------
// Dismissal filtering
// ---------------------------------------------------------------------------
test("a dismissed identity is filtered out; a still-waiting dismissed entry never returns", () => {
const runs = [run({ id: 7, status: "waiting", title: "Wait" })];
const dismissed = new Set(["7:Decision"]);
assert.deepEqual(attentionEntries(runs, dismissed), []);
// Re-deriving from the same live list stays empty.
assert.deepEqual(attentionEntries(runs, dismissed), []);
});
test("a different kind for the same run is a different identity and still appears", () => {
const waiting = run({ id: 7, status: "waiting" });
const dismissed = new Set(["7:Decision"]);
assert.deepEqual(attentionEntries([waiting], dismissed), []);
// The same run later fails: a new kind, a new identity, a new entry.
const failed = run({ id: 7, status: "failed", diagnostic: "later failure" });
const entries = attentionEntries([failed], dismissed);
assert.equal(entries.length, 1);
assert.equal(entries[0].key, "7:Failed");
assert.equal(entries[0].detail, "later failure");
});
test("dismissing one kind leaves the other kind of the same run", () => {
const runs = [
run({ id: 7, status: "waiting" }),
run({ id: 7, status: "failed" }),
];
// Two entries for one run id, one per kind.
const both = attentionEntries(runs, new Set());
assert.equal(both.length, 2);
const afterDismiss = attentionEntries(runs, new Set(["7:Failed"]));
assert.deepEqual(afterDismiss.map((entry) => entry.kind), [AttentionKind.Decision]);
});
// ---------------------------------------------------------------------------
// Dismissal bookkeeping and the Clear-all snapshot
// ---------------------------------------------------------------------------
test("attentionDismiss adds a key once and ignores a blank key", () => {
const one = attentionDismiss(new Set(), "1:Failed");
assert.deepEqual([...one], ["1:Failed"]);
const again = attentionDismiss(one, "1:Failed");
assert.deepEqual([...again], ["1:Failed"]);
const blank = attentionDismiss(one, "");
assert.deepEqual([...blank], ["1:Failed"]);
});
test("clear all applies only the identities in the open snapshot", () => {
const [a] = attentionEntries([run({ id: 1, status: "failed" })], new Set());
const snapshot = attentionDismissalSnapshot([a]);
// A later entry (arrived while the sheet was open) is not in the snapshot.
const [b] = attentionEntries([run({ id: 2, status: "failed" })], new Set());
const after = attentionDismissAll(new Set(), snapshot);
assert.deepEqual([...after].sort(), ["1:Failed"]);
assert.equal(after.has(b.key), false);
// And it does not retroactively appear from the live set either.
assert.deepEqual(
attentionEntries([run({ id: 1, status: "failed" }), run({ id: 2, status: "failed" })], after).map(
(entry) => entry.runId,
),
[2],
);
});
test("the dismissal snapshot is a defensive copy", () => {
const [a, b] = attentionEntries(
[run({ id: 1, status: "failed" }), run({ id: 2, status: "failed" })],
new Set(),
);
const live = [a, b];
const snapshot = attentionDismissalSnapshot(live);
live.push(run({ id: 3, status: "failed" }));
live.reverse();
assert.deepEqual(
snapshot.map((entry) => entry.runId),
[a.runId, b.runId],
);
assert.notEqual(snapshot, live);
});
// ---------------------------------------------------------------------------
// Persistence
// ---------------------------------------------------------------------------
test("the storage key is the one documented key", () => {
assert.equal(ATTENTION_STORAGE_KEY, "pipelines.web.attention.v1");
});
test("encoding is one sorted JSON array and round-trips", () => {
const encoded = encodeAttentionDismissals(new Set(["9:Failed", "1:Decision", "4:Failed"]));
assert.deepEqual(JSON.parse(encoded), ["1:Decision", "4:Failed", "9:Failed"]);
const decoded = decodeAttentionDismissals(encoded);
assert.deepEqual([...decoded].sort(), ["1:Decision", "4:Failed", "9:Failed"]);
assert.deepEqual(JSON.parse(encodeAttentionDismissals(new Set())), []);
});
test("blank or unreadable storage decodes to empty and never throws", () => {
for (const stored of ["", " ", null, undefined, "not json", "{", '"a string"', "{}", "42"]) {
assert.deepEqual([...decodeAttentionDismissals(stored)], []);
}
});
test("a JSON array with non-string members keeps only the strings", () => {
const decoded = decodeAttentionDismissals('["1:Failed", 2, null, {"a":1}, "3:Decision", ""]');
assert.deepEqual([...decoded].sort(), ["1:Failed", "3:Decision"]);
});
test("there is no expiry: a stored identity survives any timestamp", () => {
// The encoding is opaque to time; decoding never consults a clock or drops by age.
const stored = encodeAttentionDismissals(new Set(["7:Decision"]));
assert.deepEqual([...decodeAttentionDismissals(stored)], ["7:Decision"]);
});
+220
View File
@@ -187,6 +187,14 @@ try {
browser = await launchChrome({ width: 1440, height: 900 });
page = await browser.openPage(`http://127.0.0.1:${port}/`);
// Deterministic dismissals: the attention set is read from localStorage once at
// boot, so clear the key and reload before connecting so the surface derives
// from an empty set regardless of any stale profile state.
step("clearing the attention storage key before connecting");
await page.evaluate(`localStorage.removeItem("pipelines.web.attention.v1")`);
await page.evaluate(`location.reload()`);
await page.waitForSelector("#url", { timeout: 15000 });
step("connecting the page to the server through its own proxy");
await page.fill("#url", `ws://127.0.0.1:${port}/connect`);
await page.click("#connectBtn");
@@ -455,6 +463,218 @@ try {
console.log(` usage: ${usageLabels.join(", ")}`);
console.log(` day headers: ${dayHeaders.slice(0, 3).join(" | ")}`);
// ---------------------------------------------------------------------
// R15: the attention / "Needs you" surface. The bell lives in the top bar on
// every view; its badge, the sheet's rows and the dismissal identities all
// come from the pure src/attention.js model. The live server currently has
// failed runs and no run parked in `waiting`, so the sheet is exercised with
// real failed entries and that live-data limitation is reported below. The
// decision path (open without dismissing) is unit-tested.
// ---------------------------------------------------------------------
step("checking the attention bell + badge");
await page.waitForSelector("#attentionBell", { timeout: 15000 });
const bell = await page.evaluate(`(() => {
const b = document.querySelector("#attentionBell");
const badge = b.querySelector(".attention-badge");
return {
label: b.getAttribute("aria-label"),
hasBadge: !!badge,
count: badge ? badge.textContent : null,
dataCount: badge ? badge.dataset.count : null,
};
})()`);
const attentionCount = Number(bell.dataCount ?? 0);
if (!(attentionCount > 0)) {
throw new Error(`no attention entries on the live server: ${JSON.stringify(bell)}`);
}
if (bell.label !== `Notifications, ${attentionCount} needing attention`) {
throw new Error(`the bell's accessible name is wrong: ${bell.label}`);
}
if (!bell.hasBadge || bell.count !== String(attentionCount)) {
throw new Error(`the badge is wrong: ${JSON.stringify(bell)}`);
}
if (await page.evaluate(`!!document.querySelector(".attention-sheet")`)) {
throw new Error("the sheet was already open before the bell was clicked");
}
console.log(` bell: aria-label="${bell.label}", badge=${bell.count}`);
shots.push(await page.screenshot(`${out}/35-attention-bell.png`));
step("opening the Needs you sheet");
await page.click("#attentionBell");
await page.waitForSelector(".attention-sheet", { timeout: 15000 });
const sheet = await page.evaluate(`(() => {
const rows = [...document.querySelectorAll(".attention-sheet .attention-row")];
return {
title: document.querySelector(".attention-sheet-title")?.textContent ?? "",
clearAll: document.querySelector(".attention-clear")?.textContent ?? null,
empty: document.querySelector(".attention-empty")?.textContent ?? null,
focused: document.activeElement?.className ?? "",
rows: rows.map((row) => {
const open = row.querySelector(".attention-row-open");
const dismiss = row.querySelector(".attention-dismiss");
return {
runId: Number(open?.dataset.runId),
kind: open?.dataset.kind ?? "",
label: open?.querySelector(".attention-label")?.textContent ?? "",
desc: open?.querySelector(".attention-desc")?.textContent ?? "",
icon: open?.querySelector(".attention-icon")?.textContent ?? "",
dismissName: dismiss?.getAttribute("aria-label") ?? "",
};
}),
};
})()`);
if (sheet.title !== "Needs you") throw new Error(`the sheet title is wrong: ${sheet.title}`);
if (sheet.empty !== null) throw new Error(`a non-empty sheet showed the empty copy: ${sheet.empty}`);
if (sheet.clearAll !== "Clear all") throw new Error(`Clear all is missing: ${sheet.clearAll}`);
if (sheet.rows.length !== attentionCount) {
throw new Error(`the sheet drew ${sheet.rows.length} rows for a badge of ${attentionCount}`);
}
if (!sheet.focused.includes("attention-sheet-close")) {
throw new Error(`focus did not move into the sheet on open: ${sheet.focused}`);
}
const kindRanks = { decision: 0, failed: 1 };
let lastKindRank = -1;
for (const row of sheet.rows) {
if (!row.icon) throw new Error(`row ${row.runId} has no status icon`);
if (!row.label) throw new Error(`row ${row.runId} has no primary label`);
if (!row.desc.includes(" · ")) throw new Error(`row ${row.runId} description is wrong: ${row.desc}`);
if (row.dismissName !== "Dismiss") {
throw new Error(`row ${row.runId}'s trailing control is not named Dismiss: ${row.dismissName}`);
}
const rank = kindRanks[row.kind];
if (rank == null) throw new Error(`row ${row.runId} has an unknown kind ${row.kind}`);
if (rank < lastKindRank) throw new Error(`the rows are out of kind order at run ${row.runId}`);
lastKindRank = rank;
}
const firstRow = sheet.rows[0];
const kindName = (kind) => (kind === "decision" ? "Decision" : "Failed");
console.log(
` sheet: ${sheet.rows.length} row(s); first ${firstRow.runId} (${firstRow.kind}) ` +
`"${firstRow.label}" — ${firstRow.desc}`,
);
if (!sheet.rows.some((row) => row.kind === "decision")) {
console.log(
" -- no `waiting` run exists on the live server; the sheet used real failed runs " +
"and the decision path (open without dismissing) is covered by unit tests",
);
}
shots.push(await page.screenshot(`${out}/36-attention-sheet.png`));
step("dismissing one entry individually");
await page.evaluate(`document.querySelector(".attention-sheet .attention-row .attention-dismiss").click()`);
await eventually(
async () =>
(await page.evaluate(`document.querySelectorAll(".attention-sheet .attention-row").length`)) ===
attentionCount - 1,
{ what: "the individual dismissal to land" },
);
const afterDismiss = await page.evaluate(`(() => ({
rows: document.querySelectorAll(".attention-sheet .attention-row").length,
badge: document.querySelector("#attentionBell .attention-badge")?.dataset.count ?? null,
stored: localStorage.getItem("pipelines.web.attention.v1"),
}))()`);
if (afterDismiss.rows !== attentionCount - 1) {
throw new Error(`the sheet still shows ${afterDismiss.rows} rows after one dismissal`);
}
if (Number(afterDismiss.badge) !== attentionCount - 1) {
throw new Error(`the badge did not follow the dismissal: ${afterDismiss.badge}`);
}
if (!String(afterDismiss.stored).includes(`${firstRow.runId}:${kindName(firstRow.kind)}`)) {
throw new Error(`the dismissed identity was not persisted: ${afterDismiss.stored}`);
}
console.log(
` dismissed ${firstRow.runId}:${kindName(firstRow.kind)}; rows ${attentionCount} -> ` +
`${afterDismiss.rows}, stored ${afterDismiss.stored}`,
);
shots.push(await page.screenshot(`${out}/37-attention-dismissed.png`));
step("opening a Failed row: it opens the run and persists its dismissal");
const rowToOpen = await page.evaluate(`(() => {
const open = document.querySelector(".attention-sheet .attention-row .attention-row-open");
return { runId: Number(open.dataset.runId), kind: open.dataset.kind };
})()`);
if (rowToOpen.kind !== "failed") {
throw new Error(`expected a Failed row to open, got ${rowToOpen.kind}`);
}
await page.evaluate(`document.querySelector(".attention-sheet .attention-row .attention-row-open").click()`);
await eventually(
async () => !(await page.evaluate(`!!document.querySelector(".attention-sheet")`)),
{ what: "the sheet to close on row activation" },
);
await page.waitForSelector(".detail .detail-tabs", { timeout: 25000 });
const openedIdentity = await page.evaluate(
`document.querySelector(".run-identity")?.textContent ?? ""`,
);
if (!openedIdentity.startsWith(`run ${rowToOpen.runId} ·`)) {
throw new Error(`row ${rowToOpen.runId} did not open its run: ${openedIdentity}`);
}
const storedAfterOpen = await page.evaluate(`localStorage.getItem("pipelines.web.attention.v1")`);
if (!String(storedAfterOpen).includes(`${rowToOpen.runId}:Failed`)) {
throw new Error(`opening a Failed row did not persist its dismissal: ${storedAfterOpen}`);
}
console.log(` opened run ${rowToOpen.runId}; ${rowToOpen.runId}:Failed now persisted`);
step("reopening the sheet and clearing all");
await page.click("#attentionBell");
await page.waitForSelector(".attention-sheet", { timeout: 15000 });
const beforeClear = await page.evaluate(
`document.querySelectorAll(".attention-sheet .attention-row").length`,
);
if (beforeClear !== attentionCount - 2) {
throw new Error(`expected ${attentionCount - 2} rows before Clear all, got ${beforeClear}`);
}
await page.evaluate(`document.querySelector(".attention-sheet .attention-clear").click()`);
await eventually(
async () =>
(await page.evaluate(`document.querySelector(".attention-empty")?.textContent ?? ""`)) ===
"Nothing needs you right now.",
{ what: "the empty attention state after Clear all" },
);
const cleared = await page.evaluate(`(() => ({
empty: document.querySelector(".attention-empty")?.textContent ?? "",
rows: document.querySelectorAll(".attention-sheet .attention-row").length,
clearAll: !!document.querySelector(".attention-clear"),
badge: !!document.querySelector("#attentionBell .attention-badge"),
label: document.querySelector("#attentionBell").getAttribute("aria-label"),
badges: document.querySelectorAll(".attention-badge").length,
}))()`);
if (cleared.empty !== "Nothing needs you right now.") {
throw new Error(`the empty copy is wrong: ${cleared.empty}`);
}
if (cleared.rows !== 0) throw new Error(`the cleared sheet still shows ${cleared.rows} rows`);
if (cleared.clearAll) throw new Error("Clear all is still offered on the empty sheet");
if (cleared.badge || cleared.badges !== 0) throw new Error("the badge is still visible after Clear all");
if (cleared.label !== "Notifications") {
throw new Error(`the emptied bell is named wrong: ${cleared.label}`);
}
console.log(` after Clear all: "${cleared.empty}", no Clear all, bell aria-label="${cleared.label}"`);
shots.push(await page.screenshot(`${out}/38-attention-cleared.png`));
step("closing the sheet with Escape returns focus to the bell");
await page.evaluate(
`document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }))`,
);
await eventually(
async () => !(await page.evaluate(`!!document.querySelector(".attention-sheet")`)),
{ what: "Escape to close the attention sheet" },
);
const focusAfterEscape = await page.evaluate(`document.activeElement?.id ?? ""`);
if (focusAfterEscape !== "attentionBell") {
throw new Error(`Escape did not return focus to the bell: ${focusAfterEscape}`);
}
console.log(" Escape closed the sheet and focus returned to #attentionBell");
step("closing the sheet with a backdrop click");
await page.click("#attentionBell");
await page.waitForSelector(".attention-sheet", { timeout: 15000 });
await page.evaluate(`document.querySelector(".attention-sheet").click()`);
await eventually(
async () => !(await page.evaluate(`!!document.querySelector(".attention-sheet")`)),
{ what: "the backdrop click to close the attention sheet" },
);
console.log(" backdrop click closed the sheet");
step(`scrolling the list to the older day group (${fullDateHeader})`);
const scrolledTo = await page.evaluate(`(() => {
const scroll = document.querySelector(".run-scroll");