Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

111 KB
Raw Blame
import { act, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const terminalHarness = vi.hoisted(() => ({
  open: vi.fn(),
  host: { current: null as HTMLElement | null },
  observe: vi.fn(),
  fit: vi.fn(),
  clearTextureAtlas: vi.fn(),
  spacingAtOpen: [] as unknown[],
  /** What the terminal reports after a fit — a test moves it to grow the pane. */
  size: { cols: 80, rows: 24 },
  /**
   * The columns the grid HOLDS, when a test needs them apart from what the
   * tile proposes (null: the grid always matches the staged size).
   */
  gridCols: null as number | null,
  /** Every frame the pane hands its socket. Returns whether it went out. */
  send: vi.fn<(payload: unknown) => boolean>(() => true),
  /** Every explicit grid resize — how the pane pins itself below the floors. */
  resize: vi.fn<(cols: number, rows: number) => void>(),
  /** The live socket's handlers, so a test can play a reconnect. */
  handlers: { current: null as Record<string, (...args: never[]) => void> | null },
  /** What the pane opened its socket WITH — the handshake size among it. */
  opened: { current: null as Record<string, unknown> | null },
  /** Every socket opened, oldest first, with the frames sent on it — for two viewers of one pane. */
  sockets: [] as {
    options: Record<string, unknown>;
    handlers: Record<string, (...args: never[]) => void>;
    sent: unknown[];
  }[],
  /** Everything the pane types into the terminal on the user's behalf. */
  input: vi.fn<(data: string) => void>(),
  /** xterm's single custom key handler, so a test can press a key. */
  keys: { current: null as ((event: KeyboardEvent) => boolean) | null },
  /** xterm's wheel arbiter, which keeps the wheel on terminal history. */
  wheel: { current: null as ((event: WheelEvent) => boolean) | null },
  /** Parser state the wheel arbiter reads — a test flips these directly. */
  modes: { mouseTrackingMode: "none" as "none" | "any" },
  bufferType: "normal" as "normal" | "alternate",
  scrollLines: vi.fn<(amount: number) => void>(),
  /** Custom CSI observers installed on xterm's parser. */
  csiHandlers: [] as {
    id: { prefix?: string; final: string };
    callback: (params: (number | number[])[]) => boolean;
  }[],
  focus: vi.fn(),
  scrollToBottom: vi.fn(),
  scrollToLine: vi.fn<(line: number) => void>(),
  viewport: { baseY: 0, viewportY: 0 },
  write: vi.fn(),
  visibilityAtWrite: [] as string[],
  deferWrite: false,
  writeCallbacks: [] as (() => void)[],
  /**
   * Every terminal this pane has built, oldest first.
   *
   * A pane replaces its terminal without remounting — the grid re-measuring,
   * a restart, a rename — and what the REPLACEMENT is built with is exactly
   * where a pane lost the reader's text size. So the double keeps the options
   * it was constructed with rather than starting from an empty object, and the
   * list makes the newest instance reachable from a test.
   */
  instances: [] as { options: Record<string, unknown> }[],
}));

vi.mock("@xterm/xterm", () => ({
  Terminal: class {
    get cols() {
      return terminalHarness.gridCols ?? terminalHarness.size.cols;
    }
    get rows() {
      return terminalHarness.size.rows;
    }
    get modes() {
      return terminalHarness.modes;
    }
    get buffer() {
      return {
        active: { type: terminalHarness.bufferType, ...terminalHarness.viewport, cursorX: 0, cursorY: 0, getLine: () => undefined },
      };
    }
    scrollLines(amount: number) {
      terminalHarness.scrollLines(amount);
    }
    options: Record<string, unknown>;
    unicode = { activeVersion: "" };

    constructor(options: Record<string, unknown> = {}) {
      this.options = { ...options };
      terminalHarness.instances.push(this);
    }

    // The pane silences xterm's own answers to the agent's protocol queries
    // (see ./terminalQueries). The double only has to accept the handlers.
    parser = {
      registerOscHandler: () => ({ dispose() {} }),
      registerEscHandler: () => ({ dispose() {} }),
      registerCsiHandler: (
        id: { prefix?: string; final: string },
        callback: (params: (number | number[])[]) => boolean,
      ) => {
        const entry = { id, callback };
        terminalHarness.csiHandlers.push(entry);
        return {
          dispose() {
            const index = terminalHarness.csiHandlers.indexOf(entry);
            if (index >= 0) terminalHarness.csiHandlers.splice(index, 1);
          },
        };
      },
    };

    loadAddon() {}
    open(host: HTMLElement) {
      terminalHarness.open(host);
      terminalHarness.spacingAtOpen.push(this.options.letterSpacing);
      terminalHarness.host.current = host;
    }
    focus() {
      terminalHarness.focus();
    }
    paste() {}
    attachCustomKeyEventHandler(handler: (event: KeyboardEvent) => boolean) {
      terminalHarness.keys.current = handler;
    }
    attachCustomWheelEventHandler(handler: (event: WheelEvent) => boolean) {
      terminalHarness.wheel.current = handler;
    }
    input(data: string) {
      terminalHarness.input(data);
    }
    getSelection() {
      return "";
    }
    getSelectionPosition() { return undefined; }
    onSelectionChange() { return { dispose() {} }; }
    onRender() { return { dispose() {} }; }
    onData() {
      return { dispose() {} };
    }
    write(text: string, callback?: () => void) {
      terminalHarness.write(text);
      terminalHarness.visibilityAtWrite.push(
        terminalHarness.host.current?.style.visibility ?? "",
      );
      if (!callback) return;
      if (terminalHarness.deferWrite) terminalHarness.writeCallbacks.push(callback);
      else callback();
    }
    scrollToBottom() {
      terminalHarness.scrollToBottom();
      terminalHarness.viewport.viewportY = terminalHarness.viewport.baseY;
    }
    scrollToLine(line: number) {
      terminalHarness.scrollToLine(line);
      terminalHarness.viewport.viewportY = line;
    }
    reset() {}
    resize(cols: number, rows: number) {
      terminalHarness.resize(cols, rows);
    }
    dispose() {}
    clearTextureAtlas() { terminalHarness.clearTextureAtlas(this); }
  },
}));

vi.mock("@xterm/addon-fit", () => ({
  FitAddon: class {
    fit() {
      terminalHarness.fit();
    }
    // The pane measures before it applies (see `sendResize`), so the double
    // answers with whatever size the test has staged — the same value fit()
    // would land on.
    proposeDimensions() {
      return { ...terminalHarness.size };
    }
  },
}));
vi.mock("@xterm/addon-web-links", () => ({ WebLinksAddon: class {} }));
vi.mock("@xterm/addon-canvas", () => ({ CanvasAddon: class {} }));
vi.mock("@xterm/addon-unicode11", () => ({ Unicode11Addon: class {} }));

vi.mock("./paneSocket", () => ({
  openPaneSocket: (
    options: unknown,
    handlers: Record<string, (...args: never[]) => void>,
  ) => {
    terminalHarness.opened.current = options as Record<string, unknown>;
    terminalHarness.handlers.current = handlers;
    const record = { options: options as Record<string, unknown>, handlers, sent: [] as unknown[] };
    terminalHarness.sockets.push(record);
    return {
      send: (payload: unknown) => {
        record.sent.push(payload);
        return terminalHarness.send(payload);
      },
      close() {},
    };
  },
}));
vi.mock("./terminalPaste", () => ({
  installPasteBridge: () => () => undefined,
}));
vi.mock("./paneFileDrag", () => ({
  usePaneFileDrag: () => ({ dragging: false, handlers: {} }),
}));
vi.mock("@/lib/editActions", () => ({ attachTerminalBridge: () => undefined }));
vi.mock("@/lib/agenticIdeApi", () => ({ attachToTerminal: vi.fn() }));

import {
  AgenticTerminal,
  CURTAIN_MAX_MS,
  DRAG_REFIT_MS,
  NARROW_SETTLE_MS,
  REBUILD_QUIET_MS,
  REBUILD_SETTLE_MAX_MS,
  REPAINT_WAIT_MAX_MS,
  RESIZE_PARSE_WAIT_MS,
  RETURN_REPAINT_NUDGE_MS,
  UNMEASURED_SIZE,
} from "./AgenticTerminal";
import { FONT_WAIT_MS } from "@/lib/terminalFont";
import { PANE_CHROME } from "./terminalThemes";

/**
 * Past the quiet window a rebuilt pane waits out, plus the reveal frame behind
 * it. Bound to the real constant so tuning the window cannot silently turn
 * these assertions into "revealed eventually".
 */
const PAST_REBUILD = REBUILD_QUIET_MS + 40;

class ResizeObserverHarness implements ResizeObserver {
  constructor(_callback: ResizeObserverCallback) {}
  observe(target: Element) {
    terminalHarness.observe(target);
  }
  unobserve() {}
  disconnect() {}
}

describe("AgenticTerminal layout", () => {
  beforeEach(() => {
    terminalHarness.open.mockClear();
    terminalHarness.observe.mockClear();
    terminalHarness.fit.mockClear();
    terminalHarness.scrollToBottom.mockClear();
    terminalHarness.scrollToLine.mockClear();
    terminalHarness.viewport = { baseY: 0, viewportY: 0 };
    terminalHarness.write.mockClear();
    terminalHarness.host.current = null;
    terminalHarness.visibilityAtWrite = [];
    terminalHarness.deferWrite = false;
    terminalHarness.writeCallbacks = [];
    terminalHarness.wheel.current = null;
    terminalHarness.csiHandlers = [];
    terminalHarness.modes.mouseTrackingMode = "none";
    terminalHarness.bufferType = "normal";
    terminalHarness.scrollLines.mockClear();
    globalThis.ResizeObserver = ResizeObserverHarness;
  });

  afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
  });

  it("measures an unpadded host inside the padded shrinking viewport", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    const host = screen.getByTestId("agentic-terminal-host-Dana");
    const viewport = host.parentElement;

    expect(viewport).not.toBeNull();
    expect(viewport?.className).toContain("min-h-0");
    // The inset is on the VIEWPORT, whatever it currently measures — the pane
    // frame has been tightened more than once and the exact values are a visual
    // decision. What must not change is WHICH element carries it: padding on
    // the host below would make FitAddon report a row the pane cannot show.
    expect(viewport?.className).toMatch(/(?:^|\s)px-[\d.]+/);
    expect(viewport?.className).toMatch(/(?:^|\s)pb-[\d.]+/);
    expect(viewport?.className).toMatch(/(?:^|\s)pt-[\d.]+/);
    expect(host.className).toContain("h-full");
    expect(host.className).toContain("min-h-0");
    expect(host.className).not.toMatch(/(?:^|\s)p[trblxy]?-/);
    expect(terminalHarness.open).toHaveBeenCalledWith(host);
    expect(terminalHarness.observe).toHaveBeenCalledWith(host);
    expect(terminalHarness.wheel.current).not.toBeNull();
  });

  it("keeps conversation history in the header without a scrollbar overlay", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    expect(screen.getByTestId("pane-conversation-Dana")).toBeTruthy();
    expect(screen.queryByRole("scrollbar")).toBeNull();
    expect(screen.queryByTestId("pane-scroll-history-Dana")).toBeNull();
  });

  it("opts into compact chrome without recreating the terminal or exposing recap hover controls", () => {
    const onFocus = vi.fn();
    const props = { name: "Dana", displayName: "Codex", agent: "codex", appearance: "dark" as const, fontSize: 13, recap: "Detailed work summary", onFocus };
    const { rerender } = render(<AgenticTerminal {...props} />);
    const host = screen.getByTestId("agentic-terminal-host-Dana");
    const instances = terminalHarness.instances.length;
    expect(screen.getByTestId("pane-header-Dana")).toBeTruthy();
    rerender(<AgenticTerminal {...props} headerMode="compact" />);
    expect(screen.queryByTestId("pane-header-Dana")).toBeNull();
    expect(screen.getByTestId("workspace-terminal-header-Dana")).toBeTruthy();
    expect(screen.queryByTestId("pane-recap-Dana")).toBeNull();
    expect(screen.queryByTestId("pane-header-tip-Dana")).toBeNull();
    expect(screen.getByTestId("agentic-terminal-host-Dana")).toBe(host);
    expect(terminalHarness.instances.length).toBe(instances);
    expect(screen.getByTestId("agentic-pane-Dana").className).toContain("rounded-2xl");
    const title = screen.getByTestId("pane-move-Dana");
    title.focus();
    const focuses = terminalHarness.focus.mock.calls.length;
    fireEvent.click(title);
    expect(onFocus).toHaveBeenCalledOnce();
    expect(document.activeElement).toBe(title);
    expect(terminalHarness.focus.mock.calls.length).toBe(focuses);
  });

  it("draws a minimal tile: square frame, slim title row, and a signal edge on the pane in use", () => {
    const props = { name: "Dana", displayName: "Codex", agent: "codex", appearance: "dark" as const, fontSize: 13, headerMode: "minimal" as const };
    const { rerender } = render(<AgenticTerminal {...props} />);
    const pane = screen.getByTestId("agentic-pane-Dana");
    expect(pane.className).toContain("rounded-none");
    expect(pane.className).not.toContain("rounded-2xl");
    expect(pane.dataset.paneStyle).toBe("minimal");
    const header = screen.getByTestId("workspace-terminal-header-Dana");
    expect(header.dataset.variant).toBe("tile");
    expect(screen.getByTestId("pane-title-Dana")).toBeTruthy();
    expect(screen.queryByTestId("pane-header-Dana")).toBeNull();
    const resting = pane.style.borderColor;
    expect(resting).toBeTruthy();
    expect(screen.queryByTestId("pane-focus-ring-Dana")).toBeNull();
    rerender(<AgenticTerminal {...props} focused />);
    expect(pane.style.borderColor).not.toBe(resting);
    // The inner line is a layer of its own on top of the title row, so the
    // row's translucent ground cannot dim it (it read darker along the top).
    const ring = screen.getByTestId("pane-focus-ring-Dana");
    expect(ring.style.boxShadow).toContain("inset");
    expect(ring.className).toContain("pointer-events-none");
    expect(ring.className).toContain("z-[45]");
    expect(pane.lastElementChild).toBe(ring);
    expect(pane.style.boxShadow).toBe("");
    // While the reader works in the side panel, the panel wears the frame.
    rerender(<AgenticTerminal {...props} focused markFocus={false} />);
    expect(pane.style.borderColor).toBe(resting);
    expect(screen.queryByTestId("pane-focus-ring-Dana")).toBeNull();
    // One pane in the grid is still the pane in use: it is marked too.
    rerender(<AgenticTerminal {...props} focused={false} />);
    expect(pane.style.borderColor).toBe(resting);
  });

  it("selects the pane on a press into the terminal even when xterm swallows it", () => {
    const onFocus = vi.fn();
    render(<AgenticTerminal name="Dana" displayName="Codex" appearance="dark" fontSize={13} headerMode="minimal" onFocus={onFocus} />);
    const host = screen.getByTestId("agentic-terminal-host-Dana");
    // xterm's selection service stops a press it turns into a selection.
    const swallow = (event: Event) => event.stopPropagation();
    host.addEventListener("mousedown", swallow);
    fireEvent.mouseDown(host, { button: 0 });
    expect(onFocus).toHaveBeenCalledTimes(1);
    host.removeEventListener("mousedown", swallow);
    // A press that bubbles normally reaches both handlers but counts once.
    fireEvent.mouseDown(host, { button: 0 });
    expect(onFocus).toHaveBeenCalledTimes(2);
    // Only the primary button selects; a right-click is the pane menu's.
    fireEvent.mouseDown(host, { button: 2 });
    expect(onFocus).toHaveBeenCalledTimes(2);
  });

  it("keeps the wheel on terminal history even while the CLI tracks the mouse", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    // Plain pane: xterm's native wheel behaviour is already right.
    expect(
      terminalHarness.wheel.current?.(new WheelEvent("wheel", { deltaY: 120 })),
    ).toBe(true);
    expect(terminalHarness.scrollLines).not.toHaveBeenCalled();

    // A normal-buffer CLI that negotiates mouse tracking must NOT receive the
    // wheel as mouse reports — the wheel keeps scrolling xterm's history, so
    // scrolling behaves identically in every provider and every CLI mode.
    terminalHarness.modes.mouseTrackingMode = "any";
    expect(
      terminalHarness.wheel.current?.(new WheelEvent("wheel", { deltaY: 120 })),
    ).toBe(false);
    expect(terminalHarness.scrollLines).toHaveBeenCalledWith(3);
    expect(terminalHarness.input).not.toHaveBeenCalled();

    // A true alternate-screen app (vim, less) keeps its negotiated protocols.
    terminalHarness.bufferType = "alternate";
    terminalHarness.scrollLines.mockClear();
    expect(
      terminalHarness.wheel.current?.(new WheelEvent("wheel", { deltaY: 120 })),
    ).toBe(true);
    expect(terminalHarness.scrollLines).not.toHaveBeenCalled();
  });

  it("waits for the area-aware grid measurement before opening the PTY", () => {
    const view = render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        geometryReady={false}
      />,
    );

    expect(terminalHarness.open).not.toHaveBeenCalled();

    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        geometryReady
      />,
    );

    expect(terminalHarness.open).toHaveBeenCalled();
  });

  it("refits and follows the live tail before a hidden chat pane is shown", () => {
    const view = render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        active={false}
      />,
    );
    const host = screen.getByTestId("agentic-terminal-host-Dana");
    Object.defineProperty(host, "clientWidth", { configurable: true, value: 600 });
    Object.defineProperty(host, "clientHeight", { configurable: true, value: 400 });
    terminalHarness.fit.mockClear();
    terminalHarness.scrollToBottom.mockClear();

    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        active
      />,
    );

    expect(terminalHarness.fit).toHaveBeenCalled();
    expect(terminalHarness.scrollToBottom).toHaveBeenCalled();
  });

  it("restores a scrolled-back viewport when switching away and back", () => {
    const view = render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    terminalHarness.viewport = { baseY: 240, viewportY: 84 };
    terminalHarness.scrollToBottom.mockClear();
    terminalHarness.scrollToLine.mockClear();

    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active={false}
      />,
    );
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );

    expect(terminalHarness.scrollToLine).toHaveBeenCalledWith(84);
    expect(terminalHarness.scrollToBottom).not.toHaveBeenCalled();
  });

  it("keeps prompt output parked until an inactive chat pane is selected", () => {
    const view = render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        active={false}
      />,
    );
    terminalHarness.write.mockClear();

    act(() => {
      terminalHarness.handlers.current?.onPrompt?.(
        { text: "Run the tests", at: 1, chars: 13 } as never,
      );
      terminalHarness.handlers.current?.onOutput?.("working" as never);
    });
    expect(terminalHarness.write).not.toHaveBeenCalled();

    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        active
      />,
    );

    expect(terminalHarness.write).toHaveBeenCalledWith("working");
  });

  it("does not paint a reactivated chat pane before its live tail is ready", () => {
    vi.useFakeTimers();
    terminalHarness.deferWrite = true;
    const view = render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        active={false}
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;

    act(() => {
      terminalHarness.handlers.current?.onOutput?.("new live output" as never);
    });
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        active
      />,
    );

    expect(region?.className).toContain("invisible");
    expect(terminalHarness.writeCallbacks).toHaveLength(1);

    act(() => {
      // The held flush has parsed…
      terminalHarness.writeCallbacks.shift()?.();
    });
    // …and the pane placed a queue barrier behind it: an earlier write (a
    // replay flushed while hidden) may still be mid-parse, and the curtain
    // must not lift onto its tail printing.
    expect(terminalHarness.write).toHaveBeenLastCalledWith("");
    expect(terminalHarness.writeCallbacks).toHaveLength(1);

    act(() => {
      terminalHarness.writeCallbacks.shift()?.();
      vi.advanceTimersByTime(PAST_REBUILD);
    });

    expect(terminalHarness.scrollToBottom).toHaveBeenCalled();
    expect(region?.className).not.toContain("invisible");
  });

  it("hides an active pane while a replay rebuilds it, then reveals it at the tail", () => {
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
    act(() => {
      vi.advanceTimersByTime(20);
    });
    expect(region?.className).not.toContain("invisible");

    // A normal-buffer CLI's replay is its whole scrollback, parsed in slices:
    // painted onto a visible surface it prints top to bottom with the
    // viewport chasing it. The pane must hide until the tail scroll landed.
    terminalHarness.deferWrite = true;
    terminalHarness.scrollToBottom.mockClear();
    act(() => {
      terminalHarness.handlers.current?.onReplay?.(
        "the whole recorded session" as never,
      );
    });
    // Captured INSIDE `term.write`, before React gets another render. This is
    // the timing the real WebView exposed: a state-only curtain arrived after
    // xterm had already begun painting the replay.
    expect(terminalHarness.visibilityAtWrite.at(-1)).toBe("hidden");
    expect(screen.getByTestId("agentic-terminal-host-Dana").style.visibility).toBe(
      "hidden",
    );
    expect(region?.className).toContain("invisible");
    expect(terminalHarness.writeCallbacks).toHaveLength(1);

    act(() => {
      terminalHarness.writeCallbacks.shift()?.();
      vi.advanceTimersByTime(PAST_REBUILD);
    });

    expect(terminalHarness.scrollToBottom).toHaveBeenCalled();
    expect(screen.getByTestId("agentic-terminal-host-Dana").style.visibility).toBe("");
    expect(region?.className).not.toContain("invisible");
  });

  it("lifts a replay curtain even when the reveal frame never arrives", () => {
    // A WebView that believes its window is hidden stops delivering animation
    // frames, and the reveal is chained behind one. Before the watchdog that
    // left the pane an empty black rectangle under a green "live" dot until
    // something unrelated rebuilt it (reported 2026-09-28).
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const host = screen.getByTestId("agentic-terminal-host-Dana");
    act(() => {
      vi.advanceTimersByTime(PAST_REBUILD);
    });
    expect(host.style.visibility).toBe("");

    const starved = vi
      .spyOn(window, "requestAnimationFrame")
      .mockImplementation(() => 1);
    try {
      terminalHarness.deferWrite = true;
      act(() => {
        terminalHarness.handlers.current?.onReplay?.("the screen" as never);
      });
      act(() => {
        terminalHarness.writeCallbacks.shift()?.();
        vi.advanceTimersByTime(PAST_REBUILD);
      });
      // The honest path is stuck on its frame…
      expect(host.style.visibility).toBe("hidden");

      act(() => {
        vi.advanceTimersByTime(CURTAIN_MAX_MS);
      });
      // …and the backstop shows the pane anyway.
      expect(host.style.visibility).toBe("");
      expect(host.parentElement?.className).not.toContain("invisible");
    } finally {
      starved.mockRestore();
    }
  });

  it("stays hidden while the post-replay repaint is still arriving", () => {
    // The replay is only half the rebuild: the server answers a truncated or
    // re-based one by nudging the agent into painting its whole screen again
    // (`SessionRegistry._nudge_repaint`), and that second screen lands AFTER
    // the replay parsed. Revealing in between is what still put the repaint in
    // front of the reader — a Codex pane opening on the top of its history and
    // racing down — even with the replay curtain working exactly as designed.
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
    act(() => vi.advanceTimersByTime(PAST_REBUILD));

    terminalHarness.deferWrite = true;
    act(() => {
      terminalHarness.handlers.current?.onReplay?.("recorded session" as never);
      terminalHarness.writeCallbacks.shift()?.();
    });
    expect(region?.className).toContain("invisible");

    // The repaint arrives mid-window and restarts it — the pane keeps waiting
    // rather than revealing on the schedule the replay alone would have set.
    act(() => {
      vi.advanceTimersByTime(REBUILD_QUIET_MS - 20);
      terminalHarness.handlers.current?.onOutput?.("the repainted screen" as never);
      vi.advanceTimersByTime(REBUILD_QUIET_MS - 20);
    });
    expect(region?.className).toContain("invisible");

    act(() => vi.advanceTimersByTime(PAST_REBUILD));
    expect(region?.className).not.toContain("invisible");
  });

  it.each(["\x1b[2", "\x1b[1;1H\x1b["])("waits for a promised repaint and recognizes split erases: %j", (eraseStart) => {
    // A replay the server marks as needing a repaint cannot rebuild the screen
    // by itself. A busy agent may answer the repaint request late (the server
    // repeats it after half a second), and revealing on quiet in between showed
    // the cut-off tail — terminals badly formatted for a second after a reload.
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
    act(() => vi.advanceTimersByTime(PAST_REBUILD));

    terminalHarness.deferWrite = true;
    act(() => {
      // An older erase inside the replay itself is not the answer.
      terminalHarness.handlers.current?.onReplay?.(
        "\x1b[2Jcut-off tail" as never,
        true as never,
      );
      terminalHarness.writeCallbacks.shift()?.();
    });
    act(() => vi.advanceTimersByTime(REBUILD_SETTLE_MAX_MS + 100));
    expect(region?.className).toContain("invisible");

    // The erase arrives split across two chunks; the quiet window follows it.
    act(() => {
      terminalHarness.handlers.current?.onOutput?.(eraseStart as never);
      terminalHarness.handlers.current?.onOutput?.("Jthe repainted screen" as never);
    });
    expect(region?.className).toContain("invisible");
    act(() => vi.advanceTimersByTime(PAST_REBUILD));
    expect(region?.className).not.toContain("invisible");
  });

  it("shows the pane anyway when a promised repaint never comes", () => {
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
    act(() => vi.advanceTimersByTime(PAST_REBUILD));

    terminalHarness.deferWrite = true;
    act(() => {
      terminalHarness.handlers.current?.onReplay?.("cut-off tail" as never, true as never);
      terminalHarness.writeCallbacks.shift()?.();
    });
    act(() => vi.advanceTimersByTime(REPAINT_WAIT_MAX_MS - 50));
    expect(region?.className).toContain("invisible");
    act(() => vi.advanceTimersByTime(100));
    expect(region?.className).not.toContain("invisible");
  });

  it("reveals a pane whose agent never stops talking", () => {
    // The quiet window assumes the redraw ends. An agent streaming an answer
    // never goes quiet, and waiting on it would trade a visible scroll for a
    // pane that simply does not come back.
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
    act(() => vi.advanceTimersByTime(PAST_REBUILD));

    terminalHarness.deferWrite = true;
    act(() => {
      terminalHarness.handlers.current?.onReplay?.("recorded session" as never);
      terminalHarness.writeCallbacks.shift()?.();
    });
    expect(region?.className).toContain("invisible");

    act(() => {
      // Chatty enough that the quiet window never once elapses.
      for (let tick = 0; tick < 12; tick += 1) {
        vi.advanceTimersByTime(REBUILD_QUIET_MS - 40);
        terminalHarness.handlers.current?.onOutput?.("still working…" as never);
      }
      vi.advanceTimersByTime(40);
    });

    expect(region?.className).not.toContain("invisible");
  });

  it("restores a scrolled-back viewport after replaying a Codex terminal", () => {
    vi.useFakeTimers();
    terminalHarness.deferWrite = true;
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    act(() => {
      vi.advanceTimersByTime(20);
    });
    terminalHarness.viewport = { baseY: 240, viewportY: 84 };
    terminalHarness.scrollToBottom.mockClear();
    terminalHarness.scrollToLine.mockClear();

    act(() => {
      terminalHarness.handlers.current?.onReplay?.("recorded session" as never);
    });
    // The rebuilt buffer can be longer than the one the reader left.
    terminalHarness.viewport = { baseY: 260, viewportY: 260 };
    act(() => {
      terminalHarness.writeCallbacks.shift()?.();
      vi.advanceTimersByTime(20);
    });

    expect(terminalHarness.scrollToLine).toHaveBeenCalledWith(84);
    expect(terminalHarness.scrollToBottom).not.toHaveBeenCalled();
  });

  it("does not let an older replay reveal a newer replay", () => {
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
    act(() => vi.advanceTimersByTime(20));

    terminalHarness.deferWrite = true;
    act(() => {
      terminalHarness.handlers.current?.onReplay?.("older replay" as never);
      terminalHarness.handlers.current?.onReplay?.("newer replay" as never);
    });
    expect(region?.className).toContain("invisible");
    expect(terminalHarness.writeCallbacks).toHaveLength(2);

    act(() => {
      terminalHarness.writeCallbacks.shift()?.();
      vi.advanceTimersByTime(PAST_REBUILD);
    });
    expect(region?.className).toContain("invisible");

    act(() => {
      terminalHarness.writeCallbacks.shift()?.();
      vi.advanceTimersByTime(PAST_REBUILD);
    });
    expect(region?.className).not.toContain("invisible");
  });

  it("does not let a stale replay frame bypass a new stage barrier", () => {
    vi.useFakeTimers();
    const view = render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    const region = screen.getByTestId("agentic-terminal-host-Dana").parentElement;
    act(() => vi.advanceTimersByTime(20));

    terminalHarness.deferWrite = true;
    act(() => {
      terminalHarness.handlers.current?.onReplay?.("recorded session" as never);
      terminalHarness.writeCallbacks.shift()?.();
    });
    // The replay completion has scheduled its reveal frame, but a stage switch
    // supersedes it before that frame gets to paint.
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active={false}
      />,
    );
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
        active
      />,
    );
    expect(terminalHarness.writeCallbacks).toHaveLength(1);

    act(() => vi.advanceTimersByTime(PAST_REBUILD));
    expect(region?.className).toContain("invisible");

    act(() => {
      terminalHarness.writeCallbacks.shift()?.();
      vi.advanceTimersByTime(PAST_REBUILD);
    });
    expect(region?.className).not.toContain("invisible");
  });
});

describe("pane keyboard", () => {
  beforeEach(() => {
    terminalHarness.input.mockClear();
    terminalHarness.keys.current = null;
    globalThis.ResizeObserver = ResizeObserverHarness;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  /**
   * The reported bug: Shift+Enter sent the half-written instruction, because
   * every modifier combination of Enter reaches a terminal as the same
   * carriage return. See ./terminalNewline for the sequence.
   */
  it("breaks the line on Shift+Enter instead of sending the instruction", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    const press = terminalHarness.keys.current;
    expect(press).not.toBeNull();
    const claimed = press?.(
      new KeyboardEvent("keydown", { key: "Enter", shiftKey: true }),
    );

    expect(claimed).toBe(false);
    expect(terminalHarness.input).toHaveBeenCalledWith("\x1b\r");
  });

  it("leaves a plain Enter to xterm, so Enter still sends", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    const claimed = terminalHarness.keys.current?.(
      new KeyboardEvent("keydown", { key: "Enter" }),
    );

    expect(claimed).toBe(true);
    expect(terminalHarness.input).not.toHaveBeenCalled();
  });

  it("keeps Ctrl+C out of the PTY even when nothing is selected", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Codex"
        appearance="dark"
        fontSize={13}
      />,
    );

    const claimed = terminalHarness.keys.current?.(
      new KeyboardEvent("keydown", { key: "c", ctrlKey: true }),
    );

    expect(claimed).toBe(false);
  });
});

describe("pane header recap", () => {
  beforeEach(() => {
    globalThis.ResizeObserver = ResizeObserverHarness;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it("shows the session recap instead of the agent name", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        recap="Running pytest tests/unit/test_login.py"
        recapDetail='Last asked to: "Fix the failing login test". Working now, last output 4s ago: Running pytest tests/unit/test_login.py.'
        appearance="dark"
        fontSize={13}
      />,
    );

    expect(screen.getByTestId("pane-recap-Dana").textContent).toBe(
      "Running pytest tests/unit/test_login.py",
    );
    // The agent name is not gone, only moved out of the header line.
    expect(screen.queryByTestId("pane-agent-Dana")).toBeNull();
  });

  it("opens the longer recap in a card the header line controls", async () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        recap="Running pytest tests/unit/test_login.py"
        recapDetail='Last asked to: "Fix the failing login test". Working now: Running pytest tests/unit/test_login.py.'
        appearance="dark"
        fontSize={13}
      />,
    );

    const line = screen.getByTestId("pane-recap-Dana");
    expect(screen.queryByTestId("pane-recap-card-Dana")).toBeNull();

    fireEvent.click(line);
    const card = screen.getByTestId("pane-recap-card-Dana");

    expect(card.textContent).toContain("Fix the failing login test");
    // Which CLI runs here is named in the card rather than lost.
    expect(card.textContent).toContain("Claude Code");
    // A dialog, not a tooltip: it can be clicked into, its text selected, and
    // its buttons pressed — none of which the tooltip it replaces allowed.
    expect(card.getAttribute("role")).toBe("dialog");
    expect(line.getAttribute("aria-controls")).toBe(card.id);
    expect(line.getAttribute("aria-expanded")).toBe("true");
  });

  it("closes the card again on Escape", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        recap="Running pytest tests/unit/test_login.py"
        recapDetail="Where the work stands."
        appearance="dark"
        fontSize={13}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-recap-Dana"));
    expect(screen.getByTestId("pane-recap-card-Dana")).toBeTruthy();

    fireEvent.keyDown(document, { key: "Escape" });

    expect(screen.queryByTestId("pane-recap-card-Dana")).toBeNull();
  });

  it("falls back to the agent name while a pane has no recap yet", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    expect(screen.getByTestId("pane-agent-Dana").textContent).toBe(
      "Claude Code",
    );
    expect(screen.queryByTestId("pane-recap-Dana")).toBeNull();
  });

  it("repeats nothing when the long form says what the header line says", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        recap="Not started yet."
        recapDetail="Not started yet."
        appearance="dark"
        fontSize={13}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-recap-Dana"));

    expect(screen.getByTestId("pane-recap-headline-Dana").textContent).toBe(
      "Not started yet.",
    );
    expect(screen.queryByTestId("pane-recap-detail-Dana")).toBeNull();
  });
});

describe("pane header actions", () => {
  beforeEach(() => {
    globalThis.ResizeObserver = ResizeObserverHarness;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it("recedes on an unfocused pane but stays reachable by hover and keyboard", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        focused={false}
        onToggleMaximize={() => undefined}
        onSplit={() => undefined}
        onClose={() => undefined}
      />,
    );

    const actions = screen.getByTestId("pane-maximize-Dana").parentElement;

    expect(actions).not.toBeNull();
    // Hidden by opacity only — the buttons stay in the DOM, so a header hover
    // or tabbing into the cluster reveals the same elements this test finds.
    expect(actions?.className).toContain("opacity-0");
    expect(actions?.className).toContain("group-hover/header:opacity-100");
    expect(actions?.className).toContain("focus-within:opacity-100");
    expect(screen.getByTestId("pane-split-right-Dana")).toBeTruthy();
    expect(screen.getByTestId("pane-split-down-Dana")).toBeTruthy();
    expect(screen.getByTestId("pane-close-Dana")).toBeTruthy();
  });

  it("fills the workspace on a double-click of the title bar", () => {
    const onToggleMaximize = vi.fn();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onToggleMaximize={onToggleMaximize}
      />,
    );

    fireEvent.doubleClick(screen.getByTestId("pane-header-Dana"));

    expect(onToggleMaximize).toHaveBeenCalledTimes(1);
  });

  it("starts a pane move from its explicit handle while leaving the recap clickable", () => {
    const onArrangeStart = vi.fn();
    render(<AgenticTerminal name="Dana" displayName="Claude Code" recap="Fix login"
      appearance="dark" fontSize={13} onArrangeStart={onArrangeStart} showArrangeHandle />);
    fireEvent.pointerDown(screen.getByRole("button", { name: "Move Dana" }), { button: 0, clientX: 20, clientY: 20 });
    expect(onArrangeStart).toHaveBeenCalledTimes(1);
    fireEvent.pointerDown(screen.getByTestId("pane-recap-Dana"), { button: 0, clientX: 30, clientY: 20 });
    expect(onArrangeStart).toHaveBeenCalledTimes(1);
  });

  it("explains the bar's gestures in its own card after a settled hover", () => {
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onToggleMaximize={() => undefined}
        onArrangeStart={() => undefined}
        onRename={async () => true}
      />,
    );

    // A pass-through hover shows nothing; the card waits for a settled one.
    fireEvent.pointerOver(screen.getByTestId("pane-header-Dana"));
    expect(screen.queryByTestId("pane-header-tip-Dana")).toBeNull();

    act(() => {
      vi.advanceTimersByTime(600);
    });

    // The same single sentence the native `title` tooltip carried, now in
    // the pane's own branded card.
    const tip = screen.getByTestId("pane-header-tip-Dana");
    expect(tip.textContent).toContain("Drag Dana by this bar");
    expect(tip.textContent).toContain("Double-click to fill the workspace");
    expect(
      screen.getByTestId("pane-header-Dana").getAttribute("title"),
    ).toBeNull();

    // A press is an answer, not a question — the card leaves as a drag begins.
    fireEvent.pointerDown(screen.getByTestId("pane-header-Dana"));
    expect(screen.queryByTestId("pane-header-tip-Dana")).toBeNull();
    vi.useRealTimers();
  });

  it("yields the gesture card to the bar's own controls", () => {
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onToggleMaximize={() => undefined}
        onArrangeStart={() => undefined}
      />,
    );

    // Hovering a control is a question about THAT control — the bar's card
    // never opens on top of it.
    fireEvent.pointerOver(screen.getByTestId("pane-maximize-Dana"));
    act(() => {
      vi.advanceTimersByTime(600);
    });
    expect(screen.queryByTestId("pane-header-tip-Dana")).toBeNull();
    vi.useRealTimers();
  });

  it("opens the gesture card over the title line, where the old tooltip lived", () => {
    vi.useFakeTimers();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        recap="Fix the failing login test"
        appearance="dark"
        fontSize={13}
        onToggleMaximize={() => undefined}
        onArrangeStart={() => undefined}
      />,
    );

    // The recap line is a button, but it spans nearly the whole bar — the
    // card must show there, not only on the bar's empty slivers.
    fireEvent.pointerOver(screen.getByTestId("pane-recap-Dana"));
    act(() => {
      vi.advanceTimersByTime(600);
    });

    const tip = screen.getByTestId("pane-header-tip-Dana");
    expect(tip.textContent).toContain("Drag Dana by this bar");
    vi.useRealTimers();
  });

  it("renames instead of maximizing when the call-sign is the target", () => {
    const onToggleMaximize = vi.fn();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onToggleMaximize={onToggleMaximize}
        onRename={async () => true}
      />,
    );

    // The call-sign is the more specific target and stops the event, so the
    // bar underneath never sees it — one gesture, one meaning.
    fireEvent.doubleClick(screen.getByText("Dana"));

    expect(screen.getByTestId("pane-rename-input-Dana")).toBeTruthy();
    expect(onToggleMaximize).not.toHaveBeenCalled();
  });

  it("leaves a double-click on one of its own buttons to that button", () => {
    const onToggleMaximize = vi.fn();
    const onClose = vi.fn();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onToggleMaximize={onToggleMaximize}
        onClose={onClose}
      />,
    );

    // Two clicks on Close are two closes, never a maximize — the same guard
    // the drag grip uses, for the same reason.
    fireEvent.doubleClick(screen.getByTestId("pane-close-Dana"));

    expect(onToggleMaximize).not.toHaveBeenCalled();
  });

  it("keeps every action visible on the focused pane", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        focused
        onToggleMaximize={() => undefined}
        onSplit={() => undefined}
        onClose={() => undefined}
      />,
    );

    const actions = screen.getByTestId("pane-maximize-Dana").parentElement;

    expect(actions).not.toBeNull();
    expect(actions?.className).toContain("opacity-100");
    expect(actions?.className).not.toContain("opacity-0 ");
  });
});

describe("pane split menu", () => {
  const CHOICES = [
    { name: "claude", displayName: "Claude Code", installed: true, kind: "cli" },
    { name: "codex", displayName: "Codex", installed: true, kind: "cli" },
    {
      name: "shell",
      displayName: "Plain Terminal",
      installed: true,
      kind: "shell",
      description: "PowerShell 7 — no agent, just a prompt",
    },
  ];

  beforeEach(() => {
    globalThis.ResizeObserver = ResizeObserverHarness;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it("offers a plain terminal beside the coding agents", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        agents={CHOICES}
        onSplit={() => undefined}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-split-right-Dana"));

    expect(screen.getByTestId("pane-split-right-Dana-claude").textContent).toContain(
      "Claude Code",
    );
    expect(screen.getByTestId("pane-split-right-Dana-codex").textContent).toContain(
      "Codex",
    );
    const plain = screen.getByTestId("pane-split-right-Dana-shell");
    expect(plain.textContent).toContain("Plain Terminal");
    // ...and it says what that actually opens, which is the whole difference.
    expect(plain.textContent).toContain("no agent");
  });

  it("splits with the plain-terminal entry the user picked", () => {
    const onSplit = vi.fn();
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        agents={CHOICES}
        onSplit={onSplit}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-split-down-Dana"));
    fireEvent.click(screen.getByTestId("pane-split-down-Dana-shell"));

    expect(onSplit).toHaveBeenCalledWith("down", "shell");
  });

  it("disables the plain terminal on a host with no shell, and says why", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        agents={[
          CHOICES[0],
          CHOICES[1],
          { name: "shell", displayName: "Plain Terminal", installed: false, kind: "shell" },
        ]}
        onSplit={() => undefined}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-split-right-Dana"));
    const plain = screen.getByTestId("pane-split-right-Dana-shell") as HTMLButtonElement;

    // Listed but unusable, so the absence explains itself instead of the entry
    // simply not being there — and in the terms of what is missing.
    expect(plain.disabled).toBe(true);
    expect(plain.textContent).toContain("no shell here");
  });
});

describe("pane refit", () => {
  /** jsdom measures nothing; the pane refuses to fit a host it reads as 0x0. */
  const giveTheHostASize = () => {
    for (const [property, value] of [
      ["clientWidth", 600],
      ["clientHeight", 400],
    ] as const) {
      Object.defineProperty(HTMLElement.prototype, property, {
        configurable: true,
        value,
      });
    }
  };

  const settle = () => {
    act(() => {
      vi.advanceTimersByTime(600);
    });
  };

  const pane = (
    maximized: boolean,
    extra: Partial<React.ComponentProps<typeof AgenticTerminal>> = {},
    active = true,
  ) => (
    <AgenticTerminal
      name="Dana"
      displayName="Claude Code"
      appearance="dark"
      fontSize={13}
      maximized={maximized}
      active={active}
      {...extra}
    />
  );

  beforeEach(() => {
    vi.useFakeTimers();
    globalThis.ResizeObserver = ResizeObserverHarness;
    giveTheHostASize();
    terminalHarness.fit.mockClear();
    terminalHarness.send.mockClear();
    terminalHarness.send.mockImplementation(() => true);
    terminalHarness.resize.mockClear();
    terminalHarness.handlers.current = null;
    terminalHarness.size = { cols: 80, rows: 24 };
    vi.spyOn(document, "hasFocus").mockReturnValue(true);
  });

  afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
    Reflect.deleteProperty(HTMLElement.prototype, "clientWidth");
    Reflect.deleteProperty(HTMLElement.prototype, "clientHeight");
  });

  it("gives the agent a narrower width only once the tile has held it", () => {
    // Recorded 2026-10-26: a layout switch squeezed every pane to 16 columns
    // for about a second. Claude Code sized a running command's output for
    // those 16 columns and kept that height, leaving 36 blank rows mid-pane.
    terminalHarness.gridCols = 80;
    terminalHarness.fit.mockImplementation(() => {
      terminalHarness.gridCols = terminalHarness.size.cols;
    });
    try {
      render(pane(false));
      settle();
      const sentCols = () =>
        terminalHarness.send.mock.calls
          .map(([frame]) => frame as { t?: string; cols?: number })
          .filter((frame) => frame.t === "r" || frame.t === "claim")
          .map((frame) => frame.cols);
      terminalHarness.send.mockClear();

      // A squeeze that is undone again never reaches the agent.
      terminalHarness.size = { cols: 16, rows: 24 };
      act(() => {
        window.dispatchEvent(new Event("resize"));
      });
      settle();
      terminalHarness.size = { cols: 80, rows: 24 };
      act(() => {
        window.dispatchEvent(new Event("resize"));
      });
      settle();
      act(() => {
        vi.advanceTimersByTime(NARROW_SETTLE_MS + 100);
      });
      expect(sentCols()).not.toContain(16);
      expect(terminalHarness.gridCols).toBe(80);

      // A narrowing that stays is applied once it has held.
      terminalHarness.size = { cols: 40, rows: 24 };
      act(() => {
        window.dispatchEvent(new Event("resize"));
      });
      settle();
      expect(sentCols()).not.toContain(40);
      act(() => {
        vi.advanceTimersByTime(NARROW_SETTLE_MS);
      });
      settle();
      expect(sentCols()).toContain(40);
      expect(terminalHarness.gridCols).toBe(40);

      // Widening is not held.
      terminalHarness.send.mockClear();
      terminalHarness.size = { cols: 90, rows: 24 };
      act(() => {
        window.dispatchEvent(new Event("resize"));
      });
      settle();
      expect(sentCols()).toContain(90);
    } finally {
      terminalHarness.gridCols = null;
      terminalHarness.fit.mockReset();
    }
  });

  it("follows a drag in throttled steps instead of freezing until release", () => {
    // The reported jank (2026-08-11): while a seam moved, the text froze and
    // the release re-wrapped it in one hard snap. Mid-drag the pane now takes
    // at most one fit per DRAG_REFIT_MS — the burst of observer ticks below
    // collapses to a single refit, taken while the drag is still going.
    const view = render(pane(false, { layoutBusy: true }));
    settle();
    terminalHarness.fit.mockClear();

    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(50);
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(50);
      window.dispatchEvent(new Event("resize"));
    });
    // The throttle window is still open — nothing has refitted yet.
    expect(terminalHarness.fit).not.toHaveBeenCalled();

    act(() => {
      vi.advanceTimersByTime(DRAG_REFIT_MS + 40);
    });
    // One fit for the whole burst, not one per tick.
    expect(terminalHarness.fit).toHaveBeenCalledTimes(1);

    // Letting go still lands the exact final size in its own immediate pass.
    terminalHarness.fit.mockClear();
    view.rerender(pane(false, { layoutBusy: false }));
    settle();
    expect(terminalHarness.fit).toHaveBeenCalled();
  });

  it("re-measures itself when the pane is maximized", () => {
    // The ResizeObserver harness never calls anyone back, which is the point:
    // this proves the pane no longer DEPENDS on that notification arriving.
    // When it went missing, the pane was maximized while the agent inside it
    // kept drawing at its old cell's width.
    const view = render(pane(false));
    settle();
    terminalHarness.fit.mockClear();

    view.rerender(pane(true));
    settle();

    expect(terminalHarness.fit).toHaveBeenCalled();
  });

  it("re-measures again when the pane is restored to its cell", () => {
    const view = render(pane(true));
    settle();
    terminalHarness.fit.mockClear();

    view.rerender(pane(false));
    settle();

    expect(terminalHarness.fit).toHaveBeenCalled();
  });

  it("does not re-announce a size the terminal process already has", () => {
    // Refitting is nearly free; telling the agent makes it redraw its whole
    // screen. A pane settles over several passes, so only changes go out.
    const view = render(pane(false));
    settle();
    terminalHarness.send.mockClear();

    view.rerender(pane(true));
    settle();

    expect(terminalHarness.send).not.toHaveBeenCalled();
  });

  it("reclaims the shared PTY geometry when the pane becomes active", () => {
    const view = render(pane(false, {}, false));
    settle();
    terminalHarness.send.mockClear();

    view.rerender(pane(false, {}, true));
    settle();

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 80,
      rows: 24,
    });
  });

  /*
   * A workspace the IDE keeps warm is not rebuilt when the user switches back
   * to it, so its panes never get the replay and repaint a fresh pane gets.
   * Reported 2026-10-03: a Claude pane drawing for fewer rows than its tile,
   * the prompt and status line halfway up with empty rows below. Coming back
   * asks the agent for a whole new screen — one row short, then back.
   */
  it("asks the agent for a whole new screen when a kept-warm pane comes back", () => {
    const view = render(pane(false, {}, true));
    settle();
    view.rerender(pane(false, {}, false));
    settle();
    terminalHarness.send.mockClear();

    view.rerender(pane(false, {}, true));
    act(() => {
      vi.advanceTimersByTime(RETURN_REPAINT_NUDGE_MS + 10);
    });

    const frames = terminalHarness.send.mock.calls.map(([frame]) => frame);
    const shorter = frames.findIndex((frame) => JSON.stringify(frame) === JSON.stringify({ t: "claim", cols: 80, rows: 23 }));
    expect(shorter).toBeGreaterThanOrEqual(0);
    expect(frames.slice(shorter + 1)).toContainEqual({ t: "claim", cols: 80, rows: 24 });
  });

  /*
   * Reported 2026-10-06: back from the thread layout, a pane that had followed
   * another geometry while hidden kept that grid, and the return nudge then
   * put the agent on the tile's size — lines wrapped into word fragments down
   * the pane's edge. Coming back on stage is a change made in this window, so
   * it takes the size back whatever `document.hasFocus()` answers.
   */
  it("takes the size back when a displaced pane returns to the stage without window focus", () => {
    const view = render(pane(false, {}, true));
    settle();
    view.rerender(pane(false, {}, false));
    settle();
    displacedBy(30, 10);
    vi.spyOn(document, "hasFocus").mockReturnValue(false);

    view.rerender(pane(false, {}, true));
    act(() => {
      vi.advanceTimersByTime(RETURN_REPAINT_NUDGE_MS + 10);
    });

    expect(terminalHarness.send).toHaveBeenCalledWith({ t: "claim", cols: 80, rows: 24 });
    expect(terminalHarness.send).not.toHaveBeenCalledWith(expect.objectContaining({ t: "r" }));
  });

  it("never sizes the agent while the pane is off the stage", () => {
    // The grid behind the maximized side panel is invisible but still laid
    // out in a narrowed tile; fitting there squeezed every agent to a strip.
    const view = render(pane(false, {}, true));
    settle();
    view.rerender(pane(false, {}, false));
    settle();
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 37, rows: 24 };
    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(600);
    });

    expect(terminalHarness.send).not.toHaveBeenCalled();
  });

  it("does not nudge a pane taking the stage for the first time", () => {
    const view = render(pane(false, {}, false));
    settle();
    terminalHarness.send.mockClear();

    view.rerender(pane(false, {}, true));
    act(() => {
      vi.advanceTimersByTime(RETURN_REPAINT_NUDGE_MS + 10);
    });

    expect(terminalHarness.send).not.toHaveBeenCalledWith(expect.objectContaining({ rows: 23 }));
  });

  /*
   * A pane open in two places — the desktop app and a tab some tool opened to
   * take a look — has two screens and ONE pseudo-terminal, and the server
   * hands the size to one of them. The other is told what the owner chose
   * (a `size` frame) and follows it, which is right while it is merely
   * watching and wrong the moment the user turns to it: until 2026-08-25 a
   * pane maximized in the desktop app was fitted to the whole window and
   * then resized straight back to the tab's strip, its agent drawing into
   * the top-left corner of it. Every size change the user makes HERE takes
   * the pane back.
   */
  const displacedBy = (cols: number, rows: number) => {
    act(() => {
      terminalHarness.handlers.current?.onGeometry?.({ cols, rows } as never);
    });
    expect(terminalHarness.resize).toHaveBeenCalledWith(cols, rows);
    terminalHarness.send.mockClear();
  };

  it("takes the pane back when it is maximized after another viewer took the size", () => {
    const view = render(pane(false));
    settle();
    displacedBy(30, 10);

    view.rerender(pane(true));
    settle();

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 80,
      rows: 24,
    });
  });

  it("takes the pane back when a seam drag ends after another viewer took the size", () => {
    const view = render(pane(false, { layoutBusy: true }));
    settle();
    displacedBy(30, 10);

    view.rerender(pane(false, { layoutBusy: false }));
    settle();

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 80,
      rows: 24,
    });
  });

  it("takes the pane back on a click even while the document claims to be hidden", () => {
    // The desktop shell can report an on-screen window as hidden for minutes
    // (2026-08-23). A pointer pressed on the pane is proof enough of where
    // the user is — gating this on the document's word left the app unable
    // to take its own terminals back at all.
    render(pane(false));
    settle();
    displacedBy(30, 10);
    vi.spyOn(document, "hasFocus").mockReturnValue(false);
    Object.defineProperty(document, "hidden", {
      configurable: true,
      get: () => true,
    });

    try {
      fireEvent.mouseDown(screen.getByTestId("agentic-terminal-host-Dana"));
      settle();
    } finally {
      Reflect.deleteProperty(document, "hidden");
    }

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 80,
      rows: 24,
    });
  });

  it("takes every displaced pane back on a gesture anywhere in the window", () => {
    // A tab another tool opened on the workspace took the size, and the
    // desktop shell never fired `focus`: all panes drew in the tab's geometry
    // until each one was clicked (2026-09-29). Moving the mouse over the app
    // is enough — and a pane that holds its size stays quiet.
    render(pane(false));
    settle();
    terminalHarness.send.mockClear();
    fireEvent.pointerMove(document.body);
    settle();
    expect(terminalHarness.send).not.toHaveBeenCalledWith(
      expect.objectContaining({ t: "claim" }),
    );

    displacedBy(30, 10);
    vi.spyOn(document, "hasFocus").mockReturnValue(false);
    fireEvent.pointerMove(document.body);
    settle();
    fireEvent.keyDown(document.body, { key: "a" });
    settle();

    const claims = terminalHarness.send.mock.calls.filter(
      ([frame]) => (frame as { t: string }).t === "claim",
    );
    expect(claims).toEqual([[{ t: "claim", cols: 80, rows: 24 }]]);
  });

  it("does not take a pane it is only watching from a window without focus", () => {
    // The browser tab beside the app, not in front: it follows the owner's
    // grid and stays quiet, whatever its own layout does.
    const view = render(pane(false));
    settle();
    displacedBy(30, 10);
    vi.spyOn(document, "hasFocus").mockReturnValue(false);

    view.rerender(pane(true));
    settle();

    expect(terminalHarness.send).not.toHaveBeenCalledWith(
      expect.objectContaining({ t: "claim" }),
    );
  });

  it("keeps the owner's geometry on a refit while its own tile has not changed", () => {
    // Refitting back to the tile and then staying quiet left xterm at the
    // tile's width while the agent drew for the owner's — rows drawn over
    // rows, worst on returning to the IDE, where every pane refits
    // (2026-09-29). The grid holds what the agent really draws for.
    render(pane(false));
    settle();
    displacedBy(30, 10);
    vi.spyOn(document, "hasFocus").mockReturnValue(false);
    terminalHarness.fit.mockClear();
    terminalHarness.resize.mockClear();

    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(600);
    });

    expect(terminalHarness.fit).not.toHaveBeenCalled();
    expect(terminalHarness.resize).not.toHaveBeenCalled();
    expect(terminalHarness.send).not.toHaveBeenCalled();
  });

  it("asks again when a displaced pane's tile really changes", () => {
    render(pane(false));
    settle();
    displacedBy(30, 10);
    vi.spyOn(document, "hasFocus").mockReturnValue(false);

    terminalHarness.size = { cols: 100, rows: 30 };
    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(600);
    });

    // The server answers a refused request with the owner's size again.
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "r",
      cols: 100,
      rows: 30,
    });
  });

  it("claims once, not on every pass of a settling layout", () => {
    // Three passes follow a maximize (see the effect). Holding the size after
    // the first, the pane has nothing to add on the next two.
    const view = render(pane(false));
    settle();
    displacedBy(30, 10);

    view.rerender(pane(true));
    settle();

    const claims = terminalHarness.send.mock.calls.filter(
      ([frame]) => (frame as { t: string }).t === "claim",
    );
    expect(claims).toHaveLength(1);
  });

  it("keeps offering a size the socket could not carry", () => {
    // A pane measured while its backend was restarting must not treat the
    // frame as delivered. Nothing measures a pane again on its own, so a size
    // counted as sent when it never left is lost for good — and the agent goes
    // on formatting for a size the pane no longer has.
    const view = render(pane(false));
    settle();

    terminalHarness.send.mockImplementation(() => false);
    terminalHarness.size.cols = 200;
    view.rerender(pane(true));
    settle();
    terminalHarness.send.mockClear();

    // Same size, still undelivered — so it goes out again rather than being
    // deduplicated away.
    terminalHarness.send.mockImplementation(() => true);
    view.rerender(pane(false));
    settle();

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 200,
      rows: 24,
    });
  });

  /*
   * A terminal is exactly as wide as the tile it is shown in.
   *
   * The maintainer's rule for this screen (2026-08-11), and the reason the
   * tests it replaced are gone rather than adjusted. Those pinned the opposite:
   * a pane rendered a fixed 60-column grid whatever its tile could show and cut
   * the rest off at the edge, so six terminals each showed about two thirds of
   * themselves — which is what "the sessions overlap, the right one slides over
   * the left" was describing. It was working exactly as written.
   *
   * The width a coding CLI wants did not stop mattering; it stopped being
   * enforced HERE. The launcher says from ten terminals up how narrow the panes
   * will come out and opens as many as the user asks for, and a pane too narrow
   * to be useful is a pane they can see is too narrow.
   */
  it("fits the tile it is shown in, down to the width its agent can draw in", () => {
    const view = render(pane(false));
    settle();
    terminalHarness.fit.mockClear();
    terminalHarness.send.mockClear();

    // Narrower than the pane opened at, and still a width a coding CLI lays a
    // frame out in. The tile's own measurement is what everyone gets.
    terminalHarness.size = { cols: 66, rows: 20 };
    view.rerender(pane(true));
    settle();

    expect(terminalHarness.fit).toHaveBeenCalled();
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 66,
      rows: 20,
    });
  });

  /*
   * …and to a width its agent cannot draw a tidy frame in, too.
   *
   * A pane used to HOLD its columns below 60 and show a card instead of its
   * terminal (2026-08-13, against a repaint bug measured at ~13 columns). That
   * card then covered every pane of an ordinary six-pane workspace at 56 columns
   * and had to be clicked away six times, so the maintainer retired it
   * (2026-08-18): the terminal is shown at the tile's width, whatever it is.
   */
  it("follows the tile down to a width its agent cannot draw a frame in", () => {
    const view = render(pane(false));
    settle();
    terminalHarness.fit.mockClear();
    terminalHarness.send.mockClear();

    // The crowded-grid measurement (~17 columns per cell, thirteen panes).
    terminalHarness.size = { cols: 17, rows: 6 };
    view.rerender(pane(true));
    settle();

    expect(terminalHarness.fit).toHaveBeenCalled();
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 17,
      rows: 6,
    });
    // No card, no notice — nothing between the reader and the terminal.
    expect(screen.queryByTestId("pane-too-narrow-Dana")).toBeNull();
    expect(screen.queryByTestId("pane-width-notice-Dana")).toBeNull();
  });

  /*
   * A pane that mounts HIDDEN measures nothing — and must say nothing.
   *
   * The chat stage hides every cell and draws the staged pane's transcript
   * over the canvas, so a pane opened there is display:none from its first
   * frame. The fit addon answers a hidden box with its own minimum, and
   * clamped to the floors that was a 10x4 terminal — which the handshake then
   * spawned the agent at (T11, 2026-08-27). Unmeasured, the terminal keeps
   * its constructed size and the agent starts in one it can draw in.
   *
   * And the handshake says "unmeasured" rather than passing that constructed
   * size off as a measurement. Handed 80x24, the server re-wrapped a RUNNING
   * agent for 80 columns no tile had, and again for the tile's real width the
   * moment the pane was shown — twice through ConPTY's re-flow, which is how
   * a workspace came back from a restart with every transcript cut through by
   * fragments of its own earlier layout (2026-08-27).
   */
  it("does not shrink a pane that mounts hidden to the floors", () => {
    Reflect.deleteProperty(HTMLElement.prototype, "clientWidth");
    Reflect.deleteProperty(HTMLElement.prototype, "clientHeight");
    // What the fit addon proposes for a display:none tile.
    terminalHarness.size = { cols: 2, rows: 1 };
    render(pane(false, {}, false));
    settle();

    expect(terminalHarness.resize).not.toHaveBeenCalled();
    expect(terminalHarness.fit).not.toHaveBeenCalled();
    expect(terminalHarness.send).not.toHaveBeenCalledWith(
      expect.objectContaining({ cols: 10, rows: 4 }),
    );
    expect(terminalHarness.opened.current).toEqual(
      expect.objectContaining(UNMEASURED_SIZE),
    );
  });

  it("hands over the tile's own measurement when there was one", () => {
    Object.defineProperty(HTMLElement.prototype, "clientWidth", {
      configurable: true,
      value: 600,
    });
    Object.defineProperty(HTMLElement.prototype, "clientHeight", {
      configurable: true,
      value: 400,
    });
    terminalHarness.size = { cols: 72, rows: 20 };
    render(pane(false));
    settle();

    expect(terminalHarness.fit).toHaveBeenCalled();
    expect(terminalHarness.opened.current).toEqual(
      expect.objectContaining({ cols: 72, rows: 20 }),
    );
    Reflect.deleteProperty(HTMLElement.prototype, "clientWidth");
    Reflect.deleteProperty(HTMLElement.prototype, "clientHeight");
  });

  it("gives xterm and the agent the same number, always", () => {
    // The one thing that must never drift. An agent laying its lines out for a
    // width its xterm does not have re-wraps every one of them, and the TUI's
    // cursor moves then land on rows that hold something else — a five-pane
    // grid came back as panes full of shredded one-word fragments (2026-08-10).
    for (const size of [
      { cols: 17, rows: 6 },
      { cols: 33, rows: 40 },
      { cols: 90, rows: 30 },
    ]) {
      terminalHarness.size = { cols: 80, rows: 24 };
      const view = render(pane(false));
      settle();
      terminalHarness.send.mockClear();
      terminalHarness.resize.mockClear();

      terminalHarness.size = size;
      view.rerender(pane(true));
      settle();

      // fit() sizes xterm to the tile, so the agent hearing the tile's own
      // measurement IS the two agreeing.
      expect(terminalHarness.send).toHaveBeenCalledWith({
        t: "claim",
        cols: size.cols,
        rows: size.rows,
      });
      view.unmount();
    }
  });

  it("refuses only a measurement that cannot be real", () => {
    // All the floor is for now: a tile mid-layout measures nothing, and a PTY
    // resized to zero columns permanently wrecks the agent's drawing.
    const view = render(pane(false));
    settle();
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 0, rows: 0 };
    view.rerender(pane(true));
    settle();

    expect(terminalHarness.send).not.toHaveBeenCalledWith(
      expect.objectContaining({ cols: 0 }),
    );
  });

  it("keeps the reader's text size on a narrow tile", () => {
    // The auto-shrink that walked a narrow pane's text down until 60 columns
    // fit was rejected the day it shipped (2026-08-11): it silently overrode
    // the toolbar's size on every narrow pane, which read as the size controls
    // being dead. The pane draws at the READER'S size and takes the columns
    // that leaves it.
    const view = render(pane(false));
    settle();
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 40, rows: 20 };
    view.rerender(pane(true));
    settle();

    const term =
      terminalHarness.instances[terminalHarness.instances.length - 1];
    expect(term?.options.fontSize).toBe(13);
    // The pane takes the tile's 40 columns without touching a single point of
    // the reader's text size, which is what this pins.
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 40,
      rows: 20,
    });
  });

  it("never offers a sideways scroll, at any width", () => {
    // A pane that can scroll sideways is a pane drawing past its own edge.
    // The horizontal window this used to open — scrollbar, two scroll shadows
    // — was an honest presentation of a thing that should not exist.
    const view = render(pane(false));
    settle();
    const host = screen.getByTestId("agentic-terminal-host-Dana");
    expect(host.className).toContain("overflow-hidden");
    expect(host.className).not.toContain("overflow-x-auto");

    terminalHarness.size = { cols: 12, rows: 5 };
    view.rerender(pane(true));
    settle();
    expect(host.className).toContain("overflow-hidden");
    expect(host.className).not.toContain("overflow-x-auto");
  });

  it("restores the reader's text size over one a stale build left behind", () => {
    // A pane shrunken by the rejected auto-shrink can survive into this build
    // through a rebuilt terminal or a long-lived window. The refit restates
    // the reader's choice before measuring, so the first resize puts it back.
    render(pane(false));
    settle();
    const term =
      terminalHarness.instances[terminalHarness.instances.length - 1];
    if (term) term.options.fontSize = 8;

    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(600);
    });

    expect(term?.options.fontSize).toBe(13);
  });

  it("reconnects with the size its tile has now, not the one it mounted with", () => {
    // The socket reconnects on its own. A handshake carrying the mount-time
    // size resized the agent to a grid the pane had long left (2026-09-29).
    render(pane(false));
    settle();
    expect(terminalHarness.opened.current).toEqual(
      expect.objectContaining({ cols: 80, rows: 24 }),
    );

    terminalHarness.size = { cols: 120, rows: 40 };

    expect(terminalHarness.opened.current?.cols).toBe(120);
    expect(terminalHarness.opened.current?.rows).toBe(40);
  });

  it("tells a fresh socket the pane's size whatever the last one heard", () => {
    render(pane(false));
    settle();
    terminalHarness.send.mockClear();

    // A reconnect gets a process that knows nothing about this pane, so the
    // size is announced again even though it has not changed.
    act(() => {
      terminalHarness.handlers.current?.onOpen?.();
      vi.advanceTimersByTime(600);
    });

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "r",
      cols: 80,
      rows: 24,
    });
  });

  /*
   * One pane, two viewers in ONE window: the office's pane window over the
   * same pane in the IDE grid. The cross-window rule (a gesture anywhere takes
   * a displaced pane back) had both of them take the size from each other on
   * every mouse move, two seconds apart — the agent redrawing for a wide and a
   * narrow screen in turn, the office window flickering with its text squeezed
   * into the left third (2026-09-29). The viewer opened as the pane's lead
   * keeps the size; the other follows it, and takes the size back only when
   * the lead is gone.
   */
  describe("two viewers of one pane in one window", () => {
    const claimsOn = (index: number) =>
      terminalHarness.sockets[index].sent.filter(
        (frame) => (frame as { t: string }).t === "claim",
      );
    const open = (index: number) =>
      act(() => {
        terminalHarness.sockets[index].handlers.onOpen?.();
      });
    const displace = (index: number) =>
      act(() => {
        terminalHarness.sockets[index].handlers.onGeometry?.({ cols: 30, rows: 10 } as never);
      });
    const clearSent = () => {
      for (const socket of terminalHarness.sockets) socket.sent.length = 0;
    };
    const grid = <AgenticTerminal key="grid" name="Dana" displayName="Claude Code" appearance="dark" fontSize={13} />;
    const office = <AgenticTerminal key="office" name="Dana" displayName="Claude Code" appearance="dark" fontSize={13} sizeLead headerMode="none" />;

    beforeEach(() => {
      terminalHarness.sockets = [];
    });

    it("leaves the size with the lead instead of taking it back on a gesture", () => {
      render(<>{grid}{office}</>);
      open(0);
      open(1);
      settle();
      // The office window claimed the pane; the grid behind it now shows its geometry.
      displace(0);
      clearSent();

      fireEvent.pointerMove(document.body);
      settle();
      fireEvent.keyDown(document.body, { key: "a" });
      settle();

      expect(claimsOn(0)).toEqual([]);
    });

    it("hands the size back to the grid when the lead closes", async () => {
      const view = render(<>{grid}{office}</>);
      open(0);
      open(1);
      settle();
      displace(0);
      clearSent();

      view.rerender(<>{grid}</>);
      // The hand-over waits a microtask, so a lead that merely rebuilds keeps the size.
      await act(async () => {
        await Promise.resolve();
      });
      settle();

      expect(claimsOn(0)).toEqual([{ t: "claim", cols: 80, rows: 24 }]);
    });

    it.each(["light", "dark"] as const)("immediately gives a maximized %s pane the office viewer's size lead", (appearance) => {
      const expanded = (maximized: boolean) => (
        <AgenticTerminal key="grid" name="Dana" displayName="Claude Code" appearance={appearance} fontSize={13} maximized={maximized} />
      );
      const view = render(<>{expanded(false)}{office}</>);
      open(0);
      open(1);
      settle();
      displace(0);
      clearSent();
      terminalHarness.size = { cols: 160, rows: 60 };
      const focused = document.activeElement;

      view.rerender(<>{expanded(true)}{office}</>);

      // No observer, animation frame, timer, or second click may be needed.
      expect(claimsOn(0)).toEqual([{ t: "claim", cols: 160, rows: 60 }]);
      expect(document.activeElement).toBe(focused);
      expect(terminalHarness.sockets).toHaveLength(2);
      settle();
      expect(claimsOn(0)).toHaveLength(1);

      // A passive gesture cannot return the size to the small office viewer.
      displace(1);
      clearSent();
      fireEvent.pointerMove(document.body);
      settle();
      expect(claimsOn(1)).toEqual([]);

      // Restoring also lands its geometry immediately, without reconnecting.
      terminalHarness.size = { cols: 80, rows: 24 };
      clearSent();
      view.rerender(<>{expanded(false)}{office}</>);
      expect(claimsOn(0)).toEqual([{ t: "claim", cols: 80, rows: 24 }]);
      expect(terminalHarness.sockets).toHaveLength(2);
    });

    it("gives the lead to the viewer the user presses", () => {
      const view = render(<>{grid}{office}</>);
      open(0);
      open(1);
      settle();
      displace(0);
      clearSent();

      // A press on the grid's pane is an explicit choice: it takes the size…
      const hosts = view.getAllByTestId("agentic-terminal-host-Dana");
      fireEvent.mouseDown(hosts[0]);
      settle();
      expect(claimsOn(0)).toEqual([{ t: "claim", cols: 80, rows: 24 }]);

      // …and the office window, now the one displaced, no longer takes it back
      // on the next mouse move.
      displace(1);
      clearSent();
      act(() => {
        vi.advanceTimersByTime(2_500);
      });
      fireEvent.pointerMove(document.body);
      settle();
      expect(claimsOn(1)).toEqual([]);
    });

    it("keeps the lead through a rebuild of the viewer that holds it", async () => {
      // A restart (or the font arriving) rebuilds the terminal inside the same
      // viewer. Coming back leaderless let the other viewer pass the gesture
      // test again, and the two traded the size as before the fix.
      const pressed = (token: number) => (
        <AgenticTerminal key="grid" name="Dana" displayName="Claude Code" appearance="dark" fontSize={13} restartToken={token} />
      );
      const view = render(<>{pressed(0)}{office}</>);
      open(0);
      open(1);
      settle();
      fireEvent.mouseDown(view.getAllByTestId("agentic-terminal-host-Dana")[0]);
      settle();

      view.rerender(<>{pressed(1)}{office}</>);
      await act(async () => {
        await Promise.resolve();
      });
      open(2);
      settle();
      displace(1);
      clearSent();
      act(() => {
        vi.advanceTimersByTime(2_500);
      });
      fireEvent.pointerMove(document.body);
      settle();

      expect(claimsOn(1)).toEqual([]);
    });
  });
});

/*
 * A pane may not change shape underneath a half-parsed screen.
 *
 * Resizing xterm REFLOWS its buffer, and an agent's TUI is drawn by relative
 * cursor moves — "up twelve rows, erase from here". A reflow landing between
 * two of xterm's parse slices moves the rows the rest of that stream is
 * addressing, so the erase lands on rows holding something else: the frame's
 * rule drawn twice, an answer printed under the copy it was replacing, or a
 * pane wiped down to its status line. Reported 2026-08-11 as terminals that
 * come out broken when they are moved around the workspace — which is exactly
 * when a pane's geometry changes while its agents are talking.
 */
describe("pane refit while the agent is drawing", () => {
  const giveTheHostASize = () => {
    for (const [property, value] of [
      ["clientWidth", 600],
      ["clientHeight", 400],
    ] as const) {
      Object.defineProperty(HTMLElement.prototype, property, {
        configurable: true,
        value,
      });
    }
  };

  const pane = () => (
    <AgenticTerminal
      name="Dana"
      displayName="Claude Code"
      appearance="dark"
      fontSize={13}
    />
  );

  /** Run the pane's debounced fit request without reaching the parse deadline. */
  const askForARefit = () => {
    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(RESIZE_PARSE_WAIT_MS - 100);
    });
  };

  beforeEach(() => {
    vi.useFakeTimers();
    globalThis.ResizeObserver = ResizeObserverHarness;
    giveTheHostASize();
    terminalHarness.fit.mockClear();
    terminalHarness.resize.mockClear();
    terminalHarness.send.mockClear();
    terminalHarness.send.mockImplementation(() => true);
    terminalHarness.handlers.current = null;
    terminalHarness.size = { cols: 80, rows: 24 };
    terminalHarness.deferWrite = false;
    terminalHarness.writeCallbacks = [];
    vi.spyOn(document, "hasFocus").mockReturnValue(true);
  });

  afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
    terminalHarness.deferWrite = false;
    terminalHarness.writeCallbacks = [];
    Reflect.deleteProperty(HTMLElement.prototype, "clientWidth");
    Reflect.deleteProperty(HTMLElement.prototype, "clientHeight");
  });

  /** Hand the pane output xterm has not finished parsing. */
  const startDrawing = () => {
    terminalHarness.deferWrite = true;
    act(() => {
      terminalHarness.handlers.current?.onOutput?.(
        "redrawing the frame" as never,
      );
    });
  };

  /** Let xterm finish parsing what it was handed. */
  const finishDrawing = () => {
    act(() => {
      for (const done of terminalHarness.writeCallbacks.splice(0)) done();
    });
  };

  it("holds the fit back while xterm is still parsing", () => {
    render(pane());
    act(() => {
      vi.advanceTimersByTime(600);
    });
    startDrawing();
    terminalHarness.fit.mockClear();
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 40, rows: 12 };
    askForARefit();

    // Nothing has moved: the half-parsed screen keeps the grid it was drawn
    // into until it is finished with it.
    expect(terminalHarness.fit).not.toHaveBeenCalled();
    expect(terminalHarness.resize).not.toHaveBeenCalled();
    expect(terminalHarness.send).not.toHaveBeenCalled();
  });

  it("takes the new size the moment the parser reaches a gap", () => {
    render(pane());
    act(() => {
      vi.advanceTimersByTime(600);
    });
    startDrawing();
    terminalHarness.fit.mockClear();
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 64, rows: 12 };
    askForARefit();
    finishDrawing();

    expect(terminalHarness.fit).toHaveBeenCalled();
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "r",
      cols: 64,
      rows: 12,
    });
  });

  it("fits anyway when the pane never stops talking", () => {
    // The wait is bounded on purpose: an agent midway through streaming an
    // answer offers no gap at all, and a terminal that never follows its tile
    // is worse than one frame parsed across a reflow — the agent would go on
    // formatting for a size no window is showing.
    render(pane());
    act(() => {
      vi.advanceTimersByTime(600);
    });
    startDrawing();
    terminalHarness.fit.mockClear();
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 64, rows: 12 };
    askForARefit();
    act(() => {
      vi.advanceTimersByTime(RESIZE_PARSE_WAIT_MS + 50);
    });

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "r",
      cols: 64,
      rows: 12,
    });
  });

  /*
   * A narrow pane is a terminal like any other — nothing is put over it and
   * nothing is said about it. The card that hid the terminal below 60 columns
   * ("Show it anyway") and the notice row before it were both retired by the
   * maintainer (2026-08-18): at 56 columns an ordinary six-pane workspace had
   * every pane covered. What the panes will measure is said ONCE, in the wizard,
   * before anything opens.
   */
  it("shows the terminal at the tile's width, however narrow, and says nothing", () => {
    terminalHarness.size = { cols: 22, rows: 30 };
    render(pane());
    act(() => {
      vi.advanceTimersByTime(600);
    });

    expect(screen.queryByTestId("pane-too-narrow-Dana")).toBeNull();
    expect(screen.queryByTestId("pane-width-notice-Dana")).toBeNull();
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "claim",
      cols: 22,
      rows: 30,
    });

    // Narrower still: still the tile's own width, still no argument.
    terminalHarness.send.mockClear();
    terminalHarness.size = { cols: 14, rows: 30 };
    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(600);
    });
    expect(screen.queryByTestId("pane-too-narrow-Dana")).toBeNull();
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "r",
      cols: 14,
      rows: 30,
    });
  });

  it("stays quiet on a pane with room to work", () => {
    terminalHarness.size = { cols: 80, rows: 24 };
    render(pane());
    act(() => {
      vi.advanceTimersByTime(600);
    });

    expect(screen.queryByTestId("pane-width-notice-Dana")).toBeNull();
    expect(screen.queryByTestId("pane-too-narrow-Dana")).toBeNull();
  });

  it("takes the room the moment the pane is given it", () => {
    terminalHarness.size = { cols: 22, rows: 30 };
    render(pane());
    act(() => {
      vi.advanceTimersByTime(600);
    });
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 120, rows: 30 };
    act(() => {
      window.dispatchEvent(new Event("resize"));
      vi.advanceTimersByTime(600);
    });

    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "r",
      cols: 120,
      rows: 30,
    });
  });

  it("still fits a quiet pane in the same pass", () => {
    // The gate must cost nothing when there is nothing to wait for: a pane
    // whose agent is idle refits immediately, as it always did.
    render(pane());
    act(() => {
      vi.advanceTimersByTime(600);
    });
    terminalHarness.fit.mockClear();
    terminalHarness.send.mockClear();

    terminalHarness.size = { cols: 64, rows: 12 };
    askForARefit();

    expect(terminalHarness.fit).toHaveBeenCalled();
    expect(terminalHarness.send).toHaveBeenCalledWith({
      t: "r",
      cols: 64,
      rows: 12,
    });
  });
});

/**
 * A pane opened by voice used to be an empty black rectangle for seconds on
 * end, and "open two more terminals" therefore looked like it had silently
 * failed (maintainer report 2026-07-28). The pane now says it is starting until
 * its agent draws something.
 */
describe("pane start-up feedback", () => {
  beforeEach(() => {
    globalThis.ResizeObserver = ResizeObserverHarness;
    terminalHarness.handlers.current = null;
    terminalHarness.focus.mockClear();
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it("says which CLI it is starting while the pane is still blank", () => {
    render(
      <AgenticTerminal
        name="T5"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    expect(screen.getByTestId("agentic-pane-starting-T5").textContent).toContain(
      "Starting Claude Code",
    );
  });

  it("gets out of the way as soon as the agent draws its first byte", () => {
    render(
      <AgenticTerminal
        name="T5"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    act(() => {
      terminalHarness.handlers.current?.onOutput?.(
        "Claude Code v2.1.220" as never,
      );
    });

    expect(screen.queryByTestId("agentic-pane-starting-T5")).toBeNull();
  });

  it("does not let a hidden pane steal focus when it finishes connecting", () => {
    render(
      <AgenticTerminal
        name="T5"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        focused
        active={false}
      />,
    );

    act(() => {
      terminalHarness.handlers.current?.onReady?.(
        { resumed: false, reattached: false, lastPrompt: null } as never,
      );
    });

    expect(terminalHarness.focus).not.toHaveBeenCalled();
  });

  /**
   * The overlay must never cover a pane the user could otherwise act on: an
   * exited or unreachable pane has a restart button and a reason of its own,
   * and a hopeful spinner over either would be a lie.
   */
  it("stands down when the pane reports trouble rather than progress", () => {
    render(
      <AgenticTerminal
        name="T5"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    act(() => {
      terminalHarness.handlers.current?.onTrouble?.(
        "This terminal is no longer part of the open workspace." as never,
        false as never,
      );
    });

    expect(screen.queryByTestId("agentic-pane-starting-T5")).toBeNull();
  });
});

describe("renaming a pane", () => {
  beforeEach(() => {
    globalThis.ResizeObserver = ResizeObserverHarness;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  it("offers no rename control when the owner cannot save one", () => {
    render(
      <AgenticTerminal
        name="T1"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    // A pencil that opens an editor nothing can save would be worse than the
    // plain badge it replaces.
    expect(screen.queryByTestId("pane-rename-T1")).toBeNull();
  });

  it("saves the typed call-sign", async () => {
    const onRename = vi.fn(async () => true);
    render(
      <AgenticTerminal
        name="T1"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onRename={onRename}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-rename-T1"));
    const input = screen.getByTestId("pane-rename-input-T1") as HTMLInputElement;
    // It opens on the current name, so a small correction is not a retype.
    expect(input.value).toBe("T1");
    fireEvent.change(input, { target: { value: "Frontend" } });
    fireEvent.click(screen.getByTestId("pane-rename-save-T1"));

    await act(async () => undefined);
    expect(onRename).toHaveBeenCalledWith("Frontend");
    expect(screen.queryByTestId("pane-rename-input-T1")).toBeNull();
  });

  it("keeps the typing when the name was refused", async () => {
    const onRename = vi.fn(async () => false);
    render(
      <AgenticTerminal
        name="T1"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onRename={onRename}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-rename-T1"));
    fireEvent.change(screen.getByTestId("pane-rename-input-T1"), {
      target: { value: "Api" },
    });
    fireEvent.click(screen.getByTestId("pane-rename-save-T1"));

    await act(async () => undefined);
    // A duplicate call-sign is a name to CHANGE — throwing the typing away
    // would make the user retype the part that was fine.
    const input = screen.getByTestId("pane-rename-input-T1") as HTMLInputElement;
    expect(input.value).toBe("Api");
  });

  it("closes on Escape without saving", async () => {
    const onRename = vi.fn(async () => true);
    render(
      <AgenticTerminal
        name="T1"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onRename={onRename}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-rename-T1"));
    fireEvent.keyDown(screen.getByTestId("pane-rename-input-T1"), {
      key: "Escape",
    });

    await act(async () => undefined);
    expect(onRename).not.toHaveBeenCalled();
    expect(screen.queryByTestId("pane-rename-input-T1")).toBeNull();
  });

  it("does not call the backend when the name was not changed", async () => {
    const onRename = vi.fn(async () => true);
    render(
      <AgenticTerminal
        name="T1"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        onRename={onRename}
      />,
    );

    fireEvent.click(screen.getByTestId("pane-rename-T1"));
    fireEvent.click(screen.getByTestId("pane-rename-save-T1"));

    await act(async () => undefined);
    expect(onRename).not.toHaveBeenCalled();
    expect(screen.queryByTestId("pane-rename-input-T1")).toBeNull();
  });
});

/*
 * The toolbar's text size is an ACCESSIBILITY control: somebody who cannot
 * comfortably read 13px sets 20 once and expects every pane to be readable,
 * not the one they happen to be typing in.
 *
 * What broke that was invisible in every earlier test, because the terminal
 * double ignored the options it was constructed with: the pane froze the size
 * and theme it first rendered with and handed them to every terminal it built
 * afterwards. A pane replaces its terminal without remounting — the grid
 * re-measures and `geometryReady` flips, a pane is restarted or renamed — and
 * since the size effect fires on CHANGES, nothing came along afterwards to
 * correct the resurrected value. The panes rebuilt since the last change sat
 * at the startup size for good, next to the ones that were not.
 */
describe("terminal text size across a rebuild", () => {
  beforeEach(() => {
    terminalHarness.instances.length = 0;
    terminalHarness.clearTextureAtlas.mockClear();
    terminalHarness.spacingAtOpen.length = 0;
    globalThis.ResizeObserver = ResizeObserverHarness;
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  const newest = () => terminalHarness.instances[terminalHarness.instances.length - 1];

  it.each(["light", "dark"] as const)("keeps the glyph cache warm when opening and restarting %s panes", (appearance) => {
    const pane = (name: string, restartToken = 0) => (
      <AgenticTerminal key={name} name={name} displayName="Codex" appearance={appearance} fontSize={13} restartToken={restartToken} />
    );
    const view = render(<>{pane("First")}</>);
    expect(terminalHarness.clearTextureAtlas).not.toHaveBeenCalled();
    const first = newest();

    view.rerender(<>{pane("First")}{pane("Second")}</>);
    expect(terminalHarness.instances).toHaveLength(2);
    expect(terminalHarness.instances[0]).toBe(first);
    expect(terminalHarness.clearTextureAtlas).not.toHaveBeenCalled();

    view.rerender(<>{pane("First")}{pane("Second", 1)}</>);
    expect(terminalHarness.instances).toHaveLength(3);
    expect(terminalHarness.clearTextureAtlas).not.toHaveBeenCalled();
  });

  it("still refreshes glyphs on real theme and font changes without reconnecting", () => {
    const view = render(<AgenticTerminal name="Dana" displayName="Codex" appearance="dark" fontSize={13} />);
    const terminal = newest();
    terminalHarness.clearTextureAtlas.mockClear();

    view.rerender(<AgenticTerminal name="Dana" displayName="Codex" appearance="light" fontSize={13} />);
    expect(terminalHarness.clearTextureAtlas).toHaveBeenCalledTimes(1);
    expect(newest().options.theme).not.toBeUndefined();

    view.rerender(<AgenticTerminal name="Dana" displayName="Codex" appearance="light" fontSize={18} />);
    expect(terminalHarness.clearTextureAtlas).toHaveBeenCalledTimes(2);
    expect(newest().options.fontSize).toBe(18);
    expect(newest()).toBe(terminal);
    expect(terminalHarness.instances).toHaveLength(1);
  });

  it("aligns fractional font cells before opening the renderer and measuring the first grid", () => {
    vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({
      font: "",
      measureText: () => ({ width: 249.6 }),
    } as unknown as CanvasRenderingContext2D);

    render(<AgenticTerminal name="Dana" displayName="Codex" appearance="dark" fontSize={13} />);
    expect(terminalHarness.spacingAtOpen).toEqual([1]);
    expect(terminalHarness.clearTextureAtlas).not.toHaveBeenCalled();
  });

  it("keeps the xterm canvas clear over the shared translucent pane shell", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );

    expect(newest().options.allowTransparency).toBe(true);
    // Alpha 0 keeps the canvas clear; the RGB is the ground the
    // minimum-contrast floor measures truecolor foregrounds against.
    expect(
      (newest().options.theme as Record<string, unknown>).background,
    ).toBe("rgba(18, 20, 26, 0)");
    expect(newest().options.minimumContrastRatio).toBe(4.5);
    expect(screen.getByTestId("agentic-pane-Dana").style.background).toBe(
      "rgba(18, 18, 18, 0.58)",
    );
  });

  it("draws a light pane opaque so its glyphs get subpixel smoothing", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="light"
        fontSize={13}
      />,
    );

    expect(newest().options.allowTransparency).toBe(false);
    expect((newest().options.theme as Record<string, unknown>).background).toBe("#fcfbf8");
    expect(newest().options.fontWeight).toBe(600);
  });

  it("lets a TUI canvas fill fall through to the glass instead of covering it", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
      />,
    );
    terminalHarness.write.mockClear();

    act(() => {
      terminalHarness.handlers.current?.onOutput?.(
        "\x1b[48;2;20;20;20m     \x1b[0m" as never,
      );
    });

    // GrokNight paints #141414 on every empty cell. That RGB is the canvas
    // fill; xterm must see the default background so the pane shell shows.
    // The rewrite is not keyed on the pane's product name.
    expect(terminalHarness.write).toHaveBeenCalledWith("\x1b[49m     \x1b[0m");
  });

  it("clears GrokDay's light canvas the same way", () => {
    render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="light"
        fontSize={13}
      />,
    );
    terminalHarness.write.mockClear();

    act(() => {
      terminalHarness.handlers.current?.onOutput?.(
        "\x1b[48;2;238;238;238m     \x1b[0m" as never,
      );
    });

    expect(terminalHarness.write).toHaveBeenCalledWith("\x1b[49m     \x1b[0m");
  });

  it("builds a replacement terminal at the size the user is looking at", () => {
    const view = render(
      <AgenticTerminal name="Dana" displayName="Claude Code" appearance="dark" fontSize={13} />,
    );
    expect(newest().options.fontSize).toBe(13);

    // The reader turns the text up while the pane is live.
    view.rerender(
      <AgenticTerminal name="Dana" displayName="Claude Code" appearance="dark" fontSize={20} />,
    );
    expect(newest().options.fontSize).toBe(20);

    // ...and the grid is re-measured, which rebuilds the terminal underneath a
    // pane that never unmounted. This is the pane the user was NOT typing in.
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={20}
        geometryReady={false}
      />,
    );
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={20}
        geometryReady
      />,
    );

    expect(terminalHarness.instances.length).toBeGreaterThan(1);
    expect(newest().options.fontSize).toBe(20);
  });

  it("restates the size to a terminal restarted after the change", () => {
    const view = render(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="dark"
        fontSize={13}
        restartToken={0}
      />,
    );
    // Copied now: the live restyle below writes the new theme onto THIS
    // instance too, so reading it afterwards would compare light against light.
    const openedWith = { ...(newest().options.theme as Record<string, unknown>) };
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="light"
        fontSize={18}
        restartToken={0}
      />,
    );
    view.rerender(
      <AgenticTerminal
        name="Dana"
        displayName="Claude Code"
        appearance="light"
        fontSize={18}
        restartToken={1}
      />,
    );

    // The theme travels with the size: both were frozen by the same ref, so a
    // restarted pane came back in the palette it opened with.
    expect(newest().options.fontSize).toBe(18);
    expect(newest().options.theme).not.toEqual(openedWith);
  });
});

/**
 * What a pane says about its own state, and where it says it.
 *
 * Both halves used to live somewhere that lost them. The reason a pane died was
 * written INTO the terminal, where the next thing drawn scrolls it away and the
 * one-line-per-kind-of-trouble guard means it is never written again; the way
 * out was a button in the hover-only action cluster. The badge and the notice
 * below are the durable versions of each.
 */
describe("pane status", () => {
  beforeEach(() => {
    globalThis.ResizeObserver = ResizeObserverHarness;
    terminalHarness.handlers.current = null;
    terminalHarness.write.mockClear();
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  const pane = (props: Record<string, unknown> = {}) => (
    <AgenticTerminal
      name="Dana"
      displayName="Claude Code"
      appearance="dark"
      fontSize={13}
      {...props}
    />
  );

  it("opens carrying its connecting state, with no notice to answer yet", () => {
    render(pane());

    expect(screen.getByTestId("pane-status-Dana").dataset.status).toBe(
      "connecting",
    );
    expect(screen.getByTestId("pane-activity").dataset.icon).toBe("spinner");
    expect(screen.queryByTestId("pane-notice-Dana")).toBeNull();
  });

  /**
   * `live` is a property of the PIPE and true for nearly every pane nearly all
   * the time, so a standing dot on twelve headers marks nothing. It stays in
   * the DOM and fades in with the header's other controls.
   */
  it("keeps a healthy pane's badge quiet until the header is hovered", () => {
    render(pane());

    act(() => {
      terminalHarness.handlers.current?.onReady?.(
        { resumed: false, reattached: false, lastPrompt: null } as never,
      );
    });

    const badge = screen.getByTestId("pane-status-Dana");
    expect(badge.dataset.status).toBe("live");
    expect(badge.className).toContain("opacity-0");
    expect(badge.className).toContain("group-hover/header:opacity-60");
    expect(screen.queryByTestId("pane-notice-Dana")).toBeNull();
  });

  it("says what happened when the agent exits, and offers the way back", () => {
    const onRestart = vi.fn();
    render(pane({ onRestart }));

    act(() => {
      terminalHarness.handlers.current?.onExit?.(0 as never);
    });

    expect(screen.getByTestId("pane-status-Dana").dataset.status).toBe("exited");
    const notice = screen.getByTestId("pane-notice-Dana");
    expect(notice.dataset.tone).toBe("warning");
    // The exit reason is a CLAUSE ("stopped"), so the notice puts the agent in
    // front of it rather than showing a strip that reads as one bare word.
    expect(notice.textContent).toContain("Claude Code stopped");

    fireEvent.click(screen.getByTestId("pane-restart-Dana"));
    expect(onRestart).toHaveBeenCalledTimes(1);
  });

  it("marks an unreachable pane as an error rather than a warning", () => {
    render(pane({ onRestart: () => undefined }));

    act(() => {
      terminalHarness.handlers.current?.onTrouble?.(
        "This pane could not be reached." as never,
        false as never,
      );
    });

    expect(screen.getByTestId("pane-status-Dana").dataset.status).toBe("error");
    const notice = screen.getByTestId("pane-notice-Dana");
    expect(notice.dataset.tone).toBe("error");
    expect(notice.textContent).toContain("could not be reached");
  });

  /*
   * The edge, read the way the browser reads it.
   *
   * jsdom re-serialises a colour the moment it is assigned, and it does not
   * agree with the source spelling about spaces — so the expected value is put
   * through the SAME assignment rather than compared as a string. Otherwise
   * this test is about `rgba(0,0,0,0.05)` versus `rgba(0, 0, 0, 0.05)`, which
   * is a fact about jsdom and not about the pane.
   */
  const asBorderColor = (value: string) => {
    const probe = document.createElement("div");
    probe.style.borderColor = value;
    return probe.style.borderColor;
  };

  /**
   * The edge is what a reader can SWEEP — the badge and the notice both have to
   * be landed on first. A pane whose agent is gone recedes; one that failed
   * carries the terminal's own red.
   */
  it("carries the pane's lifecycle in its edge", () => {
    render(pane({ onRestart: () => undefined }));
    const frame = screen.getByTestId("agentic-pane-Dana");
    const resting = frame.style.borderColor;

    expect(resting).toBe(asBorderColor(PANE_CHROME.dark.edge.connecting));

    act(() => {
      terminalHarness.handlers.current?.onExit?.(0 as never);
    });
    expect(frame.style.borderColor).toBe(
      asBorderColor(PANE_CHROME.dark.edge.exited),
    );
    // Dimmer than resting rather than another colour — a finished terminal is
    // not a problem, so it steps back instead of announcing itself.
    expect(frame.style.borderColor).not.toBe(resting);

    act(() => {
      terminalHarness.handlers.current?.onTrouble?.(
        "This pane could not be reached." as never,
        false as never,
      );
    });
    expect(frame.style.borderColor).toBe(
      asBorderColor(PANE_CHROME.dark.edge.error),
    );
  });

  /**
   * The three accent states paint the edge through a CLASS, and an inline
   * colour beats every class. So this is not a style preference — leaving the
   * property set is what stopped a dragged pane, and one that had just been
   * handed a prompt, from showing anything but the shadow half of its own
   * highlight.
   */
  it("lets the accent states own the edge outright", () => {
    const view = render(pane({ focused: true }));
    const frame = screen.getByTestId("agentic-pane-Dana");

    expect(frame.style.borderColor).toBe("");
    expect(frame.className).toContain("border-border-strong");

    view.rerender(pane({ focused: false }));
    expect(frame.style.borderColor).not.toBe("");

    // A prompt just landed: two seconds of ring, edge included.
    act(() => {
      terminalHarness.handlers.current?.onPrompt?.(
        { text: "Run the tests", at: 2, chars: 13 } as never,
      );
    });
    expect(frame.style.borderColor).toBe("");
  });

  /**
   * A scheduled retry is not a dead pane. Calling it an error there is what
   * painted a whole grid red over a backend that was merely restarting — the
   * notice must stand down with it.
   */
  it("takes the notice away again while the socket is only retrying", () => {
    render(pane());

    act(() => {
      terminalHarness.handlers.current?.onTrouble?.(
        "Reconnecting…" as never,
        true as never,
      );
    });

    expect(screen.getByTestId("pane-status-Dana").dataset.status).toBe(
      "connecting",
    );
    expect(screen.queryByTestId("pane-notice-Dana")).toBeNull();
  });
});

/*
 * The grid is built from ONE measured glyph, and the agent hears that grid's
 * column count in the pane's first message. Measured before the display font
 * has arrived — it is `@import`ed from the network, so after a restart it is
 * routinely a moment late — the glyph is the fallback's and the count is
 * wrong: on the desktop (2026-08-27, 20px text) a 744px tile was announced as
 * 67 columns, then as 62 once JetBrains Mono landed, and ConPTY re-flowed a
 * running agent's whole transcript through both. So a pane waits for the font
 * before it builds anything — and not for longer than the bounded wait.
 */
describe("pane font readiness", () => {
  /** A FontFaceSet the test controls: which faces exist, and whether they loaded. */
  function fakeFonts() {
    const faces: { family: string }[] = [];
    let loaded = false;
    const listeners = new Set<() => void>();
    const set = {
      check: vi.fn(() => loaded),
      load: vi.fn(() => Promise.resolve(loaded ? faces : [])),
      addEventListener: (type: string, fn: () => void) => {
        if (type === "loadingdone") listeners.add(fn);
      },
      removeEventListener: (_type: string, fn: () => void) => {
        listeners.delete(fn);
      },
      [Symbol.iterator]: () => faces[Symbol.iterator](),
    };
    return {
      set,
      /** The @import landed: the face is declared, and it has loaded. */
      arrive: () => {
        faces.push({ family: '"JetBrains Mono"' });
        loaded = true;
        for (const fn of listeners) fn();
      },
    };
  }

  const pane = () => (
    <AgenticTerminal
      name="Dana"
      displayName="Claude Code"
      appearance="dark"
      fontSize={20}
    />
  );

  let fonts: ReturnType<typeof fakeFonts>;

  beforeEach(() => {
    vi.useFakeTimers();
    globalThis.ResizeObserver = ResizeObserverHarness;
    terminalHarness.open.mockClear();
    terminalHarness.opened.current = null;
    fonts = fakeFonts();
    Object.defineProperty(document, "fonts", {
      configurable: true,
      value: fonts.set,
    });
  });

  afterEach(() => {
    Reflect.deleteProperty(document, "fonts");
    vi.useRealTimers();
    vi.restoreAllMocks();
  });

  it("does not build its grid before the display font can be measured", async () => {
    render(pane());
    await act(async () => {
      await vi.advanceTimersByTimeAsync(200);
    });
    expect(terminalHarness.open).not.toHaveBeenCalled();
    expect(terminalHarness.opened.current).toBeNull();

    fonts.arrive();
    await act(async () => {
      await vi.advanceTimersByTimeAsync(100);
    });
    expect(terminalHarness.open).toHaveBeenCalledTimes(1);
    expect(terminalHarness.opened.current).not.toBeNull();
  });

  it("builds on the font that is there once the wait runs out", async () => {
    render(pane());
    await act(async () => {
      await vi.advanceTimersByTimeAsync(FONT_WAIT_MS - 100);
    });
    expect(terminalHarness.open).not.toHaveBeenCalled();

    await act(async () => {
      await vi.advanceTimersByTimeAsync(200);
    });
    expect(terminalHarness.open).toHaveBeenCalledTimes(1);
  });

  it("mounts at once when the font is already here", () => {
    fonts.arrive();
    render(pane());
    expect(terminalHarness.open).toHaveBeenCalledTimes(1);
  });
});