describe('#set and get', function () { it('should play the role of normal set and get', function () { should.not.exist(app.get('some undefined key')); let key = 'some defined key', value = 'some value'; app.set(key, value); value.should.equal(app.get(key)); }); it('should return the value if pass just one parameter to the set method', function () { let key = 'some defined key', value = 'some value'; should.not.exist(app.get(key)); app.set(key, value); value.should.equal(app.get(key)); }); });
it('should be ok for encoding and decoding response', function() { var id = 1; var compress = 0; var msg = 'hello world~'; var buf = Message.encode(id, Message.TYPE_RESPONSE, compress, null, Protocol.strencode(msg)); should.exist(buf); var res = Message.decode(buf); should.exist(res); id.should.equal(res.id); Message.TYPE_RESPONSE.should.equal(res.type); compress.should.equal(res.compressRoute); should.not.exist(res.route); should.exist(res.body); msg.should.equal(Protocol.strdecode(res.body)); });
describe('#getUserRemotePath', function () { it('should return user remote service path for the associated server type', function () { let serverType = 'connector'; let expectSuffix = '/app/servers/connector/remote'; let p = pathUtil.getUserRemotePath(mockBase, serverType); should.exist(p); fs.existsSync(p).should.be.true; utils.endsWith(p, expectSuffix).should.be.true; }); it('should return null if the directory not exist', function () { let serverType = 'area'; let p = pathUtil.getUserRemotePath(mockBase, serverType); should.not.exist(p); serverType = 'some-dir-not-exist'; p = pathUtil.getUserRemotePath(mockBase, serverType); should.not.exist(p); }); });
describe('Package encode and decode', function() { it('should keep the same data after encoding and decoding', function() { var msg = 'hello world~'; var buf = Package.encode(Package.TYPE_DATA, Protocol.strencode(msg)); should.exist(buf); var res = Package.decode(buf); should.exist(res); Package.TYPE_DATA.should.equal(res.type); should.exist(res.body); msg.should.equal(Protocol.strdecode(res.body)); }); it('should ok when encoding and decoding package without body', function() { var buf = Package.encode(Package.TYPE_HANDSHAKE); should.exist(buf); var res = Package.decode(buf); should.exist(res); Package.TYPE_HANDSHAKE.should.equal(res.type); should.not.exist(res.body); }); });
describe('#leave', function () { it('should remove the member from channel when leave', function () { let channelService = new ChannelService(mockApp); let channel = channelService.createChannel(channelName); should.exist(channel); let uid = 'uid1', sid = 'sid1'; channel.add(uid, sid).should.be.true; let member = channel.getMember(uid); should.exist(member); channel.leave(uid, sid); member = channel.getMember(uid); should.not.exist(member); }); it('should fail if uid or sid not specified', function () { let channelService = new ChannelService(mockApp); let channel = channelService.createChannel(channelName); should.exist(channel); let uid = 'uid1', sid = 'sid1'; channel.add(uid, sid).should.be.true; channel.leave(uid, null).should.be.false; channel.leave(null, sid).should.be.false; }); });
describe('#getChannel', function () { it('should return the channel with the specified name if it exists', function () { let channelService = new ChannelService(mockApp); channelService.createChannel(channelName); let channel = channelService.getChannel(channelName); should.exist(channel); channelName.should.equal(channel.name); }); it('should return undefined if the channel dose not exist', function () { let channelService = new ChannelService(mockApp); let channel = channelService.getChannel(channelName); should.not.exist(channel); }); it('should create and return a new channel if create parameter is set', function () { let channelService = new ChannelService(mockApp); let channel = channelService.getChannel(channelName, true); should.exist(channel); channelName.should.equal(channel.name); }); });
describe('#removeLoginedUser', function () { it('should remove logined user info with the uid', function () { let service = new ConnectionService(mockApp); should.exist(service); service.loginedCount.should.equal(0); let uid = 'uid1'; let info = { msg: 'some other message' }; service.addLoginedUser(uid, info); service.loginedCount.should.equal(1); let record = service.logined[uid]; should.exist(record); let uid2 = 'uid2'; service.removeLoginedUser(uid2); service.loginedCount.should.equal(1); record = service.logined[uid]; should.exist(record); service.removeLoginedUser(uid); service.loginedCount.should.equal(0); record = service.logined[uid]; should.not.exist(record); }); });
describe('#getHandlerPath', function () { it('should return user handler path for the associated server type', function () { let serverType = 'connector'; let expectSuffix = '/app/servers/connector/handler'; let p = pathUtil.getHandlerPath(mockBase, serverType); should.exist(p); fs.existsSync(p).should.be.true; utils.endsWith(p, expectSuffix).should.be.true; }); it('should return null if the directory not exist', function () { let serverType = 'area'; let p = pathUtil.getHandlerPath(mockBase, serverType); should.not.exist(p); serverType = 'some-dir-not-exist'; p = pathUtil.getHandlerPath(mockBase, serverType); should.not.exist(p); }); });
describe('#set and get', function () { it('should play the role of normal set and get', function () { should.not.exist(app.get('some undefined key')); let key = 'some defined key', value = 'some value'; app.set(key, value); value.should.equal(app.get(key)); }); it('should return the value if pass just one parameter to the set method', function () { let key = 'some defined key', value = 'some value'; should.not.exist(app.get(key)); app.set(key, value); value.should.equal(app.get(key)); }); });
it('should be ok for encoding and decoding response', function() { var id = 1; var compress = 0; var msg = 'hello world~'; var buf = Message.encode(id, Message.TYPE_RESPONSE, compress, null, Protocol.strencode(msg)); should.exist(buf); var res = Message.decode(buf); should.exist(res); id.should.equal(res.id); Message.TYPE_RESPONSE.should.equal(res.type); compress.should.equal(res.compressRoute); should.not.exist(res.route); should.exist(res.body); msg.should.equal(Protocol.strdecode(res.body)); });
describe('Package encode and decode', function() { it('should keep the same data after encoding and decoding', function() { var msg = 'hello world~'; var buf = Package.encode(Package.TYPE_DATA, Protocol.strencode(msg)); should.exist(buf); var res = Package.decode(buf); should.exist(res); Package.TYPE_DATA.should.equal(res.type); should.exist(res.body); msg.should.equal(Protocol.strdecode(res.body)); }); it('should ok when encoding and decoding package without body', function() { var buf = Package.encode(Package.TYPE_HANDSHAKE); should.exist(buf); var res = Package.decode(buf); should.exist(res); Package.TYPE_HANDSHAKE.should.equal(res.type); should.not.exist(res.body); }); });
describe('#getUserRemotePath', function () { it('should return user remote service path for the associated server type', function () { let serverType = 'connector'; let expectSuffix = '/app/servers/connector/remote'; let p = pathUtil.getUserRemotePath(mockBase, serverType); should.exist(p); fs.existsSync(p).should.be.true; utils.endsWith(p, expectSuffix).should.be.true; }); it('should return null if the directory not exist', function () { let serverType = 'area'; let p = pathUtil.getUserRemotePath(mockBase, serverType); should.not.exist(p); serverType = 'some-dir-not-exist'; p = pathUtil.getUserRemotePath(mockBase, serverType); should.not.exist(p); }); });
describe('#getHandlerPath', function () { it('should return user handler path for the associated server type', function () { let serverType = 'connector'; let expectSuffix = '/app/servers/connector/handler'; let p = pathUtil.getHandlerPath(mockBase, serverType); should.exist(p); fs.existsSync(p).should.be.true; utils.endsWith(p, expectSuffix).should.be.true; }); it('should return null if the directory not exist', function () { let serverType = 'area'; let p = pathUtil.getHandlerPath(mockBase, serverType); should.not.exist(p); serverType = 'some-dir-not-exist'; p = pathUtil.getHandlerPath(mockBase, serverType); should.not.exist(p); }); });
describe('#removeLoginedUser', function () { it('should remove logined user info with the uid', function () { let service = new ConnectionService(mockApp); should.exist(service); service.loginedCount.should.equal(0); let uid = 'uid1'; let info = { msg: 'some other message' }; service.addLoginedUser(uid, info); service.loginedCount.should.equal(1); let record = service.logined[uid]; should.exist(record); let uid2 = 'uid2'; service.removeLoginedUser(uid2); service.loginedCount.should.equal(1); record = service.logined[uid]; should.exist(record); service.removeLoginedUser(uid); service.loginedCount.should.equal(0); record = service.logined[uid]; should.not.exist(record); }); });
describe('#getChannel', function () { it('should return the channel with the specified name if it exists', function () { let channelService = new ChannelService(mockApp); channelService.createChannel(channelName); let channel = channelService.getChannel(channelName); should.exist(channel); channelName.should.equal(channel.name); }); it('should return undefined if the channel dose not exist', function () { let channelService = new ChannelService(mockApp); let channel = channelService.getChannel(channelName); should.not.exist(channel); }); it('should create and return a new channel if create parameter is set', function () { let channelService = new ChannelService(mockApp); let channel = channelService.getChannel(channelName, true); should.exist(channel); channelName.should.equal(channel.name); }); });