/**
* workflow semantic lint tests.
*
* 8 场景:
* 1. 多结束节点且无合并 → 阻断
* 2. 无 start → 阻断
* 3. userTask 无 assignee 且无 potentialOwner → 阻断
* 4. 显式 ${deptManager} 解析为空 → 阻断并 diagnostics
* 5. userTask 无 assignee 但有 potentialOwner → ok=true
* 6. userTask camunda:assignee="12345678" (6+ 位数字) → ok=true
* 7. 空 XML → errors 含 'flowXml is empty'
* 8. 单 End + Gateway → 无 multiple-end (短路验证)
*
* 与 BPMN layout lint 分离: layout 自动修复不掩盖语义错误.
*/
import { describe, expect, it } from 'vitest'
import { semanticLint } from '../../src/commands/workflow/_semantic_lint.ts'
describe('workflow semantic lint', () => {
it('多结束无合并 → ok=false, errors 含 multiple-end', async () => {
const xml = `
`
const r = await semanticLint(xml, { webapi: {} })
expect(r.ok).toBe(false)
expect(r.errors.some(e => /multiple-end/.test(e))).toBe(true)
})
it('无 start → ok=false', async () => {
const xml = `
`
const r = await semanticLint(xml, { webapi: {} })
expect(r.ok).toBe(false)
})
it('userTask 无 assignee 且无 potentialOwner → ok=false', async () => {
const xml = `
`
const r = await semanticLint(xml, { webapi: {} })
expect(r.ok).toBe(false)
expect(r.errors.some(e => /assignee|candidate/.test(e))).toBe(true)
})
it('assignee=${deptManager} 候选人为空 → 阻断并给 reason', async () => {
const xml = `
`
const webapi = { getDeptManager: async () => [] as Array<{ id: string; name: string }> }
const r = await semanticLint(xml, { webapi })
expect(r.ok).toBe(false)
expect(r.diagnostics.some(d => /unresolved/.test(d.message))).toBe(true)
expect(r.diagnostics.some(d => /no users in dept/.test(d.message))).toBe(true)
})
// ─── critical fix 回归 (C1: potentialOwner array 访问模式) ─────────────
it('C1 回归: userTask 无 assignee + → ok=true', async () => {
// BPMN 标准 pattern: 元素表达 "存在候选人 wrapper"
// (具体用户在 里, 这里只看 potentialOwner 元素是否存在)
const xml = `
`
const r = await semanticLint(xml, { webapi: {} })
expect(r.ok).toBe(true)
expect(r.errors).toEqual([])
})
// ─── critical fix 回归 (C2: namespaced assignee) ─────────────────────
it('C2 回归: userTask camunda:assignee="12345678" (6+ 位数字) → ok=true', async () => {
// JNPF/Camunda XML 风格: userTask 用命名空间前缀属性 camunda:assignee
// 修复后: 查 ut.assignee + $attrs.assignee + $attrs['camunda:assignee'] + $attrs['flowable:assignee']
// 6+ 位数字被 _approver_resolve 视为 JNPF userId 风格单候选人占位.
const xml = `
`
const r = await semanticLint(xml, { webapi: {} })
expect(r.ok).toBe(true)
expect(r.errors).toEqual([])
})
// ─── 稳定性测试 ─────────────────────────────────────────────────────
it('稳定性: 空 XML → errors 含 "flowXml is empty"', async () => {
const r = await semanticLint('', { webapi: {} })
expect(r.ok).toBe(false)
expect(r.errors).toContain('flowXml is empty')
})
it('稳定性: 单 End + Gateway → ok=true (短路验证)', async () => {
// ends.length === 1 时 hasConvergingGateway 短路返回 true, 不会触发 multiple-end
const xml = `
`
const r = await semanticLint(xml, { webapi: {} })
expect(r.errors.some(e => /multiple-end/.test(e))).toBe(false)
})
})