var _ = require('lodash')
var Promise = require('bluebird')
var expect = require('chai').expect
var express = require('express')
var request = require('request')

Promise.promisifyAll(request);

var server = require('../src/server')

describe('Basic rendering', function() {
  this.timeout(10000)
  setupTestServers({initialThreadCount: 1, timeout: 5000})
 
  it("Returns correct HTML", function(done) {
    requestTestPage('simpleTest.html').then(function(response) {
      expect(response[0].statusCode).to.equal(200)
      expect(response[1]).to.contain('Async content loaded!')
      expect(response[1]).not.to.contain('Loading')
    }).finally(done)
  })

   it("Strips script tags", function(done) {
    requestTestPage('simpleTest.html').then(function(response) {
      expect(response[1]).to.not.contain('<script>')
    }).finally(done)
  })

  it("Starts up only one PhantomJS thread", function(done) {
    requestThreadInfo().then(function(info) {
      expect(info.length).to.equal(1)
    }).finally(done)
  })
})

describe('Concurrent requests', function() {
  this.timeout(10000)
  setupTestServers({initialThreadCount: 1, timeout: 5000})
 
  it("Returns correct HTML", function(done) {
    var current = Promise.resolve()
    Promise.all([requestTestPage('simpleTest.html'), requestTestPage('simpleTest.html')])
    .then(function(responses) {
      expect(responses.length).to.equal(2)
      _.each(responses, function(response) {
        expect(response[0].statusCode).to.equal(200)
        expect(response[1]).to.contain('Async content loaded!')
      })
    }).finally(done)
  })

  it("Starts up another PhantomJS thread", function(done) {
    requestThreadInfo().then(function(info) {
      expect(info.length).to.equal(2)
    }).finally(done)
  })
})

function requestTestPage(page) {
  return request.getAsync('http://127.0.0.1:5000/render/http://127.0.0.1:5011/' + page)
}

function requestThreadInfo() {
  return request.getAsync('http://127.0.0.1:5000/info/').then(function(response) { return JSON.parse(response[1]) })
}

function setupTestServers(config) {
  var contentServer, appServer;
  before(function(done) {
    appServer = server(config, done)
  })
  before(function(done) {
    contentServer = initTestServer(done)
  })
  after(function() {
    contentServer.close()
    appServer.close()
  })
}

function initTestServer(done) {
  var app = express()
  app.set('port', 5011)
  app.use(express.static(__dirname + '/static'))
  return app.listen(app.get('port'), done)
}

@@ -479,38 +479,31 @@
 ect(response
-%5B0%5D
 .status
-Code
 ).to.equal(2
@@ -519,35 +519,37 @@
  expect(response
-%5B1%5D
+.body
 ).to.contain('As
@@ -591,19 +591,21 @@
 response
-%5B1%5D
+.body
 ).not.to
@@ -778,27 +778,29 @@
 ect(response
-%5B1%5D
+.body
 ).to.not.con
@@ -1465,22 +1465,15 @@
 onse
-%5B0%5D
 .status
-Code
 ).to
@@ -1507,19 +1507,21 @@
 response
-%5B1%5D
+.body
 ).to.con
@@ -1881,16 +1881,143 @@
 + page)%0A
+    .then(function(response) %7B%0A      return %7B%0A        status: response%5B0%5D.statusCode,%0A        body: response%5B1%5D%0A      %7D%0A    %7D)%0A
 %7D%0A%0Afunct
@@ -2098,16 +2098,21 @@
 /info/')
+%0A    
 .then(fu

Refactor tests