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

test('instant-load consumer smoke proves cold, cached, refresh, and zero-CLS steady state', async ({
  page,
}) => {
  await page.addInitScript(() => {
    if (window.localStorage.getItem('__instant_load_smoke_seeded__') !== '1') {
      window.localStorage.clear()
      window.sessionStorage.clear()
      window.localStorage.setItem('__instant_load_smoke_seeded__', '1')
    }
  })

  await page.goto('/instant-load')

  await expect(page.getByTestId('boot-mode')).toHaveText('cold')
  await expect(page.getByTestId('query-skeleton')).toBeVisible()
  await expect(page.getByTestId('query-result')).toContainText('Version 1')
  await expect(page.getByTestId('query-skeleton')).toHaveCount(0)

  await page.reload()

  await expect(page.getByTestId('boot-mode')).toHaveText('cached')
  await expect(page.getByTestId('query-result')).toContainText('Version 1')

  await page.getByTestId('source-bump').click()
  await page.getByTestId('query-refresh').click()

  // RefreshSignal is intentionally aria-hidden (presentational, not in the a11y tree),
  // so Playwright's toBeVisible() (which excludes aria-hidden elements) doesn't apply here.
  await expect(page.getByTestId('refresh-signal')).toBeAttached()
  await expect(page.getByTestId('query-result')).toContainText('Version 2')

  const cls = Number(await page.getByTestId('cls-oracle').getAttribute('data-cls'))
  expect(Number.isFinite(cls)).toBe(true)
  expect(cls).toBeLessThan(0.01)
})
