import { test, expect } from '@playwright/test'

// AZ-472 — e2e companion for FT-P-44 (DetectionClasses load contract).
//
// The fast suite covers all four ACs (load + hotkeys + click + fallback);
// the e2e companion exists so the load path is observed end-to-end against
// the real `annotations/` service. Hotkey and click paths are not duplicated
// here — they're already deterministic in JSDOM.

test.describe('AZ-472 — DetectionClasses (e2e companion)', () => {
  test('AC-1 (FT-P-44) — GET /api/annotations/classes observed at mount', async ({ page }) => {
    const gets: { url: string }[] = []
    await page.route('**/api/annotations/classes', async (route) => {
      if (route.request().method() === 'GET') {
        gets.push({ url: route.request().url() })
      }
      await route.continue()
    })

    await page.goto('/annotations')

    // The DetectionClasses panel renders inside the left sidebar of
    // <AnnotationsPage>. Wait for it to be visible by its localized title.
    const heading = page.getByText(/Classes/i).first()
    if (!(await heading.isVisible({ timeout: 5000 }).catch(() => false))) {
      test.skip(true, 'DetectionClasses panel not rendered (auth gate?)')
    }

    expect(gets.length).toBeGreaterThan(0)
    for (const g of gets) {
      const path = new URL(g.url).pathname
      expect(path).toBe('/api/annotations/classes')
    }
  })
})
