/** E2E-SCH-003/004 — schedule operator timezone (Beijing wall clock) */
import { test, expect } from '../fixtures.mjs'
import { ISO_UTC_RE, login, nav } from '../helpers.mjs'

const mockSchedule = {
  id: 99001,
  name: 'e2e-beijing-0200',
  schedule_type: 'push',
  run_mode: 'cron',
  cron_expr: '0 2 * * *',
  fire_at: null,
  source_path: '/tmp',
  target_path: null,
  server_ids: '[1]',
  sync_mode: 'incremental',
  enabled: true,
  script_id: null,
  script_content: null,
  exec_timeout: 60,
  long_task: false,
  last_run_at: null,
  /** UTC naive — Beijing next day 02:00 */
  next_run: '2026-06-09 18:00:00',
  created_at: '2026-06-09 10:00:00',
}

test('E2E-SCH-003 schedule dialog shows Beijing timezone hint', async ({ page }) => {
  await login(page)
  await nav(page, '/schedules')
  await page.getByRole('button', { name: '新建调度' }).click()
  await expect(page.getByText(/时间为北京时间（Asia\/Shanghai）/).first()).toBeVisible()
  await page.getByRole('button', { name: '取消' }).click()
})

test('E2E-SCH-004 next_run column shows Beijing 02:00 not bare ISO UTC', async ({ page }) => {
  await page.route('**/api/schedules/**', async (route) => {
    if (route.request().method() !== 'GET') {
      await route.continue()
      return
    }
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify([mockSchedule]),
    })
  })

  await login(page)
  await nav(page, '/schedules')
  await expect(page.getByText('e2e-beijing-0200')).toBeVisible()

  const nextRunCell = page.locator('td').filter({ hasText: /2026-06-10 02:00:00/ })
  await expect(nextRunCell.first()).toBeVisible()

  const tableText = await page.locator('.v-data-table').innerText()
  expect(tableText).not.toMatch(ISO_UTC_RE)
  expect(tableText).not.toMatch(/10:00:00/)
})
