import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

import {
  compileScript,
  compileStyle,
  compileTemplate,
  parse
} from '@vue/compiler-sfc'

const componentFiles = [
  '../src/components/approval/ApprovalTaskQueue.vue',
  '../src/components/approval/ApprovalTaskWorkspace.vue',
  '../src/components/approval/ApprovalAssignmentDialog.vue',
  '../src/components/approval/ApprovalParticipantsDialog.vue',
  '../src/components/approval/ApprovalBatchResultPanel.vue'
]

for (const relativeFile of componentFiles) {
  test(`${relativeFile} 可以独立编译`, () => {
    const filename = fileURLToPath(new URL(relativeFile, import.meta.url))
    const source = readFileSync(filename, 'utf8')
    const parsed = parse(source, { filename })
    assert.deepEqual(parsed.errors, [])

    const id = `approval-${relativeFile.replace(/\W+/g, '-')}`
    const script = compileScript(parsed.descriptor, { id })
    const template = compileTemplate({
      source: parsed.descriptor.template?.content || '',
      filename,
      id,
      compilerOptions: { bindingMetadata: script.bindings }
    })
    assert.deepEqual(template.errors, [])

    for (const style of parsed.descriptor.styles) {
      const compiledStyle = compileStyle({
        source: style.content,
        filename,
        id,
        scoped: style.scoped
      })
      assert.deepEqual(compiledStyle.errors, [])
    }
  })
}
