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

const ADMIN_URL = process.env.PLAYWRIGHT_ADMIN_URL ?? 'http://localhost:3801';
const ADMIN_ACCOUNT = process.env.E2E_ADMIN_USERNAME ?? 'admin';
const ADMIN_PASSWORD = process.env.E2E_ADMIN_PASSWORD ?? '12345678';

test('管理端登录页 → 登录成功进入后台', async ({ page }) => {
  test.setTimeout(90_000);
  await page.goto(`${ADMIN_URL}/admin/login`);

  const account = page.locator('#admin-account');
  await account.waitFor({ state: 'visible', timeout: 60_000 });
  await account.fill(ADMIN_ACCOUNT);
  await page.locator('#admin-password').fill(ADMIN_PASSWORD);

  const captchaImg = page.locator('img[src^="data:image"]');
  await captchaImg.waitFor({ state: 'visible', timeout: 30_000 });

  await page.locator('#admin-captcha').fill('LOTTERY_E2E_BYPASS');
  await page.getByRole('button', { name: /^登录$|Sign in|submit/i }).click();

  await page.waitForURL(/\/admin(?!\/login)/, { timeout: 45_000 });
  expect(page.url()).toContain('/admin');
});
