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

test.beforeEach(async ({ page }) => {
  await page.addInitScript(() => {
    window.localStorage.setItem('dlbdi:staffToken', 'dev-admin-token')
  })
})

test('system health and config checks are visible to operators', async ({ page }) => {
  await page.goto('/admin/system')
  await expect(page.getByRole('heading', { name: '系统健康' })).toBeVisible()
  await expect(page.getByText('objectStorage', { exact: true })).toBeVisible()
  await expect(page.getByText(/本地上传模式|S3/)).toBeVisible()

  await page.goto('/admin/ops/config-checks')
  await expect(page.getByRole('heading', { name: '配置检查' })).toBeVisible()
  await expect(page.getByText('CORS 域名限制')).toBeVisible()
  await expect(page.getByText('登录、下单、支付与上传限流')).toBeVisible()
  await expect(page.getByText('SSR 内部 API 地址')).toBeVisible()
})

test('public SSR pages do not expose internal fetch URL errors', async ({ request }) => {
  test.setTimeout(60_000)
  const pages = [
    { path: '/', content: /近期课程|近期活动|关于研究院/ },
    { path: '/about', content: /关于我们|关于研究院/ },
    { path: '/consulting', content: /规划咨询/ },
    { path: '/data', content: /数据服务/ },
    { path: '/courses', content: /课程目录|暂无课程|推荐课程/ },
    { path: '/events', content: /活动|报名/ },
    { path: '/experts', content: /专家平台/ },
    { path: '/news', content: /新闻资讯|暂无已发布资讯|资讯/ },
  ]
  for (const { path, content } of pages) {
    const response = await request.get(path)
    expect(response.status(), `${path} should render successfully`).toBe(200)
    const body = await response.text()
    expect(body, `${path} should include page-specific content`).toMatch(content)
    expect(body).not.toContain('ERR_INVALID_URL')
    expect(body).not.toContain('Failed to parse URL')
  }
})
