import { describe, expect, it } from 'vitest';
import { buildTimelineItems, TimelineItemSchema } from './timeline.js';

describe('timeline contract baseline', () => {
  it('builds unified timeline items across phases 1 to 3', () => {
    const items = buildTimelineItems([
      {
        eventId: 'evt_capture_1',
        eventName: 'capture.transcript.created',
        occurredAt: '2026-04-03T12:00:00.000Z',
        productId: 'lysnrai',
        artifactId: 'art_transcript_1',
        actor: { actorType: 'user', actorId: 'saravana' },
        trace: { correlationId: 'corr_phase1', causationId: null, parentEventId: null },
        payload: { durationMs: 42150, transcriptSource: 'microphone' },
      },
      {
        eventId: 'evt_plan_1',
        eventName: 'artifact.created',
        occurredAt: '2026-04-03T13:00:00.000Z',
        productId: 'flowmonk',
        artifactId: 'art_plan_1',
        actor: { actorType: 'agent', actorId: 'phase2-plan-exporter' },
        trace: { correlationId: 'corr_phase2', causationId: null, parentEventId: null },
        payload: { artifactType: 'plan', title: 'FlowMonk weekly plan', status: 'draft' },
      },
      {
        eventId: 'evt_trail_1',
        eventName: 'artifact.created',
        occurredAt: '2026-04-03T14:00:00.000Z',
        productId: 'actiontrail',
        artifactId: 'art_trail_1',
        actor: { actorType: 'agent', actorId: 'phase3-audit-importer' },
        trace: {
          correlationId: 'corr_phase3',
          causationId: 'task_phase3',
          parentEventId: 'task_phase3',
        },
        payload: {
          artifactType: 'trail-report',
          title: 'Cowork audit report for task task_phase3',
          status: 'recorded',
        },
      },
      {
        eventId: 'evt_memory_1',
        eventName: 'memory.entry.created',
        occurredAt: '2026-04-03T14:10:00.000Z',
        productId: 'mindlyst',
        artifactId: 'art_memory_1',
        actor: { actorType: 'agent', actorId: 'phase3-audit-note-importer' },
        trace: {
          correlationId: 'corr_phase3',
          causationId: 'evt_note_linked_1',
          parentEventId: 'evt_note_linked_1',
        },
        payload: {
          memoryKind: 'insight',
          sourceArtifactIds: ['art_note_1', 'art_trail_1'],
        },
      },
    ]);

    expect(items.map(item => item.eventName)).toEqual([
      'memory.entry.created',
      'artifact.created',
      'artifact.created',
      'capture.transcript.created',
    ]);
    expect(items[0]?.title).toBe('Insight memory proposed');
    expect(items[1]?.title).toBe('Cowork audit report for task task_phase3');
    expect(items[2]?.summary).toBe('plan status: draft');
    expect(items[3]?.summary).toContain('microphone transcript captured');
    expect(items[0]?.relatedEventIds).toEqual(['evt_note_linked_1']);
  });

  it('exposes a stable timeline item schema', () => {
    const item = TimelineItemSchema.parse({
      itemId: 'timeline_evt_1',
      occurredAt: '2026-04-03T14:10:00.000Z',
      eventName: 'memory.entry.created',
      productId: 'mindlyst',
      title: 'Insight memory proposed',
      summary: '2 source artifacts linked',
      artifactRefs: ['art_memory_1'],
      relatedEventIds: ['evt_note_linked_1'],
      actorType: 'agent',
      visibility: 'private',
      correlationId: 'corr_phase3',
    });

    expect(item.visibility).toBe('private');
    expect(item.relatedEventIds).toHaveLength(1);
  });
});
