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

import { buildTaskTimelineView } from '@/lib/task-timeline-view';
import type { BackendTask, ProcessEvent } 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: {},
  };
}

describe('buildTaskTimelineView', () => {
  it('builds canonical task timeline cards from matching live process data', () => {
    const liveEvents: ProcessEvent[] = [
      {
        event_id: 'plan',
        run_id: 'main-run',
        kind: 'task_planned',
        actor_type: 'system',
        actor_id: 'planner',
        actor_name: 'Planner',
        text: 'Plan created',
        created_at: '2026-06-04T00:00:10.000Z',
      },
    ];

    const view = buildTaskTimelineView({
      task: task(),
      liveEvents,
      locale: 'en-US',
    });

    expect(view?.cards.map((card) => card.type)).toEqual(['task_created', 'plan']);
    expect(view?.cards.map((card) => card.title)).toEqual(['Task created', 'Execution plan']);
    expect(view?.process.events.map((event) => event.event_id)).toEqual(['plan']);
  });

  it('returns null when there is no active task to display', () => {
    expect(buildTaskTimelineView({ task: null })).toBeNull();
  });
});
