// @ts-check
const { test, expect } = require('@playwright/test');

// Console/page errors that are environmental (offline backends, missing engines) and
// must NOT fail the smoke test. Real JS exceptions (ReferenceError, etc.) still fail.
const IGNORE = [/Failed to load resource/i, /favicon/i, /net::ERR/i, /AbortError/i];

function watchForErrors(page) {
  const errors = [];
  page.on('pageerror', (e) => errors.push(String(e)));
  page.on('console', (m) => { if (m.type() === 'error' && !IGNORE.some(r => r.test(m.text()))) errors.push(m.text()); });
  return errors;
}

test('app shell loads without JS errors', async ({ page }) => {
  const errors = watchForErrors(page);
  await page.goto('/', { waitUntil: 'domcontentloaded' });
  await expect(page.locator('#sidebar')).toBeVisible();
  await expect(page.locator('.nav-item').first()).toBeVisible();
  // Let dynamic sections + module scripts settle
  await page.waitForTimeout(1500);
  const real = errors.filter(e => /ReferenceError|TypeError|SyntaxError|is not defined|is not a function/.test(e));
  expect(real, real.join('\n')).toHaveLength(0);
});

// Navigate via the app's global router (sidebar is hidden behind a menu on mobile,
// so clicking sidebar pixels is unreliable across viewports).
async function navTo(page, section) {
  await page.waitForFunction(() => typeof window.navTo === 'function', null, { timeout: 15_000 });
  await page.evaluate((s) => window.navTo(s), section);
}

test('core sections render', async ({ page }) => {
  await page.goto('/', { waitUntil: 'domcontentloaded' });
  const sections = [
    ['s-clone', /Clone a Voice/i],
    ['s-studio', /Get Voices Online/i],
    ['s-rehearser', /Script Rehearser/i],
  ];
  for (const [nav, heading] of sections) {
    await navTo(page, nav);
    await expect(page.getByText(heading).first()).toBeVisible({ timeout: 10_000 });
  }
});

test('clone tabs switch and show one panel at a time', async ({ page }) => {
  await page.goto('/', { waitUntil: 'domcontentloaded' });
  await navTo(page, 's-clone');
  await expect(page.locator('.clone-src-tab[data-src="url"]')).toBeVisible({ timeout: 10_000 });
  await page.locator('.clone-src-tab[data-src="url"]').click();
  await expect(page.locator('#clone-src-url')).toBeVisible();
  await expect(page.locator('#clone-src-mic')).toBeHidden();
});

test('PWA manifest and service worker are served', async ({ page, request }) => {
  await page.goto('/', { waitUntil: 'domcontentloaded' });
  const mani = await request.get('/manifest.webmanifest');
  expect(mani.ok()).toBeTruthy();
  const sw = await request.get('/sw.js');
  expect(sw.ok()).toBeTruthy();
});
