/* eslint-disable max-len */
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-487', (group) => {
  const loader = new AmfLoader()
  const xmlMime = 'application/xml'
  const apiFile = 'APIC-487'

  let model: IAmfDocument

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

  test('returns an example when not XML wrapped', ({ assert }) => {
    const payload = loader.getResponsePayloads(model, '/test1', 'get', '200')[0]
    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, xmlMime, {
      renderExamples: true,
    })!
    assert.typeOf(result, 'object', 'generates the example')
    const expectedExample =
      '<Person>\n  <addresses>\n    <street></street>\n    <city></city>\n  </addresses>\n</Person>'
    assert.equal(result.renderValue, expectedExample)
  })

  test('returns an example when XML wrapped', ({ assert }) => {
    const payload = loader.getResponsePayloads(model, '/test2', 'post', '200')[0]
    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, xmlMime, {
      renderExamples: true,
    })!

    assert.typeOf(result, 'object', 'generates the example')
    const expectedExample =
      '<WrappedPerson>\n  <addresses>\n    <Address>\n      <street></street>\n      <city></city>\n    </Address>\n  </addresses>\n</WrappedPerson>'
    // this RAML has no name defined for a wrapped property so it inherits the name from the parent.
    assert.equal(result.renderValue, expectedExample)
  })
})
