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, routerSource, toolsSource] = await Promise.all([
  readFile(path.join(sourceRoot, 'views/data-convert/DataConvertView.vue'), 'utf8'),
  readFile(path.join(sourceRoot, 'router/index.ts'), 'utf8'),
  readFile(path.join(sourceRoot, 'views/tools/ToolsView.vue'), 'utf8'),
])

assert.match(viewSource, /JSON 转 JSONL/, '转换页缺少明确的格式标题')
assert.match(viewSource, /class="upload-zone"/, '转换页缺少源文件上传区域')
assert.match(viewSource, /class="converter-form"/, '转换页缺少标准表单区域')
assert.match(viewSource, /class="form-row"/, '转换页缺少输出配置区域')
assert.match(viewSource, /开始转换/, '转换页缺少主操作按钮')
assert.match(viewSource, /当前为 UI 原型/, '转换页没有明确说明 UI 原型范围')
assert.match(viewSource, /<el-button type="primary" disabled>/, '未接入功能前主操作按钮必须禁用')
assert.match(viewSource, /var\(--primary-color\)/, '转换页没有使用项目主色变量')
assert.match(viewSource, /var\(--el-color-primary-light-9\)/, '转换页没有使用项目主色浅色变量')
assert.doesNotMatch(viewSource, /#1890ff/i, '转换页仍包含未对齐当前主题的旧蓝色')
assert.match(viewSource, /\.converter-panel\s*\{[\s\S]*?width:\s*100%;/, '转换区域没有铺满页面宽度')
assert.match(viewSource, /min-height:\s*calc\(100vh\s*-\s*220px\)/, '转换区域没有铺满页面可用高度')
assert.doesNotMatch(viewSource, /max-width:\s*860px/, '转换区域仍被限制为窄卡片')

// 当前迭代只允许界面开发，防止误接入文件读取、解析、转换或下载逻辑。
for (const forbiddenImplementation of [
  /FileReader/,
  /\.text\(\)/,
  /JSON\.parse/,
  /new Blob/,
  /createObjectURL/,
]) {
  assert.doesNotMatch(viewSource, forbiddenImplementation, 'UI 原型中不应包含实际转换实现')
}

assert.match(
  routerSource,
  /path:\s*['"]data-convert['"][\s\S]*?DataConvertView\.vue/,
  '数据类型转换路由未接入新页面',
)
assert.doesNotMatch(
  routerSource,
  /path:\s*['"]data-convert['"][\s\S]{0,180}?PlaceholderView\.vue/,
  '数据类型转换路由仍指向占位页',
)
assert.match(
  toolsSource,
  /id === ['"]json2jsonl['"][\s\S]{0,100}?router\.push\(['"]\/data-convert['"]\)/,
  '其他工具中的 JSON 转 JSONL 卡片未接入转换页',
)

console.log('JSON 转 JSONL UI 原型回归检查通过')
