import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import type { ImportConflict } from '@/shared'
import { conflictLabel } from '../components/baseData/entrySelection'
/**
* 「从已有名单勾选报名」必须消费服务端返回的 conflicts(P2 A12)。
*
* 勾选报名复用的是 /entries/import:服务端把「命中了库里已有人员、但字段对不上」的行放进 conflicts,
* 这些行**既没建报名也没报错**。组件原本无条件 toast.success('报名已加入'),
* 于是勾过的行一条都没进去、界面却说已加入,用户也就不会去导入页处理冲突。
*
* .vue 组件跑不进 node --test(无 jsdom,见 entryGuards.ts:6),所以按既有做法读源码断言:
* 成功提示只许长在 conflicts 为空的分支里,冲突条数要带进提示与页内明细,并说清去哪确认。
*/
const PICKER_SOURCE = readFileSync(new URL('../components/baseData/EntryPicker.vue', import.meta.url), 'utf8')
const IMPORT_SOURCE = readFileSync(new URL('../components/baseData/ImportSection.vue', import.meta.url), 'utf8')
/** 截出 importSelection 的函数体(到下一个顶格 `}` 为止),断言都限定在这段里。 */
function importSelectionBody(): string {
const start = PICKER_SOURCE.indexOf('async function importSelection')
assert.notEqual(start, -1, 'EntryPicker.vue 里找不到 importSelection')
const end = PICKER_SOURCE.indexOf('\n}', start)
assert.notEqual(end, -1, 'importSelection 没有正常闭合')
return PICKER_SOURCE.slice(start, end)
}
/** 取出 `if (result.conflicts.length === 0) { … }` 这一对花括号内的源码。 */
function emptyConflictsBranch(body: string): string {
const marker = 'if (result.conflicts.length === 0) {'
const start = body.indexOf(marker)
assert.notEqual(start, -1, 'importSelection 里没有「conflicts 为空才算成功」的分支')
let depth = 0
for (let i = start + marker.length - 1; i < body.length; i += 1) {
if (body[i] === '{') depth += 1
else if (body[i] === '}') {
depth -= 1
if (depth === 0) return body.slice(start + marker.length, i)
}
}
assert.fail('conflicts 为空的分支没有闭合')
}
test('有冲突时不出现成功提示:toast.success 只长在 conflicts 为空的分支里', () => {
const body = importSelectionBody()
const successIndex = body.indexOf('toast.success')
assert.notEqual(successIndex, -1, '全成功时仍要提示成功')
const branch = emptyConflictsBranch(body)
const branchStart = body.indexOf(branch)
assert.ok(
successIndex > branchStart && successIndex < branchStart + branch.length,
'toast.success 跑到了 conflicts 非空的路径上——冲突行没建报名却提示「已加入」',
)
// 页面里只该有这一处成功提示,避免别处又无条件说「已加入」。
assert.equal([...PICKER_SOURCE.matchAll(/toast\.success\(/g)].length, 1)
})
test('全成功时仍提示成功,且带上服务端的计数', () => {
const branch = emptyConflictsBranch(importSelectionBody())
assert.match(branch, /toast\.success\('报名已加入'/)
assert.match(branch, /新增 \$\{result\.created\} · 更新 \$\{result\.updated\} · 跳过 \$\{result\.skipped\}/)
})
test('冲突条数被正确传递:收下 conflicts 并写进失败提示', () => {
const body = importSelectionBody()
assert.match(body, /conflicts\.value = result\.conflicts/, '要把服务端给的冲突行收进界面状态')
assert.match(body, /有 \$\{result\.conflicts\.length\} 行没有加入,需要逐条确认/)
})
test('冲突明细显示在页内,逐条给出服务端的行号与决策键', () => {
assert.match(PICKER_SOURCE, /v-if="conflicts\.length > 0"/)
assert.match(PICKER_SOURCE, /v-for="conflict in conflicts"/)
assert.match(PICKER_SOURCE, /\{\{ conflict\.row \}\}/)
assert.match(PICKER_SOURCE, /conflictLabel\(conflict\)/)
})
test('页内明细复用冲突面板的决策键标签(键为空时不留空白)', () => {
const conflict: ImportConflict = {
row: 3,
idCard: '',
bib: '101',
athleteId: 7,
duplicate: false,
existing: { name: '张伟', gender: null, age: null, teamCode: 'F1', employeeNo: null, idCard: null, bib: null },
incoming: { name: '张伟', gender: null, age: null, teamCode: 'F1', employeeNo: null, idCard: null, bib: null },
changedFields: [],
}
assert.equal(conflictLabel(conflict), '101', '证件号为空时用号码布')
assert.equal(conflictLabel({ ...conflict, idCard: '110101199001011234' }), '110101199001011234')
})
test('提示说清去哪确认:本页「报名导入」分区的冲突面板 / resolve 接口', () => {
const body = importSelectionBody()
// 面板在同一个页面(ImportSection.vue 里先渲染 、再是「报名导入」卡片),
// 所以文案必须点名卡片标题,否则用户不知道去哪找。
assert.match(body, /本页「报名导入」分区的冲突面板/)
assert.match(IMPORT_SOURCE, /报名导入<\/span>/)
assert.match(body, /POST \/api\/entries\/import\/resolve/)
})
test('复用 EntryImportResult.conflicts 的类型,不另立一套', () => {
assert.match(PICKER_SOURCE, /import type \{ Athlete, EntryWithResult, ImportConflict \} from '@\/shared'/)
})
test('冲突明细照抄 ImportSection.vue 的渲染范式,没有自创样式', () => {
for (const className of ['.errors-head', '.error-list']) {
assert.ok(IMPORT_SOURCE.includes(className), `ImportSection.vue 里应有 ${className}`)
assert.ok(PICKER_SOURCE.includes(className), `EntryPicker.vue 里应照抄 ${className}`)
}
assert.match(PICKER_SOURCE, /class="mx-badge mx-badge-warning">有 \{\{ conflicts\.length \}\} 行没有加入,需要逐条确认/)
// 列冲突用的还是那一套 class,不是新造的面板。
assert.match(PICKER_SOURCE, //)
assert.match(IMPORT_SOURCE, /