import {expect} from 'chai';
import {Config} from '../../src/config';
import {Client} from '../../src/kite';
import {Map} from '../../src/types/lang';
import {Address} from '../../src/protocol/protocol';

describe('new config', function () {
    it('#1 config hosts', function (ok) {
        let config = new Config({
            'hosts': {
                'bj': [
                    {
                        'host': '127.0.0.1',
                        'port': 10200
                    }
                ]
            }
        },
        'bj');

        config.getHosts(function (err: any, hosts: Array<Address>) {
            expect(hosts.length).to.be.equal(1);
            var host = hosts[0];

            expect(hosts[0].getHost()).to.be.equal('127.0.0.1');
            expect(hosts[0].getPort()).to.be.equal(10200);

            ok();
        });
        

    });

    it('#2 config host', function (ok) {
        let config = new Config({
            'host': '127.0.0.1',
            'port': 50022
        });

        config.getHosts(function (err: any, hosts: Array<Address>) {
            expect(hosts.length).to.be.equal(1);
            expect(hosts[0].getHost()).to.be.equal('127.0.0.1');
            expect(hosts[0].getPort()).to.be.equal(50022);

            ok();
        });

    })
});

describe('new client', function () {
    it ('#1 new client Thrift', function (ok) {
        var client = new Client({
            'host': '127.0.0.1',
            'port': 9090,
            'protocol': 'THRIFT',
            'log': {
                logFile: __dirname + '/test.log'
            }
        });

        var conn = client
            .loadService(__dirname + '/../thrift/gen-nodejs/Calculator')
            .request(function (err, service, conn) {
                service.ping(function (err, response) {
                    console.log('ping()');
                });

                service.add(1, 1, function (err, response) {
                    expect(response).to.be.equal(2);
                    ok();
                });
            });


    });

    it ('#2 new client HTTP', function (ok) {
        var client = new Client({
            'host': '127.0.0.1',
            'port': 8080,
            'protocol': 'HTTP',
            'retry': 2,
            'log': {
                logFile: __dirname + '/test.log',
                logId: 1111111,
            }
        });

        client.request(function (err, data) {
            ok();
        });
    });

    it ('#3 new client HTTP with path', function (ok) {
        var client = new Client({
            host: '127.0.0.1',
            port: 8080,
            protocol: 'HTTP',
            retry: 2,
            log: {
                logFile: __dirname + '/test.log',
                logId: Date.now()
            }
        });

        client.request({path: 'test.ts'}, function (err, response, body) {
            console.log(body);
            ok();
        });
    });
});
