import { describe, expect, it } from 'vitest'
import {
  GUANGHU_PORTAL_BLOCK_TYPE,
  guanghuPortalMarkdownCodec,
} from './guanghuPortalMarkdown'
import {
  injectDurableMarkdownBlocks,
  preProcessDurableMarkdownBlocks,
  serializeDurableMarkdownBlocks,
} from './durableMarkdownBlocks'

const SOURCE = `\`\`\`guanghu-portal
{"title":"铸渊主控台","target":"🌳 铸渊 · Tolaria编号嵌套树 · NAV-CODE-TREE-001","description":"进入现实执行层","status":"运行中"}
\`\`\`
`

describe('guanghuPortalMarkdownCodec', () => {
  it('round-trips a Guanghu portal card through the rich editor pipeline', () => {
    const processed = preProcessDurableMarkdownBlocks({
      markdown: SOURCE,
      codecs: [guanghuPortalMarkdownCodec],
    })
    const blocks = injectDurableMarkdownBlocks({
      blocks: [{ type: 'paragraph', content: [{ type: 'text', text: processed.trim() }] }],
      codecs: [guanghuPortalMarkdownCodec],
    }) as Array<{ type: string; props: Record<string, string> }>

    expect(blocks[0]?.type).toBe(GUANGHU_PORTAL_BLOCK_TYPE)
    expect(blocks[0]?.props.title).toBe('铸渊主控台')
    expect(blocks[0]?.props.target).toContain('铸渊')

    expect(serializeDurableMarkdownBlocks({
      blocks,
      codecs: [guanghuPortalMarkdownCodec],
      serializeOrdinaryBlocks: () => '',
    })).toBe(SOURCE)
  })
})
