import test from 'node:test' import assert from 'node:assert/strict' import { readSource } from './helpers.ts' import { listPhaseOf, userListEmptyHint, userListErrorHint, type ListPhase, type UserListPhaseInput } from '../src/pages/account/user-list-state.ts' test('test_task_052_user_list_state_normal_primary_path', () => { // 正常主路径:有数据且无错 → ready。 const input: UserListPhaseInput = { loading: false, error: '', items: [{ id: 1 }], total: 1 } assert.equal(listPhaseOf(input), 'ready') }) test('test_task_052_user_list_state_normal_variant_input', () => { // 正常变体:加载中优先于其它;空数据且有 total 0 → empty。 assert.equal(listPhaseOf({ loading: true, error: '', items: [], total: 0 }), 'loading') assert.equal(listPhaseOf({ loading: false, error: '', items: [], total: 0 }), 'empty') }) test('test_task_052_user_list_state_normal_repeated_operation_is_idempotent', () => { // 正常重复:纯函数不改输入、结果稳定。 const input: UserListPhaseInput = { loading: false, error: 'x', items: [], total: 0 } assert.equal(listPhaseOf(input), listPhaseOf(input)) assert.equal(input.error, 'x') }) test('test_task_052_user_list_state_boundary_empty_input', () => { // 边界空值:空对象按默认(无错无加载) → empty,不崩溃。 assert.equal(listPhaseOf({} as never), 'empty') assert.ok(userListEmptyHint()) assert.ok(userListErrorHint('网络异常')) }) test('test_task_052_user_list_state_boundary_single_item', () => { // 边界单元素:单条记录 ready。 assert.equal(listPhaseOf({ loading: false, error: '', items: [{ id: 7 }], total: 1 }), 'ready') }) test('test_task_052_user_list_state_boundary_limit_or_missing_field', () => { // 边界上限/缺字段:items 空但 total>0 仍视为空(首屏缺行);缺 total 按 0 处理。 assert.equal(listPhaseOf({ loading: false, error: '', items: [], total: 5 }), 'empty') assert.equal(listPhaseOf({ loading: false, error: '', items: [{ id: 1 }] } as never), 'ready') }) test('test_task_052_user_list_state_invalid_input_rejected', () => { // 异常输入:error 存在且非加载 → error 优先于 empty/ready。 assert.equal(listPhaseOf({ loading: false, error: '无权限', items: [{ id: 1 }], total: 9 }), 'error') assert.match(userListErrorHint('服务器错误'), /服务器错误/) }) test('test_task_052_user_list_state_dependency_failure_returns_actionable_message', () => { // 依赖失败/状态落页面局部:UsersPage 有 error 状态、失败可重试,且复用局部状态模块。 const mod = readSource('src/pages/account/user-list-state.ts') assert.equal(/axios|http\.|defineStore/.test(mod), false, '列表状态模型保持纯逻辑') const page = readSource('src/pages/account/UsersPage.vue') assert.match(page, /user-list-state/) assert.match(page, /error/) // 显式错误态 assert.match(page, /loadUsers|retry|reload/i) })