diff --git a/src/commands/workflow/_semantic_lint.ts b/src/commands/workflow/_semantic_lint.ts index 17b0800..ec3e989 100644 --- a/src/commands/workflow/_semantic_lint.ts +++ b/src/commands/workflow/_semantic_lint.ts @@ -12,6 +12,12 @@ * 3. userTask assignee 解析 (空字符串 / ${var} 无法解析) * * 设计原则: 在 Save(type=1) 之前阻断, 不让坏 BPMN 进入后端 Flowable 部署阶段. + * + * bpmn-moddle 解析注意: + * - Activity.resources 是 isMany: true 的 flat ResourceRole 数组 (bpmn-moddle schema). + * 不存在 resources.potentialOwner 这种嵌套; 需 filter by $type === 'bpmn:PotentialOwner'. + * - camunda/flowable extension 属性 (assignee / candidateUsers / candidateGroups) 不在 + * moddle schema 内, 会落到 $attrs 并保留命名空间前缀 ($attrs['camunda:assignee']). */ import { BpmnModdle } from 'bpmn-moddle' @@ -83,15 +89,23 @@ export async function semanticLint(xml: string, ctx: ResolverCtx): Promise r.$type === 'bpmn:PotentialOwner') + + // 3a. 缺 assignee 且缺 potentialOwner → 阻断 if (!assignee && po.length === 0) { errors.push(`userTask ${ut.id}: missing assignee and no candidate`) diagnostics.push({ nodeId: ut.id, message: 'no assignee or candidate' }) @@ -119,4 +133,4 @@ export async function semanticLint(xml: string, ctx: ResolverCtx): Promise String(e.$type || '').includes('Gateway')) -} +} \ No newline at end of file diff --git a/tests/unit/workflow_semantic_lint.test.ts b/tests/unit/workflow_semantic_lint.test.ts index 2c8b7ce..4336ca5 100644 --- a/tests/unit/workflow_semantic_lint.test.ts +++ b/tests/unit/workflow_semantic_lint.test.ts @@ -1,12 +1,20 @@ /** * workflow semantic lint tests (Task 4 — webapi hardening) * - * 4 场景: + * 原始 4 场景 (T4): * 1. 多结束节点且无合并 → 阻断 * 2. 无 start → 阻断 - * 3. userTask assignee 空字符串 → 阻断 + * 3. userTask 无 assignee 且无 potentialOwner → 阻断 * 4. 显式 ${deptManager} 无法解析 → 阻断并 diagnostics * + * T4 critical fix 回归 (C1/C2): + * 5. userTask assignee="" + → ok=true (C1: potentialOwner array) + * 6. userTask camunda:assignee="alice" → ok=true (C2: namespaced assignee) + * + * M3 稳定性测试: + * 7. 空 XML → errors 含 'flowXml is empty' + * 8. 单 End + Gateway → ok=true (短路验证) + * * 与 BPMN layout lint 分离: layout 自动修复不掩盖语义错误. */ import { describe, expect, it } from 'vitest' @@ -40,12 +48,15 @@ describe('workflow semantic lint (Task 4)', () => { expect(r.ok).toBe(false) }) - it('userTask assignee 空字符串 → ok=false', async () => { + it('userTask 无 assignee 且无 potentialOwner → ok=false', async () => { + // 测试 "真的没候选人" 的情况: 既没有 assignee 属性, 也没有 potentialOwner 元素. + // (T4 critical fix 之前, 这个 XML 含 , 但用错误的 resources?.potentialOwner 访问 + // 一直拿到空数组, 误判为 "no candidate" 而阻断; 修复后 potentialOwner 元素被识别, 此测试改成真无候选人.) const xml = ` - + ` @@ -67,4 +78,66 @@ describe('workflow semantic lint (Task 4)', () => { expect(r.ok).toBe(false) expect(r.diagnostics.some(d => /unresolved/.test(d.message))).toBe(true) }) -}) + + // ─── T4 critical fix 回归 (C1: potentialOwner array 访问模式) ───────────── + it('C1 回归: userTask 无 assignee + → ok=true', async () => { + // BPMN 标准 pattern: 元素表达 "存在候选人 wrapper" + // (具体用户在 里, 这里只看 potentialOwner 元素是否存在) + // 修复前: resources?.potentialOwner → undefined → 错判为 "no candidate" + // 修复后: resources.filter($type === 'bpmn:PotentialOwner') → 1 → 视为有候选 + const xml = ` + + + + + + + ` + const r = await semanticLint(xml, { candidateResolver: () => [] }) + expect(r.ok).toBe(true) + expect(r.errors).toEqual([]) + }) + + // ─── T4 critical fix 回归 (C2: namespaced assignee) ───────────────────── + it('C2 回归: userTask camunda:assignee="alice" → ok=true', async () => { + // JNPF/Camunda XML 风格: userTask 用命名空间前缀属性 camunda:assignee + // 修复前: 只查 ut.assignee + $attrs.assignee → 错失命名空间属性 + // 修复后: 查 ut.assignee + $attrs.assignee + $attrs['camunda:assignee'] + $attrs['flowable:assignee'] + const xml = ` + + + + + + + ` + const r = await semanticLint(xml, { candidateResolver: () => [] }) + expect(r.ok).toBe(true) + expect(r.errors).toEqual([]) + }) + + // ─── M3 稳定性测试 ───────────────────────────────────────────────────── + it('M3 稳定性: 空 XML → errors 含 "flowXml is empty"', async () => { + const r = await semanticLint('', { candidateResolver: () => [] }) + expect(r.ok).toBe(false) + expect(r.errors).toContain('flowXml is empty') + }) + + it('M3 稳定性: 单 End + Gateway → ok=true (短路验证)', async () => { + // ends.length === 1 时 hasConvergingGateway 短路返回 true, 不会触发 multiple-end + const xml = ` + + + + + + + + + ` + const r = await semanticLint(xml, { candidateResolver: () => [] }) + // 两个 userTask 都没 assignee/po → 但 ends.length=1 + Gateway → single 短路 → only userTask errors + expect(r.errors.some(e => /multiple-end/.test(e))).toBe(false) + }) +}) \ No newline at end of file