import { Eltro as t, assert} from 'eltro'

import { createClient, startServer, log, resetLog } from './helper.server.mjs'

t.describe('Server', function() {
  let client

  t.before(function() {
    client = createClient()

    return startServer()
  })

  t.test('should run', async function() {
    resetLog()
    assert.strictEqual(log.info.callCount, 0)
    let data = await client.get('/')

    assert.ok(data)
    assert.ok(data.name)
    assert.ok(data.version)
    assert.strictEqual(log.info.callCount, 1)
    assert.strictEqual(log.info.lastCall[0].status, 200)
    assert.match(log.info.lastCall[1], /\<-- GET \//)
  })

  t.test('should handle errors fine', async function() {
    resetLog()
    assert.strictEqual(log.warn.callCount, 0)
    let data = await assert.isRejected(client.get('/error'))

    assert.ok(data)
    assert.ok(data.body)
    assert.strictEqual(data.body.status, 500)
    assert.match(data.body.message, /test/)
    assert.strictEqual(log.error.firstCall[0].message, 'This is a test')
    assert.strictEqual(log.error.callCount, 2)
    assert.strictEqual(log.error.secondCall[0].status, 500)
    assert.match(log.error.secondCall[1], /\<-- 500 GET \/error/)
  })
})
