import test from 'node:test'
import assert from 'node:assert/strict'
import { buildAppModuleExportSvg } from './appModuleExport.js'

test('buildAppModuleExportSvg renders a pure image-loadable SVG', () => {
  const svg = buildAppModuleExportSvg({
    canvas: { width: 640, height: 420 },
    caption: '系统功能结构图',
    nodes: [
      {
        id: 'root',
        title: 'BookManage',
        kind: 'root',
        lane: 'root',
        x: 190,
        y: 18,
        width: 260,
        height: 42
      },
      {
        id: 'frontend:catalog',
        title: '图书目录',
        kind: 'module',
        lane: 'frontend',
        vertical: true,
        x: 300,
        y: 198,
        width: 48,
        height: 138
      }
    ],
    edges: [
      {
        id: 'root->frontend:catalog',
        path: 'M 320 60 V 88 H 324 V 198'
      }
    ]
  })

  assert.match(svg, /^<\?xml version="1.0" encoding="UTF-8"\?>/)
  assert.equal(svg.includes('xmlns="http://www.w3.org/2000/svg"'), true)
  assert.equal(svg.includes('width="640"'), true)
  assert.equal(svg.includes('height="420"'), true)
  assert.equal(svg.includes('data-v-'), false)
  assert.equal(svg.includes('<foreignObject'), false)
  assert.equal(svg.includes('BookManage'), true)
  assert.equal(svg.includes('<tspan'), true)
})

test('buildAppModuleExportSvg escapes text and path values', () => {
  const svg = buildAppModuleExportSvg({
    canvas: { width: 320, height: 240 },
    caption: 'A&B<图>',
    nodes: [
      {
        id: 'root',
        title: '图书 & 借阅 <系统>',
        kind: 'root',
        x: 10,
        y: 10,
        width: 200,
        height: 40
      }
    ],
    edges: [
      {
        id: 'edge',
        path: 'M 1 1 L 2 2"'
      }
    ]
  })

  assert.equal(svg.includes('图书 &amp; 借阅 &lt;系统&gt;'), true)
  assert.equal(svg.includes('A&amp;B&lt;图&gt;'), true)
  assert.equal(svg.includes('d="M 1 1 L 2 2&quot;"'), true)
})

test('buildAppModuleExportSvg uses graph node font sizes', () => {
  const svg = buildAppModuleExportSvg({
    canvas: { width: 640, height: 420 },
    caption: '系统功能结构图',
    nodes: [
      {
        id: 'root',
        title: '学生社团管理系统',
        kind: 'root',
        lane: 'root',
        x: 190,
        y: 18,
        width: 260,
        height: 42,
        fontSize: 19
      },
      {
        id: 'group_1:module_1',
        title: '用户管理',
        kind: 'module',
        lane: 'group_1',
        vertical: true,
        x: 300,
        y: 198,
        width: 54,
        height: 154,
        fontSize: 18
      }
    ],
    edges: []
  })

  assert.equal(svg.includes('font-size="19"'), true)
  assert.equal(svg.includes('font-size="18"'), true)
  assert.equal(svg.includes('学生社团管理系统'), true)
  assert.equal(svg.includes('<foreignObject'), false)
})
