8b07321fbb
state 工厂返回全新对象含 user/menuTree/initialized/loading/error 五字段, 默认 null/[]/false/''/'',无凭证字段;仅一个 defineStore,守卫与 getter 依赖该初始态。8 用例全过。
65 lines
3.1 KiB
TypeScript
65 lines
3.1 KiB
TypeScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readSource } from './helpers.ts'
|
|
|
|
const store = readSource('src/stores/admin-session.ts')
|
|
|
|
test('test_task_027_store_initial_state_normal_primary_path', () => {
|
|
// 正常主路径:state 以函数返回全新对象,含 5 个契约字段。
|
|
assert.match(store, /state:\s*\(\)\s*=>\s*\(\{/, 'state 必须是返回对象字面量的工厂函数')
|
|
assert.match(store, /user:\s*null/)
|
|
assert.match(store, /menuTree:\s*\[\]/)
|
|
assert.match(store, /initialized:\s*false/)
|
|
assert.match(store, /loading:\s*false/)
|
|
assert.match(store, /error:\s*''/)
|
|
})
|
|
|
|
test('test_task_027_store_initial_state_normal_variant_input', () => {
|
|
// 正常变体:状态字段与模块契约字段一一对应,供 AdminLayout / guard 消费。
|
|
assert.match(store, /user\b/)
|
|
assert.match(store, /menuTree\b/)
|
|
assert.match(store, /initialized\b/)
|
|
assert.match(store, /loading\b/)
|
|
assert.match(store, /error\b/)
|
|
})
|
|
|
|
test('test_task_027_store_initial_state_normal_repeated_operation_is_idempotent', () => {
|
|
// 正常重复:只声明一个 Pinia store 定义,实例由 useStore 工厂创建,不共享可变单例。
|
|
assert.equal((store.match(/defineStore\(/g) || []).length, 1, '仅定义一个 admin-session store')
|
|
assert.match(store, /export const useAdminSessionStore\s*=\s*defineStore/)
|
|
})
|
|
|
|
test('test_task_027_store_initial_state_boundary_empty_input', () => {
|
|
// 边界空值:未登录时 user 为 null、error 为空串,而不是 undefined 残留。
|
|
assert.match(store, /user:\s*null\s+as\s+AdminUser/)
|
|
assert.match(store, /error:\s*''/)
|
|
})
|
|
|
|
test('test_task_027_store_initial_state_boundary_single_item', () => {
|
|
// 边界单元素:初始时无任何用户/菜单,首次进入路由需触发初始化。
|
|
assert.match(store, /initialized:\s*false/, '未初始化才能触发首次初始化')
|
|
const router = readSource('src/router/index.ts')
|
|
assert.match(router, /if\s*\(!session\.initialized\)/, '守卫在未初始化时调用 initialize')
|
|
})
|
|
|
|
test('test_task_027_store_initial_state_boundary_limit_or_missing_field', () => {
|
|
// 边界上限/缺字段:初始状态不携带任何凭证字段,凭证只走同源 Cookie。
|
|
assert.equal(/\btoken\b|\bjwt\b|\bpassword\b/.test(store), false, 'store 初始状态不得保存凭证')
|
|
})
|
|
|
|
test('test_task_027_store_initial_state_invalid_input_rejected', () => {
|
|
// 异常输入:非法/未知状态字段不应出现(state 形状稳定)。
|
|
assert.equal(/\bnickname\b|\bavatar\b/.test(store), false, 'store 不引入契约外字段')
|
|
assert.match(store, /import type \{ AdminMenuNode, AdminUser \}/)
|
|
})
|
|
|
|
test('test_task_027_store_initial_state_dependency_failure_returns_actionable_message', () => {
|
|
// 依赖失败:store 依赖会话 API 与 pinia,状态字段供 getter/action 消费。
|
|
assert.match(store, /import \{ defineStore \} from 'pinia'/)
|
|
assert.match(store, /fetchCurrentUser/)
|
|
assert.match(store, /fetchAdminMenuTree/)
|
|
assert.match(store, /getters:/)
|
|
assert.match(store, /firstVisible:/)
|
|
assert.match(store, /isSuperAdmin:/)
|
|
})
|