import { test } from '@japa/runner'
import { DataTestHelper } from './DataTestHelper.js'
import { ArcExportObject, ArcLegacyTransformer } from '../../../src/legacy.js'
import legacyRequestData from './data/legacy-request-import.json' with { type: 'json' }
import legacyData from './data/legacy-data-import.json' with { type: 'json' }

test.group('ArcLegacyTransformer: Single request import', (group) => {
  let result: ArcExportObject

  group.each.setup(async () => {
    const transformer = new ArcLegacyTransformer(structuredClone(legacyRequestData))
    result = await transformer.transform()
  })

  test('normalizes the data', ({ assert }) => {
    assert.typeOf(result, 'object')
  })

  test('contains export object properties', ({ assert }) => {
    assert.typeOf(result.createdAt, 'string')
    assert.equal(result.version, 'unknown')
    assert.equal(result.kind, 'ARC#Import')
    assert.typeOf(result.projects, 'array')
    assert.typeOf(result.requests, 'array')
  })

  test('has empty projects array', ({ assert }) => {
    assert.lengthOf(result.projects!, 0)
  })

  test('has single request', ({ assert }) => {
    assert.lengthOf(result.requests!, 1)
  })

  test('has valid request', ({ assert }) => {
    DataTestHelper.assertRequestObject(result.requests![0], assert)
  })

  test('has a default name', ({ assert }) => {
    assert.equal(result.requests![0].name, 'unnamed')
  })

  test('has request values set', ({ assert }) => {
    const request = result.requests![0]
    assert.equal(request.url, 'http://mulesoft.com')
    assert.equal(request.method, 'GET')
    assert.equal(request.headers, '')
    assert.equal(request.payload, '')
    assert.equal(request.created, 1450675637093)
  })

  test('Google Drive information is present', ({ assert }) => {
    const request = result.requests![0]
    assert.equal(request.driveId, '0Bzpy9PK_RiBOeWRYUEo0TmRyTzA')
  })
})

test.group('ArcLegacyTransformer: Multiple requests import', (group) => {
  let result: ArcExportObject

  group.each.setup(async () => {
    const transformer = new ArcLegacyTransformer(structuredClone(legacyData))
    result = await transformer.transform()
  })

  test('normalizes the data', ({ assert }) => {
    assert.typeOf(result, 'object')
  })

  test('contains export object properties', ({ assert }) => {
    assert.typeOf(result.createdAt, 'string')
    assert.equal(result.version, 'unknown')
    assert.equal(result.kind, 'ARC#Import')
    assert.typeOf(result.projects, 'array')
    assert.typeOf(result.requests, 'array')
  })

  test('has a project item', ({ assert }) => {
    assert.lengthOf(result.projects!, 1)
  })

  test('has 2 requests', ({ assert }) => {
    assert.lengthOf(result.requests!, 2)
  })

  test('has valid request object', ({ assert }) => {
    DataTestHelper.assertRequestObject(result.requests![0], assert)
    DataTestHelper.assertRequestObject(result.requests![1], assert)
  })

  test('has valid project object', ({ assert }) => {
    DataTestHelper.assertProjectObject(result.projects![0], assert)
  })

  test('has project values set', ({ assert }) => {
    const [project] = result.projects!
    assert.equal(project.name, 'test-project', 'name is set')
    assert.equal(project.created, 1506285256547, 'created is set')
    assert.strictEqual(project.order, 0, 'order is set')
  })

  test('has project information set on the request', ({ assert }) => {
    assert.equal(result.requests![0].projects![0], result.projects![0].key, 'Project ID is set')
    assert.isUndefined(result.requests![1].projects, 'Project id is undefined')
  })

  test('has project request reference', ({ assert }) => {
    const project = result.projects![0]
    assert.typeOf(project.requests, 'array')
    assert.lengthOf(project.requests!, 1)
  })
})
