/** * workflow approver 表达式解析测试. * * 覆盖: * - role:xxx (有结果 / 无结果 / trim 内部空白) * - ${deptManager} (有结果 / 无结果) * - ${currentUser} (有结果 / 无 login / 空字段) * - ${other} 未知变量 * - 显式 id (6+ 位数字) * - reserved keyword (initiator / any / starter / currentUser 字面量, 大小写 + ${...} 包裹) * - throwing webapi → 'api error' * - empty / whitespace 输入 * - 集成: lint 调用 webapi 路径 (非 candidateResolver) */ import { describe, expect, it, vi } from 'vitest' import { resolveApproverExpr } from '../../src/commands/workflow/_approver_resolve.ts' import { semanticLint } from '../../src/commands/workflow/_semantic_lint.ts' describe('workflow approver resolve', () => { it('显式 role:finance → 解析', async () => { const webapi = { getUserListByRole: vi.fn().mockResolvedValue([{ id: 'u1', name: 'Alice' }]) } const r = await resolveApproverExpr('role:finance', { webapi }) expect(r.candidates).toEqual([{ id: 'u1', name: 'Alice' }]) expect(r.reason).toBeUndefined() }) it('role:finance → 候选人为空 → no such role', async () => { const webapi = { getUserListByRole: vi.fn().mockResolvedValue([]) } const r = await resolveApproverExpr('role:finance', { webapi }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('no such role') }) it('role: finance (trim 内部空白) → 解析', async () => { const webapi = { getUserListByRole: vi.fn().mockResolvedValue([{ id: 'u1', name: 'Alice' }]) } const r = await resolveApproverExpr('role: finance ', { webapi }) expect(webapi.getUserListByRole).toHaveBeenCalledWith('finance') expect(r.candidates).toEqual([{ id: 'u1', name: 'Alice' }]) }) it('变量 ${deptManager} 解析为空 → no users in dept', async () => { const webapi = { getDeptManager: vi.fn().mockResolvedValue([]) } const r = await resolveApproverExpr('${deptManager}', { webapi }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('no users in dept') }) it('变量 ${deptManager} 解析有值 → 成功', async () => { const webapi = { getDeptManager: vi.fn().mockResolvedValue([{ id: 'd1', name: 'Manager' }]) } const r = await resolveApproverExpr('${deptManager}', { webapi }) expect(r.candidates).toEqual([{ id: 'd1', name: 'Manager' }]) }) it('变量 ${currentUser} 解析成功 → 候选人显示 prefer realName', async () => { const webapi = { getCurrentUser: vi.fn().mockResolvedValue({ id: 'u1', realName: 'Alice' }) } const r = await resolveApproverExpr('${currentUser}', { webapi }) expect(r.candidates).toEqual([{ id: 'u1', name: 'Alice' }]) }) it('变量 ${currentUser} userId 字段 + 空字段 fallback → 候选人', async () => { // JNPF 用空字符串表 unset, 用 || 链. const webapi = { getCurrentUser: vi.fn().mockResolvedValue({ userId: 'u2', realName: '', userName: '', userAccount: '', account: '' }) } const r = await resolveApproverExpr('${currentUser}', { webapi }) expect(r.candidates).toEqual([{ id: 'u2', name: 'u2' }]) }) it('变量 ${currentUser} 解析为空 (未登录) → no current user', async () => { const webapi = { getCurrentUser: vi.fn().mockResolvedValue(null) } const r = await resolveApproverExpr('${currentUser}', { webapi }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('no current user') }) it('回归 BS-新148: 变量 ${currentUser} 真实 JNPF CurrentUser 嵌套结构 (userInfo.userId) → 候选人', async () => { // JNPF /api/oauth/CurrentUser 真实响应: {menuList, permissionList, userInfo: {userId, realName, ...}, sysConfigInfo, ...} // 历史 bug: 顶层读 me.userId 永远 undefined → lint 'no current user'. // 修复: 优先 me.userInfo.userId, fallback 顶层 me.userId. const realCurrentUser = { menuList: [], permissionList: [], userInfo: { userId: '349057407209541', userAccount: 'admin', realName: '管理员', userName: '管理员' }, sysConfigInfo: {}, currentSystemId: null, } const webapi = { getCurrentUser: vi.fn().mockResolvedValue(realCurrentUser) } const r = await resolveApproverExpr('${currentUser}', { webapi }) expect(r.candidates).toEqual([{ id: '349057407209541', name: '管理员' }]) expect(r.reason).toBeUndefined() }) it('回归 BS-新148: ${currentUser} 嵌套 userInfo 但 userId 字段为空 → no current user', async () => { // userInfo 存在但 userId 为空字符串 (JNPF 用空字符串表 unset) → 应判 'no current user'. const webapi = { getCurrentUser: vi.fn().mockResolvedValue({ userInfo: { userId: '', realName: '', userName: '', userAccount: '', account: '' }, }) } const r = await resolveApproverExpr('${currentUser}', { webapi }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('no current user') }) it('unknown expression → unknown expression', async () => { const r = await resolveApproverExpr('${mystery}', { webapi: {} }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('unknown expression') }) it('initiator 不允许作为审批人 → reserved keyword', async () => { const r = await resolveApproverExpr('initiator', { webapi: {} }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('reserved keyword') }) it('any / starter / INITIATOR 大写 + ${initiator} 包裹 → reserved keyword', async () => { // 大小写不敏感: 'INITIATOR' → lowercase 'initiator' → reserved // ${...} 包裹: unwrap 后 'initiator' → reserved for (const kw of ['any', 'starter', 'INITIATOR', ' initiator ', '${initiator}']) { const r = await resolveApproverExpr(kw, { webapi: {} }) expect(r.reason, `${kw} should be reserved`).toBe('reserved keyword') } }) it('${currentUser} 字面量包装 → 非 reserved (是合法 getter)', async () => { // ${currentUser} 不是 reserved keyword, 是 webapi.getCurrentUser() 的 getter 入口. const webapi = { getCurrentUser: vi.fn().mockResolvedValue({ id: 'u1', realName: 'Alice' }) } const r = await resolveApproverExpr('${currentUser}', { webapi }) expect(r.reason).toBeUndefined() expect(r.candidates).toEqual([{ id: 'u1', name: 'Alice' }]) }) it('显式 id (6+ 位数字) → single candidate passthrough', async () => { const r1 = await resolveApproverExpr('123456', { webapi: {} }) const r2 = await resolveApproverExpr('1234567890123', { webapi: {} }) expect(r1.candidates).toEqual([{ id: '123456', name: '123456' }]) expect(r2.candidates).toEqual([{ id: '1234567890123', name: '1234567890123' }]) }) it('短 id (< 6 位) → unknown expression', async () => { const r = await resolveApproverExpr('12345', { webapi: {} }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('unknown expression') }) it('throwing webapi → api error', async () => { const webapi = { getUserListByRole: vi.fn().mockRejectedValue(new Error('upstream timeout')), } const r = await resolveApproverExpr('role:finance', { webapi }) expect(r.candidates).toEqual([]) expect(r.reason).toBe('api error') }) it('throwing webapi (getDeptManager) → api error', async () => { const webapi = { getDeptManager: vi.fn().mockRejectedValue(new Error('boom')), } const r = await resolveApproverExpr('${deptManager}', { webapi }) expect(r.reason).toBe('api error') }) it('webapi method 不存在 → api error', async () => { const r = await resolveApproverExpr('role:finance', { webapi: {} }) expect(r.reason).toBe('api error') }) it('empty / whitespace 输入 → unknown expression', async () => { expect((await resolveApproverExpr('', { webapi: {} })).reason).toBe('unknown expression') expect((await resolveApproverExpr(' ', { webapi: {} })).reason).toBe('unknown expression') expect((await resolveApproverExpr('\t\n', { webapi: {} })).reason).toBe('unknown expression') }) it('webapi 返回 null → api error', async () => { const webapi = { getUserListByRole: vi.fn().mockResolvedValue(null) } const r = await resolveApproverExpr('role:finance', { webapi }) expect(r.reason).toBe('api error') }) it('webapi 返回 non-array → api error', async () => { const webapi = { getUserListByRole: vi.fn().mockResolvedValue({ not: 'array' }) } const r = await resolveApproverExpr('role:finance', { webapi }) expect(r.reason).toBe('api error') }) }) describe('workflow semantic lint + real webapi 集成', () => { it('integration: lint 路由 role:xxx → 真实 webapi 路径', async () => { const xml = ` ` const webapi = { getUserListByRole: vi.fn().mockResolvedValue([{ id: 'u1', name: 'Alice' }]), } const r = await semanticLint(xml, { webapi }) expect(r.ok).toBe(true) expect(webapi.getUserListByRole).toHaveBeenCalledWith('finance') }) it('integration: lint 路由 ${deptManager} 时遇空 → 阻断', async () => { const xml = ` ` const webapi = { getDeptManager: vi.fn().mockResolvedValue([]) } const r = await semanticLint(xml, { webapi }) expect(r.ok).toBe(false) expect(r.errors.some(e => /no users in dept/.test(e))).toBe(true) }) it('integration: lint 路由 reserved keyword → 阻断', async () => { const xml = ` ` const r = await semanticLint(xml, { webapi: {} }) expect(r.ok).toBe(false) expect(r.errors.some(e => /reserved keyword/.test(e))).toBe(true) }) it('回归 BS-新148: lint 真实 nested CurrentUser 响应 → 通过', async () => { // 模拟 dacli create.ts:391-397 注入的真实 webapi.getCurrentUser 返回值. const xml = ` ` const webapi = { getCurrentUser: async () => ({ menuList: [], userInfo: { userId: '349057407209541', realName: '管理员', userAccount: 'admin' }, sysConfigInfo: {}, }), } const r = await semanticLint(xml, { webapi: webapi as any }) expect(r.ok).toBe(true) expect(r.errors).toEqual([]) }) })