import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { parse as parseSfc } from '@vue/compiler-sfc'

const scriptDir = path.dirname(fileURLToPath(import.meta.url))
const viewPath = path.resolve(scriptDir, '../src/views/model/ModelManageView.vue')
const source = await readFile(viewPath, 'utf8')
const { descriptor, errors } = parseSfc(source, { filename: viewPath })

assert.equal(errors.length, 0, `模型管理页面模板无法解析：${errors[0]}`)
assert.ok(descriptor.template?.content.trim(), '模型管理页面缺少可渲染模板')
assert.equal((source.match(/<template>/g) || []).length, 1, '模型管理页面只能有一个根模板标签')

const configTableStart = source.indexOf('v-if="activeTab === \'config\'"')
const trainedTableStart = source.indexOf('<DataTablePage\n      v-else')
assert.ok(configTableStart >= 0 && trainedTableStart > configTableStart, '模型管理页面缺少两个标签对应的列表分支')
assert.doesNotMatch(
  source.slice(configTableStart, trainedTableStart),
  /activeTab === 'trained'/,
  '配置模型分支不应再比较已不可能的训练模型状态',
)
assert.doesNotMatch(
  source.slice(trainedTableStart),
  /activeTab === 'config'/,
  '训练模型分支不应再比较已不可能的配置模型状态',
)

console.log('模型管理页面模板回归检查通过')
