80 lines
3.1 KiB
TypeScript
80 lines
3.1 KiB
TypeScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readSource } from './helpers.ts'
|
||
import {
|
||
actionableErrorText,
|
||
busyLabel,
|
||
endBusy,
|
||
isBusy,
|
||
normalizeFeedback,
|
||
tryBeginBusy,
|
||
} from '../src/components/admin-feedback.ts'
|
||
|
||
// module 13 task 246:反馈语义统一 —— 成功/错误/警告通道、is-busy 防重入、忙碌文案「保存中…」、
|
||
// 网络失败给可行动文案。
|
||
|
||
test('test_task_246_feedback_normal_primary_path', () => {
|
||
// 正常主路径:成功/错误/警告各有默认文案与语义 kind。
|
||
const ok = normalizeFeedback(undefined, 'success')
|
||
assert.equal(ok.kind, 'success')
|
||
assert.equal(ok.message, '操作成功')
|
||
const err = normalizeFeedback(undefined, 'error')
|
||
assert.equal(err.kind, 'error')
|
||
assert.equal(err.message, '操作失败,请稍后重试')
|
||
const warn = normalizeFeedback(undefined, 'warning')
|
||
assert.equal(warn.kind, 'warning')
|
||
})
|
||
|
||
test('test_task_246_feedback_normal_variant_input', () => {
|
||
// 正常变体:显式 message 保留并去首尾空白。
|
||
assert.equal(normalizeFeedback(' 已保存 ', 'success').message, '已保存')
|
||
})
|
||
|
||
test('test_task_246_feedback_normal_repeated_operation_is_idempotent', () => {
|
||
// 正常重复:无状态污染。
|
||
const a = normalizeFeedback('A', 'error')
|
||
const b = normalizeFeedback('A', 'error')
|
||
assert.deepEqual(a, b)
|
||
})
|
||
|
||
test('test_task_246_feedback_boundary_empty_input', () => {
|
||
// 边界空:未指定 kind 时空消息落 warning;空消息回退默认。
|
||
const none = normalizeFeedback()
|
||
assert.equal(none.kind, 'warning')
|
||
assert.equal(none.message, '请确认后再继续')
|
||
})
|
||
|
||
test('test_task_246_feedback_boundary_single_item', () => {
|
||
// 边界单元素:忙碌文案补「中…」;以「中」结尾则不重复。
|
||
assert.equal(busyLabel('保存'), '保存中…')
|
||
assert.equal(busyLabel('导出'), '导出中…')
|
||
assert.equal(busyLabel('保存中'), '保存中…')
|
||
})
|
||
|
||
test('test_task_246_feedback_boundary_limit_or_missing_field', () => {
|
||
// 边界上限:busy 锁单例防重入。
|
||
assert.equal(isBusy(), false)
|
||
assert.equal(tryBeginBusy(), true)
|
||
assert.equal(tryBeginBusy(), false)
|
||
assert.equal(isBusy(), true)
|
||
endBusy()
|
||
assert.equal(isBusy(), false)
|
||
})
|
||
|
||
test('test_task_246_feedback_invalid_input_rejected', () => {
|
||
// 异常:Error/字符串/未知错误都转成可行动文案,空值回默认错误。
|
||
assert.equal(actionableErrorText(new Error('接口超时')), '接口超时')
|
||
assert.equal(actionableErrorText('网络不可用'), '网络不可用')
|
||
assert.equal(actionableErrorText(null), '操作失败,请稍后重试')
|
||
})
|
||
|
||
test('test_task_246_feedback_dependency_failure_returns_actionable_message', () => {
|
||
// 依赖失败:纯模型不依赖 Element;UI 层接入 ElMessage 且给足 success/error/warning。
|
||
const pure = readSource('src/components/admin-feedback.ts')
|
||
assert.equal(/element-plus|ElMessage/.test(pure), false, '纯模型不得引入 Element')
|
||
const ui = readSource('src/components/admin-feedback-ui.ts')
|
||
assert.match(ui, /ElMessage/)
|
||
assert.match(ui, /showAdminFeedback/)
|
||
assert.match(ui, /runBusy/)
|
||
})
|