import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import ActivityItem from './ActivityItem.jsx';

describe('ActivityItem', () => {
  it('task 항목은 상태 뱃지와 duration을 렌더한다', () => {
    render(<ActivityItem item={{ type: 'task', agent_id: 'stock', task_id: 'a', message: 'holdings_brief', created_at: '2026-06-10T23:30:00Z', status: 'succeeded', duration_seconds: 2 }} onSelectAgent={() => {}} />);
    expect(screen.getByText('holdings_brief')).toBeInTheDocument();
    expect(screen.getByText(/완료/)).toBeInTheDocument();
    expect(screen.getByText('2s')).toBeInTheDocument();
  });

  it('log 항목은 level 아이콘을 렌더한다', () => {
    render(<ActivityItem item={{ type: 'log', agent_id: 'lotto', task_id: 'b', message: 'signal_check', created_at: '2026-06-10T23:15:00Z', level: 'error' }} onSelectAgent={() => {}} />);
    expect(screen.getByText('signal_check')).toBeInTheDocument();
    expect(screen.getByText('❌')).toBeInTheDocument();
  });

  it('클릭 시 onSelectAgent(agent_id)를 호출한다', () => {
    const onSelect = vi.fn();
    render(<ActivityItem item={{ type: 'task', agent_id: 'insta', task_id: 'c', message: '발급', created_at: '2026-06-10T23:00:00Z', status: 'pending' }} onSelectAgent={onSelect} />);
    fireEvent.click(screen.getByText('발급').closest('.ao-activity-item'));
    expect(onSelect).toHaveBeenCalledWith('insta');
  });

  it('미지정 agent_id는 id를 그대로 표시한다', () => {
    render(<ActivityItem item={{ type: 'log', agent_id: 'unknown', task_id: 'd', message: 'x', created_at: '2026-06-10T23:00:00Z', level: 'info' }} onSelectAgent={() => {}} />);
    expect(screen.getByText('unknown')).toBeInTheDocument();
  });
});
