/**
* tests/unit/workflow_auto_assign_admin.test.ts
*
* Bug 3 兜底 (tdd6 R3 2026-07-29 + R1 P5 历史踩坑):
* workflow --ai 生成的 BPMN 偶尔只输出 camunda:assignee, 缺 userTask.potentialOwner,
* semanticLint 报 'no assignee or candidate' 全员失败, 直接 throw.
* 修复: autoAssignAdmin 注入 admin potentialOwner 给所有缺候选的 userTask.
*
* 测试覆盖:
* 1. 注入 (1 个 userTask 无 potentialOwner → 加 admin)
* 2. 不覆盖 (已有 user1 → 不变)
* 3. 多个 userTask (混合, 部分已有 PO 部分无 → 只补缺失)
* 4. 空 XML / 解析失败 → 返回原 XML
* 5. 没有 userTask → 返回原 XML
* 6. 注入后, semanticLint 重跑 OK (端到端验证)
*/
import { describe, expect, it } from 'vitest'
import { autoAssignAdmin } from '../../src/commands/workflow/_auto_assign_admin.ts'
import { semanticLint } from '../../src/commands/workflow/_semantic_lint.ts'
describe('workflow autoAssignAdmin (Bug 3 兜底)', () => {
it('注入 potentialOwner admin 到无候选 userTask', async () => {
const xml = `
`
const result = await autoAssignAdmin(xml)
// 注: bpmn-moddle 序列化为 admin
expect(result).toContain(' {
const xml = `
user1
`
const result = await autoAssignAdmin(xml)
// t1 的 potentialOwner 应保留为 user1, 不被覆盖
// 注: bpmn-moddle 序列化时会将 重写成
expect(result).toContain('user1')
expect(result).not.toContain('admin')
})
it('多个 userTask 混合: 已有 PO 不变, 缺失 PO 注入 admin', async () => {
const xml = `
manager1
`
const result = await autoAssignAdmin(xml)
// t2 的 manager1 保留 (bpmn-moddle 序列化为 manager1)
expect(result).toContain('manager1')
// t1 + t3 被注入 admin (期望 2 个 admin)
const adminMatches = result.match(/admin/g) || []
expect(adminMatches.length).toBeGreaterThanOrEqual(2)
// 不应有 manager1 被覆盖为 admin
expect(result).not.toMatch(/manager1.*admin|admin.*manager1/)
})
it('空 XML → 返回原值, 不抛', async () => {
expect(await autoAssignAdmin('')).toBe('')
expect(await autoAssignAdmin(' ')).toBe(' ')
})
it('无 userTask → 返回原值', async () => {
const xml = `
`
const result = await autoAssignAdmin(xml)
expect(result).toBe(xml)
})
it('解析失败 → 返回原值, 不抛', async () => {
const broken = ''
expect(await autoAssignAdmin(broken)).toBe(broken)
})
it('端到端: 注入后 semanticLint 重跑 OK (R3 实战场景)', async () => {
// R3: 4 userTask 缺 potentialOwner, semanticLint 报 4 errors
// 修复: autoAssignAdmin 注入 admin → re-lint → OK
const xml = `
`
const before = await semanticLint(xml, { webapi: {} })
expect(before.ok).toBe(false)
expect(before.errors.filter((e) => /missing assignee|candidate/.test(e)).length).toBe(4)
// R3 兜底: autoAssignAdmin → re-lint
const fixed = await autoAssignAdmin(xml)
const after = await semanticLint(fixed, { webapi: {} })
expect(after.ok).toBe(true)
expect(after.errors).toEqual([])
})
})