// @vitest-environment jsdom
/**
 * Component tests for LocalTaskListItem — a single row in the task list. Verifies
 * the title/id/body render, the status badge reflects latestJob.status (falling
 * back to "queued"/Inbox when absent), the subtask progress fraction shows, the
 * visibility chip varies by visibility, and clicking fires onClick.
 */
import '../../test/dom-setup';
import { describe, it, expect, vi } from 'vitest';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderWithProviders } from '../../test/render-helpers';
import type { LocalTask } from '../../api';
import { LocalTaskListItem } from './TaskListItem';

function makeTask(overrides: Partial<LocalTask> = {}): LocalTask {
  return {
    id: 42,
    title: 'Build the report',
    body: 'Generate a quarterly sales report from the uploaded spreadsheet.',
    pieceName: 'chat',
    profile: 'default',
    outputFormat: 'markdown',
    askPolicy: 'auto',
    priority: 'normal',
    state: 'open',
    workspacePath: null,
    createdAt: '2026-06-01T00:00:00Z',
    updatedAt: '2026-06-01T00:00:00Z',
    latestJob: { id: 'job-1', status: 'running' },
    ...overrides,
  };
}

describe('LocalTaskListItem', () => {
  it('renders the title, id and body for a running task', () => {
    renderWithProviders(<LocalTaskListItem task={makeTask()} active={false} onClick={() => {}} />);
    expect(screen.getByText('Build the report')).toBeInTheDocument();
    expect(screen.getByText('#42')).toBeInTheDocument();
    expect(screen.getByText(/quarterly sales report/)).toBeInTheDocument();
    // Status badge reflects latestJob.status.
    expect(screen.getByText('Running')).toBeInTheDocument();
  });

  it('falls back to the queued/Inbox status when latestJob is missing', () => {
    renderWithProviders(
      <LocalTaskListItem task={makeTask({ latestJob: null })} active={false} onClick={() => {}} />,
    );
    expect(screen.getByText('Inbox')).toBeInTheDocument();
  });

  it('shows the subtask completed/total fraction when subtasks exist', () => {
    renderWithProviders(
      <LocalTaskListItem
        task={makeTask({ subtaskCount: 4, subtaskCompleted: 1 })}
        active={false}
        onClick={() => {}}
      />,
    );
    expect(screen.getByText('1/4')).toBeInTheDocument();
  });

  it('renders the private visibility chip', () => {
    renderWithProviders(
      <LocalTaskListItem task={makeTask({ visibility: 'private' })} active={false} onClick={() => {}} />,
    );
    expect(screen.getByText('private')).toBeInTheDocument();
  });

  it('renders the org name as the visibility chip for org visibility', () => {
    renderWithProviders(
      <LocalTaskListItem
        task={makeTask({ visibility: 'org', visibilityScopeOrgName: 'Acme' })}
        active={false}
        onClick={() => {}}
      />,
    );
    expect(screen.getByText('Acme')).toBeInTheDocument();
  });

  it('calls onClick when the row is clicked', async () => {
    const user = userEvent.setup();
    const onClick = vi.fn();
    renderWithProviders(<LocalTaskListItem task={makeTask()} active={false} onClick={onClick} />);
    await user.click(screen.getByRole('button'));
    expect(onClick).toHaveBeenCalledTimes(1);
  });
});
