Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

33.8 KB
Raw Blame
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { readComposerSource } from "./helpers/composer-source.mjs";
import { readMainSource } from "./helpers/main-source.mjs";
import { loadStyles } from "./helpers/styles.mjs";
import { readStoreSource } from "./helpers/store-source.mjs";
import { readTranscriptSource } from "./helpers/transcript-source.mjs";
import { readSharedTypesSource } from "./helpers/shared-types-source.mjs";

const stylesSource = await loadStyles();
const transcriptSource = await readTranscriptSource();
const inspectorSource = await readFile(
  new URL("../src/components/ContextUsageInspector.tsx", import.meta.url),
  "utf8",
);
const composerSource = await readComposerSource();
const minimapSource = await readFile(
  new URL("../src/components/ConversationMinimap.tsx", import.meta.url),
  "utf8",
);

test("user turns keep a compact right-aligned plate", () => {
  const userBubbleStyles = stylesSource.match(
    /\.message-row\.user \.message-bubble \{([^}]*)\}/,
  )?.[1];
  assert.ok(userBubbleStyles);
  assert.match(stylesSource, /\.message-row\.user \{\s*justify-content:\s*flex-end;/);
  assert.match(
    stylesSource,
    /\.message-row\.user \.message-col \{[\s\S]*?width:\s*min\(max-content,\s*82%,\s*600px\);[\s\S]*?max-width:\s*min\(82%,\s*600px\);[\s\S]*?align-items:\s*flex-end;/,
  );
  // The wrap constraint lives on the column; the bubble is max-content so a
  // percentage chip cap cannot stretch it to the 82%/600px ceiling.
  assert.match(
    userBubbleStyles,
    /width:\s*max-content;[\s\S]*?max-width:\s*100%;[\s\S]*?background:\s*color-mix\(in oklab,\s*var\(--ds-text-primary\) 8%,\s*transparent\);/,
  );
  assert.doesNotMatch(userBubbleStyles, /var\(--ds-accent\)/);
});

test("fork tools use the branch icon", () => {
  assert.match(transcriptSource, /case "fork":\s*return <IconBranch/);
});

test("tool rows render structured blocks instead of dumping JSON", async () => {
  const detailsSource = await readFile(
    new URL("../src/components/ToolDetails.tsx", import.meta.url),
    "utf8",
  );
  const permissionSource = await readFile(
    new URL("../src/components/PermissionCard.tsx", import.meta.url),
    "utf8",
  );
  // No JSON stringify path is left in either surface.
  assert.doesNotMatch(transcriptSource, /getToolSections|hasToolSections/);
  assert.doesNotMatch(permissionSource, /JSON\.stringify|formatToolValue/);
  assert.match(transcriptSource, /buildToolPresentation\(message, \{\n\s+hideSummaryArg: true,/);
  // Formatting remains lazy and the cached blocks are read only while visible.
  assert.match(transcriptSource, /if \(variant !== "topology" && open && hasDetails && disclosure\.parentVisible/);
  assert.match(transcriptSource, /const blocks = variant !== "topology" && open && hasDetails \? presentation\.current\?\.blocks : null/);
  assert.match(transcriptSource, /<ToolChips chips=\{chips\} \/>/);
  assert.match(
    transcriptSource,
    /<ToolDetailBlocks blocks=\{blocks\} plain=\{runHead\} streaming=\{status === "running"\} \/>/,
  );
  assert.match(permissionSource, /<ToolDetailBlocks blocks=\{argBlocks\} \/>/);
  // Code bodies share the transcript highlighter rather than a second cache.
  assert.match(
    detailsSource,
    /block\.highlight \? <HighlightedCode code=\{page\} lang=\{block\.lang\} \/> : page/,
  );
  // Diffs reuse the review card rails; hits and paths open in the work panel.
  assert.match(detailsSource, /className="diff-hunk"/);
  assert.match(detailsSource, /openTarget\(\{ kind: "file", path: rel \}\)/);
});

test("tool block bodies stay bounded and role-coded", () => {
  for (const selector of [
    "\\.tool-row-chips",
    "\\.tool-chip",
    "\\.tool-block \\+ \\.tool-block",
    "\\.tool-diff",
    "\\.tool-fields",
    "\\.tool-block-more",
  ]) {
    assert.match(stylesSource, new RegExp(`${selector}\\s*\\{`), selector);
  }
  // Long payloads scroll inside the row instead of stretching the transcript.
  assert.match(stylesSource, /\.tool-row-content \{[\s\S]*?max-height:\s*260px;/);
  assert.match(
    stylesSource,
    /\.tool-file-list,\s*\.tool-match-list \{[\s\S]*?max-height:\s*260px;[\s\S]*?overflow:\s*auto;/,
  );
  // Stretched <button> paths stay start-aligned; Chromium must not justify
  // the path glyphs across the row (same contract as Grep path headings).
  const fileItem = stylesSource.match(/\.tool-file-item \{([^}]*)\}/)?.[1];
  assert.ok(fileItem);
  assert.match(fileItem, /display:\s*block;/);
  assert.match(fileItem, /width:\s*100%;/);
  // A column flex list with a height cap shrinks every row whose overflow
  // is not visible: the automatic minimum size is zero, so a long result is
  // pressed into a sliver and the paths are clipped away. Rows keep their
  // content height and the list scrolls instead.
  assert.match(fileItem, /flex:\s*none;/);
  // stderr and error notes carry the error hue, host notices stay neutral.
  assert.match(stylesSource, /\.tool-row-content\.is-error \{[\s\S]*?var\(--ds-error\)/);
  assert.match(stylesSource, /\.tool-chip\.is-error \{[\s\S]*?var\(--ds-error\)/);
  assert.match(stylesSource, /\.tool-note\.is-error \{[\s\S]*?var\(--ds-error\)/);
  const note = stylesSource.match(/\.tool-note \{([^}]*)\}/)?.[1];
  assert.ok(note);
  assert.doesNotMatch(note, /--ds-error/);
  // The permission card is a block container now, not a <pre>.
  const permissionArgs = stylesSource.match(/\.permission-card-args \{([^}]*)\}/)?.[1];
  assert.ok(permissionArgs);
  assert.doesNotMatch(permissionArgs, /white-space|font-family/);
});

test("tool details do not add a second visual indent", () => {
  const toolDetailStyles = stylesSource.match(
    /\.tool-row:not\(\.thinking\):not\(\.subagent-topology-node\) > \.tool-row-body \{([^}]*)\}/
  )?.[1];
  assert.ok(toolDetailStyles);
  assert.match(toolDetailStyles, /margin-left:\s*0;/);
  assert.match(toolDetailStyles, /padding-left:\s*0;/);
  // Thinking and topology have separate visual hierarchies and keep their
  // dedicated layout rules rather than inheriting the flat tool detail rule.
  assert.match(stylesSource, /\.subagent-topology-node > \.tool-row-body,[\s\S]*?margin-left:\s*38px;/);
});


test("a review-backed edit is one tool-level row", () => {
  // The card is the tool row in the transcript: full band, no 24px inset, and
  // its diff body starts directly beneath the only header.
  const toolRow = stylesSource.match(
    /\.review-change-card\.is-tool-row \{([^}]*)\}/,
  )?.[1];
  assert.ok(toolRow);
  assert.match(toolRow, /width:\s*100%;/);
  assert.doesNotMatch(toolRow, /margin:[^;]*24px/);
  const body = stylesSource.match(
    /\.review-change-card\.is-tool-row \.review-change-card-body \{([^}]*)\}/,
  )?.[1];
  assert.ok(body);
  assert.match(body, /margin-left:\s*0;/);
  // Header order matches the file ledger: mark, path, counts, caret. The panel
  // keeps its DOM order because these rules only apply to the tool-row variant.
  const order = { mark: 0, path: 1, counts: 2, caret: 3 };
  for (const [part, position] of Object.entries(order)) {
    const rule = stylesSource.match(
      new RegExp(`\\.review-change-card\\.is-tool-row \\.review-change-card-${part} \\{([^}]*)\\}`),
    )?.[1];
    assert.ok(rule, `expected an is-tool-row ${part} rule`);
    assert.match(rule, new RegExp(`order:\\s*${position};`));
  }
  // Copy shares the rollback action rhythm in the diff body.
  assert.match(
    stylesSource,
    /\.review-change-rollback,\s*\n\.review-change-copy \{/,
  );
});

test("the turn process header and nested group headers share one left edge", () => {
  // A nested group header sits on the same vertical line as the turn header that
  // reveals it. Its body restores the released v0.20.1 step: child rows move in
  // by 12px so the group reads as one level below its own header.
  const turnBody = stylesSource.match(/\.turn-process-body \{([^}]*)\}/)?.[1];
  assert.ok(turnBody);
  assert.match(turnBody, /padding-left:\s*0;/);
  const groupedBody = stylesSource.match(
    /\.process-activity-group\.grouped > \.process-activity-body \{([^}]*)\}/,
  )?.[1];
  assert.ok(groupedBody);
  assert.match(groupedBody, /padding-inline-start:\s*12px;/);
  // Both disclosure headers use the tool-row inline geometry: content at the
  // prose edge, fill four pixels into the gutter.
  const activityHeader = stylesSource.match(/\n\.tool-activity-header \{([^}]*)\}/)?.[1];
  assert.ok(activityHeader);
  assert.match(activityHeader, /margin:\s*0 -4px;/);
  assert.match(activityHeader, /padding:\s*2px 4px;/);
});

test("nested process levels keep a subtle type and colour hierarchy", () => {
  // The turn title stays at the base size; the nested group is smaller and
  // quieter, and a run row uses the leaf size while keeping readable colour.
  const nested = stylesSource.match(
    /\.process-activity-group > \.tool-activity-header \{([^}]*)\}/,
  )?.[1];
  assert.ok(nested);
  assert.match(nested, /font-size:\s*var\(--text-sm\);/);
  assert.match(nested, /color:\s*var\(--ds-text-muted\);/);
  const runName = stylesSource.match(
    /\.tool-row-head\.is-run \.tool-row-name \{([^}]*)\}/,
  )?.[1];
  assert.ok(runName);
  assert.match(runName, /font-size:\s*var\(--text-sm\);/);
});

test("collapse rails keep separate gutter lanes", () => {
  const turnRail = stylesSource.match(
    /\.turn-process-body > \.disclosure-collapse-rail \{([^}]*)\}/,
  )?.[1];
  assert.ok(turnRail);
  assert.match(turnRail, /left:\s*-32px;/);
  assert.match(turnRail, /width:\s*14px;/);
  const groupRail = stylesSource.match(
    /\.process-activity-body > \.disclosure-collapse-rail \{([^}]*)\}/,
  )?.[1];
  assert.ok(groupRail);
  // The rail sits in the 12px gutter beside the indented child rows.
  assert.match(groupRail, /left:\s*-12px;/);
  assert.match(groupRail, /width:\s*14px;/);
  // It stays invisible at rest; only hover or focus paints the 2px bar.
  const railBase = stylesSource.match(
    /\.disclosure-collapse-rail::before \{([^}]*)\}/,
  )?.[1];
  assert.ok(railBase);
  assert.match(railBase, /background:\s*transparent;/);
  assert.match(
    stylesSource,
    /\.disclosure-collapse-rail:hover::before,\s*\n\.disclosure-collapse-rail:focus-visible::before \{[\s\S]*?background:\s*var\(--ds-text-muted\);/,
  );
  const messageRow = stylesSource.match(/\.message-row \{([^}]*)\}/)?.[1];
  assert.ok(messageRow);
  assert.match(messageRow, /overflow-clip-margin:\s*34px;/);
});

test("the header never echoes the item it is working on", () => {
  // Long shell commands and search queries pushed the counts off the row when the
  // header previewed them, so the live tail preview is gone entirely.
  assert.doesNotMatch(stylesSource, /\.tool-activity-preview/);
});

/*
 * The width regression this guards: a content-sized `inline-flex` disclosure
 * header stopped at its own label, so a tool call never used the conversation
 * width — it stayed narrower than the prose, ignored the dragged band width,
 * and let a long label overrun the chip. The header row now claims the band at
 * every level, the label ellipsizes, and the caret trails the row.
 */
test("tool-call disclosure headers span the conversation band", () => {
  const header = stylesSource.match(/\n\.tool-activity-header \{([^}]*)\}/)?.[1];
  assert.ok(header);
  assert.match(header, /display:\s*flex;/);
  assert.match(header, /width:\s*100%;/);
  assert.match(header, /min-width:\s*0;/);
  // A chip default (content width) or a max-width cap would freeze the row.
  assert.doesNotMatch(header, /inline-flex|max-width/);

  const label = stylesSource.match(/\n\.tool-activity-label \{([^}]*)\}/)?.[1];
  assert.ok(label);
  assert.match(label, /text-overflow:\s*ellipsis;/);
  assert.match(label, /min-width:\s*0;/);
  assert.match(label, /white-space:\s*nowrap;/);

  const caret = stylesSource.match(/\n\.tool-activity-caret \{([^}]*)\}/)?.[1];
  assert.ok(caret);
  assert.match(caret, /margin-inline-start:\s*auto;/);

  // No level keeps its own width or label override: every disclosure header
  // resolves through the single base row.
  assert.doesNotMatch(
    stylesSource,
    /\.(process-activity-group|turn-process) > \.tool-activity-header[^{]*\{[^}]*width:/,
  );
  assert.doesNotMatch(
    stylesSource,
    /\.tool-activity-group\.has-subagents \.tool-activity-header \{[^}]*width:/,
  );

  // A tool row itself owns the column so no parent display mode can shrink it.
  const row = stylesSource.match(/\n\.tool-row \{([^}]*)\}/)?.[1];
  assert.ok(row);
  assert.match(row, /width:\s*100%;/);
  assert.match(row, /min-width:\s*0;/);
});

test("delegation node copy wraps within the responsive card", () => {
  const metrics = stylesSource.match(
    /\.subagent-activity-metrics \{([^}]*)\}/,
  )?.[1];
  assert.ok(metrics);
  assert.match(metrics, /overflow-wrap:\s*anywhere;/);
  assert.match(metrics, /white-space:\s*normal;/);

  const titleRow = stylesSource.match(
    /\.subagent-topology-node-title-row \{([^}]*)\}/,
  )?.[1];
  assert.ok(titleRow);
  assert.match(titleRow, /flex-wrap:\s*wrap;/);

  const title = stylesSource.match(
    /\.subagent-topology-node-title \{([^}]*)\}/,
  )?.[1];
  assert.ok(title);
  assert.match(title, /-webkit-line-clamp:\s*2;/);
  assert.match(title, /overflow-wrap:\s*anywhere;/);
  assert.match(title, /white-space:\s*normal;/);
  assert.doesNotMatch(title, /white-space:\s*nowrap;/);

  const summary = stylesSource.match(
    /\.subagent-topology-node-summary \{([^}]*)\}/,
  )?.[1];
  assert.ok(summary);
  assert.match(summary, /-webkit-line-clamp:\s*2;/);
  assert.match(summary, /overflow-wrap:\s*anywhere;/);
  assert.match(summary, /white-space:\s*normal;/);

  const steps = stylesSource.match(
    /\.subagent-topology-node-steps \{([^}]*)\}/,
  )?.[1];
  assert.ok(steps);
  assert.match(steps, /overflow-wrap:\s*anywhere;/);
  assert.match(steps, /white-space:\s*normal;/);
});

test("assistant turns stay transparent full-width prose", () => {
  assert.match(
    stylesSource,
    /\.message-row\.assistant \.message-bubble[\s\S]*?background:\s*transparent;/,
  );
  assert.match(
    stylesSource,
    /\.message-row\.assistant[\s\S]*?\.message-col[\s\S]*?width:\s*min\(100%,\s*var\(--chat-prose-max-width,\s*720px\)\);/,
  );
  // D323: the live parent turn stays transparent; no rail, no reserved
  // inset, no whole-turn tile. The tile belongs only to the delegation card.
  assert.doesNotMatch(
    stylesSource,
    /\.message-row\.assistant-turn \.message-col\s*\{[^}]*padding-left:\s*14px/,
  );
  assert.doesNotMatch(
    stylesSource,
    /\.message-row\.assistant-turn \.message-col\s*\{[^}]*border-left/,
  );
  assert.doesNotMatch(
    stylesSource,
    /\.message-row\.assistant-turn\.streaming \.message-col\s*\{[^}]*background:\s*var\(--ds-tile\)/,
  );
  assert.match(
    stylesSource,
    /\.tool-activity-group\.has-subagents\s*\{[^}]*background:\s*var\(--ds-tile\)/,
  );
});
test("assistant error cards follow the responsive transcript column", () => {
  const errorCard = stylesSource.match(/\n\.message-error \{([^}]*)\}/)?.[1];
  assert.ok(errorCard);
  assert.match(errorCard, /width:\s*100%;/);
  assert.doesNotMatch(errorCard, /max-width\s*:/);
  assert.match(
    stylesSource,
    /\.message-row\.assistant \.message-col,\s*\.message-row\.system \.message-col,\s*\.message-row\.tool \.message-col \{\s*width:\s*min\(100%,\s*var\(--chat-prose-max-width,\s*720px\)\);/,
  );
});

test("decision and outcome cards follow the responsive transcript band", () => {
  const cardRules = [
    stylesSource.match(/\n\.permission-card \{([^}]*)\}/)?.[1],
    stylesSource.match(/\n\.asktool-card \{([^}]*)\}/)?.[1],
    stylesSource.match(/\n\.turn-outcome-card \{([^}]*)\}/)?.[1],
  ];
  for (const rule of cardRules) {
    assert.ok(rule);
    assert.match(
      rule,
      /width:\s*min\(100%,\s*var\(--chat-prose-max-width,\s*720px\)\);/,
    );
  }
});

test("transcript density and hover actions are quiet", () => {
  assert.match(stylesSource, /\.message-row \{[\s\S]*?padding:\s*12px 0;/);
  assert.match(
    stylesSource,
    /\.thread-content \{[\s\S]*?padding:\s*20px 32px calc\(var\(--composer-dock-height, 228px\) \+ 16px\);/,
  );
  assert.match(
    stylesSource,
    /\.message-actions \{[\s\S]*?opacity:\s*0;[\s\S]*?\.message-row:hover \.message-actions/,
  );
  assert.match(stylesSource, /\.message-row\.user \.message-actions \{[\s\S]*?justify-content:\s*flex-end;/);
});

test("transcript markup uses dedicated user text and assistant turn surfaces", () => {
  assert.match(transcriptSource, /className="message-user-text selectable"/);
  assert.match(transcriptSource, /streaming \? " streaming" : ""/);
  assert.match(transcriptSource, /CopyButton text=\{message\.content\}/);
  assert.match(transcriptSource, /className=\{`message-row assistant assistant-turn/);
  assert.match(transcriptSource, /CopyButton text=\{content\}/);
});

test("user plaintext preserves hard newlines without forced mid-word breaks", () => {
  assert.match(
    stylesSource,
    /\.message-user-text \{\s*\/\* Preserve hard newlines; wrap long tokens without splitting every CJK glyph\. \*\/\s*white-space:\s*pre-wrap;\s*overflow-wrap:\s*break-word;\s*word-break:\s*normal;/,
  );
  assert.doesNotMatch(stylesSource, /\.message-user-text br \{/);
  // Newlines come from `white-space: pre-wrap`; no manual <br> splitting.
  assert.doesNotMatch(transcriptSource, /user-line-/);
});

test("wrapped user links keep plaintext alignment", () => {
  const userLinkStyles = stylesSource.match(/\.chat-text-link \{([^}]*)\}/)?.[1];
  assert.ok(userLinkStyles);
  assert.match(userLinkStyles, /text-align:\s*start;/);
  assert.match(userLinkStyles, /overflow-wrap:\s*anywhere;/);
});

test("user-message links stay selectable inside the inert button baseline", () => {
  // base.css makes every <button> unselectable, and a class selector outranks
  // that type rule. Without this opt-in a drag across the message skips the
  // URL and copying drops it.
  assert.match(
    stylesSource,
    /button,\s*\[role="button"\],[\s\S]*?\{\s*-webkit-user-select:\s*none;\s*user-select:\s*none;/,
  );
  const userLinkStyles = stylesSource.match(/\.chat-text-link \{([^}]*)\}/)?.[1];
  assert.ok(userLinkStyles);
  assert.match(userLinkStyles, /-webkit-user-select:\s*text;/);
  assert.match(userLinkStyles, /(?<!-webkit-)user-select:\s*text;/);
});

test("user-message file chips reuse the composer chip node", () => {
  assert.match(transcriptSource, /className=\"composer-chip chat-file-chip\"/);
  assert.match(transcriptSource, /composer-chip-name/);
  assert.match(stylesSource, /\.chat-file-chip[\s\S]*?appearance:\s*none/);
  // Definite cap inside the shrink-to-fit plate; `min(100%, 240px)` stretches it.
  assert.match(
    stylesSource,
    /\.message-row\.user \.composer-chip \{[\s\S]*?max-width:\s*240px;/,
  );
});

test("stopping a turn undoes an unanswered prompt or settles the partial reply", async () => {
  const storeSource = await readStoreSource();
  assert.match(storeSource, /const submittedDraft = runtime\.submittedComposerDrafts\.get\(sessionId\)/);
  assert.match(storeSource, /resolveComposerSmartStop\(state\.messages, submittedDraft\)/);
  assert.match(storeSource, /composerPrefill:\s*\{ \.\.\.fullStop\.draft, sessionId \}/);
  assert.match(storeSource, /submittedDraft\?\.resolveAbort\?\.\(true\)/);
  assert.match(storeSource, /submittedDraft\?\.resolveAbort\?\.\(false\)/);
  assert.match(storeSource, /submittedComposerDrafts\.delete\(sessionId\)/);
  assert.match(storeSource, /smartStop\.kind === "restore"/);
  assert.match(storeSource, /status:\s*"aborted" as const/);
  // The undo rewrite starts from the full durable transcript merged with the
  // live rows, never from the renderer's paged, display-capped window (D299).
  assert.match(
    storeSource,
    /const merged = mergeLiveSessionMessages\(fullMessages, get\(\)\.messages\)/,
  );
  assert.match(storeSource, /resolveComposerSmartStop\(merged, submittedDraft\)/);
  assert.match(
    storeSource,
    /retractPendingSessionPrompt\(\{[\s\S]*?sessionId,[\s\S]*?userMessageId: pendingUserMessageId/,
  );
  // Settling a partial reply is renderer-only: the runtime's aborted final row
  // (or its checkpoint) is the durable copy, and a rewrite from this snapshot
  // could delete it.
  assert.doesNotMatch(storeSource, /replaceSessionMessages\(sessionId,\s*settled\)/);
  assert.doesNotMatch(storeSource, /replaceSessionMessages\(sessionId,\s*smartStop\.kept\)/);
});

test("delete remains on user turns and is removed from assistant toolbar", async () => {
  const storeSource = await readStoreSource();
  assert.match(storeSource, /deleteMessage:\s*async \(messageId\)/);
  assert.match(storeSource, /deleteSessionMessage\(\{ sessionId, messageId \}\)/);
  assert.match(transcriptSource, /deleteMessage\(message\.id\)/);
  assert.match(transcriptSource, /chat\.deleteMessage/);
  assert.match(
    transcriptSource,
    /const editableUserMessage = isUser && !isSessionMessage && canEditMessages;/,
  );
  assert.match(transcriptSource, /\{editableUserMessage \? \(/);
  assert.match(stylesSource, /\.copy-btn\.danger:hover/);
});

test("editing a user prompt regenerates it and keeps the old branch reachable", async () => {
  const storeSource = await readStoreSource();
  // Edit lives on the user turn (the prompt is what gets rewritten), not on
  // the assistant answer.
  assert.match(transcriptSource, /editUserMessage\(message\.id, next, message\.attachments\)/);
  assert.match(transcriptSource, /className="message-edit-input selectable"/);
  assert.match(transcriptSource, /chat\.editMessage/);
  assert.match(transcriptSource, /const retryEdit = async \(\)/);
  assert.match(transcriptSource, /void retryEdit\(\)/);
  assert.match(transcriptSource, /chat\.retryEdit/);
  assert.match(transcriptSource, /chat\.retryingEdit/);
  assert.doesNotMatch(transcriptSource, /next === editSeed\.trim\(\)/);
  assert.doesNotMatch(transcriptSource, /editAssistantMessage/);
  assert.doesNotMatch(storeSource, /editAssistantMessage/);
  // Slash prompts edit their typed form so the resend re-expands the template.
  assert.match(transcriptSource, /const editSeed =\s*\(editableUserMessage && message\.command\) \|\| \(message\.content \|\| ""\);/);
  // Same branch mechanics as regenerate, so main archives the replaced turn
  // as a revision the pager can walk back to.
  assert.match(storeSource, /editUserMessage:\s*async \(messageId, content, attachments\)/);
  // The cut is named by message identity: a window offset plus an index into
  // the deduplicated renderer array are different coordinate spaces.
  assert.match(storeSource, /truncateFromMessageId,/);
  assert.match(
    storeSource,
    /const truncateFromMessageId = state\.messages\[userIndex\]\.id/,
  );
  assert.doesNotMatch(storeSource, /truncateBefore:\s*transcriptOffset/);
  assert.match(
    storeSource,
    /editUserMessage\(root\.id, root\.content, root\.attachments\)/,
  );
  assert.match(stylesSource, /\.message-edit-input/);
  assert.match(
    stylesSource,
    /\.message-row\.user \.message-col:has\(\.message-edit\)/,
  );
  assert.match(
    stylesSource,
    /\.message-edit \{[\s\S]*?background:\s*var\(--ds-tile-deep\);[\s\S]*?box-shadow:\s*none;/,
  );
  assert.match(
    stylesSource,
    /\.message-edit:focus-within \{[\s\S]*?box-shadow:\s*inset/,
  );
  assert.match(transcriptSource, /className="icon-btn message-edit-cancel"/);
  assert.match(transcriptSource, /className="send-btn message-edit-submit"/);
});

test("message toolbars are icon-only with hover tooltips", () => {
  // No worded chips in the toolbar: labels ride on tooltip + aria-label.
  assert.doesNotMatch(
    transcriptSource,
    /<span>\{(?:forkLabel|retryLabel|editLabel|copyLabel)\}<\/span>/,
  );
  for (const label of ["editLabel", "deleteLabel"]) {
    assert.ok(
      transcriptSource.includes(`ariaLabel={${label}}`),
      `${label} needs an aria-label`,
    );
    assert.ok(
      transcriptSource.includes(`tooltip={${label}}`),
      `${label} needs a hover tooltip`,
    );
  }
  for (const key of ["chat.forkResponse", "chat.retry"]) {
    assert.match(transcriptSource, new RegExp(`ariaLabel=\\{t\\("${key}"\\)\\}`));
    assert.match(transcriptSource, new RegExp(`tooltip=\\{t\\("${key}"\\)\\}`));
  }
  assert.ok(transcriptSource.includes("label={copyLabel}"));
  assert.match(transcriptSource, /className="copy-btn icon"/);
  assert.match(transcriptSource, /tooltip=\{t\("chat\.forkResponse"\)\}/);
  assert.match(transcriptSource, /import \{ TooltipButton \} from "\.\.\/\.\.\/\.\.\/components\/ui"/);
  assert.match(transcriptSource, /<TooltipButton/);
  assert.match(stylesSource, /\.ui-tooltip\s*\{[\s\S]*?position:\s*fixed;/);
  assert.match(stylesSource, /\.ui-tooltip\s*\{[\s\S]*?z-index:\s*1000;/);
  assert.match(stylesSource, /\.ui-tooltip\s*\{[\s\S]*?transform:\s*translate\(-50%,\s*-100%\)/);
  // Worded surfaces (error details) keep their label.
  assert.match(transcriptSource, /withLabel/);
});

test("streaming assistant turns hide answer copy until idle", () => {
  assert.ok(transcriptSource.includes("{complete && actionMessage ? ("));
  assert.ok(
    transcriptSource.includes(
      '<CopyButton text={content} label={t("chat.copy")} />',
    ),
  );
});

test("assistant context inspector keeps a compact summary and retry action wired", () => {
  assert.match(transcriptSource, /function MessageMeta/);
  assert.match(transcriptSource, /message-meta-chip/);
  assert.doesNotMatch(transcriptSource, /ContextUsageInspector/);
  assert.match(transcriptSource, /showThroughput/);
  assert.match(composerSource, /latest-turn-context/);
  assert.match(composerSource, /latestTurnContextInspector/);
  assert.match(composerSource, /sessionCompactions/);
  assert.match(inspectorSource, /export function ContextUsageInspector/);
  assert.match(inspectorSource, /className="context-inspector"/);
  assert.match(inspectorSource, /chat\.usageTools/);
  assert.match(inspectorSource, /aggregateToolTokenUsage/);
  assert.match(inspectorSource, /context-inspector-summary/);
  assert.match(inspectorSource, /context-inspector-summary-row/);
  assert.match(inspectorSource, /chat\.usageToolsSummary/);
  assert.match(inspectorSource, /context-inspector-kpis/);
  assert.match(inspectorSource, /useSessionTiming/);
  assert.match(inspectorSource, /chat\.usageSessionTime/);
  assert.match(inspectorSource, /chat\.usageNetworkRequest/);
  assert.match(inspectorSource, /chat\.usageModelResponse/);
  assert.match(inspectorSource, /chat\.usageToolCalls/);
  assert.match(inspectorSource, /networkRequestPercent/);
  assert.match(inspectorSource, /toolCallPercent/);
  assert.match(inspectorSource, /context-inspector-window-percent/);
  assert.match(inspectorSource, /chat\.usageContextLeft/);
  assert.match(inspectorSource, /chat\.usageThroughput/);
  assert.match(inspectorSource, /calculateCacheRate/);
  assert.match(inspectorSource, /chat\.usageCacheRate/);
  assert.match(inspectorSource, /contextOccupancyTokens\(usage\)/);
  assert.match(inspectorSource, /usage\.cacheReadTokens/);
  assert.doesNotMatch(inspectorSource, /turnUsage\.cacheReadTokens/);
  assert.match(inspectorSource, /portalToBody\(popover\)/);
  assert.match(inspectorSource, /getBoundingClientRect\(\)/);
  assert.match(inspectorSource, /addEventListener\("scroll", handleViewportChange, true\)/);
  assert.match(inspectorSource, /ResizeObserver\(updatePopoverPosition\)/);
  assert.match(inspectorSource, /aria-controls=\{open \? panelId : undefined\}/);
  assert.match(transcriptSource, /retryAssistantMessage/);
  assert.match(transcriptSource, /chat\.retry/);
  assert.match(stylesSource, /\.context-inspector-ring-progress/);
  assert.match(stylesSource, /\.context-inspector-ring-value/);
  assert.match(stylesSource, /\.context-inspector-summary/);
  assert.match(stylesSource, /\.context-inspector-summary-values/);
  assert.doesNotMatch(
    stylesSource,
    /\.context-inspector-(window|kpis|summary|compaction)[^{]*\{[^}]*border-top:/,
  );
  assert.doesNotMatch(inspectorSource, /context-inspector-source-badge/);
  assert.doesNotMatch(inspectorSource, /context-inspector-tool-meta/);
  assert.doesNotMatch(stylesSource, /\.context-inspector-meter\s*\{/);
  assert.doesNotMatch(stylesSource, /\.context-inspector-source-badge/);
  assert.doesNotMatch(stylesSource, /\.context-inspector-tool-/);
  assert.match(
    stylesSource,
    /\.context-inspector-popover\s*\{[\s\S]*?position:\s*fixed;[\s\S]*?z-index:\s*60;/,
  );
  assert.match(stylesSource, /\.context-inspector-popover\.is-open/);
});

test("context inspector keeps generation speed completion-only", () => {
  assert.doesNotMatch(inspectorSource, /useLiveElapsedMs|usageThroughputLive/);
  assert.doesNotMatch(transcriptSource, /useLiveElapsedMs|usageThroughputLive/);
  assert.doesNotMatch(transcriptSource, /assistantTurnStreamingMessage/);
  assert.doesNotMatch(stylesSource, /message-meta-live-rate|live-rate-pulse/);
});

test("context inspector panel opens on click, not hover (D225)", () => {
  // The trigger toggles; pointer enter/leave and focus/blur no longer open or
  // close the panel, so no hover-grace timer is needed.
  assert.match(inspectorSource, /onClick=\{toggleInspector\}/);
  assert.match(inspectorSource, /aria-haspopup="dialog"/);
  assert.match(inspectorSource, /role="dialog"/);
  assert.doesNotMatch(inspectorSource, /onPointerEnter=\{(openInspector|cancelClose)\}/);
  assert.doesNotMatch(inspectorSource, /onPointerLeave=\{scheduleClose\}/);
  assert.doesNotMatch(inspectorSource, /onFocus=\{openInspector\}/);
  assert.doesNotMatch(inspectorSource, /closeTimerRef/);
  // Explicit dismissal: outside pointerdown and Escape, which refocuses.
  assert.match(
    inspectorSource,
    /addEventListener\("pointerdown", handlePointerDown, true\)/,
  );
  assert.match(inspectorSource, /event\.key !== "Escape"/);
  assert.match(inspectorSource, /triggerRef\.current\?\.focus\(\)/);
});

test("regenerate rewrites the current turn instead of appending", async () => {
  const storeSource = await readStoreSource();
  const mainSource = await readMainSource();
  const protocolSource = await readFile(
    new URL("../../../packages/shared/src/protocol.ts", import.meta.url),
    "utf8",
  );
  // The cut is named by message identity: a window offset plus an index into
  // the deduplicated renderer array are different coordinate spaces.
  assert.match(storeSource, /truncateFromMessageId,/);
  assert.match(
    storeSource,
    /const truncateFromMessageId = state\.messages\[userIndex\]\.id/,
  );
  assert.doesNotMatch(storeSource, /truncateBefore:\s*transcriptOffset/);
  assert.match(storeSource, /messages:\s*kept/);
  assert.match(mainSource, /session\.replaceMessages/);
  assert.match(mainSource, /agent\.disposeSession/);
  assert.match(mainSource, /truncateFromMessageId/);
  // The host resolves the boundary against its own transcript, and an
  // unresolvable identity is rejected instead of truncating at a guessed position.
  assert.match(
    mainSource,
    /"session\.truncateFrom",\s*\{\s*sessionId:\s*req\.sessionId/,
  );
  assert.doesNotMatch(
    mainSource,
    /resolveTranscriptTruncation|truncation\.kind === "unknown-message"/,
  );
  assert.match(protocolSource, /sessionReplaceMessages/);
});

test("conversation minimap hides until content overflows one viewport", () => {
  assert.match(minimapSource, /OVERFLOW_EPSILON_PX/);
  assert.match(
    minimapSource,
    /scrollHeight - el\.clientHeight > OVERFLOW_EPSILON_PX/,
  );
  // D269: overflow still gates a fully loaded transcript, but withheld earlier
  // history keeps the outline (and its continuation control) reachable.
  assert.match(
    minimapSource,
    /!shouldRenderConversationMinimap\(\{[\s\S]*?markerCount: markers\.length,[\s\S]*?overflows,[\s\S]*?hasEarlier,[\s\S]*?\}\)/,
  );
  assert.match(
    minimapSource,
    /window\.addEventListener\("resize", scheduleResize\)/,
  );
  assert.match(minimapSource, /new ResizeObserver\(scheduleResize\)/);
  assert.match(minimapSource, /updateOverflow/);
});

test("thread scroll reserves stable gutters before overflow appears", () => {
  assert.match(
    stylesSource,
    /\.thread-scroll\s*\{[\s\S]*?overflow:\s*auto;[\s\S]*?scrollbar-gutter:\s*stable;/,
  );
});

test("conversation minimap stays centered below titlebar at high density", () => {
  assert.match(
    minimapSource,
    /"--minimap-marker-count": markers\.length/,
  );
  assert.match(
    stylesSource,
    /\.minimap-rail \{[\s\S]*?top:\s*var\(--ds-toolbar-height\);[\s\S]*?bottom:\s*calc\(var\(--composer-dock-height, 200px\) \+ 16px\);[\s\S]*?justify-content:\s*center;/,
  );
  assert.match(
    stylesSource,
    /\.minimap-rail \{[\s\S]*?gap:\s*clamp\([\s\S]*?var\(--minimap-marker-count\)[\s\S]*?-webkit-app-region:\s*no-drag;/,
  );
  assert.match(
    stylesSource,
    /\.minimap-marker \{[\s\S]*?min-height:\s*0;/,
  );
  assert.match(
    stylesSource,
    /\.minimap-marker::before \{[\s\S]*?height:\s*min\(2px,\s*100%\);/,
  );
});

test("regenerate history pager and stable revision family are wired", async () => {
  const storeSource = await readStoreSource();
  const mainSource = await readMainSource();
  const sharedSource = await readSharedTypesSource();
  assert.match(transcriptSource, /message-revision-pager/);
  assert.match(transcriptSource, /activateMessageRevision/);
  assert.match(transcriptSource, /chat\.revisionPager/);
  assert.match(
    transcriptSource,
    /const showRevisionPager = editableUserMessage && revisionCount > 1;/,
  );
  assert.match(
    transcriptSource,
    /activateMessageRevision\(message\.id, Math\.max\(1, activeRevision - 1\)\)/,
  );
  assert.doesNotMatch(transcriptSource, /showRevisionPagerHere|revisionOwner/);
  assert.match(stylesSource, /\.message-revision-pager/);
  assert.doesNotMatch(
    stylesSource,
    /\.message-actions:has\(\.message-revision-pager\)[\s\S]*?opacity:\s*1/,
  );
  assert.match(storeSource, /revisionRootId \|\| root\.id/);
  assert.match(storeSource, /activateSessionRevision/);
  assert.match(mainSource, /session\.saveRevision/);
  assert.match(mainSource, /revisionRootId/);
  assert.match(mainSource, /revisionCount: revision\.revisionCount/);
  assert.match(
    mainSource,
    /truncate regenerate transcript failed[\s\S]*?throw error;/,
  );
  assert.match(sharedSource, /revisionRootId\?: string/);
  assert.match(sharedSource, /MessageRevisionSummary/);
});