import assert from 'node:assert/strict'
import test from 'node:test'
import { parseFlowchartJson, normalizeFlowchartSpec } from './flowchart-utils.js'

test('parseFlowchartJson extracts fenced model JSON', () => {
  const raw = `我已经生成好了。\n\n\`\`\`json\n{"title":"采购流程","nodes":[{"id":"start","label":"提交申请","actor":"申请人"}],"edges":[]}\n\`\`\``
  const parsed = parseFlowchartJson(raw)
  assert.equal(parsed.title, '采购流程')
  assert.equal(parsed.nodes[0].label, '提交申请')
})

test('normalizeFlowchartSpec assigns ids, lanes and positions', () => {
  const normalized = normalizeFlowchartSpec({
    title: '采购流程',
    nodes: [
      { label: '提交申请', actor: '申请人' },
      { label: '部门审批', actor: '部门负责人' },
    ],
    edges: [{ source: 'node_1', target: 'node_2', label: '通过' }],
  })

  assert.equal(normalized.nodes.length, 2)
  assert.equal(normalized.edges.length, 1)
  assert.equal(normalized.lanes.length, 2)
  assert.equal(normalized.nodes[0].id, 'node_1')
  assert.equal(typeof normalized.nodes[0].x, 'number')
})
