/**
* workflow process id 归一化 (BS-新 7, tdd8 debug §2.5, 2026-07-30)
*
* 根因: web/packages/jnpf/plugins/jnpf-bpmn/lib/utils/nodeUtil.js 硬编码
* `xmlDoc.querySelector('#Process_1')` (autoCreateGateWay:25, autoDelGateWay:171)
* 若落库 flowXml 的 process id ≠ 'Process_1' (如 tdd8overtime 'overtimeApply'),
* preview 时 process=null → .append()/.getElementsByTagName() 抛
* `TypeError: null is not an object (evaluating 'u.getElementsByTagName')`
*
* 修法: dacli _autoLayout.normalizeProcessId 落库前归一化 (本测试)
*/
import { describe, expect, it } from 'vitest'
import { normalizeProcessId } from '../../src/commands/workflow/_autoLayout.ts'
const sampleWithOvertimeApply = `
`
const sampleWithProcess1 = `
`
const sampleNoProcess = `
`
describe('workflow normalize process id (BS-新 7, 2026-07-30)', () => {
it('process id=overtimeApply → 归一化为 Process_1 (tdd8 实战 case)', () => {
const out = normalizeProcessId(sampleWithOvertimeApply)
// process id 改了
expect(out).toMatch(/]*\bid="Process_1"/)
expect(out).not.toMatch(/]*\bid="overtimeApply"/)
// BPMNPlane bpmnElement 也同步改了 (DI 树不能断)
expect(out).toMatch(/]*\bbpmnElement="Process_1"/)
expect(out).not.toMatch(/]*\bbpmnElement="overtimeApply"/)
})
it('process id 已是 Process_1 → noop (幂等)', () => {
const out = normalizeProcessId(sampleWithProcess1)
expect(out).toBe(sampleWithProcess1)
})
it('无 process 元素 → 原样返回 (防御)', () => {
const out = normalizeProcessId(sampleNoProcess)
expect(out).toBe(sampleNoProcess)
})
it('其它非 Process_1 id (语义化如 reimbursementApply) → 归一化', () => {
const xml = sampleWithOvertimeApply.replace('overtimeApply', 'reimbursementApply')
const out = normalizeProcessId(xml)
expect(out).toMatch(/]*\bid="Process_1"/)
expect(out).not.toMatch(/reimbursementApply/)
})
it('自定义 targetId 参数: Process_42 (跨项目兼容兜底)', () => {
const out = normalizeProcessId(sampleWithOvertimeApply, 'Process_42')
expect(out).toMatch(/]*\bid="Process_42"/)
expect(out).toMatch(/]*\bbpmnElement="Process_42"/)
})
it('正则防误匹配: 含 process 子串的其它元素 (processCategory) 不动', () => {
// 包含 bpmn2:processCategory 元素的 XML
const xml = `
`
const out = normalizeProcessId(xml)
// myFlow → Process_1
expect(out).toMatch(/]*\bid="Process_1"/)
// processCategory id=cat-1 不动
expect(out).toMatch(/]*\bid="cat-1"/)
})
it('兼容 bpmn: 前缀 (moddle.toXML canonical 输出)', () => {
const xml = `
`
const out = normalizeProcessId(xml)
expect(out).toMatch(/]*\bid="Process_1"/)
})
})