import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { resolve, dirname } from 'node:path' import { taskStatusClass, taskStatusText } from '../src/shared/utils/task-status-text.ts' const here = dirname(fileURLToPath(import.meta.url)) const tabSource = readFileSync( resolve(here, '../src/pages/brand/components/BrandAppearancePatentTab.vue'), 'utf-8', ) const apiSource = readFileSync( resolve(here, '../src/shared/api/types/modules/appearance-patent.ts'), 'utf-8', ) function functionBody(name: string): string { const match = tabSource.match(new RegExp(`function ${name}\\([\\s\\S]*?\\n\\}`)) assert.ok(match, `找不到函数 ${name}`) return match[0] } // ---- 状态显示:已中断不能掉进「等待中」兜底(task-status-text.ts 点名过的取消态误显形状)---- test('test_cancelled_status_shows_interrupted_text', () => { const body = functionBody('statusText') const cancelledIdx = body.indexOf("'CANCELLED'") assert.ok(cancelledIdx >= 0, 'statusText 缺少 CANCELLED 分支') assert.match(body, /已中断/, 'CANCELLED 必须显示「已中断」') // CANCELLED 分支必须先于结果生成中/已判死等分支(它自己就是终态,不用等文件) const preparingIdx = body.indexOf('isResultPreparing(item)') assert.ok(cancelledIdx < preparingIdx, 'CANCELLED 必须在 isResultPreparing 之前判定') }) test('test_cancelled_class_uses_failed_style_from_shared_map', () => { const body = functionBody('statusClass') assert.match(body, /taskStatusClass\('CANCELLED'\)/, 'CANCELLED 样式必须由公共映射产出') assert.equal(taskStatusClass('CANCELLED'), 'failed', '公共映射里 CANCELLED 应产出 failed 样式') assert.equal(taskStatusText('CANCELLED'), '已取消', '公共文案表必须认 CANCELLED(别让别的 Tab 掉兜底)') }) // ---- 中断按钮:在跑/待跑可中断,点按有确认、调对接口、动作后刷新 ---- test('test_interrupt_button_conditions_cover_active_states', () => { const body = functionBody('canInterruptTask') assert.match(body, /item\?\.taskId/, '没有 taskId 的行不能显示中断') for (const status of ['PENDING', 'RUNNING', 'SCHEDULED']) { assert.ok(body.includes(`'${status}'`), `ACTIVE 态 ${status} 必须可中断`) } // 终态(SUCCESS/FAILED/CANCELLED)不得出现在可中断集合里 assert.ok(!body.includes("'SUCCESS'"), '终态任务不该显示中断按钮') }) test('test_interrupt_action_confirms_then_calls_api_and_refreshes', () => { const body = functionBody('interruptTaskRecord') const confirmIdx = body.indexOf('ElMessageBox.confirm') const apiIdx = body.indexOf('interruptAppearancePatentTask(') assert.ok(confirmIdx >= 0, '中断必须先弹确认框(未完成行不再检测是用户要知道的后果)') assert.match(body, /已完成检测的数据会生成结果文件/, '确认文案要说明中断后已完成部分可下载') assert.ok(apiIdx > confirmIdx, '确认通过后才调中断接口') assert.match(body, /refreshBoard\(\)/, '中断后必须刷新任务面板') }) test('test_interrupt_button_rendered_in_both_action_slots', () => { const template = tabSource.slice(0, tabSource.indexOf('= 0 && historySlot > currentSlot, '模板缺当前/历史两个动作槽') assert.equal( (template.match(/btn-interrupt/g) || []).length, 2, '中断按钮必须同时出现在当前任务与历史任务两个动作槽', ) }) // ---- 继续:已中断任务复用重试链路,文案与确认语义不同 ---- test('test_retry_accepts_cancelled_and_labels_continue', () => { const retryBody = functionBody('canRetryTask') assert.ok(retryBody.includes("'FAILED'") && retryBody.includes("'CANCELLED'"), '重试条件必须放宽到已中断(继续)') const labelBody = functionBody('retryLabel') assert.match(labelBody, /'CANCELLED'/, '按钮文案必须按 CANCELLED 切换') assert.match(labelBody, /继续/, '已中断的任务显示「继续」') }) test('test_continue_warns_about_partial_file_replacement', () => { const body = functionBody('retryTaskRecord') const confirmIdx = body.indexOf('ElMessageBox.confirm') const apiIdx = body.indexOf('retryAppearancePatentTask(') assert.ok(confirmIdx >= 0, '带部分结果的中断任务点「继续」要弹确认') assert.match(body, /请先下载留存/, '确认文案要提醒部分结果文件将被替换') assert.ok(apiIdx > confirmIdx, '确认通过后才调重试接口') }) // ---- API:走通用中断端点,带 user_id 归属 ---- test('test_interrupt_api_uses_generic_task_endpoint', () => { const body = apiSource.match(/export function interruptAppearancePatentTask[\s\S]*?\n\}/) assert.ok(body, '缺少 interruptAppearancePatentTask API') assert.match(body[0], /\/tasks\/\$\{taskId\}\/interrupt/, '必须打 POST /api/tasks/{taskId}/interrupt') assert.match(body[0], /user_id/, '必须带 user_id(普通账号只能中断自己名下的任务)') }) // ---- 轮询收敛:已中断任务要能脱离轮询,不能一直挂「处理中」 ---- test('test_polling_settles_on_cancelled', () => { assert.match(tabSource, /settleOnTaskStatuses: \['SUCCESS', 'FAILED', 'CANCELLED'\]/, 'CANCELLED 必须在轮询收敛集合里') })