const { test, expect } = require('@playwright/test');
const { getSession, localized, login } = require('./helpers');

const CORE_PAGES = [
  { path: '/', heading: localized('Operations', 'Операции') },
  { path: '/agents', heading: localized('Agents', 'Агенты') },
  { path: '/api-keys', heading: localized('API Keys', 'API ключи') },
  { path: '/secrets', heading: localized('Secrets', 'Секреты') },
  { path: '/logs', heading: localized('Logs', 'Логи') },
  { path: '/usage', heading: localized('Usage', 'Использование') },
  { path: '/workspace-setup', heading: localized('Workspace', 'Воркспейс') },
  { path: '/settings', heading: localized('Settings', 'Настройки') },
  { path: '/stream-sessions', heading: localized('Stream Sessions', 'Stream Sessions') },
  { path: '/async-jobs', heading: localized('Async Jobs', 'Async Jobs') },
  { path: '/wizard/', heading: localized('Create', 'Создать') },
];

test('authenticated staging smoke logs in and exposes session json', async ({ page }) => {
  await login(page);

  const session = await getSession(page);
  expect(session.user).toBeTruthy();
  expect(Array.isArray(session.memberships)).toBe(true);
  expect(session.memberships.length).toBeGreaterThan(0);
});

test('authenticated staging smoke opens core pages without console errors', async ({ page }) => {
  const pageErrors = [];
  page.on('pageerror', (error) => {
    pageErrors.push(error.message);
  });

  await login(page);

  for (const pageSpec of CORE_PAGES) {
    await page.goto(pageSpec.path);
    await expect(page.locator('#login-form')).toHaveCount(0);
    await expect(page.locator('body')).toContainText(pageSpec.heading);
  }

  expect(pageErrors).toEqual([]);
});
