import { describe, test, expect } from 'vitest'
import type { Page } from 'playwright'
import { PlaywrightAutomationAdapter } from 'core/automation/infrastructure//automation'

describe('CarsFlow integration', () => {
  test('adapter emits panel-attached then action-started then action-complete for performAddCar', async () => {
    const adapter = new PlaywrightAutomationAdapter({}, undefined, undefined)
    const received: Array<{ type: string }> = []
    adapter.onLifecycle?.((e: { type: string; actionId?: string; timestamp: number; payload?: any }) => {
      received.push({ type: e.type })
    })

    // Use mock page fixture: minimal object with required methods
    const mockPage = {
      waitForSelector: async () => {},
      evaluate: async () => {},
      waitForTimeout: async () => {},
      click: async () => {},
      setDefaultTimeout: () => {},
    } as unknown as Page

    // call attachPanel which emits panel-attached and then action-started
    await adapter.attachPanel(mockPage, 'add-car')

    // simulate complete event via internal lifecycle emitter
    await (adapter as unknown as { emitLifecycle: (ev: { type: string; actionId: string; timestamp: number }) => Promise<void> }).emitLifecycle(
      {
        type: 'action-complete',
        actionId: 'add-car',
        timestamp: Date.now(),
      },
    )

    const types = received.map(r => r.type)
    expect(types.indexOf('panel-attached')).toBeGreaterThanOrEqual(0)
    expect(types.indexOf('action-started')).toBeGreaterThanOrEqual(0)
    expect(types.indexOf('action-complete')).toBeGreaterThanOrEqual(0)
  })
})