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

test.describe('Website smoke - core pages render', () => {
  const routes = [
    { path: '/', name: 'landing' },
    { path: '/dashboard', name: 'dashboard' },
    { path: '/drivers', name: 'drivers list' },
    { path: '/leagues', name: 'leagues list' },
    { path: '/profile', name: 'profile' },
    { path: '/teams', name: 'teams list' },
  ];

  for (const route of routes) {
    test(`renders ${route.name} page without console errors (${route.path})`, async ({ page }) => {
      const consoleMessages: string[] = [];

      page.on('console', (msg) => {
        const type = msg.type();
        if (type === 'error') {
          consoleMessages.push(`[${type}] ${msg.text()}`);
        }
      });

      await page.goto(route.path, { waitUntil: 'networkidle' });

      await expect(page).toHaveTitle(/GridPilot/i);

      expect(
        consoleMessages.length,
        `Console errors on route ${route.path}:\n${consoleMessages.join('\n')}`,
      ).toBe(0);
    });
  }
});