var Tickspot = require('../lib/tickspot'),
    nock = require('nock'),
    Q = require('q'),
    should = require('should'),
    fs = require('fs');

describe('Tickspot', function () {
    var ts = new Tickspot('mycompany', 'myemail', 'mypassword');
    var fakeResponses = {};
    var fake = nock(ts.host)
        .post('/api/testAuth', 'email=' + ts.email + '&password=' + ts.password).reply(201, '<foo />')
        .post('/api/okay').reply(201, '<foo />')
        .post('/api/bad').reply(400, 'Bad Request')
        .post('/api/notXML').reply(201, 'foo');

    before(function (done) {
        var readFile = Q.denodeify(fs.readFile);
        var responses = ['clients-single', 'clients-multiple', 'projects-single', 'projects-multiple'].map(function (filename) {
            return readFile('./test/sample-responses/' + filename + '.xml', 'utf-8').then(function (data) {
                fakeResponses[filename] = data;
            });
        });

        Q.all(responses).then(function () { done(); });
    });

    describe('#makeRequest()', function () {
        it('should add email and password to the request', function (done) {
            var req = ts.makeRequest('testAuth');
            req.then(function () {
                Q.isFulfilled(req).should.be.ok;
                done();
            });
        });

        var okayRequest = ts.makeRequest('okay');
        it('should return a promise', function () {
            Q.isPromise(okayRequest).should.be.ok;
        });
        it('should resolve the promise when parseable XML is returned', function (done) {
            okayRequest.then(function () {
                Q.isFulfilled(okayRequest).should.be.ok;
                done();
            });
        });

        it('should reject the promise when the request returns an error', function (done) {
            var req = ts.makeRequest('bad');
            req.fail(function () {
                Q.isRejected(req).should.be.ok;
                done();
            });
        });

        it('should reject the promise when unparseable XML is returned', function (done) {
            var req = ts.makeRequest('notXML');
            req.fail(function () {
                Q.isRejected(req).should.be.ok;
                done();
            });
        });
    });

    describe('#clients()', function () {
        it('should call a callback function', function (done) {
            fake.post('/api/clients').reply(201, fakeResponses['clients-single']);
            ts.clients(done);
        });

        it('should return a promise', function (done) {
            fake.post('/api/clients').reply(201, fakeResponses['clients-single']);
            Q.isPromise(ts.clients().then(function () { done(); })).should.be.ok;
        });

        it('should return an array of clients', function (done) {
            fake.post('/api/clients').reply(201, fakeResponses['clients-multiple']);
            ts.clients().then(function (arr) {
                arr.should.eql([
                    {
                        id: '12341',
                        name: 'Starfleet Command'
                    },
                    {
                        id: '12342',
                        name: 'The Vulcans'
                    },
                    {
                        id: '12343',
                        name: 'The Cardassians'
                    }
                ]);
                done();
            });
        });
    });

    describe('#projects()', function () {
        it('should call a callback function', function (done) {
            fake.post('/api/projects').reply(201, fakeResponses['projects-single']);
            ts.projects(done);
        });

        it('should return a promise', function (done) {
            fake.post('/api/projects').reply(201, fakeResponses['projects-single']);
            Q.isPromise(ts.projects().then(function () { done(); })).should.be.ok;
        });

        it('should return an array of projects', function (done) {
            fake.post('/api/projects').reply(201, fakeResponses['projects-multiple']);
            ts.projects().then(function (arr) {
                arr.should.be.an.instanceof(Array);
                done();
            });
        });
    });
});
@@ -1071,18 +1071,12 @@
 it('
-should 
 add
+s
  ema
@@ -1371,37 +1371,31 @@
         it('
-should 
 return
+s
  a promise',
@@ -1488,22 +1488,16 @@
 it('
-should 
 resolve
+s
  the
@@ -1717,37 +1717,31 @@
         it('
-should 
 reject
+s
  the promise
@@ -1992,21 +1992,15 @@
 it('
-should 
 reject
+s
  the
@@ -2302,35 +2302,29 @@
         it('
-should 
 call
+s
  a callback 
@@ -2486,37 +2486,31 @@
         it('
-should 
 return
+s
  a promise',
@@ -2714,37 +2714,37 @@
         it('
-should return
+resolves with
  an array of
@@ -2889,35 +2889,39 @@
 .then(function (
-arr
+clients
 ) %7B%0A            
@@ -2924,27 +2924,31 @@
         
-arr
+clients
 .should.
 eql(%5B%0A  
@@ -2943,418 +2943,30 @@
 uld.
-eql(%5B%0A                    %7B%0A                        id: '12341',%0A                        name: 'Starfleet Command'%0A                    %7D,%0A                    %7B%0A                        id: '12342',%0A                        name: 'The Vulcans'%0A                    %7D,%0A                    %7B%0A                        id: '12343',%0A                        name: 'The Cardassians'%0A                    %7D%0A                %5D
+be.an.instanceof(Array
 );%0A 
@@ -3083,19 +3083,13 @@
 it('
-should 
 call
+s
  a c
@@ -3266,29 +3266,23 @@
     it('
-should 
 return
+s
  a promi
@@ -3501,21 +3501,21 @@
 it('
-should return
+resolves with
  an 
@@ -3680,19 +3680,24 @@
 nction (
-arr
+projects
 ) %7B%0A    
@@ -3708,19 +3708,24 @@
         
-arr
+projects
 .should.

Update test names to reflect outcome, not aspiration