// @vitest-environment jsdom
/**
 * Test timestamp display in tool call rows.
 */
import '../../test/dom-setup';
import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import { ToolCallsSection, type ToolCallData } from './ToolCallsSection';

const mockToolCall = (ts?: string): ToolCallData => ({
  callId: 'call-001',
  movement: 'execute',
  name: 'Read',
  args: '{"file_path": "/tmp/test.txt"}',
  result: 'Success',
  isError: false,
  durationMs: 1500,
  cacheHit: false,
  ts,
});

describe('ToolCallsSection timestamp display', () => {
  it('displays execution time when ts is provided', () => {
    const iso = '2026-07-07T14:30:45.000Z';
    const tc = mockToolCall(iso);
    const { container } = render(<ToolCallsSection toolCalls={[tc]} />);

    // Time should be displayed (formatted as local time)
    const timeString = new Date(iso).toLocaleTimeString();
    expect(container.textContent).toContain(timeString);
  });

  it('does not display time span when ts is undefined', () => {
    const tc = mockToolCall();
    const { container } = render(<ToolCallsSection toolCalls={[tc]} />);

    // Duration should still be present
    expect(container.textContent).toContain('1.5s');
  });

  it('displays full datetime in title attribute when ts is provided', () => {
    const iso = '2026-07-07T14:30:45.000Z';
    const tc = mockToolCall(iso);
    const { container } = render(<ToolCallsSection toolCalls={[tc]} />);

    const timeSpan = container.querySelector('[title]');
    expect(timeSpan).toBeTruthy();
    expect(timeSpan?.getAttribute('title')).toBe(new Date(iso).toLocaleString());
  });

  it('renders time and duration both when ts is provided', () => {
    const iso = '2026-07-07T14:30:45.123Z';
    const tc = mockToolCall(iso);
    const { container } = render(<ToolCallsSection toolCalls={[tc]} />);

    // Both time and duration should be present
    const timeStr = new Date(iso).toLocaleTimeString();
    expect(container.textContent).toContain(timeStr);
    expect(container.textContent).toContain('1.5s');
  });

  it('renders timestamps for all tool calls when present', () => {
    // Single tool call with ts displays the timestamp
    const iso = '2026-07-07T14:30:00.000Z';
    const tc = mockToolCall(iso);
    const { container } = render(<ToolCallsSection toolCalls={[tc]} />);

    const timeStr = new Date(iso).toLocaleTimeString();
    expect(container.textContent).toContain(timeStr);
  });

  it('displays cache hit label instead of duration', () => {
    const iso = '2026-07-07T14:30:45.000Z';
    const tc = { ...mockToolCall(iso), cacheHit: true };
    const { container } = render(<ToolCallsSection toolCalls={[tc]} />);

    expect(container.textContent).toContain('cache');
    // Duration should not be shown for cache hit
    expect(container.textContent).not.toContain('1s');
  });
});
