205 lines
9.0 KiB
TypeScript
205 lines
9.0 KiB
TypeScript
/**
|
|
* 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('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 = `<?xml version="1.0"?>
|
|
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
|
|
xmlns:camunda="http://camunda.org/schema/1.0/bpmn">
|
|
<bpmn:process id="p1">
|
|
<bpmn:startEvent id="s1"/>
|
|
<bpmn:userTask id="t1" camunda:assignee="role:finance"/>
|
|
<bpmn:endEvent id="e1"/>
|
|
</bpmn:process>
|
|
</bpmn:definitions>`
|
|
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 = `<?xml version="1.0"?>
|
|
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL">
|
|
<bpmn:process id="p1">
|
|
<bpmn:startEvent id="s1"/>
|
|
<bpmn:userTask id="t1" assignee="\${deptManager}"/>
|
|
<bpmn:endEvent id="e1"/>
|
|
</bpmn:process>
|
|
</bpmn:definitions>`
|
|
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 = `<?xml version="1.0"?>
|
|
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
|
|
xmlns:camunda="http://camunda.org/schema/1.0/bpmn">
|
|
<bpmn:process id="p1">
|
|
<bpmn:startEvent id="s1"/>
|
|
<bpmn:userTask id="t1" camunda:assignee="initiator"/>
|
|
<bpmn:endEvent id="e1"/>
|
|
</bpmn:process>
|
|
</bpmn:definitions>`
|
|
const r = await semanticLint(xml, { webapi: {} })
|
|
expect(r.ok).toBe(false)
|
|
expect(r.errors.some(e => /reserved keyword/.test(e))).toBe(true)
|
|
})
|
|
}) |