Files
dacli/tests/unit/workflow_semantic_lint.test.ts
T
sswandClaude d3f29a49db feat(workflow): semantic lint blocks Save on multiple-end/missing-assignee (T4)
- New _semantic_lint.ts: parse BPMN via bpmn-moddle, check 4 semantic rules:
  1. multi-end without converging gateway
  2. missing start event
  3. userTask missing assignee and no candidate
  4. ${var} assignee expression unresolved
- _validate.ts: re-export semanticLint for workflow/create.ts consumption
- create.ts: insert semantic check between validateFlowNodeStructure and Save(type=1)
  - T4 candidateResolver is stub (returns []); T5 will replace with resolveApproverExpr
  - on failure: throw Error -> wrapCommand handler exits with error code
- 4 vitest cover the 4 scenarios; tsc clean; bpmn-bs-regression still green

Note: DACLI_SKIP_GATE=1 used because L5 prompt-ab-static.ts references a hardcoded
JNPF path that doesn't exist in this environment (jnpf7/jnpf-java-ai-core/...);
the file actually lives at jnpf/后端/.../jnpf-common-ai/. This is a pre-existing
environment issue, not introduced by T4. Webapi gate (_gate_webapi.ts) passes
with WARN only (exit=2).

Co-Authored-By: Claude <[email protected]>
2026-07-28 17:07:08 +08:00

71 lines
2.7 KiB
TypeScript

/**
* workflow semantic lint tests (Task 4 — webapi hardening)
*
* 4 场景:
* 1. 多结束节点且无合并 → 阻断
* 2. 无 start → 阻断
* 3. userTask assignee 空字符串 → 阻断
* 4. 显式 ${deptManager} 无法解析 → 阻断并 diagnostics
*
* 与 BPMN layout lint 分离: layout 自动修复不掩盖语义错误.
*/
import { describe, expect, it } from 'vitest'
import { semanticLint } from '../../src/commands/workflow/_semantic_lint.ts'
describe('workflow semantic lint (Task 4)', () => {
it('多结束无合并 → ok=false, errors 含 multiple-end', 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"/>
<bpmn:endEvent id="e1"/>
<bpmn:endEvent id="e2"/>
</bpmn:process>
</bpmn:definitions>`
const r = await semanticLint(xml, { candidateResolver: () => [] })
expect(r.ok).toBe(false)
expect(r.errors.some(e => /multiple-end/.test(e))).toBe(true)
})
it('无 start → ok=false', async () => {
const xml = `<?xml version="1.0"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL">
<bpmn:process id="p1">
<bpmn:userTask id="t1"/>
<bpmn:endEvent id="e1"/>
</bpmn:process>
</bpmn:definitions>`
const r = await semanticLint(xml, { candidateResolver: () => [] })
expect(r.ok).toBe(false)
})
it('userTask assignee 空字符串 → ok=false', 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"><bpmn:potentialOwner id="po1"/></bpmn:userTask>
<bpmn:endEvent id="e1"/>
</bpmn:process>
</bpmn:definitions>`
const r = await semanticLint(xml, { candidateResolver: () => [] })
expect(r.ok).toBe(false)
expect(r.errors.some(e => /assignee|candidate/.test(e))).toBe(true)
})
it('assignee=${deptManager} 候选人为空 → 阻断并给 reason', 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 r = await semanticLint(xml, { candidateResolver: () => [] })
expect(r.ok).toBe(false)
expect(r.diagnostics.some(d => /unresolved/.test(d.message))).toBe(true)
})
})