// Shared Playwright fixtures for the deployed-environment harness.
//
// Pre-record a cookie-consent choice via addInitScript so the §14.5
// cookie-consent banner NEVER renders. The banner is fixed to the bottom
// of the viewport and intercepts pointer events over the catalog footer
// (the row-select checkboxes SLICE-5 clicks). The previous approach —
// dismiss it after navigation (lib/ui.js dismissCookies) — raced the
// banner's render on the slower deployed edge (PPE): dismissCookies ran
// before the banner mounted, found nothing to remove, and the banner then
// appeared and swallowed the row clicks. Recording consent at
// document-start (before the app's scripts read `hasChosen()`) means the
// banner's `open` state initialises false and it never mounts — no race.
//
// Storage shape mirrors lib/consent.js (LS_KEY 'rfc-app.cookie-consent.v1';
// a non-null recorded_at == "the user has chosen"). Environment-agnostic:
// the init script runs on whatever origin the test navigates to (PPE or
// the Tier-1 localhost stack).
import { test as base, expect } from '@playwright/test'

const CONSENT = JSON.stringify({
  essential: true,
  analytics: false,
  other: false,
  recorded_at: '2000-01-01T00:00:00.000Z',
})

export const test = base.extend({
  context: async ({ context }, use) => {
    await context.addInitScript((value) => {
      try {
        window.localStorage.setItem('rfc-app.cookie-consent.v1', value)
      } catch {
        // localStorage unavailable — fall back to lib/ui.js dismissCookies.
      }
    }, CONSENT)
    await use(context)
  },
})

export { expect }
