diff --git a/admin-frontend-vue/tests/task-34.test.ts b/admin-frontend-vue/tests/task-34.test.ts new file mode 100644 index 00000000..06045053 --- /dev/null +++ b/admin-frontend-vue/tests/task-34.test.ts @@ -0,0 +1,55 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { readSource } from './helpers.ts' +import { firstVisiblePath } from '../src/router/helpers.ts' + +test('test_task_034_unauthorized_fallback_normal_primary_path', () => { + // 正常主路径:越权页面回退到菜单树首个可见页面。 + const tree = [{ key: 'g', name: '组', children: [{ key: 'users', name: '用户管理', route: '/account/users' }] }] + assert.equal(firstVisiblePath(tree), '/account/users') +}) + +test('test_task_034_unauthorized_fallback_normal_variant_input', () => { + // 正常变体:多个可路由页面时取深度优先的首个。 + const tree = [ + { key: 'a', name: 'A', route: '/a', sort: 9 }, + { key: 'b', name: 'B', route: '/b', sort: 1 }, + ] + assert.equal(firstVisiblePath(tree), '/a', '前端保序,不按 sort 重排') +}) + +test('test_task_034_unauthorized_fallback_normal_repeated_operation_is_idempotent', () => { + // 正常重复:查找不修改树、结果稳定。 + const tree = [{ key: 'g', name: '组', children: [{ key: 'p', name: '页', route: '/p' }] }] + assert.equal(firstVisiblePath(tree), firstVisiblePath(tree)) +}) + +test('test_task_034_unauthorized_fallback_boundary_empty_input', () => { + // 边界空值:无任何可见页面时回退空串,由守卫回根路由空态。 + assert.equal(firstVisiblePath([]), '') + assert.equal(firstVisiblePath(null), '') +}) + +test('test_task_034_unauthorized_fallback_boundary_single_item', () => { + // 边界单元素:单个页面即回退目标。 + assert.equal(firstVisiblePath([{ key: 's', name: '单', route: '/solo' }]), '/solo') +}) + +test('test_task_034_unauthorized_fallback_boundary_limit_or_missing_field', () => { + // 边界上限/缺字段:深层叶页面也被当作回退目标。 + const tree = [{ key: 'g', name: '组', children: [{ key: 'g2', name: '子组', children: [{ key: 'deep', name: '深', route: '/deep' }] }] }] + assert.equal(firstVisiblePath(tree), '/deep') +}) + +test('test_task_034_unauthorized_fallback_invalid_input_rejected', () => { + // 异常输入:畸形节点(无 route 无 children)被跳过,不影响回退目标。 + const tree = [{ key: 'bad', name: '坏' }, { key: 'ok', name: '好', route: '/ok' }] + assert.equal(firstVisiblePath(tree), '/ok') +}) + +test('test_task_034_unauthorized_fallback_dependency_failure_returns_actionable_message', () => { + // 依赖失败:路由守卫对越权页以首个可见页/根路由作为回退目标。 + const router = readSource('src/router/index.ts') + assert.match(router, /session\.firstVisible\s*\|\|\s*'\/'/, '越权页回退首个可见页,全无则根路由') + assert.match(router, /isRouteAllowed/) +})