/** E2E-SET-001 / MCP-SET-001 — settings save control + success toast */
import { test, expect } from '../fixtures.mjs'
import { login, nav } from '../helpers.mjs'

test('E2E-SET-001 settings page save control', async ({ page }) => {
  await login(page)
  await nav(page, '/settings')
  await expect(page.getByText('系统设置')).toBeVisible({ timeout: 20000 })
  await expect(page.getByRole('button', { name: '保存设置' })).toBeVisible()
})

test('E2E-SET-002 settings save success toast', async ({ page }) => {
  await login(page)
  await page.route(/\/api\/settings\//, async (route) => {
    if (route.request().method() === 'PUT') {
      await route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({ key: 'system_name', value: 'Nexus' }),
      })
      return
    }
    await route.continue()
  })
  await nav(page, '/settings')
  await expect(page.getByLabel('系统名称')).toBeVisible({ timeout: 20000 })
  const title = page.getByLabel('系统标题')
  if (!(await title.inputValue()).trim()) {
    await title.fill('Nexus Ops E2E')
  }
  const saveReq = page.waitForRequest(
    (req) => req.method() === 'PUT' && req.url().includes('/api/settings/'),
  )
  await page.getByRole('button', { name: '保存设置' }).click()
  await saveReq
  await expect(page.getByText('设置已保存')).toBeVisible({ timeout: 10000 })
})
