'use strict';
var path = require('path');
var expect = require('chai').expect;
var child_process = require('child_process');
var Promise = require('bluebird');
var findFreePort = Promise.promisify(require('find-free-port'));
var service = require('../lib/juttle-service');

let juttle_service_cmd = path.resolve(`${__dirname}/../bin/juttle-service`);
let juttle_service_client_cmd = path.resolve(`${__dirname}/../bin/juttle-service-client`);

describe('juttle-service-client binary', function() {

    let server;
    let juttle_service;

    before(function() {
        findFreePort(10000, 20000)
        .then((freePort) => {
            server = 'localhost:' + freePort;
            juttle_service = service.run({port: freePort, root_directory: __dirname});
        });
    });

    after(function() {
        juttle_service.stop();
    });

    it('can be run with --help', function() {
        try {
            child_process.execSync(`${juttle_service_client_cmd} --help`);
        } catch (err) {
            // The status is 1, but we can also check the output for 'usage:'
            expect(err.status).to.equal(1);
            expect(err.stdout.toString()).to.match(/^usage: /);
        }
    });

    it('can be run with list_jobs', function(done) {

        let got_output = false;

        // Can't use execSync here, as the server is running within
        // our own process, and spawnSync blocks the event loop.
        let child = child_process.exec(`${juttle_service_client_cmd} --juttle-service ${server} list_jobs`);

        child.stdout.on('data', (data) => {
            if (data.toString().match(/\[\]/)) {
                got_output = true;
            }
        });

        child.on('close', (code) => {
            expect(code).to.equal(0);
            expect(got_output).to.equal(true);
            done();
        });
    });

});


describe('juttle-service binary', function() {

    it('can be run with --help', function() {
        try {
            child_process.exec(`${juttle_service_cmd} --help`);
        } catch (err) {
            // The status is 1, but we can also check the output for 'usage:'
            expect(err.status).to.equal(1);
            expect(err.stdout.toString()).to.match(/^usage: /);
        }
    });

    it.skip('can be run and can see startup line', function(done) {
        findFreePort(10000, 20000)
        .then((freePort) => {
            let child = child_process.exec(`${juttle_service_cmd} --port ${freePort}`);
            child.stdout.on('data', (data) => {
                if (data.toString().match(/Juttle service listening at/)) {
                    child.kill('SIGKILL');
                }
            });
            child.on('close', (code) => {
                done();
            });
        });
    });
});

@@ -928,20 +928,18 @@
 ess.
-exec
+spawn
 Sync(
-%60$%7B
 jutt
@@ -955,33 +955,36 @@
 e_client_cmd
-%7D 
+, %5B'
 --help
-%60
+'%5D
 );%0A        %7D
@@ -1311,20 +1311,21 @@
 n't use 
-exec
+spawn
 Sync her
@@ -1454,32 +1454,30 @@
 ild_process.
-exec(%60$%7B
+spawn(
 juttle_servi
@@ -1489,18 +1489,20 @@
 ient_cmd
-%7D 
+, %5B'
 --juttle
@@ -1513,19 +1513,21 @@
 vice
- $%7B
+', 
 server
-%7D 
+,  '
 list
@@ -1531,17 +1531,18 @@
 ist_jobs
-%60
+'%5D
 );%0A%0A    
@@ -1992,32 +1992,30 @@
 ild_process.
-exec(%60$%7B
+spawn(
 juttle_servi
@@ -2024,17 +2024,20 @@
 _cmd
-%7D 
+, %5B'
 --help
-%60
+'%5D
 );%0A 
@@ -2274,13 +2274,8 @@
   it
-.skip
 ('ca
@@ -2434,16 +2434,14 @@
 ess.
-exec(%60$%7B
+spawn(
 jutt
@@ -2458,27 +2458,29 @@
 _cmd
-%7D 
+, %5B'
 --port
- $%7B
+', 
 freePort
 %7D%60);
@@ -2479,10 +2479,9 @@
 Port
-%7D%60
+%5D
 );%0A 
@@ -2754,32 +2754,165 @@
             %7D);%0A
+            child.on('error', (msg) =%3E %7B%0A                throw new Error(%60Got unexpected error from child: $%7Bmsg%7D%60);%0A            %7D);%0A
         %7D);%0A    

Fix tests under travis.