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

import { selectTaskProcess } from '@/lib/task-process';
import type { BackendTask, ProcessArtifact, ProcessEvent, ProcessRun } from '@/types';

function task(): BackendTask {
  return {
    task_id: 'task-1',
    session_id: 'web:default',
    description: 'Build report',
    goal: 'Build report',
    constraints: [],
    priority: 0,
    status: 'running',
    creator: 'user',
    created_at: '2026-06-04T00:00:00.000Z',
    updated_at: '2026-06-04T00:01:00.000Z',
    run_ids: ['main-run'],
    skill_names: [],
    feedback: [],
    metadata: {},
    process_runs: [
      {
        run_id: 'main-run',
        session_id: 'web:default',
        actor_type: 'agent',
        actor_id: 'main',
        actor_name: 'Main Agent',
        title: 'Persisted main run',
        status: 'waiting',
        started_at: '2026-06-04T00:00:10.000Z',
        metadata: { task_id: 'task-1' },
      },
    ],
    process_events: [
      {
        event_id: 'persisted-event',
        run_id: 'main-run',
        kind: 'task_planned',
        actor_type: 'system',
        actor_id: 'planner',
        actor_name: 'Planner',
        text: 'Persisted plan',
        created_at: '2026-06-04T00:00:20.000Z',
        metadata: { task_id: 'task-1' },
      },
    ],
    process_artifacts: [],
  };
}

describe('selectTaskProcess', () => {
  it('merges persisted and live task process data while excluding other tasks', () => {
    const liveRuns: ProcessRun[] = [
      {
        run_id: 'main-run',
        session_id: 'web:default',
        actor_type: 'agent',
        actor_id: 'main',
        actor_name: 'Main Agent',
        title: 'Live main run',
        status: 'running',
        started_at: '2026-06-04T00:00:10.000Z',
        metadata: { task_id: 'task-1' },
      },
      {
        run_id: 'child-run',
        parent_run_id: 'main-run',
        session_id: 'subagent:child',
        actor_type: 'agent',
        actor_id: 'child',
        actor_name: 'Child Agent',
        title: 'Child work',
        status: 'done',
        started_at: '2026-06-04T00:00:30.000Z',
      },
      {
        run_id: 'other-run',
        session_id: 'web:default',
        actor_type: 'agent',
        actor_id: 'other',
        actor_name: 'Other Agent',
        title: 'Other task',
        status: 'running',
        started_at: '2026-06-04T00:00:40.000Z',
        metadata: { task_id: 'task-2' },
      },
    ];
    const liveEvents: ProcessEvent[] = [
      {
        event_id: 'child-event',
        run_id: 'child-run',
        parent_run_id: 'main-run',
        kind: 'run_progress',
        actor_type: 'agent',
        actor_id: 'child',
        actor_name: 'Child Agent',
        text: 'Child finished',
        created_at: '2026-06-04T00:00:50.000Z',
      },
      {
        event_id: 'other-event',
        run_id: 'other-run',
        kind: 'run_progress',
        actor_type: 'agent',
        actor_id: 'other',
        actor_name: 'Other Agent',
        text: 'Other task progress',
        created_at: '2026-06-04T00:00:55.000Z',
        metadata: { task_id: 'task-2' },
      },
    ];
    const liveArtifacts: ProcessArtifact[] = [
      {
        artifact_id: 'child-artifact',
        run_id: 'child-run',
        actor_type: 'agent',
        actor_id: 'child',
        actor_name: 'Child Agent',
        title: 'Child result',
        artifact_type: 'text',
        created_at: '2026-06-04T00:01:00.000Z',
      },
      {
        artifact_id: 'other-artifact',
        run_id: 'other-run',
        actor_type: 'agent',
        actor_id: 'other',
        title: 'Other result',
        artifact_type: 'text',
        created_at: '2026-06-04T00:01:05.000Z',
      },
    ];

    const selected = selectTaskProcess({
      task: task(),
      liveRuns,
      liveEvents,
      liveArtifacts,
    });

    expect(selected.runs.map((run) => run.run_id)).toEqual(['main-run', 'child-run']);
    expect(selected.runs[0].title).toBe('Live main run');
    expect(selected.events.map((event) => event.event_id)).toEqual(['persisted-event', 'child-event']);
    expect(selected.artifacts.map((artifact) => artifact.artifact_id)).toEqual(['child-artifact']);
  });

  it('returns persisted task process data when no live data is available', () => {
    const selected = selectTaskProcess({
      task: task(),
      liveRuns: [],
      liveEvents: [],
      liveArtifacts: [],
    });

    expect(selected.runs.map((run) => run.run_id)).toEqual(['main-run']);
    expect(selected.events.map((event) => event.event_id)).toEqual(['persisted-event']);
    expect(selected.artifacts).toEqual([]);
  });
});
