yeg
2026-09-05 2f1220d7ce4157e1ef8e3ff06d5d8ff527229f4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import assert from 'node:assert/strict'
import { readFile, mkdir } from 'node:fs/promises'
import { chromium, expect } from '@playwright/test'
 
// 通过真实构建产物检查告警,API 拦截不会修改开发测试或设备。
const origin = 'http://offline-exceptions.test'
const output = '../.scratch/simulator-and-data-import-v1/offline-exceptions-browser'
await mkdir(output, { recursive: true })
const browser = await chromium.launch({ channel: 'chrome', headless: true })
const now = Date.UTC(2026, 8, 5, 11, 0, 0)
let receptionStatus = 'RECEIVING'
const warnings = ['CABINET_CHANNEL', 'COLLECTOR'].map((sourceType, index) => ({
  exceptionId: index + 1, testId: 1, testName: '离线告警验收', exceptionType: 'DEVICE_OFFLINE',
  sourceType, sourceIdentity: index ? '9' : 'dev-sim:22:1:1:1', metricCode: 'DEVICE_ONLINE',
  abnormalValue: null, unit: '', ruleSnapshot: '{"expectedStatus":"ONLINE"}', exceptionStatus: 'OPEN',
  firstOccurredAtUtc: now, lastOccurredAtUtc: now, occurrenceCount: 1,
}))
const detail = () => ({ testId: 1, testName: '离线告警验收', testStatus: receptionStatus === 'RECEIVING' ? 'RUNNING' : receptionStatus === 'ENDED' ? 'COMPLETED' : 'PAUSED',
  receptionStatus, creatorName: '若依', principalName: '若依', testTypeName: '性能测试', testPurpose: '离线观测',
  startedAtUtc: now, createTime: now, thresholds: [], planSteps: [], logicStepMappings: [],
  batteries: [{ testBatteryId: 1, batteryNo: 'B-01', groupName: '演示电池组', collectorId: 9, collectorName: '演示采集器', fields: [], collectorChannels: [],
    binding: { channelId: 1, cabinetId: 1, cabinetName: '演示检测柜', adapterCode: 'dev-sim', devtype: 22, devid: 1, subdevid: 1, chlid: 1 } }],
})
const live = () => ({ testId: 1, testStatus: detail().testStatus, receptionStatus, serverTime: now, testPlan: null,
  sources: ['CABINET_CHANNEL', 'COLLECTOR'].map(sourceType => ({ sourceType, sourceId: sourceType === 'COLLECTOR' ? 9 : 1, batteryNo: 'B-01', deviceStatus: 'OFFLINE', latestSnapshot: {} })),
  exceptions: receptionStatus === 'RECEIVING' ? warnings.filter(w => w.exceptionStatus === 'OPEN').map(w => ({ ...w, status: w.exceptionStatus, value: null, message: '接收期间设备离线,等待处理' })) : [],
})
try {
  const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } })
  const errors = []
  page.on('pageerror', error => errors.push(error.message))
  await page.addInitScript(() => localStorage.setItem('battery_detection_auth_token', 'browser-test'))
  await page.route(`${origin}/**`, async route => {
    const path = new URL(route.request().url()).pathname
    if (path.startsWith('/assets/')) return route.fulfill({ body: await readFile(`dist${path}`), contentType: path.endsWith('.css') ? 'text/css' : 'application/javascript' })
    if (path.endsWith('/test/1/live')) return route.fulfill({ contentType: 'text/event-stream', body: `event: snapshot\ndata: ${JSON.stringify(live())}\n\n` })
    if (path.includes('/system/test-warning-exceptions/')) {
      if (path.endsWith('/close')) {
        const id = Number(path.split('/').at(-2))
        const record = warnings.find(w => w.exceptionId === id)
        Object.assign(record, { exceptionStatus: 'CLOSED', handledAtUtc: now, handledByName: '若依', handlingNote: route.request().postDataJSON().handlingNote })
        return route.fulfill({ json: { code: 200, data: record } })
      }
      return route.fulfill({ json: { code: 200, rows: warnings, total: warnings.length } })
    }
    if (path.includes('/system/test/1')) return route.fulfill({ json: { code: 200, data: path.endsWith('/live-state') ? live() : detail() } })
    if (path.endsWith('/getInfo')) return route.fulfill({ json: { code: 200, user: { userName: 'admin' }, roles: ['admin'] } })
    return route.fulfill({ contentType: 'text/html', body: await readFile('dist/index.html', 'utf8') })
  })
  await page.goto(`${origin}/test-list/1/console`)
  const alarm = page.locator('.alarm-panel')
  await expect(alarm).toContainText('检测柜通道离线')
  await expect(alarm).toContainText('采集器离线')
  await expect(alarm).not.toContainText('超限')
  await expect(alarm).not.toContainText('暂无异常')
  await alarm.screenshot({ path: `${output}/console-alarm.png` })
  await page.setViewportSize({ width: 390, height: 844 })
  await expect(alarm).toContainText('采集器离线')
  assert.equal(await alarm.evaluate(element => element.scrollWidth > element.clientWidth), false)
  await alarm.screenshot({ path: `${output}/console-alarm-390.png` })
  await page.setViewportSize({ width: 1440, height: 1000 })
  for (const status of ['PAUSED', 'ENDED', 'RECEIVING']) {
    receptionStatus = status
    await page.reload()
    if (status === 'RECEIVING') await expect(alarm).toContainText('采集器离线')
    else await expect(alarm).toContainText('暂无异常')
  }
  if (process.argv[2] !== 'console') {
    await page.goto(`${origin}/exception`)
    const table = page.locator('.exception-page table')
    await expect(table.locator('tbody tr')).toHaveCount(2)
    await expect(table).toContainText('设备离线')
    await expect(table).toContainText('2026-09-05')
    await expect(table).not.toContainText('null')
    await table.locator('tbody tr').first().getByRole('button', { name: '处理', exact: true }).click()
    await page.getByRole('textbox').last().fill('已检查设备连接')
    await page.getByRole('button', { name: '完成处理' }).click()
    await expect(table).toContainText('已处理')
    await page.reload()
    await expect(table).toContainText('已处理')
    await page.screenshot({ path: `${output}/exception-management.png`, fullPage: true, animations: 'disabled' })
  }
  assert.deepEqual(errors, [])
  console.log('离线告警浏览器验证通过:接收中告警、暂停和结束隐藏、刷新及异常记录处理。')
} finally {
  await browser.close()
}