import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import path from 'node:path'

const scriptDir = path.dirname(fileURLToPath(import.meta.url))
const sourceRoot = path.resolve(scriptDir, '../src')

const [viewSource, typeSource, mockSource] = await Promise.all([
  readFile(path.join(sourceRoot, 'views/system/HardwareView.vue'), 'utf8'),
  readFile(path.join(sourceRoot, 'types/index.ts'), 'utf8'),
  readFile(path.join(sourceRoot, 'mock/data.ts'), 'utf8'),
])

assert.match(viewSource, /title="平台性能"/, '平台性能页缺少明确标题')
assert.match(viewSource, /硬件资源概览/, '平台性能页缺少硬件资源概览')
assert.match(viewSource, /系统资源趋势/, '平台性能页缺少资源趋势')
assert.match(viewSource, /GPU 资源池/, '平台性能页缺少 GPU 资源池')
assert.match(viewSource, /gpu-detail-drawer/, 'GPU 卡片缺少详情抽屉')
assert.match(viewSource, /GPU 进程/, 'GPU 详情缺少占用进程表')
assert.match(viewSource, /visibilitychange/, '轮询没有在页面不可见时暂停')
assert.match(viewSource, /requesting \|\| disposed/, '轮询缺少防重入保护')
assert.match(viewSource, /slice\(-60\)/, '趋势快照没有限制为最近 60 个采样点')
assert.match(viewSource, /@keydown="handleGpuKeydown/, 'GPU 卡片缺少键盘访问能力')
assert.match(viewSource, /download_mb_s/, '网络概览没有使用明确的实时速率字段')
assert.doesNotMatch(
  viewSource,
  /download_mb_s\s*\?\?\s*info\.network\?\.download_mb/,
  '不能把累计网络流量回退显示为实时速率',
)

assert.match(typeSource, /export interface GpuProcess/, '系统监控类型缺少 GPU 进程结构')
assert.match(typeSource, /processes\?: GpuProcess\[\]/, 'GPU 类型缺少进程详情')
assert.match(typeSource, /download_mb_s\?: number/, '系统类型缺少实时网络接收速率')
assert.match(typeSource, /power_limit_w\?: number/, 'GPU 类型缺少功耗上限')

for (let id = 0; id < 8; id += 1) {
  assert.match(mockSource, new RegExp(`id:\\s*${id},[\\s\\S]*?uuid:\\s*'GPU-MOCK-A800-0${id}'`), `Mock 缺少 GPU ${id} 详情`)
}

console.log('平台性能页回归检查通过')
