var rewire = require('rewire'),
    expect = require('chai').expect,
    utils = rewire('../../lib/utils'),
    dockerCerts = require('../spec_helper').dockerCerts;

var fsStub = {
    readFileSync: function(path) { return path; }
};

utils.__set__('fs', fsStub);

describe('Utils', function() {
    describe('.formatImage()', function() {
        context('when a repository is specified', function() {
            it('formats image name with repository prefix', function() {
                var formated = utils.formatImage('grounds', 'ruby');

                expect(formated).to.equal('grounds/exec-ruby');
            });
        });

        context('when no repository is specified', function() {
            it('formats image name without repository prefix', function() {
                expect(utils.formatImage('', 'ruby')).to.equal('exec-ruby');
            });
        });

        context('when no language is specified', function() {
            it('returns an empty string', function() {
                expect(utils.formatImage('grounds', '')).to.equal('');
            });
        });
    });

    describe('.formatCmd()', function() {
        it('returns an escaped string', function() {
            var code     = 'puts "Hello world\\n\\r\\t"\r\n\t',
                expected = 'puts "Hello world\\\\n\\\\r\\\\t"\\r\\n\\t';

            expect(utils.formatCmd(code)).to.equal(expected);
        });
    });

    describe('.formatStatus()', function() {
        it('returns a signed integer (range -128 to 127)', function() {
            var statusTable     = [0, 1, 128, 254, 255],
                statusExpected  = [0, 1, -128, -2, -1];

            for (var i in statusTable) {
                var formated = utils.formatStatus(statusTable[i]),
                    expected = statusExpected[i];

                expect(formated).to.equal(expected);
            }
        });
    });

    describe('.formatDockerHost()', function() {
        context('when using docker api through http', function() {
            it('returns a valid http docker host', function() {
                var dockerHost = utils.formatDockerHost('http://127.0.0.1:2375'),
                    expected   = { protocol: 'http', host: '127.0.0.1', port: 2375 };

                expect(dockerHost).to.deep.equal(expected);
            });
        });

        context('when using docker api through https', function() {
            it('returns a valid https docker host', function(){
                var dockerHost = utils.formatDockerHost('https://127.0.0.1:2376',
                                                        dockerCerts);

                expect(dockerHost).to.satisfy(validateHTTPS);
            });

            function validateHTTPS(dockerHost) {
                return dockerHost.protocol === 'https' &&
                       dockerHost.host     === '127.0.0.1' &&
                       dockerHost.port     === 2376 &&
                       !!dockerHost.key  &&
                       !!dockerHost.cert &&
                       !!dockerHost.ca;
            }
        });
    });
});

@@ -163,20 +163,27 @@
 s;%0A%0A
-var fsStub =
+utils.__set__('fs',
  %7B%0A 
@@ -236,37 +236,8 @@
  %7D%0A%7D
-;%0A%0Autils.__set__('fs', fsStub
 );%0A%0A

fix bad style in utils tests