import { test, expect, describe, beforeEach } from "bun:test"; import type { BoardItem } from "../src/types"; import { mergeMissing, removeOne, replaceRows, sameRow, shallowEqualArr, tagItem, upsertOne, upsertRows, } from "../src/lib/boardRows"; import { columnSections } from "../src/lib/board"; import { applyClusterSnapshot, applyListEval, getClusterState, resetClusterStoreForTest, setClusterState, } from "../src/lib/clusterStore"; type R = { id: string; status: string; n?: number }; const row = (id: string, status = "fixing", n = 0): R => ({ id, status, n }); describe("tagItem — stable identity per source row", () => { test("same source object → same tagged object", () => { const r = row("a"); const t1 = tagItem("bug", r as never); const t2 = tagItem("bug", r as never); expect(t1).toBe(t2); expect(t1).toEqual({ kind: "bug", ...r } as unknown as BoardItem); }); test("a replaced source row gets a new tagged object", () => { const a = row("a"); const b = { ...a, status: "fix_failed" }; expect(tagItem("bug", a as never)).not.toBe(tagItem("bug", b as never)); }); test("one WS update re-tags exactly one row", () => { const rows = [row("a"), row("b"), row("c")]; const before = rows.map((r) => tagItem("feature", r as never)); const next = upsertOne(rows, row("b", "implementing", 1)); const after = next.map((r) => tagItem("feature", r as never)); expect(after[0]).toBe(before[0]); expect(after[1]).not.toBe(before[1]); expect(after[2]).toBe(before[2]); }); }); describe("upsertOne / removeOne", () => { test("identical payload is a no-op (same array back)", () => { const rows = [row("a"), row("b")]; expect(upsertOne(rows, { ...rows[1] })).toBe(rows); }); test("changed payload replaces in place, unknown id appends", () => { const rows = [row("a"), row("b")]; const changed = upsertOne(rows, row("a", "done")); expect(changed.map((r) => r.status)).toEqual(["done", "fixing"]); expect(changed[1]).toBe(rows[1]); expect(upsertOne(rows, row("c")).map((r) => r.id)).toEqual(["a", "b", "c"]); }); test("removeOne of a missing id returns the same array", () => { const rows = [row("a")]; expect(removeOne(rows, "zz")).toBe(rows); expect(removeOne(rows, "a")).toEqual([]); }); }); describe("replaceRows — full refetch keeps unchanged rows", () => { test("nothing changed → previous array", () => { const prev = [row("a"), row("b")]; const fetched = prev.map((r) => ({ ...r })); expect(replaceRows(prev, fetched)).toBe(prev); }); test("one changed, one gone, one new", () => { const prev = [row("a"), row("b"), row("c")]; const fetched = [{ ...prev[0] }, row("b", "fix_failed"), row("d")]; const out = replaceRows(prev, fetched); expect(out.map((r) => r.id)).toEqual(["a", "b", "d"]); expect(out[0]).toBe(prev[0]); expect(out[1]).not.toBe(prev[1]); }); test("skip drops rows deleted while the request was in flight", () => { const out = replaceRows([], [row("a"), row("b")], new Set(["a"])); expect(out.map((r) => r.id)).toEqual(["b"]); }); test("reordered but identical rows still report a change (order matters to callers)", () => { const prev = [row("a"), row("b")]; const out = replaceRows(prev, [{ ...prev[1] }, { ...prev[0] }]); expect(out).not.toBe(prev); expect(out[0]).toBe(prev[1]); }); }); describe("two-phase load merge", () => { test("active upsert keeps rows outside the slice", () => { const prev = [row("done1", "fix_succeeded"), row("q1", "awaiting_fix", 1)]; const out = upsertRows(prev, [row("q1", "awaiting_fix", 2), row("q2", "awaiting_fix")]); expect(out.map((r) => r.id)).toEqual(["done1", "q1", "q2"]); expect(out[0]).toBe(prev[0]); expect(out[1].n).toBe(2); }); test("active upsert with nothing new → previous array", () => { const prev = [row("a"), row("b")]; expect(upsertRows(prev, [{ ...prev[0] }])).toBe(prev); }); test("done merge inserts only missing ids — a fresher WS row wins", () => { const wsFresh = row("x", "fix_succeeded", 9); const prev = [row("a"), wsFresh]; const out = mergeMissing(prev, [row("x", "fix_succeeded", 1), row("d1", "fix_succeeded")]); expect(out.map((r) => r.id)).toEqual(["a", "x", "d1"]); expect(out[1]).toBe(wsFresh); }); test("an old server ignoring ?phase returns everything twice — harmless", () => { const all = [row("a"), row("b", "fix_succeeded"), row("c", "archived")]; const afterActive = upsertRows([], all.map((r) => ({ ...r }))); const afterDone = mergeMissing(afterActive, all.map((r) => ({ ...r }))); expect(afterDone).toBe(afterActive); expect(afterDone.map((r) => r.id)).toEqual(["a", "b", "c"]); }); test("a row deleted over the WS meanwhile is not resurrected", () => { const out = mergeMissing([row("a")], [row("gone"), row("d")], new Set(["gone"])); expect(out.map((r) => r.id)).toEqual(["a", "d"]); }); test("duplicate ids inside one response collapse to one row", () => { expect(upsertRows([], [row("a"), row("a", "x")]).length).toBe(1); expect(mergeMissing([], [row("a"), row("a")]).length).toBe(1); }); }); describe("small helpers", () => { test("sameRow / shallowEqualArr", () => { expect(sameRow({ a: 1, b: [1] }, { a: 1, b: [1] })).toBe(true); expect(sameRow({ a: 1 }, { a: 2 })).toBe(false); const x = row("x"); const y = row("y"); expect(shallowEqualArr([x, y], [x, y])).toBe(true); expect(shallowEqualArr([x, y], [x, { ...y }])).toBe(false); expect(shallowEqualArr([x], [x, y])).toBe(false); }); }); describe("columnSections", () => { const items = [row("1", "merged"), row("2", "merged"), row("3", "evaluate_finish"), row("4", "archived"), row("5", "archived")]; const dividers = [ { before: (it: R) => it.status === "evaluate_finish", label: "EF" }, { before: (it: R) => it.status === "archived", label: "AR" }, ]; test("splits at the first match of each divider", () => { expect(columnSections(items, dividers)).toEqual([ { key: "head", label: null, start: 0, end: 2 }, { key: "d0", label: "EF", start: 2, end: 3 }, { key: "d1", label: "AR", start: 3, end: 5 }, ]); }); test("a divider matching index 0 or nothing adds no section", () => { expect(columnSections(items, [{ before: (it: R) => it.status === "merged", label: "M" }, { before: () => false, label: "Z" }])) .toEqual([{ key: "head", label: null, start: 0, end: 5 }]); }); test("two dividers at the same index: the later label wins", () => { const s = columnSections(items, [ { before: (it: R) => it.status === "archived", label: "first" }, { before: (it: R) => it.id === "4", label: "second" }, ]); expect(s).toEqual([ { key: "head", label: null, start: 0, end: 3 }, { key: "d1", label: "second", start: 3, end: 5 }, ]); }); test("no dividers / empty list", () => { expect(columnSections(items)).toEqual([{ key: "head", label: null, start: 0, end: 5 }]); expect(columnSections([], dividers)).toEqual([{ key: "head", label: null, start: 0, end: 0 }]); }); }); describe("clusterStore", () => { beforeEach(() => resetClusterStoreForTest()); const node = (id: string, lastSeenAt = 1) => ({ id, lastSeenAt, platform: "ios", status: "online" }) as never; test("an identical snapshot changes nothing (same references)", () => { applyClusterSnapshot({ nodes: [node("n1")], eval: { inFlight: 1, capacity: 2 }, fix: { inFlight: 0, capacity: 1 } }); const s1 = getClusterState(); applyClusterSnapshot({ nodes: [node("n1")], eval: { inFlight: 1, capacity: 2 }, fix: { inFlight: 0, capacity: 1 } }); expect(getClusterState()).toBe(s1); }); test("only the changed slice gets a new reference", () => { applyClusterSnapshot({ nodes: [node("n1")], eval: { inFlight: 1, capacity: 2 }, fix: { inFlight: 0, capacity: 1 } }); const s1 = getClusterState(); expect(setClusterState({ eval: { inFlight: 2, capacity: 2 } })).toBe(true); const s2 = getClusterState(); expect(s2).not.toBe(s1); expect(s2.nodes).toBe(s1.nodes); expect(s2.eval).toEqual({ inFlight: 2, capacity: 2 }); }); test("the list's eval figure is only a fallback before any snapshot", () => { applyListEval({ inFlight: 3, capacity: 4 }); expect(getClusterState().eval).toEqual({ inFlight: 3, capacity: 4 }); applyClusterSnapshot({ nodes: [], eval: { inFlight: 1, capacity: 4 }, fix: { inFlight: 0, capacity: 0 } }); applyListEval({ inFlight: 9, capacity: 9 }); expect(getClusterState().eval).toEqual({ inFlight: 1, capacity: 4 }); }); });