/**
 * Created by godsong on 16/11/6.
 */
const Channel=require('../src/components/Channel');
import chai from "chai";
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
chai.should();
chai.use(sinonChai);
describe('Channel Test',function(){

    let socketA={send:function(msg){
        console.log('socketA send:',msg);
    }};
    let socketB={send:function(msg){
        console.log('socketB send:',msg);
    }};
    let socketC={send:function(msg){
        console.log('socketB send:',msg);
    }};
    let mockSocketA = sinon.mock(socketA);
    let mockSocketB = sinon.mock(socketB);
    let mockSocketC = sinon.mock(socketC);

    let msg='how old are you?';
    let replay='i\'m fine and you?';
    afterEach(function(){
        mockSocketA.restore();
        mockSocketB.restore();
        mockSocketC.restore();
    });
    it('message dispatcher',function(){
        let channel=new Channel();
        channel.join(socketA);
        channel.join(socketB);
        channel.has(socketA).should.be.equal(true);
        channel.has(socketB).should.be.equal(true);
        channel.has(socketC).should.be.equal(false);
        mockSocketA.expects('send').once().withArgs(replay);
        mockSocketB.expects('send').once().withArgs(msg);
        channel.send(socketA,msg);
        channel.send(socketB,replay);
        mockSocketA.verify();
        mockSocketB.verify();

    });
    it('message cache&leave',function(){
        mockSocketA.expects('send').once().withArgs(replay).once().withArgs('not cached');
        mockSocketB.expects('send').once().withArgs(msg);
        let channel=new Channel();
        channel.join(socketA);
        channel.send(socketA,msg);
        channel.join(socketB);
        channel.has(socketA).should.be.equal(true);
        channel.leave(socketA);
        channel.has(socketA).should.be.equal(false);
        channel.send(socketB,'not cached',true);
        channel.send(socketB,replay);
        /*mockSocketB.expects('send').once().withArgs(msg);
        channel.join(socketA);
        channel.send(socketA,'ha ha ha!');
        channel.join(socketB);
        channel.has(socketA).should.be.equal(true);
        channel.join()
        mockSocketB.verify();*/
    })
})