Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

46.2 KB
Raw Blame
import { Children, createElement, isValidElement, type ComponentProps, type KeyboardEvent, type MemoExoticComponent, type MouseEvent, type ReactElement, type ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { initialState, type AppState, type Bot, type Group } from "@/state/store";
import type { SidebarDensity } from "@/lib/sidebar-preferences";

const fixture = vi.hoisted(() => ({ advanced: false, showThreads: true, admin: true as boolean | null, state: {} as Partial<AppState>, dispatch: vi.fn() }));
vi.mock("@/lib/interface-mode", async (original) => ({
  ...await original<typeof import("@/lib/interface-mode")>(),
  useAdvancedMode: () => fixture.advanced,
}));
vi.mock("@/lib/thread-preferences", () => ({ useShowThreads: () => fixture.showThreads, useThreadsChosenOn: () => false }));
vi.mock("@/lib/use-owner-or-admin", () => ({ useOwnerOrAdmin: () => fixture.admin }));
vi.mock("./DesktopCapabilities", () => ({ useDesktopCapabilities: () => ({}) }));
vi.mock("react-dom", () => ({ createPortal: (node: ReactNode) => node }));
vi.mock("@/state/store", async (importOriginal) => {
  const original = await importOriginal<typeof import("@/state/store")>();
  return { ...original, useStore: () => ({ state: { ...original.initialState, ...fixture.state }, dispatch: fixture.dispatch }) };
});

import { BotContextMenu, BotListItem, BotThreadList, botRowProps, GroupListItem, PinnedBotCircle, type BotRowProps } from "./Sidebar";
import { SidebarBotActivity, sidebarBotActivityTasks } from "./SidebarBotActivity";
import { forgetSearchDisclosuresForTests } from "@/lib/search-disclosure";
import { noteTurnEvent, resetTurnLive } from "@/state/turn-status";
import type { RuntimeEvent } from "../../shared/runtime-events";

const bot: Bot = {
  id: "atlas", threadId: "last-selected", name: "Atlas", title: "", description: "",
  notifications: true, color: "green", unread: true,
  modelSelection: { instanceId: "fake", model: "test" }, messages: [],
  projects: [{ id: "private-folder", name: "Quiet folder" }],
  tasks: [
    { threadId: "last-selected", title: "Last selected conversation", createdAt: 1 },
    { threadId: "idle-history", title: "Idle history", createdAt: 2, projectId: "private-folder" },
    { threadId: "approval", title: "Review permission", createdAt: 3, busy: false, activity: "waiting-on-you", projectId: "private-folder" },
    { threadId: "working", title: "Running job", createdAt: 4, busy: true, activity: "working" },
    { threadId: "queued", title: "Next job", createdAt: 5 },
    { threadId: "unread", title: "Finished reply", createdAt: 6, unread: true },
  ],
};
const quiet: Bot = { ...bot, unread: false, tasks: bot.tasks!.filter((task) => !task.busy && !task.unread && !task.activity) };
const densities: SidebarDensity[] = ["comfortable", "icons"];
const botRow = (BotListItem as MemoExoticComponent<(props: BotRowProps) => ReactNode>).type;
const pinnedCircle = (PinnedBotCircle as MemoExoticComponent<(props: BotRowProps) => ReactNode>).type;
const rowProps = (density: SidebarDensity, candidate: Bot = bot, query = "") =>
  botRowProps({ ...initialState, ...fixture.state }, fixture.dispatch, candidate, { density, query, onMenu: vi.fn() });

type ElementProps = { children?: ReactNode; onClick?: (event: MouseEvent) => void; onKeyDown?: (event: KeyboardEvent) => void; [key: string]: unknown };
function findElement(tree: ReactNode, attribute: string, value: string): ReactElement<ElementProps> | undefined {
  for (const child of Children.toArray(tree)) {
    if (!isValidElement<ElementProps>(child)) continue;
    if (child.props[attribute] === value) return child;
    const found = findElement(child.props.children, attribute, value);
    if (found) return found;
  }
}

beforeEach(() => {
  fixture.advanced = false;
  fixture.showThreads = true;
  fixture.admin = true;
  fixture.state = { bots: [bot], selectedId: "other-bot", activeView: "chat", pendingQueued: { queued: [{ queueId: "q", text: "next" }] } };
  fixture.dispatch.mockClear();
  vi.stubGlobal("window", { innerWidth: 1024, innerHeight: 768 });
  vi.stubGlobal("document", { body: {} });
  vi.stubGlobal("HTMLInputElement", class {});
});
afterEach(() => { vi.unstubAllGlobals(); });

describe("bot-first sidebar", () => {
  it.each([false, true])("uses the selected mode for bot and room tool previews (advanced: %s)", (advanced) => {
    fixture.advanced = advanced;
    fixture.showThreads = false;
    const messages: Bot["messages"] = [{ id: "step", role: "bot", kind: "activity", at: 1, tool: { name: "mcp__omb__computer_batch", ok: true } }];
    const candidate = { ...bot, messages, tasks: [], projects: [] };
    const group: Group = { id: "room", threadId: "room-thread", name: "Team", memberIds: [bot.id], messages, defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 1 };
    const expected = advanced ? "mcp__omb__computer_batch" : "Use the screen";
    const other = advanced ? "Use the screen" : "mcp__omb__computer_batch";
    for (const markup of [
      renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate))),
      renderToStaticMarkup(createElement(GroupListItem, { group, density: "comfortable", onMenu: vi.fn() })),
    ]) {
      expect(markup).toContain(expected);
      expect(markup).not.toContain(other);
    }
  });

  it.each([
    { advanced: true, enabled: true, density: "comfortable", size: 32, spacing: ["gap-2", "py-2", "pl-6"] },
    { advanced: true, enabled: true, density: "icons", size: 44, spacing: ["justify-center", "px-1", "py-1.5"] },
    { advanced: true, enabled: false, density: "comfortable", size: 44, spacing: ["gap-3", "py-2", "pl-2.5", "pr-9"] },
    { advanced: false, enabled: false, density: "comfortable", size: 42, spacing: ["gap-2.5", "rounded-xl", "p-2", "md:h-[54px]"] },
    { advanced: false, enabled: false, density: "icons", size: 36, spacing: ["h-[54px]", "justify-center"] },
  ] as const)("sizes bot portraits and row spacing in $density density with threads $enabled (advanced $advanced)", ({ advanced, enabled, density, size, spacing }) => {
    fixture.advanced = advanced;
    fixture.showThreads = enabled;
    for (const avatar of [{}, { avatarUrl: "/api/attachments/portrait.png", avatarCrop: "circle" as const }]) {
      let tree: ReactNode;
      function Capture() { tree = botRow(rowProps(density, { ...bot, ...avatar })); return tree; }
      const markup = renderToStaticMarkup(createElement(Capture));
      const row = findElement(tree, "data-sidebar-bot-row", bot.id)!;
      expect(String(row.props.className).split(" ")).toEqual(expect.arrayContaining([...spacing]));
      expect(markup).toContain(avatar.avatarUrl
        ? `width="${size}" height="${size}"`
        : `width="${size}px" height="${size}px"`);
      // Simple draws its mascot larger than the 36px it takes in the row, so the text stays where it was.
      if (!advanced && density === "comfortable") expect(markup).toContain('<span class="relative flex shrink-0 -m-[3px]">');
    }
  });

  for (const enabled of [true, false]) {
    it.each(densities)(`opens the last selected conversation on mouse or keyboard, %s density, threads ${enabled ? "on" : "off"}`, (density) => {
      fixture.showThreads = enabled;
      let tree: ReactNode;
      function Capture() { tree = botRow(rowProps(density, quiet)); return tree; }
      renderToStaticMarkup(createElement(Capture));
      const row = findElement(tree, "data-sidebar-bot-row", bot.id)!;
      row.props.onClick!({ type: "click", target: {} } as MouseEvent);
      expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith({ type: "select", id: bot.id });
      expect(quiet.threadId).toBe("last-selected");
      for (const key of ["Enter", " "]) {
        fixture.dispatch.mockClear();
        const preventDefault = vi.fn();
        row.props.onKeyDown!({ key, preventDefault } as unknown as KeyboardEvent);
        expect(preventDefault).toHaveBeenCalledOnce();
        expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith({ type: "select", id: bot.id });
      }
    });
  }

  it.each([false, true])("uses 14px medium names in comfortable rows in both modes (advanced %s)", (advanced) => {
    fixture.advanced = advanced;
    fixture.showThreads = false;
    const markup = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...bot, tasks: [], projects: [] })));
    const name = markup.match(/<span class="flex min-w-0 grow items-center gap-1\.5 truncate text-ink ([^"]*)"/)?.[1];
    expect(name).toBe("text-[14px] font-medium leading-5");
    expect(markup).toContain("truncate text-[13px] leading-[18px]");
  });

  it("puts the bot's role in its name line in Advanced mode", () => {
    fixture.advanced = true;
    const markup = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...bot, title: "Research" })));
    const nameLine = markup.match(/<span class="flex min-w-0 grow items-center gap-1\.5 truncate text-ink [^"]*">([\s\S]*?)<\/span><\/span><\/span>/)?.[1] ?? "";
    expect(nameLine.indexOf(">Atlas<")).toBeGreaterThan(-1);
    expect(nameLine.indexOf('data-testid="bot-role"')).toBeGreaterThan(nameLine.indexOf(">Atlas<"));
    expect(nameLine).toContain("Research");
    expect(renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable")))).not.toContain('data-testid="bot-role"');
  });

  it("ends a Simple row's preview line with its engine's faded mark, puts the unread dot at the name line's top right where ⋯ shows, and never moves the name", () => {
    fixture.showThreads = false;
    fixture.state = { ...fixture.state, instances: [{ instanceId: "fake", driverKind: "claudeAgent", displayName: "Claude" }] as unknown as AppState["instances"] };
    const candidate = { ...bot, tasks: [], projects: [], unread: true, messages: [{ id: "m", role: "bot", kind: "text", text: "Done.", at: 1 }] } as Bot;
    let tree: ReactNode;
    function Capture() { tree = botRow(rowProps("comfortable", candidate)); return tree; }
    const markup = renderToStaticMarkup(createElement(Capture));
    const line = markup.slice(markup.indexOf(">Done.<"));
    expect(line).toMatch(/^>Done\.<\/span><\/span><span data-testid="model-badge" role="img" aria-label="Model: [^"]+ \(Claude\)" class="[^"]*\bms-auto\b[^"]*"><span data-provider-mark="brand" class="[^"]*\bopacity-60\b/);
    expect(line).toMatch(/^>Done\.<\/span><\/span><span data-testid="model-badge"[^>]*><span data-provider-mark="brand"[^>]*><svg[\s\S]*?<\/svg><\/span><\/span><\/div><span class="sr-only">Unread<\/span>/);
    const name = markup.slice(markup.indexOf('class="flex items-baseline justify-between gap-2 pe-7 group-hover:pe-[3.25rem] group-focus-within:pe-[3.25rem] touch:pe-[3.25rem] max-md:touch:pe-7"'), markup.indexOf(">Done.<"));
    expect(name).toMatch(/<span aria-hidden="true" data-row-dot="" class="pointer-events-none absolute right-\[3px\] top-1\.5 flex size-6 items-center justify-center transition-opacity group-hover:opacity-0 group-focus-within:opacity-0 touch:opacity-0 max-md:hidden"><span data-testid="unread-dot" title="Unread" class="size-2 shrink-0 rounded-full bg-accent"><\/span><\/span>/);
    expect(name).toContain('<span aria-hidden="true" title="Unread" class="size-2 shrink-0 rounded-full bg-accent hidden self-center max-md:block touch:block"></span>');
    expect(markup.match(/data-provider-mark=/g)).toHaveLength(1);
    expect(line.slice(0, line.indexOf("data-provider-mark"))).not.toContain("title=");
    expect(String(findElement(tree, "data-sidebar-bot-row", bot.id)!.props.className)).not.toContain("group-hover:pr-9");
    expect(markup).not.toContain("group-hover:pe-7");
    expect(markup).toMatch(/data-bot-actions="atlas"[^>]*class="[^"]*end-\[3px\] top-1\.5 size-6 translate-y-0\b/);
  });

  it("shows a Simple row's engine mark only while the row is hovered, focused or on a touch screen, and Advanced's always", () => {
    fixture.showThreads = false;
    fixture.state = { ...fixture.state, instances: [{ instanceId: "fake", driverKind: "claudeAgent", displayName: "Claude" }] as unknown as AppState["instances"] };
    const candidate = { ...bot, tasks: [], projects: [], messages: [{ id: "m", role: "bot", kind: "text", text: "Done.", at: 1 }] } as Bot;
    const badgeClass = (markup: string) => /<span data-testid="model-badge"[^>]*class="([^"]*)"/.exec(markup)?.[1]?.split(" ") ?? [];
    const simple = badgeClass(renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate))));
    expect(simple).toEqual(expect.arrayContaining(["opacity-0", "group-hover:opacity-100", "group-focus-within:opacity-100", "touch:opacity-100", "motion-reduce:transition-none"]));
    // The row it reveals on is the hover group, so focus anywhere in the row (its ⋯ too) shows the mark.
    expect(renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate)))).toMatch(/^<div class="group relative/);
    fixture.advanced = true;
    const advanced = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate)));
    expect(advanced).toContain('data-testid="model-badge"');
    expect(badgeClass(advanced)).not.toContain("opacity-0");
  });

  it("fades the collapsed rail's engine badge in both modes", () => {
    fixture.showThreads = false;
    fixture.state = { ...fixture.state, instances: [{ instanceId: "fake", driverKind: "claudeAgent", displayName: "Claude" }] as unknown as AppState["instances"] };
    const candidate = { ...bot, tasks: [], projects: [] };
    const rail = renderToStaticMarkup(createElement(BotListItem, rowProps("icons", candidate)));
    expect(rail).toMatch(/<span data-testid="model-badge"[^>]*><span data-provider-mark="brand" class="[^"]*\bopacity-60\b/);
    fixture.advanced = true;
    const advanced = renderToStaticMarkup(createElement(BotListItem, rowProps("icons", candidate)));
    expect(advanced).toMatch(/<span data-testid="model-badge"[^>]*><span data-provider-mark="brand" class="[^"]*\bopacity-60\b/);
  });

  it("shows a waiting Simple row as an amber dot and preview, and a working one as a ring-cut dot on the avatar, with no badge or live region", () => {
    fixture.showThreads = false;
    const quiet = { ...bot, tasks: [], projects: [], unread: false };
    const waiting = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...quiet, activity: "waiting-on-you" })));
    expect(waiting).toContain('data-testid="waiting-dot" title="Waiting for you…" class="size-2 shrink-0 rounded-full bg-warning"');
    expect(waiting).not.toContain(">!<");
    expect(waiting).not.toContain("font-semibold text-warning");
    expect(waiting).not.toContain('role="status"');
    const working = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...quiet, busy: true })));
    expect(working).toContain('data-testid="working-dot" title="Working" class="absolute bottom-0.5 right-0.5 size-2 rounded-full ring-2 ring-[var(--row)] bg-success"');
    expect(working).toContain('<span class="sr-only">Working</span>');
    expect(working).not.toContain('role="status"');
    expect(working).toContain("[--row:var(--color-panel)]");
  });

  it.each([false, true])("says the step a working bot is on in place of its last message, and its last message again once done (advanced: %s)", (advanced) => {
    fixture.advanced = advanced;
    fixture.showThreads = false;
    resetTurnLive();
    const said: Bot["messages"] = [{ id: "hi", role: "bot", kind: "text", at: 1, text: "Here are the leads from Monday." }];
    const leads = { threadId: "leads", title: "Leads", createdAt: 1 };
    const candidate: Bot = { ...bot, threadId: "leads", messages: said, unread: false, projects: [], tasks: [{ ...leads, busy: true, activity: "working" }], busy: true };
    noteTurnEvent({ eventId: "e1", provider: "claudeAgent", threadId: "leads", createdAt: "2026-10-28T12:00:00.000Z", turnId: "t1",
      type: "turn.progress", activity: "command", itemId: "c1", target: "scrape.py", quiet: { kind: "busy" } } as RuntimeEvent);
    const working = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate)));
    expect(working).toContain('<span dir="auto" data-sidebar-working-phrase="" class="truncate">Running a command</span>');
    expect(working, "the sidebar names the kind of step, never the command").not.toContain("scrape.py");
    expect(working, "the row's second line keeps its height: no taller dots").not.toContain("h-[1.5em]");
    expect(working).not.toContain("Monday");
    expect(working, "the row says the step alone, not how it is doing").not.toContain("busy, no output yet");
    const done = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...candidate, busy: false, tasks: [{ ...leads, busy: false, activity: "idle" }] })));
    expect(done).toContain("Here are the leads from Monday.");
    expect(done).not.toContain("scrape.py");
    resetTurnLive();
  });

  it("speaks for the conversation that is working when another one is open", () => {
    fixture.advanced = true;
    fixture.showThreads = false;
    resetTurnLive();
    const candidate: Bot = { ...bot, threadId: "open", messages: [], unread: false, projects: [], busy: true,
      tasks: [{ threadId: "open", title: "Open", createdAt: 1 }, { threadId: "leads", title: "Leads", createdAt: 2, busy: true, activity: "working" }] };
    noteTurnEvent({ eventId: "e2", provider: "codex", threadId: "leads", createdAt: "2026-10-28T12:00:00.000Z", turnId: "t1",
      type: "turn.progress", activity: "web", itemId: "w1", target: "Acme construction leads" } as RuntimeEvent);
    expect(renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate)))).toContain(">Searching the web</span>");
    resetTurnLive();
  });

  it("rings a focused row or group chat inside its edge in both modes", () => {
    fixture.showThreads = false;
    const group: Group = { id: "room", threadId: "room-thread", name: "Launch", memberIds: [bot.id], messages: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 1 };
    const simple = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...bot, tasks: [], projects: [] }))) + renderToStaticMarkup(createElement(GroupListItem, { group, density: "comfortable", onMenu: vi.fn() }));
    expect(simple.match(/focus-visible:outline-2 focus-visible:-outline-offset-2! focus-visible:outline-focus/g)).toHaveLength(2);
    expect(simple).not.toContain("focus-visible:ring-accent/60");
    fixture.advanced = true;
    const advanced = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...bot, tasks: [], projects: [] })));
    expect(advanced).toContain("focus-visible:outline-2 focus-visible:-outline-offset-2! focus-visible:outline-focus");
    expect(advanced).not.toContain("focus-visible:ring-accent/60");
  });

  it("hides even the selected Simple row's actions until hover or focus, where Advanced keeps them shown", () => {
    fixture.showThreads = false;
    fixture.state = { ...fixture.state, selectedId: bot.id, activeView: "chat" };
    const candidate = { ...bot, tasks: [], projects: [] };
    const simple = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate)));
    expect(simple).toContain("[--row:var(--color-raised)]");
    expect(simple).toMatch(/data-bot-actions="atlas"[^>]*class="[^"]*\bpointer-events-none opacity-0 group-hover:pointer-events-auto/);
    expect(simple).toContain('class="flex items-baseline justify-between gap-2 pe-7 group-hover:pe-[3.25rem] group-focus-within:pe-[3.25rem] touch:pe-[3.25rem] max-md:touch:pe-7"');
    fixture.advanced = true;
    expect(renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate)))).toMatch(/data-bot-actions="atlas"[^>]*class="[^"]*\bpointer-events-auto opacity-100/);
  });

  it.each(densities)("marks a room as one place instead of its members' faces, where a two-bot channel keeps both faces (%s)", (density) => {
    const pip: Bot = { ...quiet, id: "pip", name: "Pip", color: "blue" };
    fixture.state = { ...fixture.state, bots: [bot, pip] };
    const room: Group = { id: "room", threadId: "room-thread", name: "Launch", memberIds: [bot.id, pip.id], messages: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 1 };
    const roomMarkup = renderToStaticMarkup(createElement(GroupListItem, { group: room, density, onMenu: vi.fn() }));
    expect(roomMarkup).toContain("data-room-mark");
    expect(roomMarkup).not.toContain("data-avatar-kind=\"bot\"");
    const channel = renderToStaticMarkup(createElement(GroupListItem, { group: { ...room, dm: true }, density, onMenu: vi.fn() }));
    expect(channel).not.toContain("data-room-mark");
    expect(channel.match(/data-avatar-kind="bot"/g)).toHaveLength(2);
  });

  it("marks an unread Simple group chat with the same top-right dot as a bot row, said once for a screen reader", () => {
    const group: Group = { id: "room", threadId: "room-thread", name: "Launch", memberIds: [bot.id], messages: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: true, createdAt: 1 };
    const markup = renderToStaticMarkup(createElement(GroupListItem, { group, density: "comfortable", onMenu: vi.fn() }));
    const name = markup.slice(markup.indexOf('class="flex items-baseline justify-between gap-2 pr-7"'));
    expect(name.indexOf('data-row-dot=""')).toBeLessThan(name.indexOf("</div>"));
    expect(markup.match(/data-testid="unread-dot"/g)).toHaveLength(1);
    expect(markup).toContain('<span class="sr-only">Unread</span>');
  });

  it("draws no engine mark for a bot with no AI yet or in a group chat, and a faded one in Advanced mode", () => {
    fixture.showThreads = false;
    fixture.state = { ...fixture.state, instances: [{ instanceId: "fake", driverKind: "claudeAgent", displayName: "Claude" }] as unknown as AppState["instances"] };
    const candidate = { ...bot, tasks: [], projects: [] };
    expect(renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", { ...candidate, modelSelection: { instanceId: "none", model: "m" } })))).not.toContain("data-provider-mark");
    const group: Group = { id: "room", threadId: "room-thread", name: "Launch", memberIds: [bot.id], messages: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 1 };
    expect(renderToStaticMarkup(createElement(GroupListItem, { group, density: "comfortable", onMenu: vi.fn() }))).not.toContain("data-provider-mark");
    fixture.advanced = true;
    const advanced = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate)));
    expect(advanced).toMatch(/data-testid="model-badge"[^>]*><span data-provider-mark="brand" class="[^"]*\bopacity-60\b/);
  });

  it("keeps a Simple row to its name, preview and one faded engine mark, naming role, Chief and engine in its tooltip", () => {
    fixture.showThreads = false;
    const instances = [{ instanceId: "fake", driverKind: "claude", displayName: "Claude", models: { options: [{ id: "test", label: "Sonnet" }] } }] as unknown as AppState["instances"];
    fixture.state = { ...fixture.state, instances, selectedId: bot.id };
    const candidate = { ...bot, title: "Research", chiefOfStaff: true, pinned: true, tasks: [], projects: [], messages: [{ id: "m", role: "bot", kind: "text", text: "Done.", at: 1 }] } as Bot;
    let tree: ReactNode;
    function Capture() { tree = botRow(rowProps("comfortable", candidate)); return tree; }
    const markup = renderToStaticMarkup(createElement(Capture));
    expect(markup).not.toContain('data-testid="bot-role"');
    expect(markup.match(/data-testid="model-badge"/g)).toHaveLength(1);
    expect(markup.match(/data-provider-mark=/g)).toHaveLength(1);
    expect(markup).toMatch(/<span class="absolute -left-1 -top-1 flex rounded-full bg-raised p-\[3px\]"><svg[^>]*role="img"[^>]*aria-label="Chief of Staff"[^>]*data-testid="chief-crown"/);
    expect(markup).not.toContain("lucide-pin");
    expect(markup).not.toMatch(/tabular-nums text-ink-secondary transition-opacity group-hover:opacity-0/);
    const row = findElement(tree, "data-sidebar-bot-row", bot.id)!;
    expect(row.props.title).toBe("Atlas · Research · Chief of Staff · Model: Sonnet (Claude)");
  });

  it.each([
    { advanced: false, density: "comfortable", slot: "size-9", spacing: ["gap-2.5", "rounded-xl", "pl-2", "pr-2", "md:h-[54px]"] },
    { advanced: false, density: "icons", slot: "size-9", spacing: ["h-[54px]", "justify-center"] },
    { advanced: true, density: "icons", slot: "size-12", spacing: ["justify-center", "px-1", "py-1.5"] },
    { advanced: true, density: "comfortable", slot: "size-8", spacing: ["gap-2", "py-1.5", "pl-6", "pr-9"] },
  ] as const)("sizes group chat rows in $density density (advanced $advanced), names at 14/20 medium and previews at 13/18", ({ advanced, density, slot, spacing }) => {
    fixture.advanced = advanced;
    const group: Group = { id: "room", threadId: "room-thread", name: "Launch", memberIds: [bot.id], messages: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 1 };
    let tree: ReactNode;
    function Capture() { tree = (GroupListItem as unknown as { type: (props: ComponentProps<typeof GroupListItem>) => ReactElement }).type({ group, density, onMenu: vi.fn() }); return tree; }
    const markup = renderToStaticMarkup(createElement(Capture));
    const row = findElement(tree, "data-sidebar-group-row", group.id)!;
    expect(String(row.props.className).split(" ")).toEqual(expect.arrayContaining([...spacing]));
    expect(markup).toMatch(new RegExp(`data-room-mark="" class="[^"]*\\b${slot}\\b[^"]*"><svg[^>]*width="16" height="16"`));
    expect(markup).toContain("truncate text-[14px] font-medium leading-5 text-ink");
    expect(markup).toContain("truncate text-[13px] leading-[18px] text-ink-secondary");
  });

  it("keeps the queued dot small", () => {
    const queuedBot: Bot = {
      ...bot,
      unread: false,
      projects: [],
      tasks: [{ threadId: "queued", title: "Next job", createdAt: 5 }],
    };
    const markup = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", queuedBot)));
    const queuedDot = markup.match(/<span[^>]*data-testid="queued-dot"[^>]*>/)?.[0] ?? "";
    expect(queuedDot).toContain("size-2.5");
    expect(queuedDot).toContain("bg-ink-secondary");
    expect(queuedDot).not.toContain("size-4");
    expect(queuedDot).not.toContain("size-5");
    expect(markup).toContain('class="min-w-0 overflow-hidden whitespace-nowrap">Queued</span>');
    expect(markup).not.toContain("font-semibold text-warning");
    expect(markup).not.toContain("waiting-dot");
  });

  it.each(densities)("opens the thread a row or circle is marked for, on mouse or keyboard, %s density", (density) => {
    fixture.showThreads = false;
    const trees: ReactNode[] = [];
    function Capture() { const tree = [botRow(rowProps(density)), pinnedCircle(rowProps(density))]; trees.push(...tree); return tree; }
    renderToStaticMarkup(createElement(Capture));
    for (const tree of trees) {
      const row = findElement(tree, "data-sidebar-bot-row", bot.id)!;
      fixture.dispatch.mockClear();
      row.props.onClick!({ type: "click", target: {} } as MouseEvent);
      expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith({ type: "switchTask", botId: bot.id, threadId: "approval" });
      if (!row.props.onKeyDown) continue;
      fixture.dispatch.mockClear();
      row.props.onKeyDown({ key: "Enter", preventDefault: vi.fn() } as unknown as KeyboardEvent);
      expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith({ type: "switchTask", botId: bot.id, threadId: "approval" });
    }
  });

  it.each(densities)("opens the thread the person last wrote in when threads are hidden, %s density", (density) => {
    const used = { ...quiet, mainThreadId: "idle-history" };
    for (const enabled of [false, true]) {
      fixture.showThreads = enabled;
      const opened = enabled ? { type: "select", id: bot.id } : { type: "switchTask", botId: bot.id, threadId: "idle-history" };
      const trees: ReactNode[] = [];
      function Capture() { const tree = [botRow(rowProps(density, used)), pinnedCircle(rowProps(density, used))]; trees.push(...tree); return tree; }
      renderToStaticMarkup(createElement(Capture));
      for (const tree of trees) {
        const row = findElement(tree, "data-sidebar-bot-row", bot.id)!;
        fixture.dispatch.mockClear();
        row.props.onClick!({ type: "click", target: {} } as MouseEvent);
        expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith(opened);
        if (!row.props.onKeyDown) continue;
        fixture.dispatch.mockClear();
        row.props.onKeyDown({ key: "Enter", preventDefault: vi.fn() } as unknown as KeyboardEvent);
        expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith(opened);
      }
    }
  });

  it.each(densities)("keeps selection separate from expansion in %s density", (density) => {
    fixture.state.selectedId = bot.id;
    const markup = renderToStaticMarkup(createElement(BotListItem, rowProps(density)));
    expect(markup).not.toContain("data-sidebar-thread-row");
    if (density !== "icons") expect(markup).toContain('aria-label="Expand Atlas threads" aria-expanded="false"');
  });

  it.each(densities)("with Advanced threads off, hides thread browsing and creation but keeps attention accessible in %s density", (density) => {
    fixture.advanced = true;
    fixture.showThreads = false;
    const markup = renderToStaticMarkup(createElement(BotListItem, rowProps(density)));
    expect(markup).not.toContain("data-sidebar-thread-row");
    expect(markup).not.toContain("data-sidebar-folder-row");
    expect(markup).not.toContain("Idle history");
    expect(markup).not.toContain("Quiet folder");
    expect(markup).not.toContain("New thread");
    expect(markup).not.toContain("New folder");
    expect(markup).not.toContain("Expand Atlas threads");
    expect(markup).toContain('data-testid="waiting-dot"');
    for (const id of ["approval", "working", "queued", "unread"]) expect(markup).toContain(`data-sidebar-activity-row="${id}"`);
    expect(markup).toContain('aria-label="Atlas: Review permission · Waiting for you…"');
    expect(markup).toContain('aria-label="Atlas: Next job · Queued"');
    expect(markup).toContain('aria-label="Atlas: Finished reply · Unread"');
  });

  it("removes child controls and portals from a retained hidden folder list", () => {
    const markup = renderToStaticMarkup(createElement(BotThreadList, { ...rowProps("comfortable"), selected: true, hidden: true }));
    expect(markup).toContain('hidden=""');
    expect(markup).not.toContain("<button");
    expect(markup).not.toContain("data-sidebar-thread-row");
    const shown = renderToStaticMarkup(createElement(BotThreadList, { ...rowProps("comfortable"), selected: true }));
    expect(shown).toContain('data-sidebar-folder-row="private-folder"');
    expect(shown).toContain('data-sidebar-thread-row="idle-history"');
  });

  it("only hides thread/folder creation in the bot context menu", () => {
    fixture.advanced = true;
    const render = () => renderToStaticMarkup(createElement(BotContextMenu, {
      menu: { botId: bot.id, x: 0, y: 0 }, onClose: vi.fn(), onArchive: vi.fn(), onDelete: vi.fn(), onMoveToSection: vi.fn(), onNewFolder: vi.fn(), onNewTaskFolder: vi.fn(),
    }));
    const enabled = render();
    expect(enabled).toContain("New thread");
    expect(enabled).toContain("New folder");
    expect(enabled).toContain("Delete all threads…");
    fixture.showThreads = false;
    const disabled = render();
    expect(disabled).not.toContain("New thread");
    expect(disabled).not.toContain("New folder");
    expect(disabled).toContain("Delete all threads…");
    expect(disabled).toContain("Edit Profile");
    expect(disabled).toContain("Move to department");
  });

  it("gives the Simple bot menu New thread, View threads and Set as main thread", () => {
    let tree: ReactNode;
    function Capture() {
      tree = BotContextMenu({
        menu: { botId: bot.id, x: 0, y: 0 }, onClose: vi.fn(), onArchive: vi.fn(), onDelete: vi.fn(), onMoveToSection: vi.fn(), onNewFolder: vi.fn(), onNewTaskFolder: vi.fn(),
      });
      return tree;
    }
    const items = () => {
      const found: ReactElement<ElementProps>[] = [];
      const walk = (node: ReactNode) => {
        for (const child of Children.toArray(node)) {
          if (!isValidElement<ElementProps>(child)) continue;
          if (child.props.role === "menuitem") found.push(child);
          walk(child.props.children);
        }
      };
      walk(tree);
      return found;
    };
    const named = (label: string) => items().find((item) => Children.toArray(item.props.children).includes(label));
    fixture.state = { ...fixture.state, bots: [{ ...bot, mainThreadId: "idle-history" }], selectedId: bot.id };
    renderToStaticMarkup(createElement(Capture));

    named("New thread")!.props.onClick!({} as MouseEvent);
    expect(fixture.dispatch.mock.calls).toEqual([[{ type: "newTask", botId: bot.id }]]);

    fixture.dispatch.mockClear();
    named("View threads")!.props.onClick!({} as MouseEvent);
    expect(fixture.dispatch.mock.calls).toEqual([
      [{ type: "select", id: bot.id }],
      [{ type: "toggleSettings", open: true, section: "history", botId: bot.id }],
    ]);

    fixture.dispatch.mockClear();
    const setMain = named("Set as main thread")!;
    expect(setMain.props.disabled).toBeFalsy();
    setMain.props.onClick!({} as MouseEvent);
    expect(fixture.dispatch.mock.calls).toEqual([[{ type: "updateBot", botId: bot.id, patch: { mainThreadChoice: "last-selected" } }]]);

    // Not offered until the person is known to be the owner or an admin.
    for (const admin of [null, false]) {
      fixture.admin = admin;
      renderToStaticMarkup(createElement(Capture));
      expect(named("Set as main thread")).toBeUndefined();
    }
    fixture.admin = true;

    // Already main: offered but off. Another bot's open thread, or a routine run, is never offered.
    fixture.state = { ...fixture.state, bots: [{ ...bot, mainThreadId: "last-selected" }] };
    renderToStaticMarkup(createElement(Capture));
    expect(named("Set as main thread")!.props.disabled).toBe(true);
    fixture.state = { ...fixture.state, selectedId: "other-bot" };
    renderToStaticMarkup(createElement(Capture));
    expect(named("Set as main thread")).toBeUndefined();
    expect(named("New thread")).toBeDefined();
    fixture.state = { ...fixture.state, selectedId: bot.id, bots: [{ ...bot, threadId: "run", tasks: [...bot.tasks!, { threadId: "run", title: "Run", createdAt: 9, routineRunId: "r" }] }] };
    renderToStaticMarkup(createElement(Capture));
    expect(named("Set as main thread")).toBeUndefined();
    // A Team Cloud private thread can't be the main thread either (the host refuses it).
    fixture.state = { ...fixture.state, selectedId: bot.id, bots: [{ ...bot, threadId: "mine", tasks: [...bot.tasks!, { threadId: "mine", title: "Mine", createdAt: 9, private: true }] }] };
    renderToStaticMarkup(createElement(Capture));
    expect(named("Set as main thread")).toBeUndefined();
    // A view-only teammate can look at the threads but not start one.
    fixture.state = { ...fixture.state, bots: [{ ...bot, yourAccess: "view" }] };
    renderToStaticMarkup(createElement(Capture));
    expect(named("New thread")).toBeUndefined();
    expect(named("View threads")).toBeDefined();
  });

  it("Edit profile opens the panel without naming a section", () => {
    fixture.advanced = true;
    let tree: ReactNode;
    // Called inside a render so its hooks run and the returned tree carries the items' handlers.
    function Capture() {
      tree = BotContextMenu({
        menu: { botId: bot.id, x: 0, y: 0 }, onClose: vi.fn(), onArchive: vi.fn(), onDelete: vi.fn(), onMoveToSection: vi.fn(), onNewFolder: vi.fn(), onNewTaskFolder: vi.fn(),
      });
      return tree;
    }
    const editProfile = () => {
      const items: ReactElement<ElementProps>[] = [];
      const walk = (node: ReactNode) => {
        for (const child of Children.toArray(node)) {
          if (!isValidElement<ElementProps>(child)) continue;
          if (child.props.role === "menuitem") items.push(child);
          walk(child.props.children);
        }
      };
      walk(tree);
      return items.find((item) => Children.toArray(item.props.children).includes("Edit Profile"))!;
    };
    const render = () => renderToStaticMarkup(createElement(Capture));

    render();
    editProfile().props.onClick!({} as MouseEvent);
    expect(fixture.dispatch.mock.calls).toEqual([
      [{ type: "select", id: bot.id }],
      [{ type: "toggleSettings", open: true }],
    ]);

    fixture.dispatch.mockClear();
    fixture.state.settingsOpen = true;
    render();
    editProfile().props.onClick!({} as MouseEvent);
    expect(fixture.dispatch.mock.calls).toEqual([[{ type: "select", id: bot.id }]]);

    vi.stubGlobal("window", { innerWidth: 1024, innerHeight: 768, ogb: { remoteClient: { active: true } } });
    fixture.dispatch.mockClear();
    fixture.state.settingsOpen = false;
    render();
    editProfile().props.onClick!({} as MouseEvent);
    expect(fixture.dispatch.mock.calls).toEqual([
      [{ type: "select", id: bot.id }],
      [{ type: "toggleSettings", open: true }],
    ]);
  });

  it.each(densities)("lists the bot with no thread tree in Simple mode, only a row for each thread that waits on the person or is going, keeping its dots, in %s density", (density) => {
    // Its threads were left expanded in Advanced mode.
    fixture.advanced = true;
    let tree: ReactNode;
    function Capture() { tree = botRow(rowProps(density)); return tree; }
    renderToStaticMarkup(createElement(Capture));
    findElement(tree, "aria-label", "Expand Atlas threads")?.props.onClick!({} as MouseEvent);
    if (density !== "icons") expect(renderToStaticMarkup(createElement(BotListItem, rowProps(density)))).toContain('aria-label="Atlas threads"');
    fixture.advanced = false;
    fixture.showThreads = false;
    for (const query of ["", "history"]) {
      const markup = renderToStaticMarkup(createElement(BotListItem, rowProps(density, bot, query)));
      expect(markup).toContain(`data-sidebar-bot-row="${bot.id}"`);
      expect(markup).not.toContain("data-sidebar-thread-row");
      expect(markup).not.toContain("data-sidebar-folder-row");
      if (density === "icons") {
        expect(markup).not.toContain("data-sidebar-bot-activity");
        expect(markup).not.toContain('aria-label="Atlas threads"');
      } else {
        // Folded: what asks the person something, is going or queued, or has a reply they have not read; idle threads stay out.
        expect(markup).toContain('data-bot-threads-fold="atlas" aria-expanded="false"');
        expect([...markup.matchAll(/data-sidebar-activity-row="([^"]+)"/g)].map((match) => match[1]).sort()).toEqual(["approval", "queued", "unread", "working"]);
      }
      expect(markup).toContain('data-testid="waiting-dot"');
    }
    const unread = renderToStaticMarkup(createElement(BotListItem, rowProps(density, { ...bot, tasks: [bot.tasks![0]!, bot.tasks![5]!] })));
    expect(unread.includes('data-sidebar-activity-row="unread"')).toBe(density !== "icons");
    const read = renderToStaticMarkup(createElement(BotListItem, rowProps(density, { ...bot, unread: false, tasks: [bot.tasks![0]!, bot.tasks![1]!, bot.tasks![3]!] })));
    // Nothing unread, but a thread still running keeps its row while the bot is folded.
    expect([...read.matchAll(/data-sidebar-activity-row="([^"]+)"/g)].map((match) => match[1])).toEqual(density === "icons" ? [] : ["working"]);
    expect(unread).toContain(density === "icons" ? "rounded-full border border-panel bg-accent" : density === "comfortable" ? 'data-testid="unread-dot"' : 'aria-label="Unread threads"');
    forgetSearchDisclosuresForTests();
  });

  it("reveals a matching sole thread when searching a bot", () => {
    fixture.advanced = true;
    const single = { ...bot, projects: [], tasks: [bot.tasks![0]], unread: false };
    const markup = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", single, "last selected")));
    expect(markup).toContain('data-sidebar-thread-row="last-selected"');
  });

  it("does not change group collaboration histories or creation", () => {
    fixture.showThreads = false;
    const group: Group = {
      id: "group", name: "Planning", threadId: "group-thread", memberIds: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 0, messages: [],
      tasks: [{ threadId: "group-thread", title: "Group conversation", createdAt: 1 }, { threadId: "group-earlier", title: "Earlier planning", createdAt: 0 }],
    };
    fixture.state.selectedId = group.id;
    const markup = renderToStaticMarkup(createElement(GroupListItem, { group, density: "comfortable", onMenu: vi.fn() }));
    expect(markup).toContain('data-sidebar-thread-row="group-thread"');
    expect(markup).toContain('aria-label="New thread"');
    expect(markup).toContain('aria-label="Collapse Planning threads"');
    const working = renderToStaticMarkup(createElement(GroupListItem, { group: { ...group, working: true }, density: "comfortable", onMenu: vi.fn() }));
    expect(working).toMatch(/<button type="button" disabled="" aria-label="New thread"/);
    const single = renderToStaticMarkup(createElement(GroupListItem, { group: { ...group, tasks: [group.tasks![0]] }, density: "comfortable", onMenu: vi.fn() }));
    expect(single).not.toContain("Planning threads");
    expect(single).not.toContain('data-sidebar-thread-row=');
    const searched = renderToStaticMarkup(createElement(GroupListItem, {
      group: { ...group, tasks: [group.tasks![0]] }, density: "comfortable", query: "conversation", onMenu: vi.fn(),
    }));
    expect(searched).toContain('data-sidebar-thread-row="group-thread"');
  });
});

describe("group preview", () => {
  it("previews the last reply, not the digest receipt that follows it", () => {
    const group: Group = {
      id: "group", name: "Planning", threadId: "group-thread", memberIds: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 0,
      messages: [
        { id: "b1", role: "bot", kind: "text", text: "Plan drafted.", at: 2, from: { botId: "atlas", name: "Atlas", color: "green" } },
        { id: "d1", role: "bot", kind: "digest", text: "[digest] · tools: Write ×1", at: 3, digest: { turnId: "t1", tools: [{ name: "Write", count: 1 }], hookCoverage: "full" } },
      ] as Group["messages"],
      tasks: [{ threadId: "group-thread", title: "Group conversation", createdAt: 1 }],
    };
    const markup = renderToStaticMarkup(createElement(GroupListItem, { group, density: "comfortable", onMenu: vi.fn() }));
    expect(markup).toContain("Atlas: Plan drafted.");
    expect(markup).not.toContain("[digest]");
  });
});

describe("activity-only escape hatch", () => {
  it("includes waiting approvals even with busy false and never treats aggregate activity as every sibling's status", () => {
    const tasks = sidebarBotActivityTasks({ ...bot, busy: true, activity: "waiting-on-you" }, fixture.state.pendingQueued!);
    expect(tasks.map((task) => task.threadId)).toEqual(["approval", "working", "queued", "unread"]);
  });

  it("switches directly to the requested work and offers no thread-management actions", () => {
    let tree: ReactNode;
    function Capture() { tree = SidebarBotActivity({ bot, rowThreadId: bot.threadId, density: "comfortable", pendingQueued: fixture.state.pendingQueued!, dispatch: fixture.dispatch }); return tree; }
    const markup = renderToStaticMarkup(createElement(Capture));
    findElement(tree, "data-sidebar-activity-row", "approval")!.props.onClick!({} as MouseEvent);
    expect(fixture.dispatch).toHaveBeenCalledExactlyOnceWith({ type: "switchTask", botId: bot.id, threadId: "approval" });
    expect(markup).not.toContain("Actions for");
    expect(markup).not.toContain("New thread");
    expect(markup).not.toContain("Delete");
  });

  it("removes selected and settled activity but keeps unread completed replies", () => {
    const selected = { ...bot, threadId: "approval", tasks: bot.tasks!.map((task) => task.threadId === "working" ? { ...task, busy: false, activity: "idle" as const } : task) };
    const markup = renderToStaticMarkup(createElement(SidebarBotActivity, { bot: selected, rowThreadId: selected.threadId, density: "comfortable", pendingQueued: fixture.state.pendingQueued!, dispatch: fixture.dispatch }));
    expect(markup).not.toContain('data-sidebar-activity-row="approval"');
    expect(markup).not.toContain('data-sidebar-activity-row="working"');
    expect(markup).toContain('data-sidebar-activity-row="unread"');
  });

  it("offers a running routine's thread under its bot in both modes, also beside an open thread tree, and drops it once the run ends", () => {
    const runner: Bot = { ...bot, unread: false, threadId: "main", tasks: [
      { threadId: "main", title: "Main chat", createdAt: 1 },
      { threadId: "run", title: "Morning report", createdAt: 2, routineRunId: "run-1", busy: true, activity: "working" },
    ] };
    const ended: Bot = { ...runner, tasks: runner.tasks!.map((task) => task.routineRunId ? { ...task, busy: false, activity: "idle" as const, unread: true } : task) };
    // Searching "Main" opens the thread tree, which itself keeps routine runs out: the row is the only way in.
    const rows = (candidate: Bot, query = "") => renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", candidate, query))).split('data-sidebar-activity-row="run"').length - 1;
    for (const showThreads of [false, true]) for (const advanced of [false, true]) {
      fixture.showThreads = showThreads;
      fixture.advanced = advanced;
      expect(rows(runner)).toBe(1);
      expect(rows(ended)).toBe(0);
      if (showThreads) expect(rows(runner, "Main")).toBe(1);
    }
  });

  it("lists a reply in the thread a bot was left on, since its row opens the main thread", () => {
    const left: Bot = { ...bot, threadId: "left-behind", mainThreadId: "main-chat", tasks: [
      { threadId: "left-behind", title: "Routine report", createdAt: 1, unread: true },
      { threadId: "main-chat", title: "Main chat", createdAt: 2, unread: true },
    ] };
    const rows = () => {
      const markup = renderToStaticMarkup(createElement(BotListItem, rowProps("comfortable", left)));
      return ["left-behind", "main-chat"].filter((threadId) => markup.includes(`data-sidebar-activity-row="${threadId}"`));
    };
    fixture.advanced = true;
    fixture.showThreads = false;
    expect(rows()).toEqual(["left-behind"]);
    fixture.state.selectedId = left.id;
    expect(rows()).toEqual(["main-chat"]);
    fixture.showThreads = true;
    fixture.state.selectedId = "other-bot";
    expect(rows()).toEqual(["main-chat"]);
  });
});