Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
65.7 KB
Raw Blame
import { forwardRef, useEffect, useImperativeHandle, useRef, useState, type ComponentProps, type ReactNode, type Ref } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { act, fireEvent, screen, waitFor } from "@testing-library/react";
import type { UploadResult } from "@multica/core/hooks/use-file-upload";
import type { AgentSkillSummary, Attachment } from "@multica/core/types";
import type { Editor } from "@tiptap/core";
import { useCommentComposerStore, useCommentDraftStore } from "@multica/core/issues/stores";
import { issueKeys } from "@multica/core/issues/queries";
import { workspaceKeys } from "@multica/core/workspace/queries";
import { WorkspaceSlugProvider } from "@multica/core/paths";
import { renderWithI18n } from "../../test/i18n";
import { CommentInput } from "./comment-input";
import { ReplyInput } from "./reply-input";

/** Shape of ContentEditor's `quickActionMenu` prop, as the composers pass it. */
type QuickActionMenuProp = {
  getQuickActions?: () => { id: string; name: string; description?: string }[];
  getQuickActionContent?: (quickActionId: string) => string;
  getSkills?: (editor: Editor) => AgentSkillSummary[];
};

vi.mock("@multica/core/auth", () => ({
  useAuthStore: { getState: () => ({ user: { id: "user-1" } }) },
}));

// Uploads now flow through the module-level coordinator, which calls
// `api.uploadFile(file, ctx, signal)` (MUL-5181). Tests drive uploads by
// mocking that call directly rather than the old `uploadWithToast` hook.
const apiUploadFile = vi.hoisted(() => vi.fn());
const apiListWorkspaces = vi.hoisted(() => vi.fn());
const apiListQuickActions = vi.hoisted(() => vi.fn());
const apiGetIssue = vi.hoisted(() => vi.fn());
const apiListAgents = vi.hoisted(() => vi.fn());
const apiListSquads = vi.hoisted(() => vi.fn());
const apiListMembers = vi.hoisted(() => vi.fn());
const apiPreviewCommentTriggers = vi.hoisted(() => vi.fn());
const apiListTasksByIssue = vi.hoisted(() => vi.fn());
const apiCancelTask = vi.hoisted(() => vi.fn());
const uploadWithToast = vi.hoisted(() => vi.fn());
const editorDefaultValues = vi.hoisted(() => ({
  values: [] as Array<string | undefined>,
}));
// The `/` quick-action menu is wired through a ContentEditor prop, so the mock
// editor records it — that prop being absent is exactly the MUL-5588 bug.
const editorQuickActionMenu = vi.hoisted(() => ({
  last: undefined as QuickActionMenuProp | undefined,
}));
// Observability + failure control for the write-back insert path (MUL-5181):
// `insertMarkdownAtEnd` returns false while the (simulated) Tiptap instance
// doesn't exist yet, exactly like the real handle.
// Post-send caret policy: both the top-level composer and a thread reply blur,
// so the page's keyboard shortcuts work again straight after a send.
const focusCalls = vi.hoisted(() => ({ focused: 0, blurred: 0 }));
const insertMarkdownSpy = vi.hoisted(() => vi.fn());
const insertPlaceholderSpy = vi.hoisted(() => vi.fn());
const insertMarkdownBehavior = vi.hoisted(() => ({ succeed: true }));
// Lets a test drop the editor's uploading placeholder out of the document
// (Cmd+Z after a paste) without settling the upload behind it.
const editorUploadSignal = vi.hoisted(
  () => ({ notify: undefined as ((uploading: boolean) => void) | undefined }),
);

// The real handle mints an id when it inserts the placeholder and hands it to
// the uploader, which adopts it as the draft `clientUploadId`. Mocks must do
// the same or the two records drift apart only in tests.
let mockUploadIdSeq = 0;

vi.mock("@multica/core/permissions", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/permissions")>()),
  useIsGuest: () => false,
}));
vi.mock("@multica/core/api", () => ({
  api: {
    getBaseUrl: () => "https://api.example.test",
    uploadFile: apiUploadFile,
    listWorkspaces: apiListWorkspaces,
    listQuickActions: apiListQuickActions,
    getIssue: apiGetIssue,
    listAgents: apiListAgents,
    listSquads: apiListSquads,
    listMembers: apiListMembers,
    previewCommentTriggers: apiPreviewCommentTriggers,
    listTasksByIssue: apiListTasksByIssue,
    cancelTask: apiCancelTask,
  },
}));

vi.mock("@multica/core/hooks/use-file-upload", async () => ({
  ...(await vi.importActual<typeof import("@multica/core/hooks/use-file-upload")>(
    "@multica/core/hooks/use-file-upload",
  )),
  useFileUpload: () => ({ uploadWithToast }),
}));

vi.mock("../../common/actor-avatar", () => ({
  ActorAvatar: ({ actorType, actorId }: { actorType: string; actorId: string }) => (
    <span data-testid="actor-avatar">
      {actorType}:{actorId}
    </span>
  ),
  AgentStatusDot: () => null,
}));

vi.mock("@multica/core/agents", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/agents")>()),
  useAgentPresenceDetail: () => "loading",
}));

vi.mock("../../editor", async () => ({
  // The lazy-mount controller is pure React (no Tiptap) — use the real one so
  // shell → activate → ready flows behave exactly as in production.
  ...(await vi.importActual<typeof import("../../editor/use-lazy-editor")>(
    "../../editor/use-lazy-editor",
  )),
  // Real submit gate (pure React) driven by the mock editor's
  // `hasActiveUploads` / `onUploadingChange` below.
  ...(await vi.importActual<typeof import("../../editor/use-upload-gate")>(
    "../../editor/use-upload-gate",
  )),
  // Real await-then-render submit contract (pure React) — the composers now
  // delegate their send handler to it.
  ...(await vi.importActual<typeof import("../../editor/use-composer-submit")>(
    "../../editor/use-composer-submit",
  )),
  useEditorUpload: () => ({ uploadWithToast, upload: vi.fn(), uploading: false }),
  useFileDropZone: () => ({
    isDragOver: false,
    dropZoneProps: { "data-testid": "drop-zone" },
  }),
  FileDropOverlay: () => null,
  ContentEditor: forwardRef(function MockContentEditor(
    {
      defaultValue,
      value,
      onUpdate,
      placeholder,
      onUploadFile,
      onUploadingChange,
      onSubmit,
      onReady,
      quickActionMenu,
    }: {
      defaultValue?: string;
      value?: string;
      onUpdate?: (markdown: string) => void;
      placeholder?: string;
      onUploadFile?: (file: File, uploadId: string) => Promise<UploadResult | null>;
      onUploadingChange?: (uploading: boolean) => void;
      onSubmit?: () => void;
      onReady?: () => void;
      quickActionMenu?: QuickActionMenuProp;
    },
    ref: Ref<unknown>,
  ) {
    editorDefaultValues.values.push(defaultValue);
    editorQuickActionMenu.last = quickActionMenu;
    editorUploadSignal.notify = onUploadingChange;
    const initialValue = value ?? defaultValue ?? "";
    const valueRef = useRef(initialValue);
    const [editorValue, setEditorValue] = useState(initialValue);
    // Mirrors the real editor's `uploading` node attrs: the placeholder exists
    // from before the await until the upload settles, `hasActiveUploads` reads
    // it synchronously, and the host is told through onUploadingChange.
    const inFlightRef = useRef(0);
    // Mirrors `editor.isDestroyed`: a settle continuation after unmount must
    // not write the document or emit onUpdate (uploadAndInsertFile guards on
    // isDestroyed in production).
    const destroyedRef = useRef(false);

    useEffect(() => {
      onReady?.();
      return () => {
        destroyedRef.current = true;
      };
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    useEffect(() => {
      if (value === undefined || value === valueRef.current) return;
      valueRef.current = value;
      setEditorValue(value);
    }, [value]);

    useImperativeHandle(ref, () => ({
      getMarkdown: () => valueRef.current,
      clearContent: () => {
        valueRef.current = "";
        setEditorValue("");
      },
      focus: () => { focusCalls.focused += 1; },
      focusAtCoords: () => {},
      blur: () => { focusCalls.blurred += 1; },
      uploadFile: async (file: File) => {
        inFlightRef.current += 1;
        if (inFlightRef.current === 1) onUploadingChange?.(true);
        try {
          const result = await onUploadFile?.(file, `mock-upload-${++mockUploadIdSeq}`);
          if (!result || destroyedRef.current) return;
          valueRef.current = `${valueRef.current}\n${result.url}`.trim();
          setEditorValue(valueRef.current);
          onUpdate?.(valueRef.current);
        } finally {
          inFlightRef.current -= 1;
          if (inFlightRef.current === 0 && !destroyedRef.current) onUploadingChange?.(false);
        }
      },
      hasActiveUploads: () => inFlightRef.current > 0,
      // Placeholder rebuild contract: the real handle draws a card for an
      // upload the document is not showing and reports whether it landed.
      // Mocks track ids only — no document to draw into.
      insertUploadPlaceholder: (upload: unknown) => {
        insertPlaceholderSpy(upload);
        return true;
      },
      settleUploadPlaceholder: () => false,
      insertMarkdownAtEnd: (md: string) => {
        insertMarkdownSpy(md);
        if (destroyedRef.current || !insertMarkdownBehavior.succeed) return false;
        valueRef.current = `${valueRef.current}\n\n${md}`.trim();
        setEditorValue(valueRef.current);
        onUpdate?.(valueRef.current);
        return true;
      },
    }));

    return (
      <textarea
        data-testid="editor"
        value={editorValue}
        placeholder={placeholder}
        onChange={(event) => {
          valueRef.current = event.target.value;
          setEditorValue(event.target.value);
          onUpdate?.(event.target.value);
        }}
        onKeyDown={(event) => {
          // The editor's Mod+Enter submit shortcut — the path that skips the
          // send button entirely.
          if ((event.metaKey || event.ctrlKey) && event.key === "Enter") onSubmit?.();
        }}
      />
    );
  }),
}));

function renderWithProviders(ui: ReactNode) {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: { retry: false },
    },
  });
  return renderWithI18n(
    <QueryClientProvider client={queryClient}>{ui}</QueryClientProvider>,
  );
}

function renderCommentInput(onSubmit = vi.fn().mockResolvedValue(true)) {
  const view = renderWithProviders(<CommentInput issueId="issue-1" onSubmit={onSubmit} />);
  return { ...view, onSubmit };
}

function DraftReplyInput(props: ComponentProps<typeof ReplyInput>) {
  const parentId = useCommentDraftStore((s) =>
    (props.draftKey ? s.drafts[props.draftKey]?.replyTarget?.commentId : undefined) ?? props.parentId);
  return <ReplyInput {...props} parentId={parentId} />;
}

function renderReplyInput({
  onSubmit = vi.fn().mockResolvedValue("reply-new"),
  onAccepted,
  size = "sm",
  draftKey,
}: {
  onSubmit?: (content: string, attachmentIds?: string[], suppressAgentIds?: string[]) => Promise<string | boolean>;
  onAccepted?: (commentId: string) => void;
  size?: "sm" | "default";
  draftKey?: `reply:${string}:${string}`;
} = {}) {
  const view = renderWithProviders(
    <DraftReplyInput
      issueId="issue-1"
      parentId="comment-1"
      avatarType="member"
      avatarId="user-1"
      onSubmit={onSubmit}
      onAccepted={onAccepted}
      size={size}
      draftKey={draftKey}
    />,
  );
  return { ...view, onSubmit };
}

// Composers render readonly-first: a static shell until clicked (unless an
// unsent draft exists). Tests that interact with the editor activate it the
// same way a user does.
function activateComposer(shellTestId: "comment-composer-shell" | "reply-composer-shell") {
  fireEvent.click(screen.getByTestId(shellTestId));
}

/** Native `disabled` while empty or sending; `aria-disabled` while an upload
 *  blocks it, so the reason tooltip stays reachable. */
function isSendBlocked(button: HTMLButtonElement): boolean {
  return button.disabled || button.getAttribute("aria-disabled") === "true";
}

function getSubmitButton(container: HTMLElement): HTMLButtonElement {
  // Submit is always the last button in a composer's action cluster.
  const buttons = container.querySelectorAll("button");
  const button = buttons[buttons.length - 1];
  if (!button) throw new Error("Expected submit button to render");
  return button;
}

beforeEach(() => {
  uploadWithToast.mockReset();
  apiUploadFile.mockReset();
  apiListWorkspaces.mockReset();
  apiListQuickActions.mockReset();
  apiGetIssue.mockReset().mockResolvedValue({ id: "issue-1", assignee_type: null, assignee_id: null });
  apiListAgents.mockReset().mockResolvedValue([]);
  apiListSquads.mockReset().mockResolvedValue([]);
  apiListMembers.mockReset().mockResolvedValue([{ user_id: "user-1", role: "member", name: "User" }]);
  apiPreviewCommentTriggers.mockReset();
  apiListTasksByIssue.mockReset();
  apiCancelTask.mockReset();
  insertMarkdownSpy.mockReset();
  insertPlaceholderSpy.mockReset();
  insertMarkdownBehavior.succeed = true;
  localStorage.clear();
  useCommentComposerStore.setState({ sticky: true, runningAgentReply: "steer" });
  // The composer's pinning (and the height cap that follows it) is viewport
  // dependent, so a narrow-viewport test must not leak into the next one.
  Object.defineProperty(window, "innerWidth", { configurable: true, value: 1280 });
  // The draft store is a module singleton — a draft left by a previous test
  // (e.g. the failed-send case) would trip the composers' draft-direct-mount
  // path and hide the shell the next test expects.
  useCommentDraftStore.setState({ drafts: {} });
  editorDefaultValues.values = [];
  editorQuickActionMenu.last = undefined;
  focusCalls.focused = 0;
  focusCalls.blurred = 0;
});

// ---------------------------------------------------------------------------
// Quick action `/` menu (MUL-5588)
// ---------------------------------------------------------------------------

describe("quick action `/` menu", () => {
  const workspace = { id: "ws-1", slug: "acme", name: "Acme" };

  function renderInWorkspace(ui: ReactNode) {
    apiListWorkspaces.mockResolvedValue([workspace]);
    apiListQuickActions.mockResolvedValue({
      quick_actions: [
        { id: "qa-1", name: "review", description: "Ask for a review", status: "active", assignee_type: "", prompt: "Review this issue" },
        { id: "qa-2", name: "retired", description: "", status: "archived" },
      ],
    });
    const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
    const view = renderWithI18n(
      <QueryClientProvider client={queryClient}>
        {/* useQuickActionMenu resolves the workspace from the slug context; without
            it the catalog query stays disabled and every menu reads as empty. */}
        <WorkspaceSlugProvider slug="acme">{ui}</WorkspaceSlugProvider>
      </QueryClientProvider>,
    );
    return { ...view, queryClient };
  }

  // Both composers post to the same issue, so `/` must offer the same catalog in
  // both. The reply box shipped without the prop entirely (MUL-5588): its menu
  // listed only the built-in `/note` while the top-level composer listed the
  // workspace's quick actions.
  const composers = [
    {
      name: "top-level comment composer",
      shell: "comment-composer-shell" as const,
      element: <CommentInput issueId="issue-1" onSubmit={vi.fn().mockResolvedValue(true)} />,
    },
    {
      name: "thread reply composer",
      shell: "reply-composer-shell" as const,
      element: (
        <ReplyInput
          issueId="issue-1"
          parentId="comment-1"
          avatarType="member"
          avatarId="user-1"
          onSubmit={vi.fn().mockResolvedValue(true)}
        />
      ),
    },
  ];

  async function replySkillIds({
    timeline,
    parentId = "comment-1",
    targetMissing = false,
    squads = [],
  }: {
    timeline: unknown[];
    parentId?: string;
    targetMissing?: boolean;
    squads?: unknown[];
  }) {
    const agentIds = [
      "cccccccc-cccc-cccc-cccc-cccccccccccc",
      "parent-agent",
      "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
      "root-leader",
    ];
    apiGetIssue.mockResolvedValue({
      id: "issue-1",
      assignee_type: "agent",
      assignee_id: "cccccccc-cccc-cccc-cccc-cccccccccccc",
    });
    apiListAgents.mockResolvedValue(agentIds.map((id) => ({
      id,
      runtime_id: "runtime-1",
      archived_at: null,
      owner_id: null,
      permission_mode: "public_to",
      invocation_targets: [{ target_type: "workspace", target_id: null }],
      skills: [{ id: `${id}-skill`, name: id, description: "" }],
    })));
    apiListSquads.mockResolvedValue(squads);
    const view = renderInWorkspace(
      <ReplyInput
        issueId="issue-1"
        parentId={parentId}
        avatarType="member"
        avatarId="user-1"
        targetMissing={targetMissing}
        onSubmit={vi.fn().mockResolvedValue(true)}
      />,
    );
    view.queryClient.setQueryData(issueKeys.timeline("issue-1"), timeline);
    activateComposer("reply-composer-shell");
    await waitFor(() => {
      expect(view.queryClient.getQueryData(issueKeys.detail("ws-1", "issue-1"))).toBeDefined();
      expect(view.queryClient.getQueryData(workspaceKeys.agents("ws-1"))).toHaveLength(agentIds.length);
      if (squads.length) {
        expect(view.queryClient.getQueryData(workspaceKeys.squads("ws-1"))).toHaveLength(squads.length);
      }
    });
    const editor = { state: { doc: { descendants: () => {} } } } as unknown as Editor;
    const ids = editorQuickActionMenu.last?.getSkills?.(editor).map((skill) => skill.id) ?? [];
    expect(apiListTasksByIssue).not.toHaveBeenCalled();
    return ids;
  }

  for (const composer of composers) {
    it(`offers the workspace's active quick actions in the ${composer.name}`, async () => {
      renderInWorkspace(composer.element);
      activateComposer(composer.shell);

      await waitFor(() => {
        expect(editorQuickActionMenu.last?.getQuickActions?.()).toEqual([
          { id: "qa-1", name: "review", description: "Ask for a review" },
        ]);
      });
    });

    it(`builds quick action text from the catalog in the ${composer.name}`, async () => {
      renderInWorkspace(composer.element);
      activateComposer(composer.shell);

      await waitFor(() => {
        expect(editorQuickActionMenu.last?.getQuickActionContent?.("qa-1")).toBe("Review this issue");
      });
    });

    it(`resolves Issue assignee skills in the ${composer.name}`, async () => {
      apiPreviewCommentTriggers.mockResolvedValue({ agents: [], blocked: [] });
      apiGetIssue.mockResolvedValue({
        id: "issue-1",
        assignee_type: "agent",
        assignee_id: "agent-1",
      });
      apiListAgents.mockResolvedValue([{
        id: "agent-1",
        runtime_id: "runtime-1",
        archived_at: null,
        owner_id: null,
        permission_mode: "public_to",
        invocation_targets: [{ target_type: "workspace", target_id: null }],
        skills: [{ id: "skill-1", name: "deploy", description: "Ship changes" }],
      }]);
      renderInWorkspace(composer.element);
      activateComposer(composer.shell);
      const editor = {
        state: { doc: { descendants: () => {} } },
      } as unknown as Editor;

      await waitFor(() => {
        expect(editorQuickActionMenu.last?.getSkills?.(editor).map((skill) => skill.id))
          .toEqual(["skill-1"]);
      });

      fireEvent.change(screen.getByTestId("editor"), { target: { value: "draft" } });
      await waitFor(() => expect(apiPreviewCommentTriggers).toHaveBeenCalled());
      await waitFor(() => expect(editorQuickActionMenu.last?.getSkills?.(editor)).toEqual([]));
    });
  }

  it("uses the parent comment agent's skills in ReplyInput", async () => {
    const parent = {
      type: "comment",
      id: "comment-1",
      actor_type: "agent",
      actor_id: "parent-agent",
      content: "Agent answer",
      parent_id: null,
    };

    expect(await replySkillIds({ timeline: [parent] })).toEqual(["parent-agent-skill"]);
  });

  it("uses the conversation owner when ReplyInput targets a member root", async () => {
    const root = {
      type: "comment",
      id: "root",
      actor_type: "member",
      actor_id: "user-2",
      content: "Please investigate",
      parent_id: null,
    };
    const agentReply = {
      ...root,
      id: "agent-reply",
      actor_type: "agent",
      actor_id: "parent-agent",
      content: "Agent answer",
      parent_id: root.id,
    };

    expect(await replySkillIds({ timeline: [root, agentReply], parentId: root.id }))
      .toEqual(["parent-agent-skill"]);
  });

  it.each([
    {
      name: "agent",
      content: "[@Root](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa) [@Assignee](mention://agent/cccccccc-cccc-cccc-cccc-cccccccccccc)",
      squads: [],
      expected: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa-skill",
    },
    {
      name: "squad",
      content: "[@Team](mention://squad/dddddddd-dddd-dddd-dddd-dddddddddddd) [@Assignee](mention://agent/cccccccc-cccc-cccc-cccc-cccccccccccc)",
      squads: [{ id: "dddddddd-dddd-dddd-dddd-dddddddddddd", leader_id: "root-leader", archived_at: null }],
      expected: "root-leader-skill",
    },
  ])("uses the first root $name mention in ReplyInput", async ({ content, squads, expected }) => {
    const root = {
      type: "comment",
      id: "root",
      actor_type: "member",
      actor_id: "user-2",
      content,
      parent_id: null,
    };
    const reply = { ...root, id: "comment-1", content: "Reply", parent_id: root.id };

    expect(await replySkillIds({ timeline: [root, reply], squads })).toEqual([expected]);
  });

  it("keeps member-to-member ReplyInput threads silent", async () => {
    const root = {
      type: "comment",
      id: "root",
      actor_type: "member",
      actor_id: "user-2",
      content: "Question",
      parent_id: null,
    };
    const reply = { ...root, id: "comment-1", content: "Reply", parent_id: root.id };

    expect(await replySkillIds({ timeline: [root, reply] })).toEqual([]);
  });

  it("returns no ReplyInput skills when the target is missing", async () => {
    const parent = {
      type: "comment",
      id: "comment-1",
      actor_type: "agent",
      actor_id: "parent-agent",
      content: "Agent answer",
      parent_id: null,
    };

    expect(await replySkillIds({ timeline: [parent], targetMissing: true })).toEqual([]);
  });
});

/**
 * Pick a recipient's action from its chip menu. Under CI load the composer can
 * still re-render the chips (the previous send settling, the preview catching
 * up) right after the menu opens, closing it; reopen until the item shows.
 */
async function chooseRecipientAction(chip: string, action: RegExp) {
  await waitFor(() => {
    const trigger = screen.getByRole("button", { name: chip });
    if (trigger.getAttribute("aria-expanded") !== "true") fireEvent.click(trigger);
    expect(screen.getByRole("menuitemradio", { name: action })).toBeInTheDocument();
  }, { timeout: 5000 });
  fireEvent.click(screen.getByRole("menuitemradio", { name: action }));
}

describe("comment composers", () => {
  it("renders the main comment composer without a manual expand control", () => {
    const { container } = renderCommentInput();

    // Readonly-first: shell shows the placeholder text; clicking mounts the
    // real editor in place.
    expect(screen.getByTestId("comment-composer-shell")).toHaveTextContent("Leave a comment...");
    activateComposer("comment-composer-shell");
    expect(screen.getByPlaceholderText("Leave a comment...")).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Attach file" })).toBeInTheDocument();
    expect(container.querySelectorAll("button")).toHaveLength(2);

    const shell = screen.getByTestId("drop-zone");
    expect(shell.className).not.toMatch(/max-h-/);
    expect(shell.className).not.toContain("h-[70vh]");
  });

  it("renders reply composer without a manual expand control", () => {
    const { container } = renderReplyInput();

    expect(screen.getByTestId("reply-composer-shell")).toHaveTextContent("Leave a reply...");
    activateComposer("reply-composer-shell");
    expect(screen.getByPlaceholderText("Leave a reply...")).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Attach file" })).toBeInTheDocument();
    expect(container.querySelectorAll("button")).toHaveLength(1);
    act(() => screen.getByTestId("editor").focus());
    expect(screen.getByRole("button", { name: "Send" })).toBeInTheDocument();
    expect(container.querySelectorAll("button")).toHaveLength(2);

    const shell = screen.getByTestId("drop-zone");
    expect(shell.className).not.toMatch(/max-h-/);
    expect(shell.className).not.toContain("h-[60vh]");
  });

  it("makes room for recipients a pending annotation previews while the reply is empty", async () => {
    apiListTasksByIssue.mockResolvedValue([]);
    apiPreviewCommentTriggers.mockResolvedValue({
      agents: [{ id: "agent-1", name: "Lambda", source: "thread_parent", reason: "" }],
    });
    const pending = { id: "a", sourceCommentId: "comment-1", sourceActorName: "Lambda", quote: "Selected text",
      note: "Please revise", start: 0, prefix: "", suffix: "" };
    renderWithProviders(
      <ReplyInput issueId="issue-1" parentId="comment-1" avatarType="member" avatarId="user-1"
        onSubmit={vi.fn()} draftKey="reply:issue-1:comment-1"
        annotationComposer={{ pending, render: () => null }} />,
    );

    expect(screen.getByTestId("reply-composer-shell")).toBeInTheDocument();
    await screen.findByRole("button", { name: /Lambda trigger/ }, { timeout: 5000 });
    expect(screen.getByRole("button", { name: "Send" })).toBeInTheDocument();
  });

  it("keeps main comment submission wired after removing expand", async () => {
    const { container, onSubmit } = renderCommentInput();

    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), {
      target: { value: "hello from composer" },
    });
    fireEvent.click(getSubmitButton(container));

    await waitFor(() => {
      expect(onSubmit).toHaveBeenCalledWith("hello from composer", undefined, undefined, undefined);
    });
  });

  it("steers the thread's running turn by default, and stops it first to start over", async () => {
    const turn = {
      id: "turn-1", agent_id: "agent-1", issue_id: "issue-1", status: "running", priority: 0,
      created_at: "2026-09-23T00:00:00Z", dispatched_at: null, started_at: "2026-09-23T00:00:01Z",
      completed_at: null, result: null, error: null,
      supplement_capability: "task-supplement-v1", can_supplement: true,
    };
    apiListTasksByIssue.mockResolvedValue([turn]);
    apiPreviewCommentTriggers.mockResolvedValue({
      agents: [{ id: "agent-1", name: "Lambda", source: "thread_parent", reason: "" }],
    });
    apiCancelTask.mockResolvedValue({ ...turn, status: "cancelled" });
    const onSubmit = vi.fn().mockResolvedValue("reply-new");
    const { container } = renderWithProviders(
      <ReplyInput issueId="issue-1" parentId="comment-1" avatarType="member" avatarId="user-1"
        onSubmit={onSubmit} steerByDefault={(task) => task.id === "turn-1"} />,
    );

    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "only fix web" } });
    await screen.findByText("Add to current run", {}, { timeout: 5000 });
    fireEvent.click(getSubmitButton(container));
    await waitFor(() => expect(onSubmit).toHaveBeenCalledWith("only fix web", undefined, undefined,
      ["turn-1"]), { timeout: 5000 });
    expect(apiCancelTask).not.toHaveBeenCalled();

    fireEvent.change(screen.getByTestId("editor"), { target: { value: "start over on web" } });
    await chooseRecipientAction("Lambda trigger: Add to current run", /Stop and start over/);
    fireEvent.click(await screen.findByRole("button", { name: "Stop and send" }, { timeout: 5000 }));
    await waitFor(() => expect(onSubmit).toHaveBeenCalledWith("start over on web", undefined, undefined, undefined), { timeout: 5000 });
    expect(apiCancelTask).toHaveBeenCalledWith("issue-1", "turn-1");
    expect(apiCancelTask.mock.invocationCallOrder[0]!).toBeLessThan(onSubmit.mock.invocationCallOrder[1]!);
  }, 15_000);

  it("starts after the run by default when the personal preference says so", async () => {
    useCommentComposerStore.setState({ runningAgentReply: "after_run" });
    const turn = {
      id: "turn-1", agent_id: "agent-1", issue_id: "issue-1", status: "running", priority: 0,
      created_at: "2026-09-23T00:00:00Z", dispatched_at: null, started_at: "2026-09-23T00:00:01Z",
      completed_at: null, result: null, error: null,
      supplement_capability: "task-supplement-v1", can_supplement: true,
    };
    apiListTasksByIssue.mockResolvedValue([turn]);
    apiPreviewCommentTriggers.mockResolvedValue({ agents: [{ id: "agent-1", name: "Lambda", source: "thread_parent", reason: "" }] });
    const onSubmit = vi.fn().mockResolvedValue("reply-new");
    const { container } = renderWithProviders(
      <ReplyInput issueId="issue-1" parentId="comment-1" avatarType="member" avatarId="user-1"
        onSubmit={onSubmit} steerByDefault={(task) => task.id === "turn-1"} />,
    );

    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "only fix web" } });
    await screen.findByRole("button", { name: "Lambda trigger: Start after this run" }, { timeout: 5000 });
    fireEvent.click(getSubmitButton(container));
    await waitFor(() => expect(onSubmit).toHaveBeenCalledWith("only fix web", undefined, undefined, undefined), { timeout: 5000 });

    // One message can still go into the running turn.
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "and keep desktop as is" } });
    await chooseRecipientAction("Lambda trigger: Start after this run", /Add to current run/);
    await screen.findByRole("button", { name: "Lambda trigger: Add to current run" }, { timeout: 5000 });
    fireEvent.click(getSubmitButton(container));
    await waitFor(() => expect(onSubmit).toHaveBeenLastCalledWith("and keep desktop as is", undefined, undefined,
      ["turn-1"]), { timeout: 5000 });
  }, 15_000);

  it("steers every running recipient the message addresses", async () => {
    const turn = (id: string, agentId: string) => ({
      id, agent_id: agentId, issue_id: "issue-1", status: "running", priority: 0,
      created_at: "2026-09-23T00:00:00Z", dispatched_at: null, started_at: "2026-09-23T00:00:01Z",
      completed_at: null, result: null, error: null,
      supplement_capability: "task-supplement-v1", can_supplement: true,
    });
    apiListTasksByIssue.mockResolvedValue([turn("turn-1", "agent-1"), turn("turn-2", "agent-2")]);
    apiPreviewCommentTriggers.mockResolvedValue({ agents: [
      { id: "agent-1", name: "Lambda", source: "mention_agent", reason: "" },
      { id: "agent-2", name: "Orion", source: "mention_agent", reason: "" },
    ] });
    const onSubmit = vi.fn().mockResolvedValue("reply-new");
    const { container } = renderWithProviders(
      <ReplyInput issueId="issue-1" parentId="comment-1" avatarType="member" avatarId="user-1"
        onSubmit={onSubmit} steerByDefault={() => true} />,
    );
    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "only fix web" } });
    fireEvent.click(await screen.findByText("2 agents will receive this", {}, { timeout: 5000 }));
    expect(await screen.findByRole("button", { name: "Lambda trigger: Add to current run" }, { timeout: 5000 })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Orion trigger: Add to current run" })).toBeInTheDocument();
    fireEvent.click(getSubmitButton(container));
    await waitFor(() => expect(onSubmit).toHaveBeenCalledWith("only fix web", undefined, undefined,
      ["turn-1", "turn-2"]), { timeout: 5000 });
  }, 15_000);

  it("never stops the previous recipient after the mentions change under a stale preview", async () => {
    const turn = {
      id: "turn-1", agent_id: "agent-1", issue_id: "issue-1", status: "running", priority: 0,
      created_at: "2026-09-23T00:00:00Z", dispatched_at: null, started_at: "2026-09-23T00:00:01Z",
      completed_at: null, result: null, error: null,
      supplement_capability: "task-supplement-v1", can_supplement: true,
    };
    apiListTasksByIssue.mockResolvedValue([turn]);
    apiPreviewCommentTriggers.mockResolvedValue({ agents: [{ id: "agent-1", name: "Lambda", source: "thread_parent", reason: "" }] });
    apiCancelTask.mockResolvedValue({ ...turn, status: "cancelled" });
    const onSubmit = vi.fn().mockResolvedValue("reply-new");
    renderWithProviders(<ReplyInput issueId="issue-1" parentId="comment-1" avatarType="member" avatarId="user-1"
      onSubmit={onSubmit} steerByDefault={(task) => task.id === "turn-1"} />);
    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "start over" } });
    await chooseRecipientAction("Lambda trigger: Add to current run", /Stop and start over/);
    // A slow preview still holds Lambda's restart, though the comment now
    // explicitly addresses only a different agent.
    apiPreviewCommentTriggers.mockImplementation(() => new Promise(() => {}));
    fireEvent.change(screen.getByTestId("editor"), {
      target: { value: "[@Orion](mention://agent/11111111-1111-4111-8111-111111111111) review desktop" },
    });
    fireEvent.click(screen.getByRole("button", { name: "Stop and send" }));
    await waitFor(() => expect(onSubmit).toHaveBeenCalled(), { timeout: 5000 });
    expect(apiCancelTask).not.toHaveBeenCalled();
  }, 15_000);

  it("keeps reply submission wired after removing expand", async () => {
    const { container, onSubmit } = renderReplyInput();

    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), {
      target: { value: "thread reply" },
    });
    fireEvent.click(getSubmitButton(container));

    await waitFor(() => {
      expect(onSubmit).toHaveBeenCalledWith("thread reply", undefined, undefined, undefined);
    });
  });

  it("keeps the main comment editor's initial draft snapshot after persistence rerenders", () => {
    renderCommentInput();
    activateComposer("comment-composer-shell");

    fireEvent.change(screen.getByTestId("editor"), {
      target: { value: "test.de" },
    });

    expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toBe("test.de");
    expect(editorDefaultValues.values.at(-1)).toBeUndefined();
  });

  it("keeps the reply editor's initial draft snapshot after persistence rerenders", () => {
    renderReplyInput({ draftKey: "reply:issue-1:comment-1" });
    activateComposer("reply-composer-shell");

    fireEvent.change(screen.getByTestId("editor"), {
      target: { value: "test.de" },
    });

    expect(
      useCommentDraftStore
        .getState()
        .getDraft("reply:issue-1:comment-1"),
    ).toBe("test.de");
    expect(editorDefaultValues.values.at(-1)).toBeUndefined();
  });

  it("locks the editor while the send is in flight, then clears on success", async () => {
    let resolveSubmit: (ok: boolean) => void = () => {};
    const onSubmit = vi.fn(
      () => new Promise<boolean>((resolve) => { resolveSubmit = resolve; }),
    );
    const { container } = renderCommentInput(onSubmit);

    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "sending" } });
    fireEvent.click(getSubmitButton(container));

    // In flight: text kept, editor wrapper locked (aria-busy), not cleared yet.
    await waitFor(() =>
      expect(screen.getByTestId("editor").closest("[aria-busy]")).toHaveAttribute(
        "aria-busy",
        "true",
      ),
    );
    expect(onSubmit).toHaveBeenCalledWith("sending", undefined, undefined, undefined);

    resolveSubmit(true);

    // Success: the composer clears (now empty → submit disabled, lock released).
    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(true));
    expect(screen.getByTestId("editor").closest("[aria-busy]")).toBeNull();
  });

  // Post-send caret policy. Both composers drop the caret once the send is
  // accepted: a focused editor swallows the page's keyboard shortcuts, so
  // leaving the caret in a reply box traps the user until they click away.
  it("blurs the top-level composer after a posted comment", async () => {
    const { container } = renderCommentInput();

    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "posted" } });
    // Discard the focus the lazy shell→editor swap performed; what follows is
    // entirely the post-send policy.
    focusCalls.focused = 0;
    fireEvent.click(getSubmitButton(container));

    await waitFor(() => expect(focusCalls.blurred).toBeGreaterThan(0));
    expect(focusCalls.focused).toBe(0);
  });

  it("blurs the reply box after a posted reply", async () => {
    const { container } = renderReplyInput();

    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "replied" } });
    // Discard the shell→editor activation focus; what follows is entirely the
    // post-send policy.
    focusCalls.focused = 0;
    fireEvent.click(getSubmitButton(container));

    await waitFor(() => expect(focusCalls.blurred).toBeGreaterThan(0));
    expect(focusCalls.focused).toBe(0);
  });

  it("reports the new reply id as the accepted scroll target", async () => {
    const onAccepted = vi.fn();
    const { container } = renderReplyInput({ onAccepted });

    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "replied" } });
    fireEvent.click(getSubmitButton(container));

    await waitFor(() => expect(onAccepted).toHaveBeenCalledTimes(1));
    expect(onAccepted).toHaveBeenCalledWith("reply-new");
  });

  it("leaves reply focus alone when the send fails", async () => {
    const onSubmit = vi.fn().mockResolvedValue(false);
    const { container } = renderReplyInput({ onSubmit });

    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "nope" } });
    focusCalls.focused = 0;
    fireEvent.click(getSubmitButton(container));

    await waitFor(() => expect(onSubmit).toHaveBeenCalled());
    await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));
    expect(focusCalls.focused).toBe(0);
    expect(focusCalls.blurred).toBe(0);
  });

  // Regression: the tab-switch flush re-writes IDENTICAL content mid-flight;
  // that must not read as "edited during the request" — the posted comment's
  // draft still clears (it previously resurrected with Send re-enabled).
  it("a tab switch during the send does not resurrect the posted draft", async () => {
    let resolveSubmit!: (v: boolean) => void;
    const onSubmit = vi.fn(() => new Promise<boolean>((r) => { resolveSubmit = r; }));
    renderCommentInput(onSubmit);
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "draft A" } });
    fireEvent.keyDown(screen.getByTestId("editor"), { key: "Enter", metaKey: true });
    await waitFor(() => expect(onSubmit).toHaveBeenCalled());

    // Backgrounding the tab fires the visibility flush with unchanged content.
    Object.defineProperty(document, "visibilityState", {
      configurable: true,
      get: () => "hidden",
    });
    fireEvent(document, new Event("visibilitychange"));

    await act(async () => {
      resolveSubmit(true);
      await Promise.resolve();
    });

    expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toBeUndefined();

    Object.defineProperty(document, "visibilityState", {
      configurable: true,
      get: () => "visible",
    });
  });

  // MUL-5181 P0: the editor stays interactive during a send — text typed
  // while draft A is in flight must survive A's success, in store AND editor.
  it("text typed while a comment send is in flight survives the success", async () => {
    let resolveSubmit!: (v: boolean) => void;
    const onSubmit = vi.fn(() => new Promise<boolean>((r) => { resolveSubmit = r; }));
    renderCommentInput(onSubmit);
    activateComposer("comment-composer-shell");
    const editor = screen.getByTestId("editor");
    fireEvent.change(editor, { target: { value: "draft A" } });
    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });
    await waitFor(() => expect(onSubmit).toHaveBeenCalled());

    // Still mounted, still typing while the request is in flight.
    fireEvent.change(editor, { target: { value: "draft A plus more" } });

    await act(async () => {
      resolveSubmit(true);
      await Promise.resolve();
    });

    expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toBe("draft A plus more");
    // ...and the post-send caret policy must stand down with it: the guard left
    // the editor holding "draft A plus more", so blurring would yank the caret
    // out of the sentence the user is still writing.
    await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));
    expect(focusCalls.blurred).toBe(0);
  });

  // Same coupling on the reply side: the blur is bound to having actually wiped
  // the editor, not merely to acceptance.
  it("does not blur a reply box whose mid-flight draft was kept", async () => {
    let resolveSubmit!: (v: boolean) => void;
    const onSubmit = vi.fn(() => new Promise<boolean>((r) => { resolveSubmit = r; }));
    renderReplyInput({ onSubmit, draftKey: "reply:issue-1:comment-1" });
    activateComposer("reply-composer-shell");
    const editor = screen.getByTestId("editor");
    fireEvent.change(editor, { target: { value: "reply A" } });
    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });
    await waitFor(() => expect(onSubmit).toHaveBeenCalled());

    fireEvent.change(editor, { target: { value: "reply A plus more" } });
    focusCalls.focused = 0;

    await act(async () => {
      resolveSubmit(true);
      await Promise.resolve();
    });

    expect(useCommentDraftStore.getState().getDraft("reply:issue-1:comment-1")).toBe(
      "reply A plus more",
    );
    await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));
    expect(focusCalls.blurred).toBe(0);
    expect(focusCalls.focused).toBe(0);
  });

  // MUL-5181 P0: a submit that outlives its composer may only clear the draft
  // it submitted — never one typed after the composer unmounted and reopened.
  it("a late comment success does NOT clear a draft typed after unmount", async () => {
    let resolveSubmit!: (v: boolean) => void;
    const onSubmit = vi.fn(() => new Promise<boolean>((r) => { resolveSubmit = r; }));
    const view = renderCommentInput(onSubmit);
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "draft A" } });
    fireEvent.keyDown(screen.getByTestId("editor"), { key: "Enter", metaKey: true });
    await waitFor(() => expect(onSubmit).toHaveBeenCalled());

    view.unmount();
    // A reopened composer typed draft B under the same key.
    useCommentDraftStore.getState().setDraft("new:issue-1", "draft B");

    await act(async () => {
      resolveSubmit(true);
      await Promise.resolve();
    });

    expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toBe("draft B");
  });

  it("a late comment success still clears an untouched draft", async () => {
    let resolveSubmit!: (v: boolean) => void;
    const onSubmit = vi.fn(() => new Promise<boolean>((r) => { resolveSubmit = r; }));
    const view = renderCommentInput(onSubmit);
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "draft A" } });
    fireEvent.keyDown(screen.getByTestId("editor"), { key: "Enter", metaKey: true });
    await waitFor(() => expect(onSubmit).toHaveBeenCalled());

    view.unmount();
    await act(async () => {
      resolveSubmit(true);
      await Promise.resolve();
    });

    expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toBeUndefined();
  });

  it("keeps the draft when the send fails (no optimistic clear)", async () => {
    const onSubmit = vi.fn().mockResolvedValue(false);
    const { container } = renderCommentInput(onSubmit);

    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "will fail" } });
    fireEvent.click(getSubmitButton(container));

    await waitFor(() => expect(onSubmit).toHaveBeenCalled());
    // Failed send must NOT clear — the box still has content, submit stays live.
    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(false));
  });
});

// MUL-4808 — posting mid-upload strips the pending image's blob URL out of the
// body and binds no attachment id, so the comment lands without the file.
describe("comment composers — upload submit gate", () => {
  const uploadAttachment = (id: string, url: string) =>
    ({
      id,
      url,
      download_url: url,
      markdown_url: url,
      filename: `${id}.png`,
      content_type: "image/png",
      size_bytes: 1,
    }) as unknown as Attachment;

  /**
   * Start an upload that stays in flight until the returned resolver runs. The
   * coordinator calls `api.uploadFile`, so this controls THAT promise: resolve
   * it with an attachment (success) or reject it (failure).
   */
  function startPendingUpload(container: HTMLElement, filename = "slow.png") {
    let resolveUpload!: (att: Attachment) => void;
    let rejectUpload!: (err: Error) => void;
    apiUploadFile.mockImplementationOnce(
      () =>
        new Promise<Attachment>((resolve, reject) => {
          resolveUpload = resolve;
          rejectUpload = reject;
        }),
    );
    // FileUploadButton's visible control is a button that clicks a hidden
    // input; the input is what carries the selection.
    const input = container.querySelector('input[type="file"]');
    if (!input) throw new Error("Expected a file input to render");
    fireEvent.change(input, {
      target: { files: [new File(["x"], filename, { type: "image/png" })] },
    });
    return {
      resolve: (att: Attachment) => resolveUpload(att),
      fail: () => rejectUpload(new Error("upload failed")),
    };
  }

  it("disables send while an upload is in flight and re-enables once it settles", async () => {
    const { container } = renderCommentInput();
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "look at this" } });

    const pending = startPendingUpload(container);

    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(true));
    // Screen readers must hear "busy", not just a dead control.
    expect(getSubmitButton(container)).toHaveAttribute("aria-busy", "true");

    await act(async () => {
      pending.resolve(uploadAttachment("att-1", "https://cdn.example/att-1.png"));
    });

    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(false));
    expect(getSubmitButton(container)).not.toHaveAttribute("aria-busy");
  });



  it("never asks the rebuild to draw an upload this mount started", async () => {
    // The editor drew that node itself, synchronously, before the draft record
    // existed. Registering the id at handleUpload rather than waiting for the
    // effect to discover the node closes the window in which a delete could be
    // undone by a redraw.
    const { container } = renderCommentInput();
    activateComposer("comment-composer-shell");
    const pending = startPendingUpload(container, "mine.png");

    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(true));
    expect(insertPlaceholderSpy).not.toHaveBeenCalled();

    await act(async () => {
      pending.resolve(uploadAttachment("att-mine", "https://cdn.example/att-mine.png"));
    });
    expect(insertPlaceholderSpy).not.toHaveBeenCalled();
  });

  it("does not redraw a placeholder the user deleted mid-upload", async () => {
    // MUL-5181's rule: a placeholder the user removed stays removed. The
    // rebuild runs once per id per mount, so the next store write cannot undo
    // that decision — the send button is what still says the upload is live.
    useCommentDraftStore.getState().addUpload("new:issue-1", {
      clientUploadId: "u-deleted",
      status: "uploading",
      filename: "gone.png",
      size: 1,
    });
    renderCommentInput();
    await screen.findByTestId("editor");
    await waitFor(() => expect(insertPlaceholderSpy).toHaveBeenCalledTimes(1));

    // A second upload changes `uploads`, which re-runs the rebuild effect —
    // the moment a naive implementation would redraw the first one.
    act(() => {
      useCommentDraftStore.getState().addUpload("new:issue-1", {
        clientUploadId: "u-second",
        status: "uploading",
        filename: "other.png",
        size: 1,
      });
    });

    await waitFor(() =>
      expect(
        insertPlaceholderSpy.mock.calls.some(([u]) => u.uploadId === "u-second"),
      ).toBe(true),
    );
    expect(
      insertPlaceholderSpy.mock.calls.filter(([u]) => u.uploadId === "u-deleted"),
    ).toHaveLength(1);
  });

  it("leaves nothing behind when an upload fails", async () => {
    // The toast already said it, at the moment it happened, and the file is
    // still on disk. A chip cannot retry (the bytes were never persisted) and
    // `isMeaningful` counts it, so keeping one would hold an otherwise-empty
    // draft alive across reloads until dismissed by hand.
    const { container } = renderCommentInput();
    activateComposer("comment-composer-shell");

    const pending = startPendingUpload(container, "doomed.png");
    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(true));

    await act(async () => {
      pending.fail();
    });

    await waitFor(() =>
      expect(useCommentDraftStore.getState().getUploads("new:issue-1")).toHaveLength(0),
    );
    expect(screen.queryByText(/doomed\.png/)).toBeNull();
    // The empty draft is gone with it, not kept alive by a dead placeholder.
    expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toBeFalsy();
  });

  it("rebuilds a placeholder for an upload inherited from the persisted draft", async () => {
    // Started by a mount that is gone: its placeholder node died with that
    // editor, and placeholders are never serialised, so the reopened composer
    // has the record but no node. It draws one again — otherwise the composer
    // looks idle while `gate` quietly blocks the send.
    useCommentDraftStore.getState().addUpload("new:issue-1", {
      clientUploadId: "from-a-previous-mount",
      status: "uploading",
      filename: "orphan.png",
      size: 1,
    });

    renderCommentInput();
    await screen.findByTestId("editor");

    await waitFor(() =>
      expect(insertPlaceholderSpy).toHaveBeenCalledWith(
        expect.objectContaining({ uploadId: "from-a-previous-mount", filename: "orphan.png" }),
      ),
    );
  });

  it("blocks the Cmd+Enter path while an upload is in flight", async () => {
    const { container, onSubmit } = renderCommentInput();
    activateComposer("comment-composer-shell");
    const editor = screen.getByTestId("editor");
    fireEvent.change(editor, { target: { value: "look at this" } });

    const pending = startPendingUpload(container);

    // The shortcut never touches the disabled button — the handler's own
    // re-read of the queue is the only thing standing between this keystroke
    // and a comment posted without its attachment.
    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });
    await Promise.resolve();
    expect(onSubmit).not.toHaveBeenCalled();

    await act(async () => {
      pending.resolve(uploadAttachment("att-1", "https://cdn.example/att-1.png"));
    });

    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });
    await waitFor(() => expect(onSubmit).toHaveBeenCalled());
  });

  it("blocks send while a coordinator upload from a PREVIOUS mount is still in flight", async () => {
    // Reopened-composer scenario: the upload placeholder lives in the draft
    // store (coordinator-owned), but this mount's editor is clean — the editor
    // gate alone sees no active uploads. Sending here would clear the draft
    // out from under the settling upload and silently drop the file.
    useCommentDraftStore.getState().setDraft("new:issue-1", "recovered draft");
    useCommentDraftStore.getState().addUpload("new:issue-1", {
      clientUploadId: "prev-mount-upload",
      status: "uploading",
      filename: "shot.png",
      size: 10,
    });

    const { container, onSubmit } = renderCommentInput();
    // Draft + pending upload mount the editor directly (no shell).
    const editor = await screen.findByTestId("editor");

    expect(isSendBlocked(getSubmitButton(container))).toBe(true);
    expect(getSubmitButton(container)).toHaveAttribute("aria-busy", "true");

    // The shortcut path bypasses the button — the submit-time re-read of the
    // DRAFT's uploads is the only guard.
    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });
    await Promise.resolve();
    expect(onSubmit).not.toHaveBeenCalled();

    // The old upload settles (coordinator onSettled → store.settleUpload) —
    // the gate opens and the send goes through.
    await act(async () => {
      useCommentDraftStore
        .getState()
        .settleUpload(
          "new:issue-1",
          "prev-mount-upload",
          uploadAttachment("att-prev", "https://cdn.example/att-prev.png"),
        );
    });
    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(false));
    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });
    await waitFor(() => expect(onSubmit).toHaveBeenCalled());
  });

  it("stays gated until the LAST of two concurrent uploads settles", async () => {
    const { container } = renderCommentInput();
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "two files" } });

    const first = startPendingUpload(container, "a.png");
    const second = startPendingUpload(container, "b.png");

    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(true));

    // First one lands — the second is still in flight, so send must stay shut.
    await act(async () => {
      first.resolve(uploadAttachment("att-a", "https://cdn.example/att-a.png"));
    });
    expect(isSendBlocked(getSubmitButton(container))).toBe(true);

    await act(async () => {
      second.resolve(uploadAttachment("att-b", "https://cdn.example/att-b.png"));
    });
    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(false));
  });

  it("re-enables send after a FAILED upload so the draft isn't stuck", async () => {
    const { container } = renderCommentInput();
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "will fail" } });

    const pending = startPendingUpload(container);
    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(true));

    // uploadWithToast reports the failure and resolves null — the placeholder
    // is dropped and the body never referenced it, so submit must come back.
    await act(async () => {
      pending.fail();
    });
    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(false));
  });

  it("sends the attachment id once the upload completes", async () => {
    const { container, onSubmit } = renderCommentInput();
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "shipping it" } });

    const pending = startPendingUpload(container);
    await act(async () => {
      pending.resolve(uploadAttachment("att-9", "https://cdn.example/att-9.png"));
    });

    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(false));
    fireEvent.click(getSubmitButton(container));

    await waitFor(() =>
      expect(onSubmit).toHaveBeenCalledWith(
        expect.stringContaining("https://cdn.example/att-9.png"),
        ["att-9"],
        undefined,
        undefined,
      ),
    );
  });

  it("does NOT bind an upload the user deleted from the body", async () => {
    const { container, onSubmit } = renderCommentInput();
    activateComposer("comment-composer-shell");
    const editor = screen.getByTestId("editor");
    fireEvent.change(editor, { target: { value: "keep this" } });

    const pending = startPendingUpload(container);
    await act(async () => {
      pending.resolve(uploadAttachment("att-del", "https://cdn.example/att-del.png"));
    });

    // The completed upload's link is in the body; the user deletes it. What
    // is not referenced must not ship — deleting really unbinds (MUL-5181).
    fireEvent.change(editor, { target: { value: "keep this, dropped the image" } });
    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });

    await waitFor(() => expect(onSubmit).toHaveBeenCalled());
    expect(onSubmit).toHaveBeenCalledWith("keep this, dropped the image", undefined, undefined, undefined);
  });

  it("writes the finished upload's link into the persisted draft after the composer unmounts", async () => {
    const { container, unmount } = renderCommentInput();
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "wip text" } });

    const pending = startPendingUpload(container);
    unmount();

    await act(async () => {
      pending.resolve(uploadAttachment("att-wb", "https://cdn.example/att-wb.png"));
    });

    // The upload outlived the composer; its link must land in the draft BODY —
    // that's what a future submit binds, and what a reopen renders.
    const draft = useCommentDraftStore.getState().getDraft("new:issue-1");
    expect(draft).toContain("wip text");
    expect(draft).toContain("https://cdn.example/att-wb.png");
    const uploads = useCommentDraftStore.getState().getUploads("new:issue-1");
    expect(uploads).toHaveLength(1);
    expect(uploads[0]?.status).toBe("uploaded");
  });

  it("hands the finished upload's link to a REOPENED composer's live editor", async () => {
    const first = renderCommentInput();
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "draft body" } });

    const pending = startPendingUpload(first.container);
    first.unmount();

    // Reopen: the draft + pending upload mount the editor directly (no shell).
    renderCommentInput();
    await screen.findByTestId("editor");

    await act(async () => {
      pending.resolve(uploadAttachment("att-live", "https://cdn.example/att-live.png"));
    });

    // The LIVE EDITOR received the insert (not just the store — an append the
    // mounted editor never saw would be erased by its next emit)...
    expect(insertMarkdownSpy).toHaveBeenCalledWith(
      expect.stringContaining("https://cdn.example/att-live.png"),
    );
    // ...and the draft body converged through the normal onUpdate pipeline.
    await waitFor(() =>
      expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toContain(
        "https://cdn.example/att-live.png",
      ),
    );
  });

  it("retries the insert while the reopened editor's instance is still warming up", async () => {
    const first = renderCommentInput();
    activateComposer("comment-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "draft body" } });

    const pending = startPendingUpload(first.container);
    first.unmount();

    renderCommentInput();
    await screen.findByTestId("editor");

    // Simulate the real handle's window where the component committed but the
    // Tiptap instance hasn't been created yet: insert reports failure.
    insertMarkdownBehavior.succeed = false;
    await act(async () => {
      pending.resolve(uploadAttachment("att-retry", "https://cdn.example/att-retry.png"));
    });

    // Nothing may land in the store while a mounted editor can't show it —
    // that append would be silently erased by the editor's first emit.
    expect(useCommentDraftStore.getState().getDraft("new:issue-1") ?? "").not.toContain(
      "att-retry.png",
    );

    // Instance comes up → the retry delivers into the editor.
    insertMarkdownBehavior.succeed = true;
    await waitFor(
      () =>
        expect(useCommentDraftStore.getState().getDraft("new:issue-1")).toContain(
          "https://cdn.example/att-retry.png",
        ),
      { timeout: 3000 },
    );
  });

  it("gates the reply composer's send button too", async () => {
    const { container } = renderReplyInput();
    activateComposer("reply-composer-shell");
    fireEvent.change(screen.getByTestId("editor"), { target: { value: "replying" } });

    startPendingUpload(container);

    await waitFor(() => expect(isSendBlocked(getSubmitButton(container))).toBe(true));
    expect(getSubmitButton(container)).toHaveAttribute("aria-busy", "true");
  });

  it("blocks the reply composer's Cmd+Enter path", async () => {
    const onSubmit = vi.fn().mockResolvedValue(true);
    const { container } = renderReplyInput({ onSubmit });
    activateComposer("reply-composer-shell");
    const editor = screen.getByTestId("editor");
    fireEvent.change(editor, { target: { value: "replying" } });

    startPendingUpload(container);

    fireEvent.keyDown(editor, { key: "Enter", metaKey: true });
    await Promise.resolve();
    expect(onSubmit).not.toHaveBeenCalled();
  });
});

describe("sticky composer preference", () => {
  it("caps the editor height while the sticky preference is on (default)", () => {
    renderCommentInput();

    activateComposer("comment-composer-shell");
    // The height cap lives on the editor wrapper, not the card shell.
    expect(screen.getByTestId("editor").parentElement?.className).toContain("max-h-[40vh]");
  });

  it("lets the editor grow when the preference is off", () => {
    useCommentComposerStore.setState({ sticky: false });
    renderCommentInput();

    activateComposer("comment-composer-shell");
    expect(screen.getByTestId("editor").parentElement?.className).not.toContain("max-h-[40vh]");
  });

  // The cap only earns its keep while the composer is pinned: it stops a long
  // draft from swallowing the timeline it floats over. Below the mobile
  // breakpoint nothing is pinned (see `useStickyComposer`), so a capped
  // composer would just be a scroll-inside-a-scroll for no reason.
  it("lets the editor grow below the mobile breakpoint, where nothing is pinned", () => {
    Object.defineProperty(window, "innerWidth", { configurable: true, value: 390 });
    renderCommentInput();

    activateComposer("comment-composer-shell");
    expect(screen.getByTestId("editor").parentElement?.className).not.toContain("max-h-[40vh]");
  });
});

describe.each(["reply", "description"])("annotations in %s drafts", (source) => {
  const draftKey = source === "reply" ? "reply:issue-1:comment-1" as const : "new:issue-1" as const;
  const renderAnnotatedComposer = (onSubmit: Parameters<typeof renderCommentInput>[0]) => source === "reply"
    ? renderReplyInput({ draftKey: "reply:issue-1:comment-1", onSubmit }) : renderCommentInput(onSubmit);
  const annotation = { id: "a", sourceCommentId: "agent-source", sourceActorName: "Emacs", quote: "Selected text", note: "Please revise", start: 0, prefix: "", suffix: "" };

  it("sends an annotation-only draft once without requiring a mounted editor", async () => {
    useCommentDraftStore.getState().addAnnotation(draftKey, annotation);
    const onSubmit = vi.fn().mockResolvedValue("reply-new");
    renderAnnotatedComposer(onSubmit);
    expect(screen.queryByTestId("editor")).not.toBeInTheDocument();
    const send = screen.getByRole("button", { name: "Send" });
    fireEvent.click(send);
    fireEvent.click(send);
    await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
    expect(onSubmit.mock.calls[0]?.[0]).toContain("> Selected text");
    expect(onSubmit.mock.calls[0]?.[0]).toContain("Please revise");
    await waitFor(() => expect(useCommentDraftStore.getState().getAnnotations(draftKey)).toHaveLength(0));
  });

  it("sends annotations above the composer body with a divider", async () => {
    useCommentDraftStore.getState().setDraft(draftKey, "Overall reply");
    useCommentDraftStore.getState().addAnnotation(draftKey, annotation);
    const onSubmit = vi.fn().mockResolvedValue("reply-new");
    renderAnnotatedComposer(onSubmit);
    fireEvent.click(screen.getByRole("button", { name: "Send" }));
    await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
    expect(onSubmit.mock.calls[0]?.[0]).toBe("> Selected text\n\nPlease revise\n\n---\n\nOverall reply");
  });

  it("ignores empty selections and keeps saved annotations on send failure", async () => {
    useCommentDraftStore.getState().addAnnotation(draftKey, { ...annotation, note: "" });
    const onSubmit = vi.fn().mockResolvedValue(false);
    renderAnnotatedComposer(onSubmit);
    // Nothing to send: the comment box keeps a blocked send, a reply rests as one line.
    if (source === "description") expect(isSendBlocked(screen.getByRole("button", { name: "Send" }) as HTMLButtonElement)).toBe(true);
    else expect(screen.queryByRole("button", { name: "Send" })).not.toBeInTheDocument();
    expect(screen.queryByRole("button", { name: /1 annotation/ })).not.toBeInTheDocument();
    act(() => { useCommentDraftStore.getState().addAnnotation(draftKey, { ...annotation, note: "New note" }); });
    fireEvent.click(screen.getByRole("button", { name: "Send" }));
    await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
    expect(useCommentDraftStore.getState().getAnnotations(draftKey)[0]?.note).toBe("New note");
  });

  it("retains new annotations collected while a send is pending", async () => {
    useCommentDraftStore.getState().addAnnotation(draftKey, annotation);
    let accept!: (id: string) => void;
    const onSubmit = vi.fn(() => new Promise<string>((resolve) => { accept = resolve; }));
    renderAnnotatedComposer(onSubmit);
    fireEvent.click(screen.getByRole("button", { name: "Send" }));
    await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
    act(() => { useCommentDraftStore.getState().addAnnotation(draftKey, { ...annotation, id: "b", quote: "Another point" }); });
    await act(async () => accept("reply-new"));
    expect(useCommentDraftStore.getState().getAnnotations(draftKey)).toHaveLength(2);
  });
});