import { describe, it, expect } from 'vitest'; import BpmnModdle from 'bpmn-moddle'; import activiti from './activiti.json'; import flowable from './flowable.json'; import { SYNTHETIC_BPMN } from './fixtures/synthetic-full-surface.bpmn'; // 元素名大小写回归(CRITICAL:断部署)。 // // 根因:moddle 把「被扩展类型」的合并描述符 $pkg 设为**最后一个 trait 的包** // (moddle index.esm.js:577)。activiti.json 用 `extends` 给 bpmn:Process/UserTask/StartEvent… // 加属性 → 这些基础类型的 $pkg 变成 activiti 包;moddle-xml 序列化时按 $pkg 的 // `xml.tagAlias` 决定元素名首字母是否小写。bpmn 包有 `tagAlias:"lowerCase"`(Process→process), // activiti 包若无此声明 → 写出 PascalCase `//`。 // **Flowable 大小写敏感**:`` 不被识别为流程 → 部署时 ProcessValidator 报 // flowable-process-definition-not-executable(看到 0 个可执行流程),部署完全失败。 // // 为何 T1.1/T1.2 没抓到:它们只断言 activiti 扩展属性/元素多重集,**从不断言基础 BPMN 元素名的 // 大小写**;且 fromXML→toXML 与 create 都走同一 moddle-xml Writer,均产 PascalCase(本文件实证)。 // // 为何用 BpmnModdle 而非真 Modeler:bpmn-js Modeler 依赖 diagram-js 的 SVG 布局(getBBox 等), // jsdom 跑不动;而 Modeler.saveXML 在 moddle 层就是 `moddle.toXML(定义树)`,diagram-js 从不改元素名。 // 故 `new BpmnModdle({activiti})` 的 fromXML→toXML / create→toXML 忠实复现 saveXML 的元素命名路径。 const BASE_TYPES: Array<[create: string, lower: string, pascal: string]> = [ ['bpmn:Process', 'process', 'Process'], ['bpmn:UserTask', 'userTask', 'UserTask'], ['bpmn:StartEvent', 'startEvent', 'StartEvent'], ['bpmn:ScriptTask', 'scriptTask', 'ScriptTask'], ['bpmn:EndEvent', 'endEvent', 'EndEvent'], ['bpmn:SequenceFlow', 'sequenceFlow', 'SequenceFlow'], ['bpmn:BoundaryEvent', 'boundaryEvent', 'BoundaryEvent'], ]; /** 元素名正则(可选 bpmn: 前缀 + 精确 token + 分隔符),大小写敏感。 */ const openTag = (name: string) => new RegExp('<(bpmn:)?' + name + '[\\s/>]'); describe('moddle 序列化——基础 BPMN 元素名小写(断部署回归)', () => { it('打开存量再导出(fromXML→toXML):基础元素名全小写,无 PascalCase(Flowable 可识别)', async () => { const moddle = new BpmnModdle({ activiti }); const { rootElement } = await moddle.fromXML(SYNTHETIC_BPMN); const { xml } = await moddle.toXML(rootElement, { format: true }); for (const [, lower, pascal] of BASE_TYPES) { expect(xml, `应含小写 <${lower}>`).toMatch(openTag(lower)); expect(xml, `不应含 PascalCase <${pascal}>(Flowable 大小写敏感)`).not.toMatch(openTag(pascal)); } }); it('isExecutable 症状:process 元素小写且保留 isExecutable="true"', async () => { const moddle = new BpmnModdle({ activiti }); const { rootElement } = await moddle.fromXML(SYNTHETIC_BPMN); const { xml } = await moddle.toXML(rootElement, { format: true }); // 真正的 bug 是 不被识别("isExecutable 丢失"只是症状)——断言 process 小写即可。 expect(xml).toMatch(/<(bpmn:)?process[\s][^>]*isExecutable="true"/); expect(xml).not.toMatch(openTag('Process')); }); it('新建元素(create→toXML):新建流程各基础节点名也全小写', async () => { const moddle = new BpmnModdle({ activiti }); const defs = moddle.create('bpmn:Definitions', { targetNamespace: 'http://x' }); const proc = moddle.create('bpmn:Process', { id: 'p1', isExecutable: true }); const start = moddle.create('bpmn:StartEvent', { id: 's1' }); const ut = moddle.create('bpmn:UserTask', { id: 't1', 'activiti:candidateUsers': 'X', 'activiti:formKey': 'K', }); const flow = moddle.create('bpmn:SequenceFlow', { id: 'f1', sourceRef: start, targetRef: ut }); const end = moddle.create('bpmn:EndEvent', { id: 'e1' }); proc.flowElements = [start, ut, flow, end]; defs.rootElements = [proc]; const { xml } = await moddle.toXML(defs, { format: true }); for (const [, lower, pascal] of BASE_TYPES.filter(([, l]) => l !== 'scriptTask' && l !== 'boundaryEvent')) { expect(xml, `应含小写 <${lower}>`).toMatch(openTag(lower)); expect(xml, `不应含 PascalCase <${pascal}>`).not.toMatch(openTag(pascal)); } // 扩展属性 / 自定义元素不受影响 expect(xml).toContain('activiti:candidateUsers="X"'); expect(xml).toContain('activiti:formKey="K"'); }); it('flowable 描述符隔离同样小写(虽不进 Modeler,防同类潜伏 bug)', async () => { const moddle = new BpmnModdle({ flowable }); const defs = moddle.create('bpmn:Definitions', { targetNamespace: 'http://x' }); const proc = moddle.create('bpmn:Process', { id: 'p1', isExecutable: true }); const ut = moddle.create('bpmn:UserTask', { id: 't1', 'flowable:assignee': 'u1' }); proc.flowElements = [ut]; defs.rootElements = [proc]; const { xml } = await moddle.toXML(defs, { format: true }); expect(xml).toMatch(openTag('process')); expect(xml).toMatch(openTag('userTask')); expect(xml).not.toMatch(openTag('Process')); expect(xml).not.toMatch(openTag('UserTask')); expect(xml).toContain('flowable:assignee="u1"'); }); });