'use strict';

var should = require('should');
should = should || should;
var concord = require('../index.js');

const PORT = 44044;

describe('server', () => {
    it('should announce client connection', (done) => {
        let server = new concord.Server();
        server.listen(PORT).then(() => {
            let client = new concord.Client();
            client.connect(PORT);
            server.on('client connected', (cl) => {
                should.exist(server.getClient(cl.name));
                server.clients.size.should.equal(1);
                done();
            });
        });
    });

    it('should receive message from client', (done) => {
        getServerClient().then((members) => {
            let server = members.server;
            let client = members.client;
            let message = 'test123';
            server.on('message', (msg) => {
                msg.should.equal(message);
                done();
            });
            client.sendMessage(message);
        });
        done();
    });
});

describe('client', () => {
    it('should connect to server', (done) => {
        let server = new concord.Server();
        server.listen(PORT).then(() => {
            let client = new concord.Client();
            client.connect(PORT);
            server.on('client connected', (cl) => {
                should.exist(client.connection);
                should.exist(client.disconnectTimeout);
                should.exist(cl.disconnectTimeout);
                done();
            });
        });
    });
});

function getServerClient() {
    let server = new concord.Server();
    return server.listen(PORT).then(() => {
        let client = new concord.Client();
        return client.connect(PORT).then(() => {
            return {
                server: server,
                client: client
            };
        });
    });
}


@@ -1019,24 +1019,872 @@
 e();%0A    %7D);
+%0A%0A    it('should support multiple client connections', (done) =%3E %7B%0A        getServerClient().then((members) =%3E %7B%0A            let server = members.server;%0A            let todo = %5B%5D;%0A            for (let i = 0; i %3C 20; ++i) %7B%0A                let client = new concord.Client();%0A                todo.push(new Promise((resolve) =%3E %7B%0A                    return client.connect(PORT).then(resolve);%0A                %7D));%0A            %7D%0A            Promise.all(todo).then(() =%3E %7B%0A                server.clients.size.should.equal(todo.length + 1);%0A            %7D);%0A            let count = 0;%0A            server.on('client connected', () =%3E %7B%0A                if (++count !== todo.length) %7B%0A                    return;%0A                %7D%0A                server.clients.size.should.equal(todo.length + 1);%0A                done();%0A            %7D);%0A        %7D);%0A    %7D);
 %0A%7D);%0A%0Adescri

Test multiple connections.