import { describe, expect, it } from 'vitest';

import {
  markTransientAgentHistoryItem,
  type AgentHistoryItem,
} from '../../agent-runtime/src/index';
import {
  agentTurnToThreadTurnDto,
  codexTurnToAgentTurn,
  liveCodexItemToHistoryItem,
  shouldPersistLiveHistoryItem,
  shouldPersistRuntimeFinalHistoryItem,
} from './historyItems';

describe('codex history item persistence policy', () => {
  it('keeps streamed assistant text out of live history persistence', () => {
    expect(
      shouldPersistLiveHistoryItem({
        id: 'agent-live-1',
        kind: 'agentMessage',
        text: 'Streaming draft',
      }),
    ).toBe(false);
  });

  it('keeps transient streamed assistant text out of final runtime persistence', () => {
    const transientMessage = markTransientAgentHistoryItem<AgentHistoryItem>({
      id: 'agent-live-1',
      kind: 'agentMessage',
      text: 'Streaming draft',
    });

    expect(shouldPersistRuntimeFinalHistoryItem(transientMessage, [transientMessage])).toBe(
      false,
    );
  });

  it('hides transient streamed assistant text once a final assistant message exists', () => {
    const transientMessage = markTransientAgentHistoryItem<AgentHistoryItem>({
      id: 'agent-live-1',
      kind: 'agentMessage',
      text: 'Streaming draft',
    });
    const finalMessage: AgentHistoryItem = {
      id: 'agent-final-1',
      kind: 'agentMessage',
      text: 'Final answer',
    };

    expect(
      agentTurnToThreadTurnDto({
        providerTurnId: 'turn-1',
        status: 'completed',
        error: null,
        items: [transientMessage, finalMessage],
      }).items,
    ).toEqual([finalMessage]);
  });

  it('maps Codex collab agent tool calls to dedicated agent tool call items', () => {
    const turn = codexTurnToAgentTurn({
      id: 'turn-1',
      status: 'inProgress',
      error: null,
      items: [
        {
          id: 'agent-tool-1',
          type: 'collabAgentToolCall',
          status: 'running',
          action: {
            name: 'Review checkout flow',
            input: {
              prompt: 'Inspect checkout UI regressions.',
            },
          },
        },
      ],
    });

    expect(turn.items[0]).toMatchObject({
      id: 'agent-tool-1',
      kind: 'agentToolCall',
      text: 'Agent: Review checkout flow',
      previewText: 'Agent',
      status: 'running',
    });
  });

  it('keeps Codex collab agent tool calls visible while running', () => {
    expect(
      liveCodexItemToHistoryItem(
        {
          id: 'agent-tool-1',
          type: 'collabAgentToolCall',
          status: 'running',
          action: {
            name: 'Inspect backend runtime boundaries',
          },
        },
        'started',
      ),
    ).toMatchObject({
      id: 'agent-tool-1',
      kind: 'agentToolCall',
      text: 'Agent: Inspect backend runtime boundaries',
      previewText: 'Agent',
      status: 'running',
    });
  });
});
