Files
dacli/tests/integration/drift/workflow-autolayout.spec.ts
T
ssw adf35f81ec Round 7b+: 修复 BPMNEdge waypoint 不跟节点位置 (浏览器箭头画在节点上)
问题: Round 7b B方案 autoLayout 只重排 BPMNShape (节点位置), 没动 BPMNEdge (边 waypoint).
      AI 默认 waypoint=(400,300), 跟新节点位置不一致,
      浏览器箭头全画在节点上 (BPMNEdge 还在原始 (400,300) 位置).

修复: applyLayout 同时更新 BPMNEdge.di:waypoint.
      算法 (镜像 web handleAutoLayoutByHorizontal 简化):
        直线 waypoint = source 右边缘中点 → target 左边缘中点 (2 个 waypoint).
      注意: bpmn-moddle 用 moddle.create('dc:Point', {x, y}) 创建 waypoint.

端到端实测 (vehicleRefuelApprovalDemoV6):
  节点:
    Activity_nTVZAED  (400, 300) 90x32  ←start
    Activity_yuseGvQ  (750, 272) 200x88
    Activity_VXbHuZK  (1100, 272) 200x88
    Activity_lGQLuO2  (1450, 272) 200x88
    Activity_NQOU0EM  (1800, 272) 200x88
    Activity_YtTP6dH  (2040, 300) 90x32  ←end
  边 waypoint:
    Flow_3jWzClO: (490, 316) → (750, 316)
    Flow_fiXItfh: (950, 316) → (1100, 316)
    Flow_Nnk1Pve: (1300, 316) → (1450, 316)
    Flow_O4jZTZM: (1650, 316) → (1800, 316)
    Flow_lNzWLZt: (2000, 316) → (2040, 316)
    → 全部对齐节点边缘 ✓

vitest: 71/71 PASS (4 文件, +1 新增 edge waypoint 测试)
2026-07-14 10:58:20 +08:00

220 lines
10 KiB
TypeScript

/**
* workflow-autolayout.spec.ts — C方案 autoLayout 单元测试
*
* 目的: 验证 Node 端 BPMN 自动布局 (镜像 web handleAutoLayoutByHorizontal 思路)
*
* 镜像源:
* - web/packages/jnpf/plugins/jnpf-bpmn/lib/utils/constructTreeUtil.ts:BPMNTreeBuilder
* - web/apps/jnpf-web-apps-main/src/components/FlowProcess/src/propPanel/components/FlowButton.vue:354
*
* 算法 (简化版):
* 1. 用 bpmn-moddle (纯 XML, 无 DOM) 解析 flowXml
* 2. BFS 找 start → 分配 level 0/1/2/...
* 3. 按 level 分组, x 按 level * spacing, y 同层错开
* 4. 写回 BPMNShape.bounds
*
* v0.21+ Round 7 C方案: 解决 AI 返回 BPMN XML 所有节点 bounds=(400,300) 全重叠问题
*/
import { describe, it, expect } from 'vitest'
import { autoLayoutFlowXml, isLayoutNeeded } from '../../../src/commands/workflow/_autoLayout.ts'
/** 构造一个模拟 AI 生成的 BPMN XML (所有 bounds=(400,300) 重叠, waypoint 全错) */
function makeAiXml(nodeCount: number): string {
let nodes = ''
let flows = ''
let shapes = ''
let edges = ''
for (let i = 0; i < nodeCount; i++) {
const id = i === 0 ? `Event_start` : (i === nodeCount - 1 ? `Event_end` : `Activity_${i}`)
const bpmnType = i === 0 ? 'startEvent' : (i === nodeCount - 1 ? 'endEvent' : 'userTask')
nodes += ` <bpmn2:${bpmnType} id="${id}" name="节点${i}">\n </bpmn2:${bpmnType}>\n`
shapes += ` <bpmndi:BPMNShape id="${id}_di" bpmnElement="${id}">\n <dc:Bounds x="400" y="300" width="120" height="80"/>\n </bpmndi:BPMNShape>\n`
}
for (let i = 0; i < nodeCount - 1; i++) {
const from = i === 0 ? `Event_start` : `Activity_${i}`
const to = i + 1 === nodeCount - 1 ? `Event_end` : `Activity_${i + 1}`
flows += ` <bpmn2:sequenceFlow id="Flow_${i}" sourceRef="${from}" targetRef="${to}"/>\n`
// AI 默认边 waypoint = (400, 300) — 错的, 跟新节点位置不一致
edges += ` <bpmndi:BPMNEdge id="Flow_${i}_di" bpmnElement="Flow_${i}">\n <di:waypoint x="400" y="300"/>\n </bpmndi:BPMNEdge>\n`
}
return `<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="test">
<bpmn2:process id="proc1" isExecutable="true">
${nodes}${flows} </bpmn2:process>
<bpmndi:BPMNDiagram id="diagram1">
<bpmndi:BPMNPlane id="plane1" bpmnElement="proc1">
${shapes}${edges} </bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>`
}
async function getBounds(flowXml: string, elemId: string): Promise<{ x: number; y: number } | null> {
const { BpmnModdle } = await import('bpmn-moddle')
const moddle = new BpmnModdle()
const { rootElement } = (await moddle.fromXML(flowXml)) as any
for (const pe of rootElement.diagrams?.[0]?.plane?.planeElement || []) {
if (pe.$type === 'bpmndi:BPMNShape' && pe.bpmnElement?.id === elemId) {
return { x: pe.bounds.x, y: pe.bounds.y }
}
}
return null
}
describe('autoLayout: isLayoutNeeded 检测 AI 全重叠 bounds', () => {
it('AI 默认 6 节点全 (400,300) → true', async () => {
const xml = makeAiXml(6)
expect(await isLayoutNeeded(xml)).toBe(true)
})
it('节点坐标已分散 (每个不同 x,y) → false', async () => {
// 用 makeAiXml 但改不同节点不同坐标 (镜像 autoLayout 输出)
const xml = `<?xml version="1.0"?>
<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" id="test">
<bpmn2:process id="proc1"><bpmn2:startEvent id="s1"/><bpmn2:userTask id="a1"/><bpmn2:endEvent id="e1"/></bpmn2:process>
<bpmndi:BPMNDiagram><bpmndi:BPMNPlane bpmnElement="proc1">
<bpmndi:BPMNShape id="s1_d" bpmnElement="s1"><dc:Bounds x="200" y="200" width="90" height="32"/></bpmndi:BPMNShape>
<bpmndi:BPMNShape id="a1_d" bpmnElement="a1"><dc:Bounds x="550" y="200" width="200" height="88"/></bpmndi:BPMNShape>
<bpmndi:BPMNShape id="e1_d" bpmnElement="e1"><dc:Bounds x="900" y="200" width="90" height="32"/></bpmndi:BPMNShape>
</bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</bpmn2:definitions>`
expect(await isLayoutNeeded(xml)).toBe(false)
})
})
describe('autoLayout: BFS 水平布局算法', () => {
it('6 节点直线流程 → x 递增, y 对齐', async () => {
const xml = makeAiXml(6)
const layouted = await autoLayoutFlowXml(xml)
const start = await getBounds(layouted, 'Event_start')
const end = await getBounds(layouted, 'Event_end')
const act1 = await getBounds(layouted, 'Activity_1')
expect(start).not.toBeNull()
expect(end).not.toBeNull()
expect(act1).not.toBeNull()
// x 严格递增: start < act1 < end
expect(start!.x).toBeLessThan(act1!.x)
expect(act1!.x).toBeLessThan(end!.x)
// y 对齐 (水平布局)
expect(start!.y).toBe(act1!.y)
expect(act1!.y).toBe(end!.y)
})
it('5 节点直线 → 5 个不同 x 坐标', async () => {
const xml = makeAiXml(5)
const layouted = await autoLayoutFlowXml(xml)
const positions: number[] = []
for (const id of ['Event_start', 'Activity_1', 'Activity_2', 'Activity_3', 'Event_end']) {
const b = await getBounds(layouted, id)
if (b) positions.push(b.x)
}
expect(new Set(positions).size).toBe(5)
})
it('3 节点 start→approver→end → web BPMNTreeBuilder 重排 (makeAiXml 节点 width=120)', async () => {
// web BPMNTreeBuilder 算法: childX = parentX + AUTO_HEIGHT(150) + childWidth
// makeAiXml 默认所有节点 width=120
// start.x=400 (root 不变), approver.x = 400+150+120 = 670, end.x = 670+150+120 = 940
const xml = makeAiXml(3)
const layouted = await autoLayoutFlowXml(xml)
expect((await getBounds(layouted, 'Event_start'))!.x).toBe(400)
expect((await getBounds(layouted, 'Activity_1'))!.x).toBe(670)
expect((await getBounds(layouted, 'Event_end'))!.x).toBe(940)
})
it('空 BPMN (0 节点) → 返回原 XML 不抛错', async () => {
const emptyXml = `<?xml version="1.0"?>
<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" id="test">
<bpmn2:process id="proc1"/>
</bpmn2:definitions>`
const result = await autoLayoutFlowXml(emptyXml)
expect(result).toBe(emptyXml)
})
})
describe('autoLayout: 边界场景', () => {
it('已分布好的 XML → 不改变 (no-op)', async () => {
// 用自定义分布式 XML (每个节点不同坐标), isLayoutNeeded 返回 false
const xml = `<?xml version="1.0"?>
<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" id="test">
<bpmn2:process id="proc1"><bpmn2:startEvent id="s1"/><bpmn2:userTask id="a1"/><bpmn2:userTask id="a2"/><bpmn2:endEvent id="e1"/></bpmn2:process>
<bpmndi:BPMNDiagram><bpmndi:BPMNPlane bpmnElement="proc1">
<bpmndi:BPMNShape id="s1_d" bpmnElement="s1"><dc:Bounds x="200" y="200" width="90" height="32"/></bpmndi:BPMNShape>
<bpmndi:BPMNShape id="a1_d" bpmnElement="a1"><dc:Bounds x="550" y="200" width="200" height="88"/></bpmndi:BPMNShape>
<bpmndi:BPMNShape id="a2_d" bpmnElement="a2"><dc:Bounds x="900" y="200" width="200" height="88"/></bpmndi:BPMNShape>
<bpmndi:BPMNShape id="e1_d" bpmnElement="e1"><dc:Bounds x="1250" y="200" width="90" height="32"/></bpmndi:BPMNShape>
</bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</bpmn2:definitions>`
expect(await isLayoutNeeded(xml)).toBe(false)
})
it('无效 XML → 返回原 XML 不抛错', async () => {
const bad = '<not-xml></not-xml>'
const result = await autoLayoutFlowXml(bad)
expect(result).toBe(bad) // 容错: 失败时返回原值
})
})
describe('autoLayout: BPMNEdge waypoint 重算 (修复: 箭头画在节点上的 bug)', () => {
/**
* 问题 (Round 7b 后续): autoLayout 只重排 BPMNShape (节点), 没动 BPMNEdge (边 waypoint).
* AI 默认 waypoint=(400,300), 跟新节点位置不一致, 浏览器箭头全画在节点上.
* 修复 (Round 7b+): applyLayout 同时更新 BPMNEdge.di:waypoint.
*
* 算法: 直线 waypoint = source 右边缘中点 → target 左边缘中点 (2 个 waypoint).
* 镜像 web FlowButton.vue:354 handleAutoLayoutByHorizontal 简化逻辑.
*/
it('边 waypoint 跟新节点位置对齐', async () => {
const xml = makeAiXml(3)
const layouted = await autoLayoutFlowXml(xml)
const { BpmnModdle } = await import('bpmn-moddle')
const moddle = new BpmnModdle()
const { rootElement } = await moddle.fromXML(layouted) as any
// 找 BPMNEdge
const edges: any[] = []
for (const pe of rootElement.diagrams?.[0]?.plane?.planeElement || []) {
if (pe.$type === 'bpmndi:BPMNEdge') edges.push(pe)
}
expect(edges.length).toBe(2) // 3 节点流程有 2 条边
// 找 BPMNShape map (查 x/y/w/h)
const shapeMap = new Map<string, { x: number; y: number; w: number; h: number }>()
for (const pe of rootElement.diagrams?.[0]?.plane?.planeElement || []) {
if (pe.$type === 'bpmndi:BPMNShape' && pe.bounds) {
shapeMap.set(pe.bpmnElement.id, {
x: pe.bounds.x, y: pe.bounds.y,
w: pe.bounds.width, h: pe.bounds.height,
})
}
}
// 找 flow source/target
const flows = new Map<string, { src: string; tgt: string }>()
for (const e of rootElement.rootElements.find((e: any) => e.$type === 'bpmn:Process').flowElements) {
if (e.$type === 'bpmn:SequenceFlow') {
flows.set(e.id, { src: e.sourceRef.id, tgt: e.targetRef.id })
}
}
// 验证每条边的 waypoint = source 右边缘中点 → target 左边缘中点
for (const edge of edges) {
const flow = flows.get(edge.bpmnElement.id)
expect(flow).toBeDefined()
const src = shapeMap.get(flow!.src)!
const tgt = shapeMap.get(flow!.tgt)!
const wps = edge.waypoint
expect(wps.length).toBe(2)
// wp[0] = (src.x + src.w, src.y + src.h/2)
expect(wps[0].x).toBe(src.x + src.w)
expect(wps[0].y).toBe(src.y + src.h / 2)
// wp[1] = (tgt.x, tgt.y + tgt.h/2)
expect(wps[1].x).toBe(tgt.x)
expect(wps[1].y).toBe(tgt.y + tgt.h / 2)
}
})
})