Official
sidebar
Built-in sidebar UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/sidebar@0.2.6
Permissions in 0.2.6
Files
sections.test.ts35 KB
// The sidebar's list rules, without a window: real imports of the pure
// model.
import assert from "node:assert/strict";
import test from "node:test";
import type { Chat } from "divergence";
import type { Task } from "divergence/protocol";
import {
applyAgentEvent,
applyChatChange,
applyChatRemoved,
applyTranscriptReset,
arrangementFrom,
collapsedFrom,
isSnoozed,
snooze,
unsnooze,
chatById,
DEFAULT_ARRANGEMENT,
dragSection,
dropSection,
moveSection,
archivedChild,
archivedRow,
canPlace,
chatStatus,
dragOnDock,
dragOver,
dropDragged,
shownChats,
endDrag,
elapsed,
FINISHED_FOR_MS,
nextTimedMove,
initialState,
listedChats,
prunePlaced,
recentAgent,
sectionOf,
sections,
togglePinned,
undoLabel,
visibleChats,
stillWorking,
completedOf,
noteTask,
workspaceLooks,
} from "./sections.ts";
import { COMMANDS_HOLD_FOR_MS } from "../sdk/ui/status.ts";
import { workspaceMarkOf } from "../sdk/ui/workspace.ts";
import type { Over, SidebarState } from "./sections.ts";
/** A chat's own task, as the host keeps one for every chat; `completedAt` checks it off. */
const task = (id: string, completedAt: string | null = null, chats: string[] = [id]): Task => ({
id,
layout: {
root: { kind: "pane", id: `pane-${id}`, tabs: chats.map((chatId) => ({ kind: "chat" as const, chatId })), active: { kind: "chat", chatId: chats[0]! } },
focused: `pane-${id}`,
},
completedAt,
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
});
/** Checks a row off (or gives it an open task, with `null`). */
const checkOff = (state: SidebarState, id: string, at: string | null) => noteTask(state, task(id, at));
let n = 0;
const chat = (over: Partial<Chat> = {}): Chat => ({
id: `chat-${++n}`,
workspaceId: "ws",
agentId: "agent",
title: "Chat",
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-02T00:00:00.000Z",
archived: false,
options: {},
status: "idle",
...over,
});
test("chat status ranks blocked first, then running, review, idle", () => {
const state = initialState();
state.finished = { r: Date.now() };
assert.equal(chatStatus(state, { id: "a", status: "waiting_approval" }), "blocked");
assert.equal(chatStatus(state, { id: "a", status: "waiting_question" }), "blocked");
assert.equal(chatStatus(state, { id: "a", status: "running" }), "running");
assert.equal(chatStatus(state, { id: "r", status: "idle", sessionId: "s" }), "review");
assert.equal(chatStatus(state, { id: "x", status: "idle" }), "idle");
});
test("a chat whose background subagents still work stays running after its turn", () => {
const state = initialState();
state.subagents = {
a: new Map([
["t1", "running"],
["t2", "completed"],
]),
};
assert.equal(chatStatus(state, { id: "a", status: "idle", sessionId: "s" }), "running");
state.subagents.a?.set("t1", "completed");
assert.equal(chatStatus(state, { id: "a", status: "idle", sessionId: "s" }), "idle");
});
test("a chat the host counts subagents or background commands for runs after its turn", () => {
const state = initialState();
const chat = (agents: number, commands: number) => ({ id: "a", status: "idle" as const, sessionId: "s", working: { agents, commands } });
assert.equal(chatStatus(state, chat(1, 0)), "running", "a subagent keeps the spinner");
assert.equal(sectionOf(state, chat(1, 0)), "running", "and keeps the chat in Running");
assert.equal(chatStatus(state, chat(0, 1)), "idle", "a background command alone shows no spinner");
assert.equal(sectionOf(state, chat(0, 1)), "open", "a chat whose agent is done shows where it would without its commands");
delete state.finished.a;
assert.equal(chatStatus(state, chat(0, 0)), "idle");
// The host's count is the answer, even where this sidebar saw a subagent start and never end.
state.subagents = { a: new Map([["t1", "running"]]) };
assert.equal(chatStatus(state, chat(0, 0)), "idle");
assert.deepEqual(stillWorking(state, { id: "a", status: "idle" }), { agents: 1, commands: 0 }, "an older host's chat falls back to them");
});
test("a chat whose background command still runs stays in Running a minute after its run ends, without the spinner", () => {
const state = initialState();
const now = Date.now();
const running = chat({ id: "a", sessionId: "s", working: { agents: 0, commands: 1 } });
state.chats = [running];
state.finished.a = now - COMMANDS_HOLD_FOR_MS + 1_000;
assert.equal(sectionOf(state, running), "running");
assert.equal(chatStatus(state, running), "review", "its mark is the finished one, not the spinner");
assert.equal(nextTimedMove(state, now), now + 1_000, "the sidebar draws again when its minute is up");
const done = { ...running, working: { agents: 0, commands: 0 } };
assert.equal(sectionOf(state, done), "finished", "the command ending moves it to Finished at once");
state.finished.a = now - COMMANDS_HOLD_FOR_MS;
assert.equal(sectionOf(state, running), "finished", "after the minute it moves to Finished while the command runs");
assert.equal(nextTimedMove(state, now), now - COMMANDS_HOLD_FOR_MS + FINISHED_FOR_MS, "and then waits for its hour");
});
test("each chat shows in the section of its status", () => {
const state = initialState();
state.queued = new Set(["q"]);
state.finished = { f: Date.now() };
assert.equal(sectionOf(state, { id: "d", status: "idle" }), "drafts");
assert.equal(sectionOf(state, { id: "q", status: "idle" }), "queued");
assert.equal(sectionOf(state, { id: "b", status: "waiting_question", sessionId: "s" }), "needs-you");
assert.equal(sectionOf(state, { id: "r", status: "running" }), "running", "a sent first prompt is no draft");
state.starting = new Set(["s"]);
assert.equal(sectionOf(state, { id: "s", status: "idle" }), "running", "nor is one the host has not reported yet");
assert.equal(sectionOf(state, { id: "f", status: "idle", sessionId: "s" }), "finished");
assert.equal(sectionOf(state, { id: "o", status: "idle", sessionId: "s" }), "open");
checkOff(state, "c", "2026-01-01T00:00:00Z");
assert.equal(sectionOf(state, { id: "c", status: "idle", sessionId: "s" }), "settled");
});
test("a finished chat moves from Finished to Open an hour after its run ended", () => {
const state = initialState();
const now = Date.now();
const done = chat({ id: "d", sessionId: "s" });
const failed = chat({ id: "f", sessionId: "s", status: "failed", updatedAt: new Date(now - 10 * 60_000).toISOString() });
state.chats = [done, failed];
state.finished.d = now - FINISHED_FOR_MS + 60_000;
assert.equal(sectionOf(state, done), "finished");
assert.equal(chatStatus(state, done), "review");
assert.equal(sectionOf(state, failed), "finished", "a failed turn without a stamp counts from its last activity");
assert.equal(nextTimedMove(state, now), now + 60_000, "the sidebar draws again when the first one is due");
state.finished.d = now - FINISHED_FOR_MS;
assert.equal(sectionOf(state, done), "open");
assert.equal(chatStatus(state, done), "idle");
state.chats = [done, { ...failed, updatedAt: new Date(now - FINISHED_FOR_MS).toISOString() }];
assert.equal(sectionOf(state, state.chats[1]!), "open");
assert.equal(chatStatus(state, state.chats[1]!), "failed", "it still shows its turn failed");
assert.equal(nextTimedMove(state, now), undefined);
});
test("a chat checked off moves to Complete at once, whatever it was doing", () => {
const state = initialState();
state.finished.c = Date.now();
checkOff(state, "c", "2026-01-01T00:00:00Z");
assert.equal(sectionOf(state, { id: "c", status: "idle" as const, sessionId: "s" }), "settled");
});
test("sections leave empty ones out and put the newest checked off first", () => {
const state = initialState();
state.chats = [
chat({ id: "old-done", sessionId: "s" }),
chat({ id: "new-done", sessionId: "s" }),
chat({ id: "open", sessionId: "s" }),
];
checkOff(state, "old-done", "2026-01-01T00:00:00Z");
checkOff(state, "new-done", "2026-02-01T00:00:00Z");
assert.deepEqual(
sections(state).map((s) => [s.id, s.chats.map((c) => c.id)]),
[
["open", ["open"]],
["settled", ["new-done", "old-done"]],
],
);
});
test("the list holds the workspace's chats, newest activity first, without archived ones", () => {
const state = initialState();
state.chats = [
chat({ id: "old", updatedAt: "2026-01-01T00:00:00.000Z" }),
chat({ id: "new", updatedAt: "2026-03-01T00:00:00.000Z" }),
chat({ id: "archived", archived: true, updatedAt: "2026-04-01T00:00:00.000Z" }),
chat({ id: "elsewhere", workspaceId: "other", updatedAt: "2026-02-01T00:00:00.000Z" }),
];
assert.deepEqual(
visibleChats(state).map((c) => c.id),
["new", "elsewhere", "old"],
);
state.filter = ["ws"];
assert.deepEqual(
visibleChats(state).map((c) => c.id),
["new", "old"],
);
state.filter = ["ws", "other"];
assert.deepEqual(
visibleChats(state).map((c) => c.id),
["new", "elsewhere", "old"],
);
});
test("event mutators keep chats and previews current", () => {
const state = initialState();
const c = chat({ id: "c" });
applyChatChange(state, c);
applyChatChange(state, { ...c, title: "Renamed" });
assert.equal(state.chats[0]?.title, "Renamed");
state.previews.c = { line: "x" } as SidebarState["previews"][string];
applyTranscriptReset(state, "c");
assert.equal("c" in state.previews, false);
state.previews.c = { line: "x" } as SidebarState["previews"][string];
applyChatRemoved(state, "c");
assert.equal(state.chats.length, 0);
assert.equal("c" in state.previews, false);
});
test("recentAgent follows the most recently touched enabled chat", () => {
const state = initialState();
state.agents = [
{ id: "a", name: "A" },
{ id: "b", name: "B" },
] as SidebarState["agents"];
state.chats = [
chat({ id: "old", agentId: "a", updatedAt: "2026-01-01T00:00:00.000Z" }),
chat({ id: "new", agentId: "b", updatedAt: "2026-02-01T00:00:00.000Z" }),
chat({ id: "disabled", agentId: "z", updatedAt: "2026-03-01T00:00:00.000Z" }),
];
assert.equal(recentAgent(state, "ws"), "b");
});
test("elapsed renders short relative times", () => {
const now = new Date("2026-01-02T00:00:00.000Z").getTime();
assert.equal(elapsed(now, "2026-01-01T00:00:30.000Z"), "23h");
assert.equal(elapsed(now, "2026-01-01T23:59:10.000Z"), "50s");
assert.equal(elapsed(now, "2026-01-02T00:00:10.000Z"), "");
});
test("the chat service starts unfollowed, with an empty queue and no draft titles", () => {
const state = initialState();
assert.equal(state.chat, undefined);
assert.equal(state.follow, undefined);
assert.deepEqual([...state.queued], []);
assert.deepEqual(state.draftTitles, {});
});
test("agent events redraw only when the visible line changes", () => {
const state = initialState();
assert.equal(
applyAgentEvent(state, {
event: "agent",
chatId: "c",
kind: { event: "task", id: "t1", title: "T", status: "running" },
}),
true,
);
assert.equal(
applyAgentEvent(state, {
event: "agent",
chatId: "c",
kind: { event: "task", id: "t1", title: "T", status: "running" },
}),
false,
);
// A subagent's own turn is not the chat's line.
assert.equal(
applyAgentEvent(state, {
event: "agent",
chatId: "c",
taskId: "t1",
kind: { event: "text_delta", itemId: "a", text: "hi", mode: "append" },
}),
false,
);
assert.equal(
applyAgentEvent(state, {
event: "agent",
chatId: "c",
kind: { event: "text_delta", itemId: "a", text: "hello", mode: "append" },
}),
true,
);
assert.equal(
applyAgentEvent(state, {
event: "agent",
chatId: "c",
kind: { event: "run_finished", outcome: { status: "completed" } },
}),
true,
);
});
test("an older host's task events count the subagents a chat runs, not its background commands", () => {
const state = initialState();
const task = (id: string, kind?: "agent" | "terminal") =>
applyAgentEvent(state, {
event: "agent",
chatId: "c",
kind: { event: "task", id, title: "T", status: "running", ...(kind ? { kind } : {}) },
});
const chat = { id: "c", status: "idle" as const };
assert.equal(task("shell", "terminal"), false);
assert.deepEqual(stillWorking(state, chat), { agents: 0, commands: 0 });
assert.equal(task("agent"), true);
assert.deepEqual(stillWorking(state, chat), { agents: 1, commands: 0 });
});
test("a workspace's mark is its icon, else its coloured folder, or the folder when chosen", () => {
const base = { id: "w", name: "w", path: "/w", position: 0 };
assert.deepEqual(workspaceMarkOf({ ...base, icon: "/w/icon.png", color: "teal" }), { image: "/w/icon.png" });
assert.deepEqual(workspaceMarkOf({ ...base, icon: "/w/icon.png", color: "teal", useColor: true }), {
icon: "folder-solid",
color: "teal",
});
assert.deepEqual(workspaceMarkOf({ ...base, color: "pink" }), { icon: "folder-solid", color: "pink" });
assert.deepEqual(workspaceMarkOf({ ...base, mark: "divergence" }), { icon: "divergence" });
// An older host sends no colour: a plain folder.
assert.deepEqual(workspaceMarkOf(base), { icon: "folder" });
const looks = workspaceLooks({ ...base, icon: "/w/icon.png", color: "teal" }, ["blue", "teal"]);
assert.equal(looks.value, "icon");
assert.deepEqual(
looks.options.map((option) => option.value),
["icon", "blue", "teal"],
);
assert.equal(workspaceLooks({ ...base, color: "teal" }, ["blue", "teal"]).value, "teal");
});
test("the listed chats run top to bottom: section order, folded sections out, paged sections a page", () => {
const state = initialState();
state.collapsed = {};
const drafts = Array.from({ length: 7 }, (_, i) => chat({ updatedAt: `2026-01-0${9 - i}T00:00:00.000Z` }));
const running = chat({ status: "running", sessionId: "s1" });
const open = chat({ sessionId: "s2", updatedAt: "2026-01-09T00:00:00.000Z" });
const done = chat({ sessionId: "s3" });
state.chats = [done, open, running, ...drafts];
checkOff(state, done.id, "2026-01-03T00:00:00.000Z");
const ids = () => listedChats(state).map((c) => c.id);
assert.deepEqual(ids(), [...drafts.slice(0, 5), open, running, done].map((c) => c.id));
state.showAll.drafts = true;
assert.deepEqual(ids(), [...drafts, open, running, done].map((c) => c.id));
state.collapsed.drafts = true;
assert.deepEqual(ids(), [open, running, done].map((c) => c.id));
});
test("a chat whose turn failed shows failed, not finished, in Finished", () => {
const state = initialState();
const failed = chat({ sessionId: "s", status: "failed" });
state.finished[failed.id] = Date.now();
assert.equal(chatStatus(state, failed), "failed");
assert.equal(sectionOf(state, failed), "finished");
assert.equal(chatStatus(state, { ...failed, status: "running" }), "running", "a new turn runs again");
});
// Drags the chat `id` over each of `overs` in turn and drops it, as an
// older kernel does: it tells only where the chat was dropped.
const drag = (state: SidebarState, id: string, ...overs: Over[]) => {
for (const over of overs.slice(0, -1)) dragOver(state, id, over);
return dropDragged(state, id, overs.at(-1));
};
const ids = (state: SidebarState, section: string) =>
sections(state)
.find((s) => s.id === section)
?.chats.map((c) => c.id);
test("a chat dragged in its section stays where it was put, even as its neighbours get newer", () => {
const state = initialState();
const at = (day: number) => `2026-01-0${day}T00:00:00.000Z`;
state.chats = [1, 2, 3, 4].map((day) => chat({ id: `c${day}`, sessionId: "s", updatedAt: at(day) }));
assert.deepEqual(ids(state, "open"), ["c4", "c3", "c2", "c1"]);
assert.deepEqual(drag(state, "c1", { row: "c4", zone: "before" }), { completed: undefined });
assert.deepEqual(ids(state, "open"), ["c1", "c4", "c3", "c2"]);
drag(state, "c4", { row: "c2", zone: "after" });
assert.deepEqual(ids(state, "open"), ["c1", "c3", "c2", "c4"]);
// A kernel without `reorder` reports `into`: the chat takes the other's place.
drag(state, "c2", { row: "c1", zone: "into" });
assert.deepEqual(ids(state, "open"), ["c2", "c1", "c3", "c4"]);
// A neighbour's new activity does not move it past the chat put there.
applyChatChange(state, { ...state.chats.find((c) => c.id === "c4")!, updatedAt: "2026-02-01T00:00:00.000Z" });
assert.deepEqual(ids(state, "open"), ["c2", "c1", "c3", "c4"]);
// A chat new to the section comes in at the top.
state.chats = [...state.chats, chat({ id: "new", sessionId: "s", updatedAt: new Date(Date.now() + 1000).toISOString() })];
assert.deepEqual(ids(state, "open"), ["new", "c2", "c1", "c3", "c4"]);
// Dropped where it already is, nothing moves.
assert.equal(drag(state, "c1", { row: "c2", zone: "after" }), null);
// A chat that starts running leaves Open; its place there is forgotten.
applyChatChange(state, { ...state.chats.find((c) => c.id === "c2")!, status: "running" });
assert.equal(prunePlaced(state), true);
assert.equal(state.placed["c2"], undefined);
assert.equal(prunePlaced(state), false);
});
test("while a chat is dragged its place follows the pointer, and over a section it may not enter it goes home", () => {
const state = initialState();
state.chats = [chat({ id: "a", sessionId: "s" }), chat({ id: "b", sessionId: "s" }), chat({ id: "r", sessionId: "s", status: "running" })];
checkOff(state, "d", "2026-01-03T00:00:00.000Z");
state.chats = [...state.chats, chat({ id: "d", sessionId: "s" })];
assert.deepEqual(
sections(state).map((s) => s.id),
["open", "running", "settled"],
);
assert.equal(dragOver(state, "a", { row: "a", zone: "before" }), true, "the first word starts the drag");
// Over Complete's row, a's place is there; over Running, which does not take an idle chat, it goes home.
assert.equal(dragOver(state, "a", { row: "d", zone: "after" }, "dock"), true);
assert.deepEqual(ids(state, "settled"), ["d", "a"]);
assert.equal(dragOver(state, "a", { row: "r", zone: "after" }, "dock"), true);
assert.deepEqual(ids(state, "open"), ["a", "b"]);
assert.deepEqual(ids(state, "settled"), ["d"]);
assert.equal(dropDragged(state, "a"), null, "let go over Running, it lands nowhere");
// A heading puts it at the top of its section.
dragOver(state, "b", { heading: "settled" }, "dock");
assert.deepEqual(ids(state, "settled"), ["b", "d"]);
// Off the list it goes back where it was, and a drag let go there lands nowhere.
assert.equal(dragOver(state, "b", { inside: false }), true);
assert.deepEqual(ids(state, "open"), ["a", "b"]);
assert.equal(endDrag(state), true);
assert.deepEqual(state.placed, {});
});
test("a drag shows the empty sections it may land in only where they do not move the dragged row", () => {
const state = initialState();
state.chats = [chat({ id: "a", sessionId: "s" }), chat({ id: "r", sessionId: "s", status: "running" })];
// From Open in the list: empty Pinned and Finished above it stay hidden, and empty Snoozed and Complete show in the dock.
dragOver(state, "a", { row: "a", zone: "before" });
assert.deepEqual(
sections(state).map((s) => s.id),
["open", "running", "snoozed", "settled"],
);
endDrag(state);
// From Running in the dock: the list's sections show, and Complete docked below Running stays hidden.
dragOver(state, "r", { row: "r", zone: "before" }, "dock");
assert.deepEqual(
sections(state).map((s) => s.id),
["pinned", "open", "running"],
);
endDrag(state);
// A section moved above Running in the dock shows, since the dock grows up from its foot.
state.arrangement = { list: DEFAULT_ARRANGEMENT.list, dock: ["settled", "running"] };
dragOver(state, "r", { row: "r", zone: "before" }, "dock");
assert.deepEqual(
sections(state).map((s) => s.id),
["pinned", "open", "settled", "running"],
);
});
test("while the pointer is over the dock, the rows of the list it covers are not heard", () => {
const state = initialState();
state.chats = [chat({ id: "a", sessionId: "s" }), chat({ id: "b", sessionId: "s" }), chat({ id: "d", sessionId: "s" })];
checkOff(state, "d", "2026-01-03T00:00:00.000Z");
dragOver(state, "a", { row: "a", zone: "before" });
assert.equal(dragOnDock(state, "a", true), false, "the dock said nothing yet");
dragOver(state, "a", { row: "d", zone: "before" }, "dock");
// b lies under the dock: what it says waits.
assert.equal(dragOver(state, "a", { row: "b", zone: "after" }), false);
assert.deepEqual(ids(state, "settled"), ["a", "d"]);
// Off the dock, the list's last word counts.
assert.equal(dragOnDock(state, "a", false), true);
assert.deepEqual(ids(state, "open"), ["b", "a"]);
});
test("a chat dragged past a page's end keeps its place in view", () => {
const state = initialState();
state.collapsed = {};
// Complete in the list, where it shows a page of five.
state.arrangement = { list: [...DEFAULT_ARRANGEMENT.list, "settled"], dock: ["running"] };
const done = [1, 2, 3, 4, 5, 6, 7].map((day) => chat({ id: `d${day}`, sessionId: "s" }));
state.chats = [chat({ id: "a", sessionId: "s" }), ...done];
done.forEach((c, at) => checkOff(state, c.id, `2026-01-0${9 - at}T00:00:00.000Z`));
dragOver(state, "a", { row: "d6", zone: "after" }, "list");
const complete = sections(state).find((s) => s.id === "settled")!;
assert.deepEqual(shownChats(state, complete).map((c) => c.id), ["d1", "d2", "d3", "d4", "d5", "d6", "a"]);
});
test("dragging a chat to another section changes its state", () => {
const state = initialState();
const open = chat({ id: "o", sessionId: "s" });
const done = chat({ id: "d", sessionId: "s" });
const failed = chat({ id: "f", sessionId: "s", status: "failed" });
const reviewed = chat({ id: "v", sessionId: "s" });
state.finished[reviewed.id] = Date.now();
state.chats = [open, done, failed, reviewed];
for (const c of state.chats) checkOff(state, c.id, c.id === "d" ? "2026-01-03T00:00:00.000Z" : null);
// To Pinned: pinned, above Running, whatever it does next.
assert.deepEqual(drag(state, "o", { heading: "pinned" }), { completed: undefined });
assert.equal(sectionOf(state, state.chats[0]!), "pinned");
applyChatChange(state, { ...state.chats[0]!, status: "running" });
assert.equal(prunePlaced(state), false);
assert.equal(sectionOf(state, state.chats[0]!), "pinned");
// To Complete: checked off, and the pin is gone.
assert.deepEqual(drag(state, "o", { row: "d", zone: "before" }), { completed: true });
assert.ok(completedOf(state, { id: "o" }));
assert.deepEqual(ids(state, "settled"), ["o", "d"]);
// Out of Complete: opened again.
assert.deepEqual(drag(state, "d", { heading: "open" }), { completed: false });
assert.equal(completedOf(state, { id: "d" }), undefined);
assert.equal(sectionOf(state, state.chats.find((c) => c.id === "d")!), "open");
// Finished to Open: a finished chat is seen; a failed one stays in Open until it runs again.
drag(state, "v", { heading: "open" });
assert.equal(state.finished["v"], undefined);
drag(state, "f", { heading: "open" });
assert.equal(sectionOf(state, failed), "open");
// Open to Finished, until it runs again.
drag(state, "d", { heading: "finished" });
assert.equal(sectionOf(state, state.chats.find((c) => c.id === "d")!), "finished");
applyChatChange(state, { ...state.chats.find((c) => c.id === "d")!, status: "running" });
prunePlaced(state);
applyChatChange(state, { ...state.chats.find((c) => c.id === "d")!, status: "idle" });
assert.equal(sectionOf(state, state.chats.find((c) => c.id === "d")!), "open");
});
test("a pin survives the moment a just-sent chat shows among the drafts", () => {
const state = initialState();
const sent = chat({ id: "s" });
state.chats = [sent];
state.starting = new Set(["s"]);
togglePinned(state, sent);
assert.equal(sectionOf(state, sent), "pinned");
// The send ended before the host reported the run.
state.starting = new Set();
assert.equal(sectionOf(state, sent), "drafts");
assert.equal(prunePlaced(state), false);
applyChatChange(state, { ...sent, status: "running", sessionId: "x" });
assert.equal(sectionOf(state, state.chats[0]!), "pinned");
// Checked off, the pin goes.
checkOff(state, state.chats[0]!.id, "2026-01-03T00:00:00.000Z");
assert.equal(prunePlaced(state), true);
});
test("a running chat can be pinned or checked off, but not moved to Open or Finished", () => {
const state = initialState();
state.chats = [chat({ id: "r", sessionId: "s", status: "running" }), chat({ id: "o", sessionId: "s" })];
dragOver(state, "r", { row: "o", zone: "before" });
assert.deepEqual(ids(state, "running"), ["r"]);
assert.equal(dropDragged(state, "r"), null);
});
test("a draft moves only among the drafts", () => {
const state = initialState();
state.chats = [chat({ id: "d1" }), chat({ id: "d2" }), chat({ id: "o", sessionId: "s" })];
assert.equal(drag(state, "d1", { heading: "pinned" }), null);
assert.equal(drag(state, "d2", { row: "o", zone: "before" }), null);
assert.deepEqual(drag(state, "d2", { row: "d1", zone: "before" }), { completed: undefined });
});
test("the pin button pins a chat at the top of Pinned and unpins it back", () => {
const state = initialState();
const a = chat({ id: "a", sessionId: "s" });
const b = chat({ id: "b", sessionId: "s" });
state.chats = [a, b];
togglePinned(state, a);
assert.deepEqual(
sections(state).map((s) => s.id),
["pinned", "open"],
);
togglePinned(state, b);
assert.deepEqual(ids(state, "pinned"), ["b", "a"]);
togglePinned(state, a);
assert.deepEqual(ids(state, "open"), ["a"]);
});
test("queued chats keep the queue's order and cannot be dragged", () => {
const state = initialState();
state.chats = [chat({ id: "q1" }), chat({ id: "q2" })];
state.queued = new Set(["q1", "q2"]);
assert.equal(canPlace("queued"), false);
assert.equal(drag(state, "q1", { row: "q2", zone: "after" }), null);
});
test("an archived row keeps where it was: its section and rank, a pin, its check and its task's chats and name", () => {
const state = initialState();
const newer = chat({ id: "n", sessionId: "s", updatedAt: "2026-01-03T00:00:00.000Z" });
const older = chat({ id: "o", sessionId: "s", updatedAt: "2026-01-02T00:00:00.000Z", title: "Older" });
const done = chat({ id: "d", sessionId: "s" });
const lead = chat({ id: "a", sessionId: "s", title: "Plan" });
const member = chat({ id: "b", sessionId: "s", title: "Build" });
state.chats = [newer, older, done, lead, member];
for (const id of ["n", "o"]) checkOff(state, id, null);
checkOff(state, "d", "2026-01-05T00:00:00.000Z");
noteTask(state, { ...task("t", null, ["a", "b"]), title: "Release" });
// An open chat keeps its rank there, which coming back would otherwise change.
assert.deepEqual(archivedRow(state, older), {
title: "Older",
chatIds: ["o"],
name: null,
completed: false,
queued: false,
placed: { section: "open", rank: Date.parse(older.updatedAt) },
from: null,
});
// A checked-off one keeps when it was checked off, and a pin is the user's own place.
assert.deepEqual(archivedRow(state, done).placed, { section: "settled", rank: Date.parse("2026-01-05T00:00:00.000Z") });
assert.equal(archivedRow(state, done).completed, true);
togglePinned(state, newer);
assert.deepEqual(archivedRow(state, newer).placed, state.placed["n"]);
// A task row takes its chats and its name; one of its chats alone, its tab's place.
assert.deepEqual(archivedRow(state, lead).chatIds, ["a", "b"]);
assert.equal(archivedRow(state, lead).name, "Release");
assert.deepEqual(archivedChild(state, member).from, { taskId: "t", paneId: "pane-t", index: 1 });
assert.equal(archivedChild(state, member).placed, null);
// The notice names one row, and counts the chats of several.
assert.equal(undoLabel([archivedRow(state, older)]), "Archived “Older”");
assert.equal(undoLabel([archivedRow(state, older), archivedRow(state, lead)]), "Archived 3 chats");
});
test("a section moves beside another into that one's part, or to the end of the list or the dock", () => {
const start = DEFAULT_ARRANGEMENT;
assert.deepEqual(moveSection(start, "pinned", { section: "settled", zone: "after" }).dock, ["running", "snoozed", "settled", "pinned"]);
assert.deepEqual(moveSection(start, "settled", { section: "drafts", zone: "before" }), {
list: ["settled", "drafts", "pinned", "queued", "needs-you", "finished", "open"],
dock: ["running", "snoozed"],
});
// The boundary: after the list's last section stays in the list, before the dock's first docks.
assert.deepEqual(moveSection(start, "pinned", { section: "open", zone: "after" }).dock, ["running", "snoozed", "settled"]);
assert.deepEqual(moveSection(start, "pinned", { section: "running", zone: "before" }).dock, ["pinned", "running", "snoozed", "settled"]);
assert.deepEqual(moveSection({ list: start.list, dock: [] }, "open", { area: "dock" }).dock, ["open"]);
});
test("a saved order is made whole: unknown and repeated sections go, and missing ones follow their default neighbour", () => {
assert.deepEqual(arrangementFrom({ list: ["open", "nope", "open"], dock: ["settled", "open"] }), {
list: ["open", "drafts", "pinned", "queued", "needs-you", "finished", "running", "snoozed"],
dock: ["settled"],
});
// An order saved before Snoozed existed gets it right after Running, wherever Running is.
assert.deepEqual(arrangementFrom({ list: DEFAULT_ARRANGEMENT.list, dock: ["running", "settled"] }), DEFAULT_ARRANGEMENT);
assert.deepEqual(arrangementFrom({ list: ["running", ...DEFAULT_ARRANGEMENT.list], dock: ["settled"] }).list.slice(0, 2), ["running", "snoozed"]);
});
test("a dragged section's place follows the pointer, a pointer gone puts it back, and only a drop keeps the order", () => {
const state = initialState();
state.chats = [chat({ sessionId: "s" })];
assert.equal(dragSection(state, "open", { section: "open", zone: "before" }), true, "the first word starts the drag");
assert.deepEqual(sections(state).find((s) => s.id === "open")?.chats.length, 1, "the section keeps its chats");
assert.equal(dragSection(state, "open", { section: "settled", zone: "after" }), true);
assert.deepEqual(sections(state).map((s) => [s.id, s.docked]), [["open", true]]);
assert.equal(dragSection(state, "open", { inside: true }), false, "over the sidebar elsewhere it stays");
assert.equal(dragSection(state, "open", { inside: false }), true);
assert.deepEqual(state.movingSection?.to, state.arrangement);
assert.equal(dropSection(state, "open"), false, "dropped where it came from, nothing changes");
dragSection(state, "open", { section: "running", zone: "before" });
assert.equal(dropSection(state, "open"), true);
assert.equal(state.movingSection, null);
assert.deepEqual(state.arrangement.dock, ["open", "running", "snoozed", "settled"]);
assert.equal(dropSection(state, "open"), false, "a second drop of the same drag changes nothing");
});
// A draw asked for a row's task chats once or more per row, and each call
// indexed every chat again: a draw grew with the square of the chats, and a
// sidebar of 830 chats spent 900 ms on each.
test("the chats are indexed once per list, and a change indexes the new list", () => {
const state = initialState();
state.chats = [chat({ id: "a", sessionId: "s" }), chat({ id: "b", sessionId: "s" })];
const first = chatById(state);
assert.equal(chatById(state), first, "the same list keeps its index");
applyChatChange(state, { ...state.chats[0]!, title: "Renamed" });
assert.notEqual(chatById(state), first, "a change replaces the list, and with it the index");
assert.equal(chatById(state).get("a")?.title, "Renamed");
applyChatChange(state, chat({ id: "c", sessionId: "s" }));
assert.deepEqual([...chatById(state).keys()], ["c", "a", "b"]);
});
test("a snoozed chat waits in Snoozed until its time, then comes back at the top of Open", () => {
const state = initialState();
const now = Date.now();
const older = chat({ id: "older", sessionId: "s", updatedAt: "2026-01-01T00:00:00.000Z" });
const newer = chat({ id: "newer", sessionId: "s", updatedAt: "2026-01-05T00:00:00.000Z" });
const forever = chat({ id: "forever", sessionId: "s" });
state.chats = [older, newer, forever];
togglePinned(state, older);
snooze(state, older, now + 60_000);
snooze(state, forever, null);
assert.equal(sectionOf(state, older), "snoozed", "snoozing a pinned chat takes its pin");
assert.deepEqual(ids(state, "snoozed"), ["forever", "older"], "the last snoozed first");
assert.equal(nextTimedMove(state, now), now + 60_000, "the sidebar draws again when the snooze ends");
assert.equal(prunePlaced(state, now), false, "a snooze still running is kept");
// Its time is up: it leaves Snoozed for the top of Open, above a chat that was active later.
assert.equal(isSnoozed(state, older, now + 60_000), false);
assert.equal(prunePlaced(state, now + 60_000), true);
assert.deepEqual(state.placed["older"], { section: "open", rank: now + 60_000, chosen: true });
assert.deepEqual(ids(state, "open"), ["older", "newer"]);
// One without a time stays until the user ends it, and a check-off wins over it.
assert.equal(sectionOf(state, forever), "snoozed");
checkOff(state, "forever", "2026-01-06T00:00:00.000Z");
assert.equal(sectionOf(state, forever), "settled");
checkOff(state, "forever", null);
unsnooze(state, forever);
assert.equal(sectionOf(state, forever), "open");
});
test("a chat dragged into Snoozed is snoozed until the user ends it, and the snoozed chats it passes keep their time", () => {
const state = initialState();
const until = Date.now() + 3_600_000;
const timed = chat({ id: "timed", sessionId: "s" });
const moved = chat({ id: "moved", sessionId: "s" });
state.chats = [timed, moved];
snooze(state, timed, until);
dragOver(state, "moved", { row: "moved", zone: "before" });
dragOver(state, "moved", { row: "timed", zone: "after" }, "dock");
dropDragged(state, "moved");
assert.deepEqual(ids(state, "snoozed"), ["timed", "moved"]);
assert.equal(state.placed["timed"]?.until, until);
assert.equal(state.placed["moved"]?.until, undefined);
// Dragged out to Open, its snooze ends.
dragOver(state, "timed", { row: "timed", zone: "before" }, "dock");
dragOver(state, "timed", { heading: "open" });
dropDragged(state, "timed");
assert.equal(sectionOf(state, timed), "open");
});
test("Snoozed starts folded, also for a folded set saved before it existed", () => {
assert.deepEqual(initialState().collapsed, { snoozed: true, settled: true });
assert.deepEqual(collapsedFrom(["open"]), { open: true, snoozed: true });
assert.deepEqual(collapsedFrom({ open: false, snoozed: false, settled: true }), { settled: true });
});
test("a snooze that ends while the agent works leaves the chat where its work puts it, and finishing later takes it to Finished", () => {
const state = initialState();
const now = Date.now();
const busy = chat({ id: "busy", sessionId: "s", status: "running" });
state.chats = [busy];
snooze(state, busy, now + 1_000);
assert.equal(sectionOf(state, busy), "snoozed");
assert.equal(prunePlaced(state, now + 1_000), true);
assert.equal(state.placed["busy"], undefined, "no place in Open is left behind");
assert.equal(sectionOf(state, busy), "running");
applyChatChange(state, { ...busy, status: "idle" });
applyAgentEvent(state, { chatId: "busy", kind: { event: "run_finished" } } as never);
assert.equal(sectionOf(state, state.chats[0]!), "finished");
});
No comments yet.