Files
crawler-plugin/admin-frontend-vue/tests/task-48.test.ts
T
huangzd1997 95e2318c42 feat(账号到期+公司超管): 级联过期判定、过期停任务巡检与两端续费提醒弹窗
需求两条:站点超管可开设其他公司的超级管理员(站点超管看全站、公司超管只看本公司、
组内管理员仍只看本组);账号要有过期时间,管理员过期后名下普通账户一并停用、
正在跑的任务也要停掉。落库只有 users.expires_at(V146,NULL=永不过期),级联一律现算,
不写"上级过期就把下级置成过期"那种续费也救不回来的数据。

- AccountExpiryService:生效到期 = min(自身, 本公司公司超管, 成员表组长, 创建人)。
  登录拦 403(文案给续费提示)、每次请求鉴权拦 401(前端按下线踢回登录页),super_admin
  永不适用。级联口径与数据可见范围同源(成员表基本未用、实际按 created_by_id 派生)。
- ExpiredAccountTaskStopService:每分钟把过期账号(含级联成员)名下 biz_file_task
  PENDING/SCHEDULED/RUNNING → FAILED,含未领取的 PENDING(防内部通道继续派发)。原因文案
  刻意不以「客户端异常中断」开头,否则会被 TaskResumeService 自动复活。品牌采集与跟价循环
  不在那张表上,经 ExpiredAccountTaskStopSpi 由各自模块停:品牌 pending/running→cancelled
  且 UPDATE 的 WHERE 二次核对状态(抢到行后被客户端推进成完成的行不得误杀),跟价循环只置
  stop_requested=1、沿用用户点「停止循环」那条收尾路径,绝不动 status。任务心跳接口保持匿名
  不走 requireUser,所以过期账号的客户端仍能拿到 alive=false 协作停止。
- 角色 company_super_admin:由站点超管开设(唯一要显式选公司的建号场景),建号/授菜单/改用户/
  看数据四条链路都按"本公司"收口;公司超管之间、以及公司超管与站点超管互相碰不得。
- 续费提醒:剩余 <5 天在数富后台与用户端弹窗,只能收起不能关闭(无关闭按钮,收起态是右下角
  浮标),续费到 ≥5 天自动消失。后台读 /api/admin/current-user 的 expiresAt/daysToExpire;
  用户端无 Pinia,走 localStorage 的 account_expires_at(登录/check_login 落键、被顶下线清键,
  换账号不残留)。只反映账号自身到期,弹窗不做任何级联推断。
- V146 撞掉了 V145 那批的一条护栏:它按"db/ 里不许出现 V146__ 前缀"编死号位,被一条正常的
  前向迁移占用就红。改成钉动作——剥注释后的可执行 SQL 不得写 flyway_schema_history(回滚
  一律是扫描路径外的人工补丁),并把盘点文档改 V146 时丢掉的 V145 文件名补回去。

验证(本会话亲跑,不是转述):后端定向 14 类 123/0(同一轮 mvn 里连批次③的 4 类 53 例、以及既有 ShopDataCrawl/SimilarAsin 两条回归对照 21 例一起跑,合计 197 例 0 红);admin-frontend-vue 本批定向 44/0(弹窗 7 +
公司建号/列表/入口/日期默认 21 + 角色 task-48/task-251 16),全量 1726 例里那 1 支红是另一条
会话刻意留的「公司 tab 接线缺口」尺子(点名 admin_task_center 等 6 页,与本批无关);
frontend-vue 弹窗 9/0、全量 710/0。变异自证两批共 34 点:弹窗语义 16 点全杀(阈值 5、只能收起
不能关闭、挂载点含"注释掉挂载"、落键/清键链路),本批 SPI+迁移护栏 17 点全杀(含 G2 反向:
前向迁移只在注释里提 flyway_schema_history 必须照样绿),注入文件还原 dirty=0、探针残留 0。
2026-10-05 11:51:33 +08:00

74 lines
3.6 KiB
TypeScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readSource } from './helpers.ts'
import {
roleDomainOf,
selectRoleOf,
userRoleLockMessage,
roleDomainLabel,
USER_ROLE_OPTIONS,
} from '../src/pages/account/user-role-map.ts'
test('test_task_048_user_role_map_normal_primary_path', () => {
// 正常主路径:role 字段直接映射为角色域。
assert.equal(roleDomainOf({ role: 'admin' }), 'admin')
assert.equal(roleDomainOf({ role: 'normal' }), 'normal')
assert.equal(roleDomainOf({ role: 'super_admin' }), 'super_admin')
// 2026-10-05 新增角色:公司超级管理员(数据只看本公司,由站点超管开设)
assert.equal(roleDomainOf({ role: 'company_super_admin' }), 'company_super_admin')
})
test('test_task_048_user_role_map_normal_variant_input', () => {
// 正常变体:role 为空时按 is_admin 布尔推断(1=管理员);未知角色归 normal。
assert.equal(roleDomainOf({ role: '', isAdmin: true }), 'admin')
assert.equal(roleDomainOf({ role: '', isAdmin: false }), 'normal')
assert.equal(roleDomainOf({ role: 'biz', isAdmin: false }), 'normal')
})
test('test_task_048_user_role_map_normal_repeated_operation_is_idempotent', () => {
// 正常重复:映射为纯函数、不改输入、结果稳定。
const row = { role: 'admin', isAdmin: true }
assert.equal(roleDomainOf(row), 'admin')
assert.equal(roleDomainOf(row), 'admin')
assert.equal(row.role, 'admin')
})
test('test_task_048_user_role_map_boundary_empty_input', () => {
// 边界空值:缺省/空角色行回退 normal,不崩溃。
assert.equal(roleDomainOf({}), 'normal')
assert.equal(selectRoleOf({}), 'normal')
})
test('test_task_048_user_role_map_boundary_single_item', () => {
// 边界单元素:admin 单行 → 编辑可选 admin。
assert.equal(selectRoleOf({ role: 'admin' }), 'admin')
})
test('test_task_048_user_role_map_boundary_limit_or_missing_field', () => {
// 边界上限:站点/公司超管不可作为可编辑角色(null);候选含公司超管但绝不含站点超管。
assert.equal(selectRoleOf({ role: 'super_admin' }), null)
assert.equal(selectRoleOf({ role: 'company_super_admin' }), null)
const values = USER_ROLE_OPTIONS.map((o) => o.value)
assert.deepEqual([...values].sort(), ['admin', 'company_super_admin', 'normal'])
assert.equal(values.includes('super_admin'), false)
})
test('test_task_048_user_role_map_invalid_input_rejected', () => {
// 异常输入:目标为超管/操作者非超管给出可操作锁定原因。
assert.match(userRoleLockMessage('super_admin', { role: 'super_admin' }) || '', /超级管理员/)
assert.match(userRoleLockMessage('admin', { role: 'normal' }) || '', /超管|管理员/)
assert.equal(userRoleLockMessage('super_admin', { role: 'normal' }), undefined)
// 2026-10-05:公司超管目标只有站点超管能编辑(改密码/到期时间做续费),角色锁定要单独说清
assert.match(userRoleLockMessage('super_admin', { role: 'company_super_admin' }) || '', /公司超级管理员/)
})
test('test_task_048_user_role_map_dependency_failure_returns_actionable_message', () => {
// 依赖失败/可操作:角色映射为纯逻辑、无 axios/http;展示文案与域一一对应。
const mod = readSource('src/pages/account/user-role-map.ts')
assert.equal(/axios|http\./.test(mod), false, '角色映射保持纯逻辑')
assert.equal(roleDomainLabel('admin'), '管理员')
assert.equal(roleDomainLabel('normal'), '普通用户')
assert.equal(roleDomainLabel('super_admin'), '超级管理员')
assert.equal(roleDomainLabel('company_super_admin'), '公司超级管理员')
})