import { test } from '@japa/runner'
import { Response, CoreEngine, IHttpRequest, Headers, ResponseRedirect } from '../../../../src/index.js'

test.group('Cleaning up', (group) => {
  let request: IHttpRequest

  group.each.setup(() => {
    request = {
      url: 'https://domain.com',
      method: 'GET',
    }
  })

  test('_cleanUp()', ({ assert }) => {
    const base = new CoreEngine(request)
    base.redirects = []
    base.currentResponse = new Response()
    base.currentHeaders = new Headers('content-type: test')
    base._rawBody = Buffer.from('test')
    base.stats = { connectedTime: 123 }
    base._cleanUp()
    assert.deepEqual(base.redirects, [])
    assert.isUndefined(base.currentResponse)
    assert.equal(base.currentHeaders.toString(), '')
    assert.isUndefined(base._rawBody)
    assert.deepEqual(base.stats, {})
  })

  test('_cleanUpRedirect()', ({ assert }) => {
    const base = new CoreEngine(request)
    base.redirects.push(new ResponseRedirect())
    base.currentResponse = new Response()
    base.currentHeaders = new Headers('content-type: test')
    base._rawBody = Buffer.from('test')
    base.stats = { connectedTime: 123 }
    base._cleanUpRedirect()
    assert.lengthOf(base.redirects, 1)
    assert.isUndefined(base.currentResponse)
    assert.equal(base.currentHeaders.toString(), '')
    assert.isUndefined(base._rawBody)
    assert.deepEqual(base.stats, {})
  })
})
