From c4d0057b0ba64e7b3b04d7b357028a440714a07c Mon Sep 17 00:00:00 2001 From: yuez Date: Tue, 27 Oct 2026 19:00:26 +0800 Subject: [PATCH] test(web): pin the dot matrix, the shared blink clock and markSeen sessionDot had no test at all, which is how its priority order drifted away from the plugin. session-dot.test.ts locks the whole matrix: the parent order (exited > error > attention > done > busy > compacting > idle), every collision that used to be wrong, the viewed -> current overlay, the separate child matrix (steady red while viewed, never green, never attention) and the subagent table. blink.test.ts covers the one shared clock: phase lockstep, ref counting (two subscribers, one interval), idempotent unsubscribe, and prefers-reduced-motion pinning the dots solid. chat.tree.test.tsx grows up from "a .dot exists" to the actual classes; sessions-store.test.ts covers markSeen; components.test.tsx covers the dead status and the steady/current modifiers. Also makes the current-tab overlay a positive check (idle | busy | compacting), matching the plugin markable list: a session that has not reported a status yet ("starting") no longer claims the blue marker. --- web/src/features/chat/session-state.ts | 4 +- web/tests/blink.test.ts | 180 +++++++++ web/tests/chat.tree.test.tsx | 83 +++- web/tests/components.test.tsx | 37 ++ web/tests/session-dot.test.ts | 503 +++++++++++++++++++++++++ web/tests/sessions-store.test.ts | 174 ++++++++- 6 files changed, 965 insertions(+), 16 deletions(-) create mode 100644 web/tests/blink.test.ts create mode 100644 web/tests/session-dot.test.ts diff --git a/web/src/features/chat/session-state.ts b/web/src/features/chat/session-state.ts index e25c852..ee56067 100644 --- a/web/src/features/chat/session-state.ts +++ b/web/src/features/chat/session-state.ts @@ -466,7 +466,9 @@ export interface DotView { function withCurrent(view: DotView, state: SessionState, viewed: boolean): DotView { if (!viewed) return view; if (state.flags.error || state.flags.done || attentionPending(state)) return view; - if (state.status === "exited" || state.status === "interrupted") return view; + // The plugin's `markable` is a positive list: only a live session carries the + // marker, and a `compacting` one blinks at the busy rate under it. + if (state.status !== "idle" && state.status !== "busy" && state.status !== "compacting") return view; return { current: true, status: state.status === "compacting" ? "busy" : view.status }; } diff --git a/web/tests/blink.test.ts b/web/tests/blink.test.ts new file mode 100644 index 0000000..fc1db62 --- /dev/null +++ b/web/tests/blink.test.ts @@ -0,0 +1,180 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { BLINK_MS, blinkTick, resetBlinkClock, startBlinkClock } from "@/lib/blink"; + +describe("blink clock", () => { + const originalMatchMedia = window.matchMedia; + + beforeEach(() => { + vi.useFakeTimers(); + resetBlinkClock(); + }); + + afterEach(() => { + resetBlinkClock(); + window.matchMedia = originalMatchMedia; + vi.restoreAllMocks(); + vi.clearAllTimers(); + vi.useRealTimers(); + }); + + it("applies 'on'/'on' synchronously on subscribe and transitions phases in lockstep", () => { + const unsub = startBlinkClock(); + + // Synchronously applied on subscribe at tick 0 (no flash of dim). + expect(blinkTick()).toBe(0); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("on"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("on"); + + // After 500ms: fast -> "dim", slow stays "on". + vi.advanceTimersByTime(BLINK_MS); + expect(blinkTick()).toBe(1); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("dim"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("on"); + + // After 1000ms: fast -> "on", slow -> "dim". + vi.advanceTimersByTime(BLINK_MS); + expect(blinkTick()).toBe(2); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("on"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("dim"); + + // After 1500ms: both "dim". + vi.advanceTimersByTime(BLINK_MS); + expect(blinkTick()).toBe(3); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("dim"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("dim"); + + // After 2000ms: cycle restarts at both "on". + vi.advanceTimersByTime(BLINK_MS); + expect(blinkTick()).toBe(4); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("on"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("on"); + + // The two phases remain derived from ONE tick counter in lockstep. + for (let i = 5; i <= 12; i++) { + vi.advanceTimersByTime(BLINK_MS); + expect(blinkTick()).toBe(i); + const expectedFast = i % 2 === 0 ? "on" : "dim"; + const expectedSlow = Math.floor(i / 2) % 2 === 0 ? "on" : "dim"; + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe(expectedFast); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe(expectedSlow); + } + + unsub(); + }); + + it("ref-counts subscribers: two subscribers install ONE interval", () => { + const setIntervalSpy = vi.spyOn(window, "setInterval"); + + const unsub1 = startBlinkClock(); + expect(setIntervalSpy).toHaveBeenCalledTimes(1); + expect(vi.getTimerCount()).toBe(1); + + const unsub2 = startBlinkClock(); + // Second subscriber must reuse the existing interval. + expect(setIntervalSpy).toHaveBeenCalledTimes(1); + expect(vi.getTimerCount()).toBe(1); + + // Unsubscribing one keeps it ticking. + unsub1(); + expect(vi.getTimerCount()).toBe(1); + + vi.advanceTimersByTime(BLINK_MS); + expect(blinkTick()).toBe(1); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("dim"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("on"); + + // The last unsubscribe clears the interval, removes attributes, and resets tick to 0. + unsub2(); + expect(vi.getTimerCount()).toBe(0); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBeNull(); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBeNull(); + expect(blinkTick()).toBe(0); + + // Advancing time further produces no ticks. + vi.advanceTimersByTime(BLINK_MS * 4); + expect(blinkTick()).toBe(0); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBeNull(); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBeNull(); + }); + + it("returns an idempotent unsubscribe function", () => { + const unsub = startBlinkClock(); + expect(vi.getTimerCount()).toBe(1); + + // Multiple calls to the same unsubscribe handle do not decrement below zero or throw. + unsub(); + expect(vi.getTimerCount()).toBe(0); + expect(blinkTick()).toBe(0); + + unsub(); + expect(vi.getTimerCount()).toBe(0); + expect(blinkTick()).toBe(0); + + unsub(); + expect(vi.getTimerCount()).toBe(0); + expect(blinkTick()).toBe(0); + + // A subsequent subscription starts cleanly with a single interval. + const unsubNew = startBlinkClock(); + expect(vi.getTimerCount()).toBe(1); + expect(blinkTick()).toBe(0); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("on"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("on"); + + unsubNew(); + expect(vi.getTimerCount()).toBe(0); + }); + + it("pins both attributes to 'on' without installing an interval under prefers-reduced-motion: reduce", () => { + const matchMediaMock = vi.fn().mockImplementation((query: string) => ({ + matches: query.includes("prefers-reduced-motion: reduce"), + media: query, + onchange: null, + addListener: vi.fn(), + removeListener: vi.fn(), + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + dispatchEvent: vi.fn(), + })); + window.matchMedia = matchMediaMock; + const setIntervalSpy = vi.spyOn(window, "setInterval"); + + const unsub = startBlinkClock(); + + expect(matchMediaMock).toHaveBeenCalledWith("(prefers-reduced-motion: reduce)"); + // Both attributes pinned "on". + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("on"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("on"); + // No interval installed at all. + expect(vi.getTimerCount()).toBe(0); + expect(setIntervalSpy).not.toHaveBeenCalled(); + + // Advancing time does not change attributes or tick. + vi.advanceTimersByTime(BLINK_MS * 10); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("on"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("on"); + expect(blinkTick()).toBe(0); + + // Unsubscribe clears attributes. + unsub(); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBeNull(); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBeNull(); + expect(blinkTick()).toBe(0); + }); + + it("resetBlinkClock stops timer, clears attributes, and resets tick to 0", () => { + startBlinkClock(); + vi.advanceTimersByTime(BLINK_MS * 3); + expect(blinkTick()).toBe(3); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBe("dim"); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBe("dim"); + + resetBlinkClock(); + + expect(vi.getTimerCount()).toBe(0); + expect(document.documentElement.getAttribute("data-pi-blink-fast")).toBeNull(); + expect(document.documentElement.getAttribute("data-pi-blink-slow")).toBeNull(); + expect(blinkTick()).toBe(0); + }); +}); diff --git a/web/tests/chat.tree.test.tsx b/web/tests/chat.tree.test.tsx index 538b6b4..e9c94de 100644 --- a/web/tests/chat.tree.test.tsx +++ b/web/tests/chat.tree.test.tsx @@ -122,25 +122,45 @@ function menuItems(): HTMLElement[] { describe("session tree rows", () => { it("renders every session row as a single line: dot + name + attention", () => { - renderTree({ - sessions: [ - sessionFixture({ - sessionId: "s1", - name: "Alpha", - subagents: [], - attention: [ - { requestId: "r1", method: "ask", createdAt: 1, receivedAt: 2 }, - { requestId: "r2", method: "ask", createdAt: 3, receivedAt: 4 }, - ], - }), - sessionFixture({ sessionId: "s2", name: "Beta", subagents: [] }), - ], + const sessions = [ + sessionFixture({ + sessionId: "s1", + name: "Alpha", + subagents: [], + attention: [ + { requestId: "r1", method: "ask", createdAt: 1, receivedAt: 2 }, + { requestId: "r2", method: "ask", createdAt: 3, receivedAt: 4 }, + ], + }), + sessionFixture({ sessionId: "s2", name: "Beta", status: "idle", subagents: [] }), + sessionFixture({ + sessionId: "s3", + name: "Gamma", + status: "idle", + flags: { attention: false, done: true, error: false }, + subagents: [], + }), + sessionFixture({ sessionId: "s4", name: "Delta", status: "busy", subagents: [] }), + childSession({ sessionId: "c1", name: "Child exited", parentId: "s1", status: "exited" }), + childSession({ + sessionId: "c2", + name: "Child error", + parentId: "s1", + flags: { attention: false, done: false, error: true }, + }), + ]; + const { rerender } = renderTree({ + sessions, selectedId: "s1", }); const main = rowMain("s1"); // Single line: dot + name + `!N` only. - expect(main.querySelector(".dot")).not.toBeNull(); + const s1Dot = main.querySelector(".dot"); + expect(s1Dot).not.toBeNull(); + expect(s1Dot).toHaveClass("dot", "dot--attention"); + expect(s1Dot).not.toHaveClass("dot--current"); + expect(main.querySelector(".t-name")).toHaveTextContent("Alpha"); expect(main.querySelector(".t-attn")).toHaveTextContent("!2"); // No second line, no child count, no inline row actions. @@ -161,6 +181,41 @@ describe("session tree rows", () => { // A row without attention keeps no badge at all. expect(rowMain("s2").querySelector(".t-attn")).toBeNull(); + + // Status dot modifiers and classes: + // 1. Unselected done row renders dot--ok. + const s3Dot = rowMain("s3").querySelector(".dot"); + expect(s3Dot).toHaveClass("dot", "dot--ok"); + expect(s3Dot).not.toHaveClass("dot--current"); + + // 2. Child exited row renders hollow dot--exited (never dead red). + const c1Dot = rowMain("c1").querySelector(".dot"); + expect(c1Dot).toHaveClass("dot", "dot--exited"); + expect(c1Dot).not.toHaveClass("dot--dead"); + expect(c1Dot).not.toHaveClass("dot--current"); + + // 3. Child error row unselected renders dot--err without steady modifier. + const c2DotUnselected = rowMain("c2").querySelector(".dot"); + expect(c2DotUnselected).toHaveClass("dot", "dot--err"); + expect(c2DotUnselected).not.toHaveClass("dot--steady"); + + // 4. Selected idle row renders dot--current (and dot--idle when done flag consumed). + rerender(treeElement({ sessions, selectedId: "s2" })); + const s2DotSelected = rowMain("s2").querySelector(".dot"); + expect(s2DotSelected).toHaveClass("dot", "dot--idle", "dot--current"); + expect(s2DotSelected).not.toHaveClass("dot--steady"); + + // 5. Selected busy row renders dot--busy + dot--current. + rerender(treeElement({ sessions, selectedId: "s4" })); + const s4DotSelected = rowMain("s4").querySelector(".dot"); + expect(s4DotSelected).toHaveClass("dot", "dot--busy", "dot--current"); + expect(s4DotSelected).not.toHaveClass("dot--steady"); + + // 6. Selected child with error renders dot--err + dot--steady. + rerender(treeElement({ sessions, selectedId: "c2" })); + const c2DotSelected = rowMain("c2").querySelector(".dot"); + expect(c2DotSelected).toHaveClass("dot", "dot--err", "dot--steady"); + expect(c2DotSelected).not.toHaveClass("dot--current"); }); it("draws child rows as a single line without a child-count badge", () => { diff --git a/web/tests/components.test.tsx b/web/tests/components.test.tsx index ade8bcb..fd46925 100644 --- a/web/tests/components.test.tsx +++ b/web/tests/components.test.tsx @@ -60,11 +60,48 @@ describe("StatusDot", () => { ["err", "dot--err"], ["attention", "dot--attention"], ["exited", "dot--exited"], + ["dead", "dot--dead"], ] as const)("maps %s to %s", (status, expected) => { const { container } = render(); expect(container.firstElementChild).toHaveClass("dot", expected); }); + it("applies dot--steady modifier when steady is true", () => { + const { container } = render(); + expect(container.firstElementChild).toHaveClass("dot", "dot--err", "dot--steady"); + }); + + it("does not apply dot--steady when steady is false or omitted", () => { + const { container: c1 } = render(); + expect(c1.firstElementChild).not.toHaveClass("dot--steady"); + + const { container: c2 } = render(); + expect(c2.firstElementChild).not.toHaveClass("dot--steady"); + }); + + it("applies dot--current modifier when current is true", () => { + const { container } = render(); + expect(container.firstElementChild).toHaveClass("dot", "dot--busy", "dot--current"); + }); + + it("does not apply dot--current when current is false or omitted", () => { + const { container: c1 } = render(); + expect(c1.firstElementChild).not.toHaveClass("dot--current"); + + const { container: c2 } = render(); + expect(c2.firstElementChild).not.toHaveClass("dot--current"); + }); + + it("supports steady and current modifiers together", () => { + const { container } = render(); + expect(container.firstElementChild).toHaveClass("dot", "dot--busy", "dot--steady", "dot--current"); + }); + + it("preserves custom className alongside modifiers", () => { + const { container } = render(); + expect(container.firstElementChild).toHaveClass("dot", "dot--idle", "dot--steady", "dot--current", "custom-dot"); + }); + it("exposes the state label when a title is provided", () => { render(); expect(screen.getByLabelText("Online")).toHaveAttribute("title", "Online"); diff --git a/web/tests/session-dot.test.ts b/web/tests/session-dot.test.ts new file mode 100644 index 0000000..b8d28c9 --- /dev/null +++ b/web/tests/session-dot.test.ts @@ -0,0 +1,503 @@ +import { describe, expect, it } from "vitest"; + +import type { SessionFlags, SessionState } from "@protocol/types"; +import { + childSessionDot, + emptySessionState, + sessionDot, + sessionStatusKey, + subagentDot, +} from "@/features/chat/session-state"; + +type SessionOverrides = Omit, "flags"> & { + flags?: Partial; +}; + +function makeSession(overrides: SessionOverrides = {}): SessionState { + return { + ...emptySessionState({ sessionId: overrides.sessionId ?? "s1", workspaceId: "w1" }), + ...overrides, + flags: { + attention: false, + done: false, + error: false, + ...(overrides.flags ?? {}), + }, + }; +} + +function makeChild(overrides: SessionOverrides = {}): SessionState { + return makeSession({ parentId: "parent-1", ...overrides }); +} + +function makeSubagent( + overrides: Partial = {}, +): SessionState["subagents"][number] { + return { + id: "sub-1", + status: "active", + ...overrides, + }; +} + +describe("sessionDot (parent rows)", () => { + describe("base status matrix", () => { + it.each([ + { + name: "exited -> dead", + state: makeSession({ status: "exited" }), + expected: { status: "dead" }, + }, + { + name: "error -> err", + state: makeSession({ flags: { error: true } }), + expected: { status: "err" }, + }, + { + name: "attention flag -> attention", + state: makeSession({ flags: { attention: true } }), + expected: { status: "attention" }, + }, + { + name: "attention array non-empty -> attention", + state: makeSession({ attention: [{ id: "q1" } as any] }), + expected: { status: "attention" }, + }, + { + name: "done -> ok", + state: makeSession({ flags: { done: true } }), + expected: { status: "ok" }, + }, + { + name: "busy -> busy", + state: makeSession({ status: "busy" }), + expected: { status: "busy" }, + }, + { + name: "compacting -> compacting", + state: makeSession({ status: "compacting" }), + expected: { status: "compacting" }, + }, + { + name: "interrupted -> dead", + state: makeSession({ status: "interrupted" }), + expected: { status: "dead" }, + }, + { + name: "starting -> idle", + state: makeSession({ status: "starting" }), + expected: { status: "idle" }, + }, + { + name: "idle -> idle", + state: makeSession({ status: "idle" }), + expected: { status: "idle" }, + }, + ])("$name", ({ state, expected }) => { + expect(sessionDot(state)).toEqual(expected); + }); + }); + + describe("priority collisions (bug fixes: exited > error > attention > done > busy)", () => { + it("error beats attention (flags.error outranks flags.attention)", () => { + const state = makeSession({ + flags: { error: true, attention: true }, + }); + expect(sessionDot(state)).toEqual({ status: "err" }); + }); + + it("error beats attention (flags.error outranks populated attention array)", () => { + const state = makeSession({ + flags: { error: true }, + attention: [{ id: "q1" } as any], + }); + expect(sessionDot(state)).toEqual({ status: "err" }); + }); + + it("exited beats error (status: exited outranks flags.error)", () => { + const state = makeSession({ + status: "exited", + flags: { error: true }, + }); + expect(sessionDot(state)).toEqual({ status: "dead" }); + }); + + it("attention beats done (flags.attention outranks flags.done)", () => { + const state = makeSession({ + flags: { attention: true, done: true }, + }); + expect(sessionDot(state)).toEqual({ status: "attention" }); + }); + + it("attention beats done (populated attention array outranks flags.done)", () => { + const state = makeSession({ + flags: { done: true }, + attention: [{ id: "q1" } as any], + }); + expect(sessionDot(state)).toEqual({ status: "attention" }); + }); + + it("done beats busy (flags.done outranks status: busy)", () => { + const state = makeSession({ + status: "busy", + flags: { done: true }, + }); + expect(sessionDot(state)).toEqual({ status: "ok" }); + }); + + it("exited beats attention (status: exited outranks flags.attention and attention array)", () => { + const state = makeSession({ + status: "exited", + flags: { attention: true }, + attention: [{ id: "q1" } as any], + }); + expect(sessionDot(state)).toEqual({ status: "dead" }); + }); + + it("error beats done (flags.error outranks flags.done)", () => { + const state = makeSession({ + flags: { error: true, done: true }, + }); + expect(sessionDot(state)).toEqual({ status: "err" }); + }); + + it("attention beats busy (flags.attention outranks status: busy)", () => { + const state = makeSession({ + status: "busy", + flags: { attention: true }, + }); + expect(sessionDot(state)).toEqual({ status: "attention" }); + }); + }); + + describe("viewed overlay (you are here)", () => { + it.each([ + { + name: "idle -> current", + state: makeSession({ status: "idle" }), + expected: { status: "idle", current: true }, + }, + { + name: "busy -> current (busy)", + state: makeSession({ status: "busy" }), + expected: { status: "busy", current: true }, + }, + { + name: "compacting -> current with status flipped to busy", + state: makeSession({ status: "compacting" }), + expected: { status: "busy", current: true }, + }, + ])("$name", ({ state, expected }) => { + expect(sessionDot(state, { viewed: true })).toEqual(expected); + }); + + describe("no overlay when notification flags are set", () => { + it("suppresses current overlay when error is set", () => { + const state = makeSession({ status: "idle", flags: { error: true } }); + expect(sessionDot(state, { viewed: true })).toEqual({ status: "err" }); + }); + + it("suppresses current overlay when done is set", () => { + const state = makeSession({ status: "idle", flags: { done: true } }); + expect(sessionDot(state, { viewed: true })).toEqual({ status: "ok" }); + }); + + it("suppresses current overlay when flags.attention is set", () => { + const state = makeSession({ status: "idle", flags: { attention: true } }); + expect(sessionDot(state, { viewed: true })).toEqual({ status: "attention" }); + }); + + it("suppresses current overlay when attention array is non-empty", () => { + const state = makeSession({ status: "idle", attention: [{ id: "q1" } as any] }); + expect(sessionDot(state, { viewed: true })).toEqual({ status: "attention" }); + }); + }); + + describe("no overlay when session is exited or interrupted", () => { + it("suppresses current overlay when status is exited", () => { + const state = makeSession({ status: "exited" }); + expect(sessionDot(state, { viewed: true })).toEqual({ status: "dead" }); + }); + + it("suppresses current overlay when status is interrupted", () => { + const state = makeSession({ status: "interrupted" }); + expect(sessionDot(state, { viewed: true })).toEqual({ status: "dead" }); + }); + }); + + describe("no overlay when viewed is false or omitted", () => { + it("does not apply current when viewed: false on idle", () => { + const state = makeSession({ status: "idle" }); + expect(sessionDot(state, { viewed: false })).toEqual({ status: "idle" }); + }); + + it("does not apply current when viewed: false on busy", () => { + const state = makeSession({ status: "busy" }); + expect(sessionDot(state, { viewed: false })).toEqual({ status: "busy" }); + }); + + it("does not apply current or flip status when viewed: false on compacting", () => { + const state = makeSession({ status: "compacting" }); + expect(sessionDot(state, { viewed: false })).toEqual({ status: "compacting" }); + }); + + it("does not apply current when opts is omitted", () => { + const state = makeSession({ status: "idle" }); + expect(sessionDot(state)).toEqual({ status: "idle" }); + }); + }); + }); +}); + +describe("childSessionDot (sub-session / tree depth > 0)", () => { + describe("base status matrix", () => { + it.each([ + { + name: "exited -> exited (hollow ring)", + state: makeChild({ status: "exited" }), + opts: undefined, + expected: { status: "exited" }, + }, + { + name: "interrupted -> dead", + state: makeChild({ status: "interrupted" }), + opts: undefined, + expected: { status: "dead" }, + }, + { + name: "error with viewed -> { status: 'err', steady: true }", + state: makeChild({ flags: { error: true } }), + opts: { viewed: true }, + expected: { status: "err", steady: true }, + }, + { + name: "error without viewed (viewed: false) -> { status: 'err', steady: false } (blinking)", + state: makeChild({ flags: { error: true } }), + opts: { viewed: false }, + expected: { status: "err", steady: false }, + }, + { + name: "error without viewed (opts omitted) -> { status: 'err', steady: false } (blinking)", + state: makeChild({ flags: { error: true } }), + opts: undefined, + expected: { status: "err", steady: false }, + }, + { + name: "busy -> busy", + state: makeChild({ status: "busy" }), + opts: undefined, + expected: { status: "busy" }, + }, + { + name: "compacting -> busy (NOT compacting)", + state: makeChild({ status: "compacting" }), + opts: undefined, + expected: { status: "busy" }, + }, + { + name: "idle -> idle", + state: makeChild({ status: "idle" }), + opts: undefined, + expected: { status: "idle" }, + }, + ])("$name", ({ state, opts, expected }) => { + expect(childSessionDot(state, opts)).toEqual(expected); + }); + }); + + describe("child never returns ok or attention", () => { + it("returns idle instead of ok when flags.done is set on idle child", () => { + const state = makeChild({ status: "idle", flags: { done: true } }); + const dot = childSessionDot(state); + expect(dot.status).not.toBe("ok"); + expect(dot.status).toBe("idle"); + }); + + it("returns busy instead of ok when flags.done is set on busy child", () => { + const state = makeChild({ status: "busy", flags: { done: true } }); + const dot = childSessionDot(state); + expect(dot.status).not.toBe("ok"); + expect(dot.status).toBe("busy"); + }); + + it("returns idle instead of attention when flags.attention is set on idle child", () => { + const state = makeChild({ status: "idle", flags: { attention: true } }); + const dot = childSessionDot(state); + expect(dot.status).not.toBe("attention"); + expect(dot.status).toBe("idle"); + }); + + it("returns idle instead of attention when attention array is populated on idle child", () => { + const state = makeChild({ + status: "idle", + attention: [{ id: "q1" } as any], + }); + const dot = childSessionDot(state); + expect(dot.status).not.toBe("attention"); + expect(dot.status).toBe("idle"); + }); + + it("returns busy instead of attention when flags.attention is set on busy child", () => { + const state = makeChild({ + status: "busy", + flags: { attention: true }, + attention: [{ id: "q1" } as any], + }); + const dot = childSessionDot(state); + expect(dot.status).not.toBe("attention"); + expect(dot.status).toBe("busy"); + }); + + it("does not return ok or current when flags.done is set with viewed: true", () => { + const state = makeChild({ status: "idle", flags: { done: true } }); + expect(childSessionDot(state, { viewed: true })).toEqual({ status: "idle" }); + }); + + it("does not return attention or current when flags.attention is set with viewed: true", () => { + const state = makeChild({ status: "idle", flags: { attention: true } }); + expect(childSessionDot(state, { viewed: true })).toEqual({ status: "idle" }); + }); + }); + + describe("viewed overlay on child rows", () => { + it("applies current overlay to idle child when viewed", () => { + const state = makeChild({ status: "idle" }); + expect(childSessionDot(state, { viewed: true })).toEqual({ + status: "idle", + current: true, + }); + }); + + it("applies current overlay to busy child when viewed", () => { + const state = makeChild({ status: "busy" }); + expect(childSessionDot(state, { viewed: true })).toEqual({ + status: "busy", + current: true, + }); + }); + + it("applies current overlay to compacting child with status flipped to busy", () => { + const state = makeChild({ status: "compacting" }); + expect(childSessionDot(state, { viewed: true })).toEqual({ + status: "busy", + current: true, + }); + }); + + it("does not apply current overlay to exited child", () => { + const state = makeChild({ status: "exited" }); + expect(childSessionDot(state, { viewed: true })).toEqual({ + status: "exited", + }); + }); + + it("does not apply current overlay to interrupted child", () => { + const state = makeChild({ status: "interrupted" }); + expect(childSessionDot(state, { viewed: true })).toEqual({ + status: "dead", + }); + }); + }); +}); + +describe("subagentDot (manifest-only subagent tree rows)", () => { + it.each([ + { + name: "active -> busy", + subagent: makeSubagent({ status: "active" }), + opts: undefined, + expected: { status: "busy" }, + }, + { + name: "completed -> exited", + subagent: makeSubagent({ status: "completed" }), + opts: undefined, + expected: { status: "exited" }, + }, + { + name: "dormant -> exited", + subagent: makeSubagent({ status: "dormant" }), + opts: undefined, + expected: { status: "exited" }, + }, + { + name: "failed with viewed -> steady err", + subagent: makeSubagent({ status: "failed" }), + opts: { viewed: true }, + expected: { status: "err", steady: true }, + }, + { + name: "failed without viewed (viewed: false) -> blinking err", + subagent: makeSubagent({ status: "failed" }), + opts: { viewed: false }, + expected: { status: "err", steady: false }, + }, + { + name: "failed without viewed (opts omitted) -> blinking err", + subagent: makeSubagent({ status: "failed" }), + opts: undefined, + expected: { status: "err", steady: false }, + }, + { + name: "interrupted -> dead", + subagent: makeSubagent({ status: "interrupted" }), + opts: undefined, + expected: { status: "dead" }, + }, + { + name: "unknown status -> idle", + subagent: makeSubagent({ status: "unknown" as any }), + opts: undefined, + expected: { status: "idle" }, + }, + ])("$name", ({ subagent, opts, expected }) => { + expect(subagentDot(subagent, opts)).toEqual(expected); + }); +}); + +describe("sessionStatusKey (stays distinct from dot)", () => { + it("returns 'interrupted' for interrupted session while dot returns 'dead'", () => { + const state = makeSession({ status: "interrupted" }); + expect(sessionStatusKey(state)).toBe("interrupted"); + expect(sessionDot(state)).toEqual({ status: "dead" }); + }); + + it("returns 'attention' when flags.attention is true while dot returns 'attention'", () => { + const state = makeSession({ flags: { attention: true } }); + expect(sessionStatusKey(state)).toBe("attention"); + expect(sessionDot(state)).toEqual({ status: "attention" }); + }); + + it("returns 'attention' when attention array has items while dot returns 'attention'", () => { + const state = makeSession({ attention: [{ id: "q1" } as any] }); + expect(sessionStatusKey(state)).toBe("attention"); + expect(sessionDot(state)).toEqual({ status: "attention" }); + }); + + it("does not collapse interrupted into attention", () => { + const interrupted = makeSession({ status: "interrupted" }); + const attention = makeSession({ flags: { attention: true } }); + expect(sessionStatusKey(interrupted)).not.toBe(sessionStatusKey(attention)); + }); + + it.each([ + { status: "busy" as const, expected: "busy" }, + { status: "starting" as const, expected: "busy" }, + { status: "compacting" as const, expected: "compacting" }, + { status: "exited" as const, expected: "exited" }, + { status: "idle" as const, expected: "idle" }, + ])("maps status $status to key '$expected'", ({ status, expected }) => { + const state = makeSession({ status }); + expect(sessionStatusKey(state)).toBe(expected); + }); + + it("maps flags.error to 'error'", () => { + const state = makeSession({ flags: { error: true } }); + expect(sessionStatusKey(state)).toBe("error"); + }); + + it("maps flags.done to 'done'", () => { + const state = makeSession({ flags: { done: true } }); + expect(sessionStatusKey(state)).toBe("done"); + }); +}); diff --git a/web/tests/sessions-store.test.ts b/web/tests/sessions-store.test.ts index 1555f33..826d858 100644 --- a/web/tests/sessions-store.test.ts +++ b/web/tests/sessions-store.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it } from "vitest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; import { emptySessionState, normalizeSnapshotState } from "@/features/chat/session-state"; import { useSessionsStore } from "@/stores/sessions"; @@ -136,3 +136,175 @@ describe("sessions store — tree metadata", () => { expect(state().getSession("c1")?.visible).toBe(false); }); }); + +function fixture( + overrides: Omit>, "flags"> & { + flags?: Partial["flags"]>; + }, +) { + return sessionFixture({ + ...overrides, + flags: { + attention: false, + done: false, + error: false, + ...(overrides.flags ?? {}), + }, + }); +} + +describe("sessions store — markSeen", () => { + it("clears flags.done when seen", () => { + state().setSessions("w1", [fixture({ sessionId: "s1", workspaceId: "w1", flags: { done: true } })]); + expect(state().getSession("s1")?.flags.done).toBe(true); + + state().markSeen("s1"); + expect(state().getSession("s1")?.flags).toEqual({ + attention: false, + done: false, + error: false, + }); + }); + + it("clears flags.error when seen", () => { + state().setSessions("w1", [fixture({ sessionId: "s1", workspaceId: "w1", flags: { error: true } })]); + expect(state().getSession("s1")?.flags.error).toBe(true); + + state().markSeen("s1"); + expect(state().getSession("s1")?.flags).toEqual({ + attention: false, + done: false, + error: false, + }); + }); + + it("clears both flags.done and flags.error simultaneously", () => { + state().setSessions("w1", [ + fixture({ sessionId: "s1", workspaceId: "w1", flags: { done: true, error: true } }), + ]); + + state().markSeen("s1"); + expect(state().getSession("s1")?.flags).toEqual({ + attention: false, + done: false, + error: false, + }); + }); + + it("keeps flags.attention when attention array is non-empty", () => { + state().setSessions("w1", [ + fixture({ + sessionId: "s1", + workspaceId: "w1", + flags: { done: true, attention: true }, + attention: [{ id: "q1" } as any], + }), + ]); + + state().markSeen("s1"); + expect(state().getSession("s1")?.flags).toEqual({ + attention: true, + done: false, + error: false, + }); + }); + + it("sets flags.attention to true if attention array has items even when flag was false", () => { + state().setSessions("w1", [ + fixture({ + sessionId: "s1", + workspaceId: "w1", + flags: { done: true, attention: false }, + attention: [{ id: "q1" } as any], + }), + ]); + + state().markSeen("s1"); + expect(state().getSession("s1")?.flags).toEqual({ + attention: true, + done: false, + error: false, + }); + }); + + it("clears stale flags.attention when attention array is empty", () => { + state().setSessions("w1", [ + fixture({ + sessionId: "s1", + workspaceId: "w1", + flags: { attention: true, done: false, error: false }, + attention: [], + }), + ]); + + state().markSeen("s1"); + expect(state().getSession("s1")?.flags).toEqual({ + attention: false, + done: false, + error: false, + }); + }); + + it("leaves other rows untouched in the same and other workspaces", () => { + const s1 = fixture({ sessionId: "s1", workspaceId: "w1", flags: { done: true } }); + const s2 = fixture({ sessionId: "s2", workspaceId: "w1", flags: { done: true } }); + const s3 = fixture({ sessionId: "s3", workspaceId: "w2", flags: { error: true } }); + + state().setSessions("w1", [s1, s2]); + state().setSessions("w2", [s3]); + + const s2Before = state().getSession("s2"); + const s3Before = state().getSession("s3"); + + state().markSeen("s1"); + + expect(state().getSession("s1")?.flags.done).toBe(false); + // Untouched rows preserve flags and object identity + expect(state().getSession("s2")).toBe(s2Before); + expect(state().getSession("s2")?.flags.done).toBe(true); + expect(state().getSession("s3")).toBe(s3Before); + expect(state().getSession("s3")?.flags.error).toBe(true); + }); + + it("returns the same state object (identity) and does not notify subscribers when there is nothing to consume", () => { + state().setSessions("w1", [ + fixture({ + sessionId: "s1", + workspaceId: "w1", + flags: { done: false, error: false, attention: false }, + attention: [], + }), + ]); + + const before = useSessionsStore.getState(); + const listener = vi.fn(); + const unsub = useSessionsStore.subscribe(listener); + + state().markSeen("s1"); + + const after = useSessionsStore.getState(); + expect(after).toBe(before); + expect(listener).not.toHaveBeenCalled(); + + unsub(); + }); + + it("is a no-op for an unknown id, returning the same state object without notifying subscribers", () => { + state().setSessions("w1", [ + fixture({ sessionId: "s1", workspaceId: "w1", flags: { done: true } }), + ]); + + const before = useSessionsStore.getState(); + const listener = vi.fn(); + const unsub = useSessionsStore.subscribe(listener); + + state().markSeen("unknown-id"); + + const after = useSessionsStore.getState(); + expect(after).toBe(before); + expect(listener).not.toHaveBeenCalled(); + expect(state().getSession("s1")?.flags.done).toBe(true); + + unsub(); + }); +});