/**
 * Test case for apemanApp.
 * Runs with mocha.
 */
'use strict'

const apemanApp = require('../lib/apeman_app.js')
const assert = require('assert')
const apemanport = require('apemanport')
const apemansleep = require('apemansleep')
const apemanrequest = require('apemanrequest')
const co = require('co')
const injectmock = require('injectmock')

describe('apeman-app', () => {
  let request = apemanrequest.create({})
  let sleep = apemansleep.create({})

  before(() => co(function * () {
  }))

  after(() => co(function * () {
    injectmock.restoreAll()
    yield sleep.sleep(400)
  }))

  it('Run app.', () => co(function * () {
    let port = yield apemanport.find()
    let server = yield apemanApp('my-testing-app', {
      port,
      configuration: require.resolve('../doc/mocks/mock-Apemanfile.js'),
      log: `${__dirname}/../tmp/testing-log.log`
    })
    {
      let res = yield request({
        url: `http://localhost:${port}/foo/bar/baz`,
        method: 'GET'
      })
      let { body } = res
      assert.equal(res.statusCode, 200)
      assert.equal(body, 'This is it!')
    }

    yield server.close()
  }))

  //
  it('Run child command.', () => co(function * () {
    let port = yield apemanport.find()
    let server = yield apemanApp('$child01#my-testing-child-app01', {
      configuration: require.resolve('../doc/mocks/mock-Apemanfile.js'),
      port: port
    })
    let res = yield request(`http://localhost:${port}/child/foo/bar`)
    assert.equal(res.statusCode, 200)
    let { body } = res
    assert.equal(body, 'This is it in child!')
    yield server.close()
  }))

  it('List apps.', () => co(function * () {
    let list = yield apemanApp({
      configuration: require.resolve('../doc/mocks/mock-Apemanfile.js')
    })
    console.log(list)
  }))
})

/* global describe, before, after, it */
