import { test } from '@japa/runner'
import { AmfLoader } from '../AmfLoader.js'
import { AmfSerializer } from '../../../../src/amf/AmfSerializer.js'
import type { IAmfDocument } from '../../../../src/amf/definitions/Amf.js'
import { AmfNamespace } from '../../../../src/amf/definitions/Namespace.js'

const loader = new AmfLoader()

test.group('OAS documentation', (group) => {
  let api: IAmfDocument
  let serializer: AmfSerializer
  group.setup(async () => {
    api = await loader.getGraph('petstore')
    serializer = new AmfSerializer()
    serializer.amf = api
  })

  test('has the documentation properties', ({ assert }) => {
    const enc = serializer._computeEncodes(api)!
    // @ts-expect-error Used in testing
    const docs = enc[serializer._getAmfKey(AmfNamespace.aml.vocabularies.core.documentation)][0]
    const result = serializer.documentation(docs)
    assert.typeOf(result, 'object', 'has the result')
    assert.equal(result.url, 'http://swagger.io', 'has the url')
    assert.equal(result.description, 'Find out more about Swagger', 'has the description')
    assert.deepEqual(result.customDomainProperties, [], 'has no customDomainProperties')
    assert.typeOf(result.sourceMaps, 'object', 'has source maps')
  })
})

test.group('RAML documentation', (group) => {
  let api: IAmfDocument
  let serializer: AmfSerializer
  group.setup(async () => {
    api = await loader.getGraph('amf-helper-api')
    serializer = new AmfSerializer()
    serializer.amf = api
  })

  test('has the documentation properties', ({ assert }) => {
    const enc = serializer._computeEncodes(api)!
    // @ts-expect-error Used in testing
    const docs = enc[serializer._getAmfKey(AmfNamespace.aml.vocabularies.core.documentation)][0]
    const result = serializer.documentation(docs)
    assert.typeOf(result, 'object', 'has the result')
    assert.isUndefined(result.url, 'has no url')
    assert.equal(result.title, 'How to begin', 'has the title')
    assert.equal(result.description, 'Example content\n', 'has the description')
    assert.deepEqual(result.customDomainProperties, [], 'has no customDomainProperties')
    assert.typeOf(result.sourceMaps, 'object', 'has source maps')
  })
})
