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 typesPath = path.resolve(scriptDir, '../src/types/index.ts')
const dataPath = path.resolve(scriptDir, '../src/mock/data.ts')
const viewPath = path.resolve(scriptDir, '../src/views/dataset/DatasetListView.vue')
const tablePagePath = path.resolve(scriptDir, '../src/components/DataTablePage.vue')

const [typesSource, dataSource, viewSource, tablePageSource] = await Promise.all([
  readFile(typesPath, 'utf8'),
  readFile(dataPath, 'utf8'),
  readFile(viewPath, 'utf8'),
  readFile(tablePagePath, 'utf8'),
])

assert.match(typesSource, /export type DatasetSource = 'upload' \| 'task'/)
assert.match(typesSource, /source\?: DatasetSource/)
assert.match(typesSource, /task_name\?: string/)

assert.equal((dataSource.match(/source: 'upload'/g) || []).length, 6)
assert.equal((dataSource.match(/source: 'task'/g) || []).length, 4)

for (const name of [
  '客服对话清洗集',
  '通用指令构造集',
  '用户反馈脱敏集',
  '多轮对话增强集',
]) {
  const datasetLine = dataSource.split('\n').find((line) => line.includes(`name: '${name}'`))
  assert.ok(datasetLine, `缺少数据任务 Mock：${name}`)
  assert.match(datasetLine, /source: 'task'/, `${name} 必须标记为数据任务来源`)
  assert.match(datasetLine, /task_name: '/, `${name} 必须提供来源任务名称`)
}

assert.match(viewSource, /route\.query\.tab === 'task' \? 'task' : 'upload'/)
assert.doesNotMatch(viewSource, /hasSplitCounts|splitLabels/)
assert.match(viewSource, /DATASET_TYPE_MAP\[String\(row\.type\)\.toLowerCase\(\)\]/)
assert.match(
  viewSource,
  /if \(activeTab\.value === 'task'\) \{\s*return dataList\.value\.filter\(\(item\) => item\.source === 'task'\)\s*\}/,
)
assert.match(viewSource, /return dataList\.value\.filter\(\(item\) => item\.source !== 'task'\)/)
assert.doesNotMatch(viewSource, /数据任务产生的数据集[\s\S]*?return \[\]/)
assert.match(viewSource, /:search-fields="activeTab === 'task' \? \['task_id', 'task_name'\] : \['name'\]"/, '搜索字段应与当前页签展示的名称一致')
assert.match(viewSource, /activeTab === 'task' \? '训练任务名称' : '数据集名称'/, '数据任务页应展示训练任务名称表头')
assert.match(viewSource, /activeTab === 'task' \? \(row\.task_name \|\| '-'\) : row\.name/, '数据任务页应展示接口返回的来源任务名称')
assert.match(viewSource, /formatMegabytes\(row\.size_bytes, row\.size\)/, '数据集大小应统一转换为 MB')
assert.match(viewSource, /<el-table-column label="版本"/, '描述列应替换为真实版本列')
assert.doesNotMatch(viewSource, /<el-table-column label="描述"/, '列表不应继续显示描述列')
assert.match(viewSource, /current_version_nos/, '版本列应使用接口返回的真实当前版本号')
assert.match(viewSource, /:multi-select="activeTab === 'task' && batchMode"/, '多选框只能在数据任务的批量模式显示')
assert.match(viewSource, /:show-batch-bar="false"/, '数据任务应使用搜索框旁的批量删除入口')
assert.match(viewSource, /<template #toolbar-extra>[\s\S]*?批量删除[\s\S]*?v-if="selectedCount > 0"[\s\S]*?删除（\{\{ selectedCount \}\}）/, '批量删除按钮没有按选择状态切换')
assert.match(viewSource, /const completed = await tablePageRef\.value\?\.handleBatchDelete\(\)/, '批量删除按钮没有调用通用批量删除流程')
assert.match(viewSource, /ElMessageBox\.confirm\([\s\S]*?删除后无法恢复/, '单条删除仍缺少二次确认')
assert.match(tablePageSource, /emit\('selectionChange', rows\)/, '通用表格没有向外同步多选状态')
assert.match(tablePageSource, /function clearSelection\(\)[\s\S]*?emit\('selectionChange', \[\]\)/, '清空选择后没有同步重置外部选择状态')
assert.match(tablePageSource, /确认批量删除[\s\S]*?confirmButtonText: '删除'[\s\S]*?confirmButtonClass: 'el-button--danger'/, '批量删除确认弹窗缺少危险操作样式')
assert.match(tablePageSource, /let ok = 0[\s\S]*?let fail = 0[\s\S]*?for \(const row of rows\)/, '批量删除没有汇总逐条删除结果')
assert.match(tablePageSource, /defineExpose\(\{ handleDelete, handleBatchDelete, clearSelection \}\)/, '页面无法触发通用批量删除')

console.log('数据任务 Mock 数据与页签分流回归检查通过')
