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('SE-10469', (group) => {
  const loader = new AmfLoader()
  const jsonMime = 'application/json'
  const apiFile = 'SE-10469'

  let model: IAmfDocument

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

  test('generates example from JSON schema', ({ assert }) => {
    const payload = loader.getPayloads(model, '/purina/b2b/supplier/purchaseOrder', 'post')[0]
    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, jsonMime, {
      renderExamples: true,
      renderOptional: true,
    })!
    assert.typeOf(result, 'object', 'generates the example')
    assert.typeOf(result.renderValue, 'string', 'has the value')

    const data = JSON.parse(result.renderValue as string)
    assert.typeOf(data, 'object', 'generates an object')
    assert.typeOf(data.purchaseOrder, 'object', 'has the purchaseOrder property')
    assert.equal(data.purchaseOrder.actionCode, '', 'has the purchaseOrder.actionCode')
    assert.equal(data.purchaseOrder.typeCode, '', 'has the purchaseOrder.typeCode')
    assert.typeOf(data.purchaseOrder.purchaseOrderHeader, 'object', 'has the purchaseOrder.purchaseOrderHeader')
  })
})
