let assert = require('assert');
import Utils from '../utils';
let path = require('path');

let options = {
    'deep1': {
        'deep2': {
            'deep3': {
                'int': 4
            }
        }
    },
    'string': "mda"
}

class Test {
    public a = 0;
    public test(){
        return "test";
    }
}

class Test2 extends Test {

}

describe('test utils', function () {
    it('deepExtend', function (done) {
        assert.equal(options.deep1.deep2.deep3.int, 4);
        assert.equal(options.string, "mda");
        let config = Utils.deepExtend({}, options, {'deep1': {'deep2': {'deep3': {'int': 6}}}});
        assert.equal(config.deep1.deep2.deep3.int, 6);
        assert.equal(config.string, "mda");
        done();
    });

    it('pad', function (done) {
        assert.equal(Utils.pad(4), "04");
        assert.equal(Utils.pad(0), "00");
        assert.equal(Utils.pad(10), "10");
        assert.equal(Utils.pad(444), "444");
        done();
    });

    it('getIp', function (done) {
        assert.equal(Utils.getIp({'ip': "::1"}), "127.0.0.1");  
        assert.equal(Utils.getIp({'_remoteAddress': "::1"}), "127.0.0.1");        
        assert.equal(Utils.getIp({'connection': {'remoteAddress': "::1"}}), "127.0.0.1");

        assert.equal(Utils.getIp({'ip': "::ffff:ab87:3422"}), "ab87:3422");  
        assert.equal(Utils.getIp({'_remoteAddress': "::ffff:342"}), "342");        
        assert.equal(Utils.getIp({'connection': {'remoteAddress': "::ffff:ffff"}}), "ffff");

        assert.equal(Utils.getIp({'connection': null}), undefined); 
        assert.equal(Utils.getIp({'simple': null}), undefined);         
        assert.equal(Utils.getIp(null), undefined);
        
        done();
    });

    it('getFullRouteFrom', function (done) {
        let baseRoot = __dirname;
        if(require.main){
            baseRoot = path.dirname(require.main.filename); 
        }
        assert.equal(Utils.getFullRouteFrom(null), baseRoot);
        assert.equal(Utils.getFullRouteFrom(), baseRoot);
        assert.equal(Utils.getFullRouteFrom("test"), baseRoot + "/test");
        assert.equal(Utils.getFullRouteFrom("../test"), baseRoot + "/../test");
        assert.equal(Utils.getFullRouteFrom(""), baseRoot + "/");

        done();
    }); 
    
    it('convertToBytesSize', function (done) {
        assert.equal(Utils.convertToBytesSize(984), "984.00B");
        assert.equal(Utils.convertToBytesSize(0), "0");
        assert.equal(Utils.convertToBytesSize(1), "1.00B");
        assert.equal(Utils.convertToBytesSize(-12), "-12");
        assert.equal(Utils.convertToBytesSize(1024), "1.00KB");
        assert.equal(Utils.convertToBytesSize(1024 + 10), "1.01KB");
        assert.equal(Utils.convertToBytesSize(1024 - 10), "1014.00B");        
        assert.equal(Utils.convertToBytesSize(Math.pow(1024,2)), "1.00MB");        
        assert.equal(Utils.convertToBytesSize(Math.pow(1024,3)), "1.00GB");
        assert.equal(Utils.convertToBytesSize(Math.pow(1024,4)), "1.00TB");
        assert.equal(Utils.convertToBytesSize(Math.pow(1024,5)), "1.00PB");
        assert.equal(Utils.convertToBytesSize(Math.pow(1024,6)), "+1023.99PB");
        done();
    })
});