team.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015 rain1017.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var env = require('../env');
  17. var quick = require('quick-pomelo');
  18. var P = quick.Promise;
  19. var logger = quick.logger.getLogger('test', __filename);
  20. describe('team test', function(){
  21. beforeEach(env.initMemdbSync);
  22. afterEach(env.closeMemdbSync);
  23. it('team test', function(cb){
  24. var app = env.createApp('team-server-1', 'team');
  25. return P.try(function(){
  26. return P.promisify(app.start, app)();
  27. })
  28. .then(function(){
  29. var teamController = app.controllers.team;
  30. var playerController = app.controllers.player;
  31. var goose = app.memdb.goose;
  32. return goose.transaction(function(){
  33. var teamId = 't1', playerId = 'p1';
  34. return P.try(function(){
  35. return playerController.createAsync({_id : playerId, name : 'rain'});
  36. })
  37. .then(function(){
  38. return teamController.createAsync({_id : teamId, name : 'team1'});
  39. })
  40. .then(function(){
  41. return teamController.joinAsync(teamId, playerId);
  42. })
  43. .then(function(){
  44. return teamController.getPlayersAsync(teamId)
  45. .then(function(players){
  46. players.length.should.eql(1);
  47. players[0]._id.should.eql(playerId);
  48. });
  49. })
  50. .then(function(){
  51. return playerController.connectAsync(playerId, 'c1');
  52. })
  53. .then(function(){
  54. return teamController.pushAsync(teamId, null, 'chat', 'hello', true);
  55. })
  56. .then(function(){
  57. return teamController.getMsgsAsync(teamId, 0)
  58. .then(function(msgs){
  59. msgs.length.should.eql(1);
  60. msgs[0].msg.should.eql('hello');
  61. });
  62. })
  63. .then(function(){
  64. //Should automatically quit team
  65. return playerController.removeAsync(playerId);
  66. })
  67. .then(function(){
  68. return teamController.removeAsync(teamId);
  69. });
  70. }, app.getServerId());
  71. })
  72. .then(function(){
  73. return P.promisify(app.stop, app)();
  74. })
  75. .nodeify(cb);
  76. });
  77. });