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

  let model: IAmfDocument

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

  test(`computes a false example`, ({ assert }) => {
    const payload = loader.getPayloads(model, '/record', 'post')[0]
    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, jsonMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    const data = JSON.parse(String(result.renderValue))
    assert.isFalse(data.allOrNone)
    assert.typeOf(data.records, 'array')
  })
})
