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-655', (group) => {
  const loader = new AmfLoader()
  const xmlMime = 'application/xml'
  const apiFile = 'APIC-655'

  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, '/delivery', 'post')[0]
    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, xmlMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    assert.equal(
      result.renderValue,
      '<Delivery>\n' +
        '  <orderId>732482783718</orderId>\n' +
        '  <lineItems>\n' +
        '    <lineItemId>9738187235</lineItemId>\n' +
        '    <qty>10</qty>\n' +
        '  </lineItems>\n' +
        '  <lineItems>\n' +
        '    <lineItemId>9832837238</lineItemId>\n' +
        '    <qty>70</qty>\n' +
        '  </lineItems>\n' +
        '</Delivery>\n'
    )
  })
})
