web: attention / "Needs you" surface — bell, sheet, dismissals (R15, R2)
This commit is contained in:
@@ -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"]);
|
||||
});
|
||||
Reference in New Issue
Block a user