import { test, expect } from '@playwright/test';
import { ae_app_local_data_defaults } from './_helpers/ae_defaults';

test.describe('Archive Content Page - smoke', () => {
  test.beforeEach(async ({ page }) => {
    page.on('pageerror', (err) => console.error(`BROWSER ERROR: ${err.message}`));
    page.on('console', (msg) => {
      if (msg.type() === 'error' || msg.type() === 'warning') console.error(`BROWSER [${msg.type().toUpperCase()}]: ${msg.text()}`);
    });

    await page.route('**/*oneskyit.com/**', async (route) => {
      const url = route.request().url();
      if (url.includes('site_domain/search')) {
        return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ data: [{ id: 'td', site_id: 'ts' }] }) });
      }
      return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ data: [] }) });
    });

    await page.addInitScript((defaults) => {
      const testData = { ...defaults, account_id: 'smoke-account', manager_access: true };
      window.localStorage.setItem('ae_loc', JSON.stringify(testData));
    }, ae_app_local_data_defaults);
  });

  test('loads archive content view without console errors', async ({ page }) => {
    const errs: string[] = [];
    page.on('pageerror', (e) => errs.push(e.message));

    await page.goto('/archives/test-archive');
    await page.waitForResponse((r) => r.url().includes('site_domain/search') && r.status() === 200);

    expect(errs).toHaveLength(0);
  });
});
