g6-to-logicflow.spec.ts 2.09 KB
import { describe, expect, it } from 'vitest'
import {
  normalizeGraphData,
  toLogicFlowGraphData,
} from '../../packages/flow-editor/src/core/adapter/g6-to-logicflow'
import baseSingleNode from '../fixtures/graphs/base-single-node.json'
import branchingFlow from '../fixtures/graphs/branching-flow.json'
import linearApprovalFlow from '../fixtures/graphs/linear-approval-flow.json'

describe('g6-to-logicflow 适配器', () => {
  it('应能把旧项目单节点数据归一化为兼容结构', () => {
    const normalizedGraph = normalizeGraphData(baseSingleNode)

    expect(normalizedGraph.nodes).toHaveLength(1)
    expect(normalizedGraph.nodes[0]).toMatchObject({
      id: 'start-node',
      label: '开始节点',
      type: 'start',
      x: 160,
      y: 180,
    })
    expect(normalizedGraph.edges).toHaveLength(0)
  })

  it('应兼容已经整理过的新结构图数据', () => {
    const normalizedGraph = normalizeGraphData(linearApprovalFlow)

    expect(normalizedGraph.nodes).toHaveLength(3)
    expect(normalizedGraph.edges).toHaveLength(2)
    expect(normalizedGraph.nodes[1]).toMatchObject({
      id: 'approve',
      label: '审批节点',
      type: 'approval',
    })
    expect(normalizedGraph.edges[0]).toMatchObject({
      sourceNodeId: 'start',
      targetNodeId: 'approve',
      label: '提交审批',
    })
  })

  it('应能把旧项目分支流程映射为 LogicFlow 2.x 可消费结构', () => {
    const logicFlowGraph = toLogicFlowGraphData(branchingFlow)

    expect(logicFlowGraph.nodes).toHaveLength(5)
    expect(logicFlowGraph.edges).toHaveLength(5)
    expect(logicFlowGraph.nodes[0]).toMatchObject({
      id: 'start-node',
      type: 'circle',
      text: '开始节点',
    })
    expect(logicFlowGraph.nodes[2]).toMatchObject({
      id: 'approve-node',
      type: 'rect',
      text: '通过分支',
    })
    expect(logicFlowGraph.edges[0]).toMatchObject({
      sourceNodeId: 'start-node',
      targetNodeId: 'review-node',
      type: 'polyline',
      text: '提交审批',
    })
    expect(logicFlowGraph.edges[0].id).toContain('edge-start-node-review-node')
  })
})