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

test.group('RAML examples', (group) => {
  const loader = new AmfLoader()
  const jsonMime = 'application/json'
  const xmlMime = 'application/xml'
  const apiFile = 'example-generator-api'
  let model: IAmfDocument

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

  test('generates JSON example for a payload', ({ assert }) => {
    const payload = loader.getPayloads(model, '/employees', 'post')[1]
    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, jsonMime, {
      renderExamples: true,
      renderOptional: true,
    })!
    const { mediaType, value, renderValue } = result
    assert.equal(mediaType, jsonMime, 'mediaType is set')
    assert.include(value, 'firstName: Other Pawel', 'value has the raw value')
    assert.include(renderValue, '"firstName": "Other Pawel",', 'renderValue has the example value')
  })

  test('generates XML example for a payload', ({ assert }) => {
    const payload = loader.getPayloads(model, '/employees', 'post')[1]
    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, xmlMime, {
      renderExamples: true,
      renderOptional: true,
    })!
    const { mediaType, value, renderValue } = result
    assert.equal(mediaType, xmlMime, 'mediaType is set')
    assert.include(value, 'firstName: Other Pawel', 'value has the raw value')
    assert.include(renderValue, '<firstName>Other Pawel</firstName>', 'renderValue has the example value')
  })
})
