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

import { login, navigateTo } from '../smoke/helpers';

const NAV_TARGETS = [
  '/clients',
  '/yachts',
  '/companies',
  '/berths',
  '/interests',
  '/invoices',
  '/expenses',
];

test.describe('exhaustive: navigation', () => {
  test.beforeEach(async ({ page }) => {
    await login(page, 'super_admin');
  });

  test('every primary nav target loads without console or network errors', async ({ page }) => {
    const errors: string[] = [];

    page.on('console', (msg) => {
      if (msg.type() === 'error')
        errors.push(`[console:${page.url()}] ${msg.text().slice(0, 200)}`);
    });
    page.on('response', (resp) => {
      if (resp.status() >= 500)
        errors.push(`[network:${page.url()}] ${resp.status()} ${resp.url()}`);
    });

    for (const target of NAV_TARGETS) {
      await navigateTo(page, target);
      await page.waitForLoadState('networkidle');
      // Confirm we landed on the requested page.
      expect(page.url(), `navigation to ${target}`).toContain(target);
    }

    expect(errors, JSON.stringify(errors, null, 2)).toEqual([]);
  });
});
