import type { Page } from '@playwright/test'

/** §24 DoD "No critical browser console errors" (dod-24-abgleich.md line 35:
 * "kein E2E-Beweis, nur jsdom-sauber") — the first REAL browser-level
 * evidence for that line. Call this right after `mount()` (before any
 * interaction) so it captures the mount itself too, not just what follows.
 * Collects every `console.error` the page emits PLUS uncaught page
 * exceptions (`pageerror`, e.g. an unhandled render-time throw) — jsdom-based
 * vitest specs have no equivalent signal for either.
 *
 * Returns the live, mutating array — assert `expect(consoleErrors).toEqual([])`
 * at the point in the spec where it matters (usually at the very end, after
 * the interaction sequence under test). */
export function collectConsoleErrors(page: Page): string[] {
  const errors: string[] = []
  page.on('console', (msg) => {
    if (msg.type() === 'error') errors.push(msg.text())
  })
  page.on('pageerror', (err) => {
    errors.push(err.message)
  })
  return errors
}
