Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

63.8 KB
Raw Blame
import React from 'react';
import { getDefaultStore } from 'jotai';
import { ContentTypes, Tools } from 'librechat-data-provider';
import { act, fireEvent, render, screen } from '@testing-library/react';
import {
  subagentProgressKey,
  reduceSubagentProgress,
  subagentProgressByToolCallId,
} from '@librechat/chat/react';
import type {
  TMessageContentParts,
  SubagentUpdateEvent,
  TAttachment,
} from 'librechat-data-provider';
import type { SubagentProgress } from '@librechat/chat/react';
import { preserveStreamedContentIdentity } from '~/utils/messages';
import { mapAttachments, groupSequentialToolCalls } from '~/utils';

jest.mock('~/utils', () => ({
  cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),
  mapAttachments: jest.fn(() => ({})),
  filterAttachmentsForPart: (attachments: unknown) => attachments,
  groupSequentialToolCalls: jest.fn(),
  hasPendingApprovalInPart: jest.requireActual('~/utils/groupToolCalls').hasPendingApprovalInPart,
  getPartKeyIndex: jest.requireActual('~/utils/messages').getPartKeyIndex,
  /** Real implementations: the media helpers are pure and drive the phase
   * card's attachment row, so stubbing them would make that path inert here. */
  ...jest.requireActual<typeof import('~/utils/media')>('~/utils/media'),
}));

jest.mock('~/Providers', () => {
  return {
    /** Use the real context: cloning the immediate child assumes Part has no
     * intervening providers and does not exercise context propagation. */
    MessageContext: jest.requireActual<typeof import('~/Providers/MessageContext')>(
      '~/Providers/MessageContext',
    ).MessageContext,
    SearchContext: {
      Provider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
    },
    /** `WebSearch` reads this; leaving it off the mock threw inside the render and
     * the component's own catch swallowed it, so the sources path was dead here
     * while the suite still passed. */
    useSearchContext: () => ({ searchResults: undefined }),
    MediaContext: {
      Provider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
    },
    useMediaContext: () => ({ attachmentsByName: undefined }),
  };
});

jest.mock('../Parts', () => ({
  EmptyText: ({ underHeaderIcon }: { underHeaderIcon?: boolean }) => (
    <div data-testid="empty-text" data-under-header-icon={String(underHeaderIcon === true)} />
  ),
  AttachmentGroup: ({ attachments }: { attachments?: unknown[] }) => (
    <div data-testid="attachment-group" data-count={String(attachments?.length ?? 0)} />
  ),
  AgentUpdate: ({ currentAgentId }: { currentAgentId: string }) => (
    <div data-testid="post-steer-agent-update" data-agent-id={currentAgentId} />
  ),
}));

jest.mock('../MemoryArtifacts', () => ({
  __esModule: true,
  default: () => <div data-testid="memory-artifacts" />,
  hasMemoryArtifacts:
    jest.requireActual<typeof import('../MemoryArtifacts')>('../MemoryArtifacts')
      .hasMemoryArtifacts,
}));

jest.mock('../Parts/PendingSkillCall', () => ({
  __esModule: true,
  default: ({ skillName, loaded }: { skillName: string; loaded: boolean }) => (
    <div data-testid="pending-skill-call" data-skill={skillName} data-loaded={String(loaded)} />
  ),
}));

jest.mock('../Parts/WorkspaceChanges', () => ({
  __esModule: true,
  default: ({ attachments }: { attachments: TAttachment[] }) =>
    attachments.length > 0 ? (
      <div data-testid="workspace-changes" data-count={attachments.length} />
    ) : null,
  partitionWorkspaceChanges: (attachments?: TAttachment[]) => ({
    inlineAttachments: (attachments ?? []).filter((attachment) => !attachment.workspaceChange),
    workspaceChanges: (attachments ?? []).filter((attachment) => attachment.workspaceChange),
  }),
}));

jest.mock('../ToolCallGroup', () => ({
  __esModule: true,
  default: ({
    initialExpansionState,
    onExpansionChange,
    groupAttachments,
  }: {
    initialExpansionState?: { isExpanded: boolean; userOverride: boolean };
    onExpansionChange?: (state: { isExpanded: boolean; userOverride: boolean }) => void;
    groupAttachments?: TAttachment[];
  }) => (
    <button
      data-testid="tool-call-group"
      data-attachments={(groupAttachments ?? []).map((file) => file.filename).join(',')}
      data-initial-expanded={
        initialExpansionState == null ? 'unset' : String(initialExpansionState.isExpanded)
      }
      onClick={() => onExpansionChange?.({ isExpanded: false, userOverride: true })}
    />
  ),
}));

jest.mock('../ActivityPhaseGroup', () => ({
  __esModule: true,
  default: ({
    children,
    showCursor,
    animateEntrance,
  }: {
    children: React.ReactNode;
    showCursor?: boolean;
    animateEntrance?: boolean;
  }) => (
    <div
      data-testid="activity-phase-group"
      data-show-cursor={String(showCursor === true)}
      data-animate-entrance={String(animateEntrance === true)}
    >
      {children}
    </div>
  ),
}));

jest.mock('../Container', () => ({
  __esModule: true,
  default: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="container">{children}</div>
  ),
}));

jest.mock('../Part', () => {
  const { useMessageContext } = jest.requireActual<typeof import('~/Providers/MessageContext')>(
    '~/Providers/MessageContext',
  );
  const { SoleToolContext } =
    jest.requireActual<typeof import('@librechat/chat/react')>('@librechat/chat/react');
  const { useContext } = jest.requireActual<typeof import('react')>('react');
  return {
    __esModule: true,
    default: function MockPart({
      part,
      showCursor,
      isLast,
    }: {
      part: TMessageContentParts;
      showCursor?: boolean;
      isLast?: boolean;
    }) {
      const { partIndex } = useMessageContext();
      const soleTool = useContext(SoleToolContext);
      return (
        <div
          data-testid={`real-part-${part.type}`}
          data-index={partIndex}
          data-sole-tool={String(soleTool)}
          data-show-cursor={String(showCursor === true)}
          data-is-last={String(isLast === true)}
        />
      );
    },
  };
});

/** What the server answers when asked which subagent transcripts hold the files in doubt. */
let mockOwners: unknown;
const mockUseSubagentAttachmentCallsQueries = jest.fn(
  (params: { files: unknown[] }, enabled: boolean) =>
    params.files.length === 0 ? [] : [{ data: enabled ? mockOwners : undefined }],
);
jest.mock('~/data-provider', () => ({
  useSubagentAttachmentCallsQueries: (
    ...args: Parameters<typeof mockUseSubagentAttachmentCallsQueries>
  ) => mockUseSubagentAttachmentCallsQueries(...args),
}));

jest.mock('../ParallelContent', () => ({
  /** Invokes `renderResumeAttribution` per content index like the real
   *  renderer does for its sequential stretches, so the wiring is testable. */
  ParallelContentRenderer: ({
    content,
    contentIndexOffset = 0,
    renderResumeAttribution,
  }: {
    content?: Array<TMessageContentParts | undefined>;
    contentIndexOffset?: number;
    renderResumeAttribution?: (idx: number) => React.ReactNode;
  }) => (
    <div data-testid="parallel-renderer" data-index-offset={contentIndexOffset}>
      {content?.map((_, idx) => renderResumeAttribution?.(idx + contentIndexOffset))}
    </div>
  ),
}));

import ContentParts from '../ContentParts';

const baseProps = {
  messageId: 'msg-1',
  isLast: false,
  isSubmitting: false,
  isLatestMessage: false,
  isCreatedByUser: false,
  showThinking: false,
  content: [],
};

beforeEach(() => {
  jest
    .mocked(groupSequentialToolCalls)
    .mockImplementation((parts) => parts.map((part) => ({ type: 'single', part })));
});

describe('ContentParts: interim skill cards', () => {
  it('renders stateful workspace changes once at message level', () => {
    const content: TMessageContentParts[] = [
      { type: ContentTypes.TEXT, text: 'done' } as TMessageContentParts,
    ];
    const attachment = {
      filename: 'report.csv',
      filepath: '/uploads/report.csv',
      conversationId: 'conversation-1',
      messageId: 'msg-1',
      toolCallId: 'tool-1',
      workspaceChange: {
        profile: 'stateful',
        operation: 'created',
        path: 'report.csv',
      },
    } as TAttachment;

    render(<ContentParts {...baseProps} content={content} attachments={[attachment]} />);

    expect(screen.getAllByTestId('workspace-changes')).toHaveLength(1);
    expect(screen.getByTestId('workspace-changes')).toHaveAttribute('data-count', '1');
  });

  it('renders stateful workspace changes when the assistant message has no content yet', () => {
    const attachment = {
      filename: 'report.csv',
      filepath: '/uploads/report.csv',
      conversationId: 'conversation-1',
      messageId: 'msg-1',
      toolCallId: 'tool-1',
      workspaceChange: {
        profile: 'stateful',
        operation: 'created',
        path: 'report.csv',
      },
    } as TAttachment;

    render(<ContentParts {...baseProps} content={undefined} attachments={[attachment]} />);

    expect(screen.getByTestId('workspace-changes')).toHaveAttribute('data-count', '1');
  });

  it('renders a PendingSkillCall per manual skill on assistant messages', () => {
    render(<ContentParts {...baseProps} manualSkills={['brand-guidelines', 'pptx']} />);
    const cards = screen.getAllByTestId('pending-skill-call');
    expect(cards).toHaveLength(2);
    expect(cards[0]).toHaveAttribute('data-skill', 'brand-guidelines');
    expect(cards[1]).toHaveAttribute('data-skill', 'pptx');
  });

  it('starts pending skill cards in the not-loaded state (no real content yet)', () => {
    render(<ContentParts {...baseProps} manualSkills={['pptx']} />);
    expect(screen.getByTestId('pending-skill-call')).toHaveAttribute('data-loaded', 'false');
  });

  it('flips pending cards to loaded once any real content part arrives', () => {
    const content: TMessageContentParts[] = [
      { type: ContentTypes.TEXT, text: 'streamed' } as unknown as TMessageContentParts,
    ];
    render(<ContentParts {...baseProps} content={content} manualSkills={['pptx']} />);
    expect(screen.getByTestId('pending-skill-call')).toHaveAttribute('data-loaded', 'true');
  });

  it('does NOT render skill cards on user messages', () => {
    render(<ContentParts {...baseProps} isCreatedByUser manualSkills={['pptx']} />);
    expect(screen.queryByTestId('pending-skill-call')).toBeNull();
  });

  it('renders nothing when manualSkills is empty and content is undefined', () => {
    const { container } = render(
      <ContentParts {...baseProps} content={undefined} manualSkills={[]} />,
    );
    expect(container.firstChild).toBeNull();
  });

  it('renders pending skill cards even when content is undefined', () => {
    render(<ContentParts {...baseProps} content={undefined} manualSkills={['pptx']} />);
    expect(screen.getAllByTestId('pending-skill-call')).toHaveLength(1);
  });

  it('renders pending skill cards above parallel content', () => {
    /** Two agents, so the group renders as columns at all. */
    const parallelContent: TMessageContentParts[] = [
      {
        type: ContentTypes.TEXT,
        text: 'primary',
        agentId: 'agent_a',
        groupId: 1,
      } as unknown as TMessageContentParts,
      {
        type: ContentTypes.TEXT,
        text: 'added',
        agentId: 'agent_b____1',
        groupId: 1,
      } as unknown as TMessageContentParts,
    ];
    render(<ContentParts {...baseProps} content={parallelContent} manualSkills={['pptx']} />);
    const skillCard = screen.getByTestId('pending-skill-call');
    const parallelRenderer = screen.getByTestId('parallel-renderer');
    expect(skillCard).toBeTruthy();
    expect(parallelRenderer).toBeTruthy();
    expect(skillCard.compareDocumentPosition(parallelRenderer)).toBe(
      Node.DOCUMENT_POSITION_FOLLOWING,
    );
  });

  it('renders pending skill cards above sequential content', () => {
    const sequentialContent: TMessageContentParts[] = [
      { type: ContentTypes.TEXT, text: 'streamed' } as unknown as TMessageContentParts,
    ];
    render(<ContentParts {...baseProps} content={sequentialContent} manualSkills={['pptx']} />);
    const skillCard = screen.getByTestId('pending-skill-call');
    const textPart = screen.getByTestId(`real-part-${ContentTypes.TEXT}`);
    expect(skillCard).toBeTruthy();
    expect(textPart).toBeTruthy();
    expect(skillCard.compareDocumentPosition(textPart)).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
  });
});

describe('ContentParts: thinking-dot header alignment', () => {
  const submittingProps = { ...baseProps, isSubmitting: true, isLatestMessage: true };
  const memoryAttachment = {
    type: Tools.memory,
    [Tools.memory]: { type: 'update', key: 'user', value: 'test value' },
  } as TAttachment;

  it('nudges the cursor onto the header-icon axis when nothing precedes it', () => {
    render(<ContentParts {...submittingProps} />);
    expect(screen.getByTestId('empty-text')).toHaveAttribute('data-under-header-icon', 'true');
  });

  it('keeps the cursor flush when pending skill rows render above it', () => {
    render(<ContentParts {...submittingProps} manualSkills={['pptx']} />);
    expect(screen.getByTestId('empty-text')).toHaveAttribute('data-under-header-icon', 'false');
  });

  it('keeps the cursor flush when a memory-artifact row renders above it', () => {
    render(<ContentParts {...submittingProps} attachments={[memoryAttachment]} />);
    expect(screen.getByTestId('empty-text')).toHaveAttribute('data-under-header-icon', 'false');
  });

  it('keeps the cursor flush inside a nested activity phase', () => {
    render(<ContentParts {...submittingProps} nestedActivityPhase />);
    expect(screen.getByTestId('empty-text')).toHaveAttribute('data-under-header-icon', 'false');
  });

  it('routes a solitary seeded empty text part through the nudged placeholder', () => {
    const seeded = [{ type: ContentTypes.TEXT, text: { value: '' } }] as TMessageContentParts[];
    render(<ContentParts {...submittingProps} content={seeded} />);
    expect(screen.getByTestId('empty-text')).toHaveAttribute('data-under-header-icon', 'true');
    expect(screen.queryByTestId('real-part-text')).toBeNull();
  });

  it('keeps the gate for a seeded placeholder behind pending skill rows', () => {
    const seeded = [{ type: ContentTypes.TEXT, text: '' }] as TMessageContentParts[];
    render(<ContentParts {...submittingProps} content={seeded} manualSkills={['pptx']} />);
    expect(screen.getByTestId('empty-text')).toHaveAttribute('data-under-header-icon', 'false');
  });

  it('renders a persisted empty text part normally once submission ends', () => {
    const seeded = [{ type: ContentTypes.TEXT, text: { value: '' } }] as TMessageContentParts[];
    render(<ContentParts {...baseProps} content={seeded} />);
    expect(screen.queryByTestId('empty-text')).toBeNull();
    expect(screen.getByTestId('real-part-text')).toBeInTheDocument();
  });

  it('leaves real text content out of the placeholder path', () => {
    const textContent = [{ type: ContentTypes.TEXT, text: 'hello' }] as TMessageContentParts[];
    render(<ContentParts {...submittingProps} content={textContent} />);
    expect(screen.queryByTestId('empty-text')).toBeNull();
    expect(screen.getByTestId('real-part-text')).toBeInTheDocument();
  });
});

describe('ContentParts: post-steer author re-attribution', () => {
  const steerPart = {
    type: ContentTypes.STEER,
    steer: 'go left',
  } as unknown as TMessageContentParts;
  const textPart = (text: string) =>
    ({ type: ContentTypes.TEXT, text }) as unknown as TMessageContentParts;
  const header = <div data-testid="author-header" />;

  it('re-renders the author header between a steer and the content that resumes after it', () => {
    render(
      <ContentParts
        {...baseProps}
        content={[textPart('a'), steerPart, textPart('b')]}
        authorHeader={header}
      />,
    );
    const headers = screen.getAllByTestId('author-header');
    expect(headers).toHaveLength(1);
    const steer = screen.getByTestId(`real-part-${ContentTypes.STEER}`);
    const textParts = screen.getAllByTestId(`real-part-${ContentTypes.TEXT}`);
    expect(steer.compareDocumentPosition(headers[0])).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
    expect(headers[0].compareDocumentPosition(textParts[1])).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
  });

  it('renders one header per steer block and none after a trailing steer', () => {
    render(
      <ContentParts
        {...baseProps}
        content={[textPart('a'), steerPart, steerPart, textPart('b'), steerPart]}
        authorHeader={header}
      />,
    );
    expect(screen.getAllByTestId('author-header')).toHaveLength(1);
  });

  it('skips empty content slots when finding the part that resumes after a steer', () => {
    render(
      <ContentParts
        {...baseProps}
        content={[steerPart, undefined, textPart('b')]}
        authorHeader={header}
      />,
    );
    expect(screen.getAllByTestId('author-header')).toHaveLength(1);
  });

  it('renders no header when authorHeader is not provided', () => {
    render(<ContentParts {...baseProps} content={[textPart('a'), steerPart, textPart('b')]} />);
    expect(screen.queryByTestId('author-header')).toBeNull();
  });

  it('re-attributes to the ACTIVE agent when a handoff preceded the steer', () => {
    const agentUpdate = {
      type: ContentTypes.AGENT_UPDATE,
      [ContentTypes.AGENT_UPDATE]: { agentId: 'agent_b', index: 1 },
    } as unknown as TMessageContentParts;
    render(
      <ContentParts
        {...baseProps}
        content={[textPart('a'), agentUpdate, textPart('b'), steerPart, textPart('c')]}
        authorHeader={header}
      />,
    );
    const marker = screen.getAllByTestId('post-steer-agent-update');
    expect(marker).toHaveLength(1);
    expect(marker[0]).toHaveAttribute('data-agent-id', 'agent_b');
    expect(screen.queryByTestId('author-header')).toBeNull();
  });

  it('provides resume attribution to the parallel renderer for its sequential stretches', () => {
    const laneText = (agentId: string) =>
      ({
        type: ContentTypes.TEXT,
        text: `column ${agentId}`,
        agentId,
        groupId: 1,
      }) as unknown as TMessageContentParts;
    render(
      <ContentParts
        {...baseProps}
        content={[laneText('agent_a'), laneText('agent_b____1'), steerPart, textPart('resumed')]}
        authorHeader={header}
      />,
    );
    const renderer = screen.getByTestId('parallel-renderer');
    expect(renderer).toBeTruthy();
    expect(screen.getAllByTestId('author-header')).toHaveLength(1);
  });

  it('keeps the top-level header when the handoff comes AFTER the steer', () => {
    const agentUpdate = {
      type: ContentTypes.AGENT_UPDATE,
      [ContentTypes.AGENT_UPDATE]: { agentId: 'agent_b', index: 2 },
    } as unknown as TMessageContentParts;
    render(
      <ContentParts
        {...baseProps}
        content={[textPart('a'), steerPart, agentUpdate, textPart('b')]}
        authorHeader={header}
      />,
    );
    expect(screen.getAllByTestId('author-header')).toHaveLength(1);
    expect(screen.queryByTestId('post-steer-agent-update')).toBeNull();
  });

  it('retains steer attribution when resumed content moves into a phase slice', () => {
    const phase = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: 'Reconciled both release paths',
      activity_label_type: 'phase',
      activity_start_index: 2,
      activity_count: 2,
      pending: false,
    } as unknown as TMessageContentParts;
    render(
      <ContentParts
        {...baseProps}
        content={[textPart('a'), steerPart, textPart('resumed'), phase]}
        authorHeader={header}
      />,
    );

    expect(screen.getByTestId('activity-phase-group')).toBeTruthy();
    expect(screen.getAllByTestId('author-header')).toHaveLength(1);
    expect(screen.getAllByTestId(`real-part-${ContentTypes.TEXT}`)[1]).toHaveAttribute(
      'data-index',
      '2',
    );
  });
});

describe('ContentParts — activity phase state', () => {
  it('keeps a streaming cursor on visible text when a provider appends an empty placeholder', () => {
    render(
      <ContentParts
        {...baseProps}
        content={[
          { type: ContentTypes.TEXT, text: 'Visible answer' } as TMessageContentParts,
          { type: ContentTypes.TEXT, text: '' } as TMessageContentParts,
        ]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    const textParts = screen.getAllByTestId(`real-part-${ContentTypes.TEXT}`);
    expect(textParts[0]).toHaveAttribute('data-show-cursor', 'true');
    expect(textParts[1]).toHaveAttribute('data-show-cursor', 'false');
  });

  /** Activity phases split one response into several bodies, and every settled
   *  body has a trailing part. Only the body holding the message's cursor may
   *  own a live one — otherwise a phase that finished minutes ago keeps its
   *  reasoning shimmering while later phases stream. */
  it("leaves an earlier phase's trailing part settled while a later part streams", () => {
    const think = {
      type: ContentTypes.THINK,
      think: 'weighing the options',
    } as unknown as TMessageContentParts;
    const phase = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: 'Mapped the schema',
      activity_label_type: 'phase',
      activity_start_index: 0,
      activity_end_index: 1,
      activity_count: 1,
      pending: false,
    } as unknown as TMessageContentParts;
    render(
      <ContentParts
        {...baseProps}
        content={[
          think,
          phase,
          { type: ContentTypes.TEXT, text: 'Good — schema mapped.' } as TMessageContentParts,
        ]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    expect(screen.getByTestId(`real-part-${ContentTypes.THINK}`)).toHaveAttribute(
      'data-is-last',
      'false',
    );
    expect(screen.getByTestId(`real-part-${ContentTypes.TEXT}`)).toHaveAttribute(
      'data-is-last',
      'true',
    );
  });

  it('renders a completion-appended parent before the final root text', () => {
    const tool = {
      type: ContentTypes.TOOL_CALL,
      [ContentTypes.TOOL_CALL]: { id: 'tool-1', name: 'search', args: {}, output: 'done' },
    } as unknown as TMessageContentParts;
    const final = {
      type: ContentTypes.TEXT,
      text: 'Final answer',
    } as unknown as TMessageContentParts;
    const phase = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: 'Completed the full investigation',
      activity_label_type: 'phase',
      activity_start_index: 0,
      activity_end_index: 2,
      activity_count: 2,
      pending: false,
    } as unknown as TMessageContentParts;

    render(<ContentParts {...baseProps} content={[tool, tool, final, phase]} />);

    const parent = screen.getByTestId('activity-phase-group');
    const finalPart = screen.getByTestId(`real-part-${ContentTypes.TEXT}`);
    expect(parent.compareDocumentPosition(finalPart)).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
    expect(parent).toHaveAttribute('data-animate-entrance', 'false');
    expect(finalPart).toHaveAttribute('data-index', '2');
  });

  it('opens the only tool call inside a phase, but not one of several', () => {
    const tool = (id: string) =>
      ({
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id, name: 'bash_tool', args: {}, output: 'ok' },
      }) as unknown as TMessageContentParts;
    const phase = (count: number) =>
      ({
        type: ContentTypes.ACTIVITY_LABEL,
        [ContentTypes.ACTIVITY_LABEL]: 'Ran the setup',
        activity_label_type: 'phase',
        activity_start_index: 0,
        activity_end_index: count,
        activity_count: count,
        pending: false,
      }) as unknown as TMessageContentParts;

    const { unmount } = render(
      <ContentParts {...baseProps} content={[tool('tool-1'), phase(1)]} />,
    );
    expect(screen.getByTestId(`real-part-${ContentTypes.TOOL_CALL}`)).toHaveAttribute(
      'data-sole-tool',
      'true',
    );
    unmount();

    render(<ContentParts {...baseProps} content={[tool('tool-1'), tool('tool-2'), phase(2)]} />);
    for (const part of screen.getAllByTestId(`real-part-${ContentTypes.TOOL_CALL}`)) {
      expect(part).toHaveAttribute('data-sole-tool', 'false');
    }
  });

  it('keeps a streaming cursor when a completed phase marker is the visible tail', () => {
    const phase = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: 'Compared both search results',
      activity_label_type: 'phase',
      activity_start_index: 0,
      activity_count: 2,
      pending: false,
    } as unknown as TMessageContentParts;

    render(
      <ContentParts
        {...baseProps}
        content={[
          { type: ContentTypes.TEXT, text: 'working' } as unknown as TMessageContentParts,
          phase,
        ]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    expect(screen.getByTestId('activity-phase-group')).toHaveAttribute('data-show-cursor', 'true');
  });

  it('carries the absolute index offset into a parallel phase segment', () => {
    const phase = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: 'Compared both agent results',
      activity_label_type: 'phase',
      activity_start_index: 1,
      activity_count: 2,
      pending: false,
    } as unknown as TMessageContentParts;
    const lane = (agentId: string) =>
      ({
        type: ContentTypes.TEXT,
        text: `lane result ${agentId}`,
        agentId,
        groupId: 1,
      }) as unknown as TMessageContentParts;

    render(
      <ContentParts
        {...baseProps}
        content={[
          { type: ContentTypes.TEXT, text: 'before' } as unknown as TMessageContentParts,
          lane('agent_a'),
          lane('agent_b____1'),
          phase,
        ]}
      />,
    );

    expect(screen.getByTestId('parallel-renderer')).toHaveAttribute('data-index-offset', '1');
  });

  it('hoists files a tool saved inside a grouped subagent run onto the group', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const childImage = (subagent_content?: unknown[]) =>
      [
        {
          type: ContentTypes.TOOL_CALL,
          [ContentTypes.TOOL_CALL]: {
            id: 'subagent-1',
            name: 'subagent',
            args: {},
            output: 'done',
            subagent_content,
          },
        },
        {
          type: ContentTypes.TOOL_CALL,
          [ContentTypes.TOOL_CALL]: { id: 'tool-2', name: 'search', args: {}, output: 'two' },
        },
      ] as unknown as TMessageContentParts[];
    const attachments = [
      {
        filename: 'cat.png',
        filepath: '/images/cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'child-image',
      },
    ] as TAttachment[];
    const nested = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'child-image', name: 'image_gen_oai', args: {} },
      },
    ];

    const { rerender } = render(
      <ContentParts {...baseProps} content={childImage(nested)} attachments={attachments} />,
    );
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute('data-attachments', 'cat.png');

    rerender(<ContentParts {...baseProps} content={childImage()} attachments={attachments} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute('data-attachments', '');
  });

  it('hoists a grouped subagent file whose transcript the server left out of the preview', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: {
          id: 'subagent-previewed',
          name: 'subagent',
          args: {},
          output: 'done',
          progress: 1,
          subagentContentOmitted: true,
          subagentContentParts: 3,
          subagentAttachmentCalls: [{ id: 'omitted-image' }],
        },
      },
    ] as unknown as TMessageContentParts[];
    const attachments = [
      {
        filename: 'previewed-cat.png',
        filepath: '/images/previewed-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'omitted-image',
      },
    ] as TAttachment[];

    render(<ContentParts {...baseProps} content={content} attachments={attachments} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'previewed-cat.png',
    );
  });

  const previewedSubagent = (id: string, extra: Record<string, unknown> = {}) => ({
    type: ContentTypes.TOOL_CALL,
    [ContentTypes.TOOL_CALL]: {
      id,
      name: 'subagent',
      args: {},
      output: 'done',
      progress: 1,
      subagentContentOmitted: true,
      subagentContentParts: 1,
      ...extra,
    },
  });
  const lateFile = (toolCallId: string, filename: string, agentId?: string) =>
    ({
      file_id: `${filename}-id`,
      filename,
      filepath: `/images/${filename}`,
      conversationId: 'conversation-1',
      messageId: 'msg-1',
      toolCallId,
      ...(agentId != null ? { agentId } : {}),
    }) as TAttachment;

  it('asks the server which transcript holds a file a previewed subagent run delivered late', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [previewedSubagent('subagent-reloaded')] as unknown as TMessageContentParts[];
    mockOwners = {
      parts: [{ toolCallId: 'subagent-reloaded', calls: [{ id: 'background-child' }] }],
    };
    const props = { ...baseProps, conversationId: 'conversation-1', content };

    const { rerender } = render(<ContentParts {...props} attachments={[]} />);
    expect(mockUseSubagentAttachmentCallsQueries).toHaveBeenLastCalledWith(
      { conversationId: 'conversation-1', messageId: 'msg-1', files: [] },
      false,
    );
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute('data-attachments', '');

    rerender(
      <ContentParts {...props} attachments={[lateFile('background-child', 'late-chart.png')]} />,
    );
    expect(mockUseSubagentAttachmentCallsQueries).toHaveBeenLastCalledWith(
      {
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        files: [{ toolCallId: 'background-child' }],
      },
      true,
    );
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'late-chart.png',
    );
    mockOwners = undefined;
  });

  it('asks for a scoped late file an unscoped call reusing its id took', () => {
    jest
      .mocked(mapAttachments)
      .mockImplementation(
        jest.requireActual<typeof import('~/utils/map')>('~/utils/map').mapAttachments,
      );
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => parts.map((part) => ({ type: 'tool-group', parts: [part] })));
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'call_0', name: 'search', args: {}, output: 'top' },
      },
      previewedSubagent('subagent-reloaded'),
    ] as unknown as TMessageContentParts[];
    mockOwners = {
      parts: [
        { toolCallId: 'subagent-reloaded', calls: [{ id: 'call_0', agentId: 'child-agent' }] },
      ],
    };

    render(
      <ContentParts
        {...baseProps}
        conversationId="conversation-1"
        content={content}
        attachments={[lateFile('call_0', 'child-chart.png', 'child-agent')]}
      />,
    );
    expect(mockUseSubagentAttachmentCallsQueries).toHaveBeenCalledWith(
      expect.objectContaining({ files: [{ toolCallId: 'call_0', agentId: 'child-agent' }] }),
      true,
    );
    const [topLevel, subagentGroup] = screen.getAllByTestId('tool-call-group');
    expect(topLevel).toHaveAttribute('data-attachments', '');
    expect(subagentGroup).toHaveAttribute('data-attachments', 'child-chart.png');
    mockOwners = undefined;
  });

  it('asks for a scoped late file only an unscoped hint of another subagent matched', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => parts.map((part) => ({ type: 'tool-group', parts: [part] })));
    const content = [
      previewedSubagent('subagent-a', { subagentAttachmentCalls: [{ id: 'shared' }] }),
      previewedSubagent('subagent-b'),
    ] as unknown as TMessageContentParts[];
    mockOwners = {
      parts: [
        { toolCallId: 'subagent-a', calls: [{ id: 'shared' }] },
        { toolCallId: 'subagent-b', calls: [{ id: 'shared', agentId: 'agent-b' }] },
      ],
    };

    render(
      <ContentParts
        {...baseProps}
        conversationId="conversation-1"
        content={content}
        attachments={[lateFile('shared', 'b-chart.png', 'agent-b')]}
      />,
    );
    const [first, second] = screen.getAllByTestId('tool-call-group');
    expect(first).toHaveAttribute('data-attachments', '');
    expect(second).toHaveAttribute('data-attachments', 'b-chart.png');
    mockOwners = undefined;
  });

  it('hoists files from a grouped subagent run that is still streaming', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-live', name: 'subagent', args: {} },
      },
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'tool-2', name: 'search', args: {}, output: 'two' },
      },
    ] as unknown as TMessageContentParts[];
    const attachments = [
      {
        filename: 'live-cat.png',
        filepath: '/images/live-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'live-child-image',
      },
    ] as TAttachment[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-live', 0),
    );

    const streaming = { ...baseProps, isSubmitting: true, isLatestMessage: true };
    const { rerender } = render(
      <ContentParts {...streaming} content={content} attachments={attachments} />,
    );
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute('data-attachments', '');
    /** A streamed delta replaces the content array before the first progress update. */
    rerender(<ContentParts {...streaming} content={[...content]} attachments={attachments} />);

    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [
          {
            type: ContentTypes.TOOL_CALL,
            [ContentTypes.TOOL_CALL]: { id: 'live-child-image', name: 'image_gen_oai', args: '' },
          },
        ],
      } as unknown as SubagentProgress);
    });
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'live-cat.png',
    );

    /** The bounded live activity evicts early parts as later ones arrive. */
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [{ type: ContentTypes.TEXT, text: 'later output' }],
      } as unknown as SubagentProgress);
    });
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'live-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('keeps a live child file routed when the message remounts after eviction', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-remount', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const attachments = [
      {
        filename: 'remount-cat.png',
        filepath: '/images/remount-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'remount-child',
      },
    ] as TAttachment[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-remount', 0),
    );
    const streaming = { ...baseProps, isSubmitting: true, isLatestMessage: true };
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [
          {
            type: ContentTypes.TOOL_CALL,
            [ContentTypes.TOOL_CALL]: { id: 'remount-child', name: 'image_gen_oai', args: '' },
          },
        ],
      } as unknown as SubagentProgress);
    });
    const first = render(
      <ContentParts {...streaming} content={content} attachments={attachments} />,
    );
    first.unmount();
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [{ type: ContentTypes.TEXT, text: 'later output' }],
      } as unknown as SubagentProgress);
    });

    render(<ContentParts {...streaming} content={content} attachments={attachments} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'remount-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('hoists a file a settled message receives late through its subagent progress', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: {
          id: 'subagent-settled',
          name: 'subagent',
          args: {},
          output: 'done',
          progress: 1,
          subagentContentOmitted: true,
        },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-settled', 0),
    );
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [
          {
            type: ContentTypes.TOOL_CALL,
            [ContentTypes.TOOL_CALL]: { id: 'late-child', name: 'bash_tool', args: '' },
          },
        ],
      } as unknown as SubagentProgress);
    });
    const late = [
      {
        filename: 'late-report.csv',
        filepath: '/files/late-report.csv',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'late-child',
      },
    ] as TAttachment[];

    render(<ContentParts {...baseProps} content={content} attachments={late} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'late-report.csv',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('routes a live child file whose call was evicted before the message mounted', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-unread', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-unread', 0),
    );
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [
          {
            type: ContentTypes.TOOL_CALL,
            agentId: 'agent-unread',
            [ContentTypes.TOOL_CALL]: { id: 'unread-child', name: 'image_gen_oai', args: '' },
          },
        ],
      } as unknown as SubagentProgress);
      getDefaultStore().set(progressAtom, (previous) => ({
        ...(previous as SubagentProgress),
        contentParts: [{ type: ContentTypes.TEXT, text: 'later output' }],
      }));
    });
    const files = [
      {
        filename: 'unread-cat.png',
        filepath: '/images/unread-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'unread-child',
        agentId: 'agent-unread',
      },
    ] as TAttachment[];

    render(<ContentParts {...baseProps} content={content} attachments={files} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'unread-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('routes a live child file when one batch shows and evicts its call while mounted', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-batched', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-batched', 0),
    );
    const files = [
      {
        filename: 'batched-cat.png',
        filepath: '/images/batched-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'batched-child',
      },
    ] as TAttachment[];
    const streaming = { ...baseProps, isSubmitting: true, isLatestMessage: true };
    const { rerender } = render(<ContentParts {...streaming} content={content} />);
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [
          {
            type: ContentTypes.TOOL_CALL,
            [ContentTypes.TOOL_CALL]: { id: 'batched-child', name: 'image_gen_oai', args: '' },
          },
        ],
      } as unknown as SubagentProgress);
      getDefaultStore().set(progressAtom, (previous) => ({
        ...(previous as SubagentProgress),
        contentParts: [{ type: ContentTypes.TEXT, text: 'later output' }],
      }));
    });
    rerender(<ContentParts {...streaming} content={content} attachments={files} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'batched-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('routes a child file whose call a single bounded batch evicted', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-batch', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-batch', 0),
    );
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [{ type: ContentTypes.TEXT, text: 'after the bound' }],
        aggregatorState: { evictedToolCalls: [{ id: 'batch-evicted' }] },
      } as unknown as SubagentProgress);
    });
    const files = [
      {
        filename: 'batch-cat.png',
        filepath: '/images/batch-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'batch-evicted',
      },
    ] as TAttachment[];

    render(<ContentParts {...baseProps} content={content} attachments={files} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'batch-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('keeps the owner of a shown file past the bounded call history', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-long', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-long', 0),
    );
    const call = (id: string) => ({
      type: ContentTypes.TOOL_CALL,
      [ContentTypes.TOOL_CALL]: { id, name: 'bash_tool', args: '' },
    });
    const files = [
      {
        filename: 'early-cat.png',
        filepath: '/images/early-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'early-image',
      },
    ] as TAttachment[];
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [call('early-image')],
      } as unknown as SubagentProgress);
    });
    render(<ContentParts {...baseProps} content={content} attachments={files} />);
    for (let batch = 0; batch < 6; batch++) {
      act(() => {
        getDefaultStore().set(progressAtom, {
          contentParts: Array.from({ length: 100 }, (_, index) => call(`later-${batch}-${index}`)),
        } as unknown as SubagentProgress);
      });
    }
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'early-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('keeps a shown file owner through reduced progress that replays its eviction list', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-reduced', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-reduced', 0),
    );
    const runStep = (id: string, sequence: number) =>
      ({
        runId: 'parent-run',
        subagentRunId: 'child-run',
        subagentType: 'self',
        phase: 'run_step',
        activityEventId: `reduced-${sequence}`,
        data: { stepDetails: { type: 'tool_calls', tool_calls: [{ id, name: 'bash_tool' }] } },
        timestamp: '',
      }) as unknown as SubagentUpdateEvent;
    const files = [
      {
        filename: 'reduced-cat.png',
        filepath: '/images/reduced-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'reduced-image',
      },
    ] as TAttachment[];
    act(() => {
      getDefaultStore().set(progressAtom, (previous) =>
        reduceSubagentProgress(previous, [runStep('reduced-image', 0)]),
      );
    });
    render(<ContentParts {...baseProps} content={content} attachments={files} />);
    for (let index = 1; index <= 600; index++) {
      act(() => {
        getDefaultStore().set(progressAtom, (previous) =>
          reduceSubagentProgress(previous, [runStep(`reduced-later-${index}`, index)]),
        );
      });
    }
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'reduced-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('routes a file from a nested subagent run the live activity evicted', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-outer', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-outer', 0),
    );
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [
          {
            type: ContentTypes.TOOL_CALL,
            [ContentTypes.TOOL_CALL]: {
              id: 'inner-subagent',
              name: 'subagent',
              args: '',
              subagent_content: [
                {
                  type: ContentTypes.TOOL_CALL,
                  [ContentTypes.TOOL_CALL]: { id: 'inner-image', name: 'image_gen_oai' },
                },
              ],
            },
          },
        ],
      } as unknown as SubagentProgress);
      getDefaultStore().set(progressAtom, (previous) => ({
        ...(previous as SubagentProgress),
        contentParts: [{ type: ContentTypes.TEXT, text: 'later output' }],
      }));
    });
    const files = [
      {
        filename: 'inner-cat.png',
        filepath: '/images/inner-cat.png',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'inner-image',
      },
    ] as TAttachment[];

    render(<ContentParts {...baseProps} content={content} attachments={files} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'inner-cat.png',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('reads live progress under the index a compacted part streamed at', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => [{ type: 'tool-group', parts }]);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        streamedIndex: 1,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-compacted', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const progressAtom = subagentProgressByToolCallId(
      subagentProgressKey('msg-1', 'subagent-compacted', 1),
    );
    act(() => {
      getDefaultStore().set(progressAtom, {
        contentParts: [
          {
            type: ContentTypes.TOOL_CALL,
            [ContentTypes.TOOL_CALL]: { id: 'compacted-child', name: 'bash_tool', args: '' },
          },
        ],
      } as unknown as SubagentProgress);
    });
    const files = [
      {
        filename: 'compacted.csv',
        filepath: '/files/compacted.csv',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'compacted-child',
      },
    ] as TAttachment[];

    render(<ContentParts {...baseProps} content={content} attachments={files} />);
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute(
      'data-attachments',
      'compacted.csv',
    );
    act(() => getDefaultStore().set(progressAtom, null));
  });

  it('keeps a scoped child file off an unscoped top-level call that reuses its id', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => parts.map((part) => ({ type: 'tool-group', parts: [part] })));
    jest.mocked(mapAttachments).mockImplementation((files) => {
      const map: Record<string, TAttachment[] | undefined> = {};
      for (const file of files) {
        if (file?.toolCallId != null) {
          (map[file.toolCallId] ??= []).push(file);
        }
      }
      return map;
    });
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: {
          id: 'subagent-owner',
          name: 'subagent',
          args: {},
          output: 'done',
          subagent_content: [
            {
              type: ContentTypes.TOOL_CALL,
              [ContentTypes.TOOL_CALL]: {
                id: 'reused-id',
                name: 'bash_tool',
                stepId: 'step-child',
              },
            },
          ],
        },
      },
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'reused-id', name: 'search', args: {}, output: 'x' },
      },
    ] as unknown as TMessageContentParts[];
    const files = [
      {
        filename: 'child.csv',
        filepath: '/files/child.csv',
        conversationId: 'conversation-1',
        messageId: 'msg-1',
        toolCallId: 'reused-id',
        stepId: 'step-child',
      },
    ] as TAttachment[];

    render(<ContentParts {...baseProps} content={content} attachments={files} />);
    const [subagentGroup, topLevelGroup] = screen.getAllByTestId('tool-call-group');
    expect(subagentGroup).toHaveAttribute('data-attachments', 'child.csv');
    expect(topLevelGroup).toHaveAttribute('data-attachments', '');
    jest.mocked(mapAttachments).mockImplementation(() => ({}));
  });

  it('frees the progress member of a streaming subagent no update reached', () => {
    jest
      .mocked(groupSequentialToolCalls)
      .mockImplementation((parts) => parts.map((part) => ({ type: 'single', part })));
    const key = subagentProgressKey('msg-1', 'subagent-silent', 0);
    const content = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'subagent-silent', name: 'subagent', args: {} },
      },
    ] as unknown as TMessageContentParts[];
    const streaming = { ...baseProps, isSubmitting: true, isLatestMessage: true };
    const { unmount } = render(<ContentParts {...streaming} content={content} />);
    expect([...subagentProgressByToolCallId.getParams()]).toContain(key);
    unmount();
    expect([...subagentProgressByToolCallId.getParams()]).not.toContain(key);
  });

  it('retains a tool-group expansion override when a completed phase remounts the group', () => {
    jest.mocked(groupSequentialToolCalls).mockImplementation((parts) => {
      const toolParts = parts.filter(({ part }) => part.type === ContentTypes.TOOL_CALL);
      if (toolParts.length > 0) {
        return [{ type: 'tool-group', parts: toolParts }];
      }
      return parts.map((part) => ({ type: 'single', part }));
    });
    const tools = [
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'tool-1', name: 'search', args: {}, output: 'one' },
      },
      {
        type: ContentTypes.TOOL_CALL,
        [ContentTypes.TOOL_CALL]: { id: 'tool-2', name: 'search', args: {}, output: 'two' },
      },
    ] as unknown as TMessageContentParts[];
    const pendingPhase = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: '',
      activity_label_type: 'phase',
      activity_start_index: 0,
      activity_count: 2,
      pending: true,
    } as unknown as TMessageContentParts;
    const { rerender } = render(<ContentParts {...baseProps} content={[...tools, pendingPhase]} />);

    const pendingGroup = screen.getByTestId('tool-call-group');
    expect(pendingGroup).toHaveAttribute('data-initial-expanded', 'unset');
    fireEvent.click(pendingGroup);

    const completedPhase = {
      ...pendingPhase,
      [ContentTypes.ACTIVITY_LABEL]: 'Compared both search results',
      pending: false,
    } as unknown as TMessageContentParts;
    rerender(<ContentParts {...baseProps} content={[...tools, completedPhase]} />);

    expect(screen.getByTestId('activity-phase-group')).toBeInTheDocument();
    expect(screen.getByTestId('activity-phase-group')).toHaveAttribute(
      'data-animate-entrance',
      'true',
    );
    expect(screen.getByTestId('tool-call-group')).toHaveAttribute('data-initial-expanded', 'false');
  });

  test('does not replay the entrance when switching to a sibling with its own phases', () => {
    const phase = (label: string) =>
      ({
        type: ContentTypes.ACTIVITY_LABEL,
        [ContentTypes.ACTIVITY_LABEL]: label,
        activity_label_type: 'phase',
        activity_start_index: 0,
        activity_count: 1,
        pending: false,
      }) as unknown as TMessageContentParts;

    const tool = {
      type: ContentTypes.TOOL_CALL,
      [ContentTypes.TOOL_CALL]: { id: 'tool-1', name: 'search', args: {}, output: 'one' },
    } as unknown as TMessageContentParts;

    const { rerender } = render(
      <ContentParts {...baseProps} messageId="sibling-a" content={[tool, phase('First')]} />,
    );
    expect(screen.getByTestId('activity-phase-group')).toHaveAttribute(
      'data-animate-entrance',
      'false',
    );

    /** MultiMessage swaps siblings without a key, so this instance keeps its
     *  refs while messageId and content change wholesale. The incoming
     *  sibling's phase is history and must not animate. */
    rerender(
      <ContentParts {...baseProps} messageId="sibling-b" content={[tool, tool, phase('Second')]} />,
    );

    expect(screen.getByTestId('activity-phase-group')).toHaveAttribute(
      'data-animate-entrance',
      'false',
    );
  });
});

describe('ContentParts: settled content identity across compaction', () => {
  /** Mirrors a captured run: the aggregator leaves holes at the source indexes
   *  of steps that produced nothing, and `finalHandler` swaps in the server's
   *  compacted array. Without the streamed-index stamp every index-derived key
   *  shifts and the settled message remounts wholesale. */
  const toolPart = {
    type: ContentTypes.TOOL_CALL,
    [ContentTypes.TOOL_CALL]: { id: 'call_a', name: 'search', args: {}, output: 'one' },
  } as unknown as TMessageContentParts;
  const batchLabel = {
    type: ContentTypes.ACTIVITY_LABEL,
    [ContentTypes.ACTIVITY_LABEL]: 'Recorded the fact',
    tool_call_ids: ['call_a'],
  } as unknown as TMessageContentParts;
  const answer = { type: ContentTypes.TEXT, text: 'done' } as unknown as TMessageContentParts;
  const phaseLabel = (bounds: { start: number; end: number }) =>
    ({
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: 'Researched the question',
      activity_label_type: 'phase',
      activity_start_index: bounds.start,
      activity_end_index: bounds.end,
      activity_count: 1,
      pending: false,
    }) as unknown as TMessageContentParts;

  const streamed: Array<TMessageContentParts | undefined> = [
    undefined,
    toolPart,
    batchLabel,
    undefined,
    answer,
    phaseLabel({ start: 1, end: 4 }),
  ];
  const compacted = [toolPart, batchLabel, answer, phaseLabel({ start: 0, end: 2 })];

  const renderStreaming = () =>
    render(<ContentParts {...baseProps} content={streamed} isLast isSubmitting isLatestMessage />);

  it('keeps every part and the phase group mounted when the final content is stamped', () => {
    const { rerender } = renderStreaming();
    const phaseNode = screen.getByTestId('activity-phase-group');
    const toolNode = screen.getByTestId('real-part-tool_call');
    const textNode = screen.getByTestId('real-part-text');

    const finalContent = preserveStreamedContentIdentity(streamed, compacted);
    rerender(<ContentParts {...baseProps} content={finalContent} isLast />);

    expect(screen.getByTestId('activity-phase-group')).toBe(phaseNode);
    expect(screen.getByTestId('real-part-tool_call')).toBe(toolNode);
    expect(screen.getByTestId('real-part-text')).toBe(textNode);
    expect(phaseNode).toHaveAttribute('data-animate-entrance', 'false');
  });

  /** When the stamp cannot pair the two arrays every index-derived key
   *  shifts and the phase group remounts — but its label text was already on
   *  screen, so the remounted card must mount settled instead of replaying
   *  the fold over content the reader already watched fold. */
  it('remounts without replaying the phase entrance when the settle re-keys the content', () => {
    const { rerender } = renderStreaming();
    const phaseNode = screen.getByTestId('activity-phase-group');

    rerender(<ContentParts {...baseProps} content={compacted} isLast />);

    const settledPhase = screen.getByTestId('activity-phase-group');
    expect(settledPhase).not.toBe(phaseNode);
    expect(settledPhase).toHaveAttribute('data-animate-entrance', 'false');
  });

  /** Identical summaries are legitimate across phases of one run. A second
   *  marker with an already-seen text is a grown occurrence count, not a
   *  re-key of the first — only the newcomer animates. */
  it('animates a second phase that repeats an earlier label text', () => {
    const repeatedPhase = (index: number, bounds: { start: number; end: number }) =>
      ({
        type: ContentTypes.ACTIVITY_LABEL,
        [ContentTypes.ACTIVITY_LABEL]: 'Completed the activity phase',
        activity_label_type: 'phase',
        activity_start_index: bounds.start,
        activity_end_index: bounds.end,
        activity_count: 1,
        pending: false,
        streamedIndex: index,
      }) as unknown as TMessageContentParts;
    const { rerender } = render(
      <ContentParts
        {...baseProps}
        content={[toolPart, repeatedPhase(1, { start: 0, end: 1 })]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    rerender(
      <ContentParts
        {...baseProps}
        content={[
          toolPart,
          repeatedPhase(1, { start: 0, end: 1 }),
          toolPart,
          repeatedPhase(3, { start: 2, end: 3 }),
        ]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    const phases = screen.getAllByTestId('activity-phase-group');
    expect(phases).toHaveLength(2);
    expect(phases[0]).toHaveAttribute('data-animate-entrance', 'false');
    expect(phases[1]).toHaveAttribute('data-animate-entrance', 'true');
  });

  /** Concurrent fills can resolve out of order: a later-index marker
   *  renders first, then an earlier reserved marker fills with an identical
   *  summary. No previously rendered key vanished, so key identity stays
   *  authoritative and the newly filled earlier marker still animates. */
  it('animates an earlier marker that fills out of order behind a same-text twin', () => {
    const twinPhase = (bounds: { start: number; end: number }) =>
      ({
        type: ContentTypes.ACTIVITY_LABEL,
        [ContentTypes.ACTIVITY_LABEL]: 'Completed the activity phase',
        activity_label_type: 'phase',
        activity_start_index: bounds.start,
        activity_end_index: bounds.end,
        activity_count: 1,
        pending: false,
      }) as unknown as TMessageContentParts;
    const pendingReservation = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: '',
      activity_label_type: 'phase',
      activity_start_index: 0,
      pending: true,
    } as unknown as TMessageContentParts;
    const { rerender } = render(
      <ContentParts
        {...baseProps}
        content={[toolPart, pendingReservation, toolPart, twinPhase({ start: 2, end: 3 })]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    rerender(
      <ContentParts
        {...baseProps}
        content={[
          toolPart,
          twinPhase({ start: 0, end: 1 }),
          toolPart,
          twinPhase({ start: 2, end: 3 }),
        ]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    const phases = screen.getAllByTestId('activity-phase-group');
    expect(phases).toHaveLength(2);
    expect(phases[0]).toHaveAttribute('data-animate-entrance', 'true');
    expect(phases[1]).toHaveAttribute('data-animate-entrance', 'false');
  });

  /** A settle can re-key every existing marker AND land a new same-text
   *  phase in the same commit. Occurrences pair by position: the re-keyed
   *  first and second stay settled; only the third — past the previously
   *  rendered count — animates. */
  it('animates only the new occurrence when a re-key lands with a repeated label', () => {
    const repeatedPhase = (bounds: { start: number; end: number }) =>
      ({
        type: ContentTypes.ACTIVITY_LABEL,
        [ContentTypes.ACTIVITY_LABEL]: 'Completed the activity phase',
        activity_label_type: 'phase',
        activity_start_index: bounds.start,
        activity_end_index: bounds.end,
        activity_count: 1,
        pending: false,
      }) as unknown as TMessageContentParts;
    const { rerender } = render(
      <ContentParts
        {...baseProps}
        content={[
          toolPart,
          repeatedPhase({ start: 0, end: 1 }),
          toolPart,
          repeatedPhase({ start: 2, end: 3 }),
        ]}
        isLast
        isSubmitting
        isLatestMessage
      />,
    );

    rerender(
      <ContentParts
        {...baseProps}
        content={[
          toolPart,
          toolPart,
          repeatedPhase({ start: 0, end: 2 }),
          toolPart,
          repeatedPhase({ start: 3, end: 4 }),
          toolPart,
          repeatedPhase({ start: 5, end: 6 }),
        ]}
        isLast
      />,
    );

    const phases = screen.getAllByTestId('activity-phase-group');
    expect(phases).toHaveLength(3);
    expect(phases[0]).toHaveAttribute('data-animate-entrance', 'false');
    expect(phases[1]).toHaveAttribute('data-animate-entrance', 'false');
    expect(phases[2]).toHaveAttribute('data-animate-entrance', 'true');
  });

  it('still animates a phase whose label first appears at settle', () => {
    const { rerender } = renderStreaming();

    const lateLabel = {
      type: ContentTypes.ACTIVITY_LABEL,
      [ContentTypes.ACTIVITY_LABEL]: 'Wrote the final summary',
      activity_label_type: 'phase',
      activity_start_index: 0,
      activity_end_index: 2,
      activity_count: 1,
      pending: false,
    } as unknown as TMessageContentParts;
    rerender(
      <ContentParts {...baseProps} content={[toolPart, batchLabel, answer, lateLabel]} isLast />,
    );

    expect(screen.getByTestId('activity-phase-group')).toHaveAttribute(
      'data-animate-entrance',
      'true',
    );
  });
});