import { test } from '@japa/runner'
import { ApiSchemaGenerator } from '../../../src/amf/ApiSchemaGenerator.js'
import { IAmfDocument } from '../../../src/amf/definitions/Amf.js'
import { IApiAnyShape } from '../../../src/amf/definitions/Shapes.js'
import { AmfLoader } from './AmfLoader.js'

test.group('APIC-391', (group) => {
  const loader = new AmfLoader()
  const xmlMime = 'application/xml'
  const apiFile = 'APIC-391'

  let model: IAmfDocument

  group.setup(async () => {
    model = await loader.getGraph(apiFile)
  })

  test('generate XML tags correctly for payloads examples', ({ assert }) => {
    const payload = loader.getPayloads(model, '/shipment-requests', 'post')[0]

    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, xmlMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    assert.equal(
      result.renderValue,
      '<unknown-type>\n' +
        '  <address>250 HIDEOUT LN</address>\n' +
        '  <comments>Orgin comments entered here.</comments>\n' +
        '  <references>\n' +
        '    <referenceType>Delivery Note</referenceType>\n' +
        '    <referenceValue>7328</referenceValue>\n' +
        '  </references>\n' +
        '</unknown-type>'
    )
  })
})
