io-spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. const server = require('../io');
  2. const client = require('socket.io-client');
  3. const url = 'http://127.0.0.1:5000';
  4. const options = {
  5. transports: ['websocket'],
  6. 'force new connection': true
  7. };
  8. server.io.listen(5000);
  9. var p1, p2, p3, c1, c2, games, token;
  10. function getGames() {
  11. games = server.getGames();
  12. }
  13. describe('io', () => {
  14. it('should create new game', done => {
  15. p1 = client.connect(url, options);
  16. p1.on('connect', data => {
  17. p1.emit('start');
  18. });
  19. p1.on('created', data => {
  20. getGames();
  21. token = data.token;
  22. expect(games.has(token)).toBeTruthy();
  23. expect(games.getIn([token, 'players']).isEmpty()).toBeTruthy();
  24. expect(games.getIn([token, 'timeout'])).toBeDefined();
  25. done();
  26. });
  27. });
  28. it('should join player to the game and emit events to both of them', done => {
  29. p2 = client.connect(url, options);
  30. p2.on('connect', data => {
  31. p2.emit('join', {
  32. token: token,
  33. time: 10 * 60,
  34. inc: 5
  35. });
  36. });
  37. p1.on('ready', () => {
  38. p1.emit('join', {
  39. token: token,
  40. time: 10 * 60,
  41. inc: 5
  42. });
  43. });
  44. p1.on('joined', data => {
  45. c1 = data.color;
  46. expect(data.color === 'white' || data.color === 'black').toBeTruthy();
  47. });
  48. p2.on('joined', data => {
  49. c2 = data.color;
  50. expect(data.color === 'white' || data.color === 'black').toBeTruthy();
  51. });
  52. p1.on('both-joined', data => {
  53. getGames();
  54. var players = games.getIn([token, 'players']);
  55. expect(games.size).toBe(1);
  56. expect(players.size).toBe(2);
  57. expect(players.getIn([0, 'color']) !== players.getIn([1, 'color']))
  58. .toBeTruthy();
  59. expect(players.getIn([0, 'inc'])).toBe(5);
  60. done();
  61. });
  62. });
  63. it('should emit that game is full to third player', done => {
  64. p3 = client.connect(url, options);
  65. p3.on('connect', data => {
  66. p3.emit('join', {
  67. token: token,
  68. time: 10,
  69. inc: 5
  70. });
  71. });
  72. p3.on('full', () => {
  73. getGames();
  74. expect(games.getIn([token, 'players']).size).toBe(2);
  75. done();
  76. });
  77. });
  78. it('should run clock', done => {
  79. p1.emit('clock-run', {
  80. token: token,
  81. color: c1
  82. });
  83. p1.on('countdown', data => {
  84. expect(data.color).toBe(c1);
  85. expect(data.time).toBe(600);
  86. done();
  87. });
  88. });
  89. it('should make new move', done => {
  90. p1.emit('new-move', {
  91. token: token,
  92. color: c1,
  93. move: {
  94. from: 'd2',
  95. to: 'd4',
  96. capture: undefined,
  97. gameOver: false
  98. },
  99. });
  100. p2.on('move', data => {
  101. expect(data.from).toBe('d2');
  102. expect(data.to).toBe('d4');
  103. expect(data.gameOver).toBeFalsy();
  104. done();
  105. });
  106. });
  107. it('should send and receive messages', done => {
  108. p1.emit('send-message', {
  109. token: token,
  110. color: c1,
  111. message: 'hi there'
  112. });
  113. p2.on('receive-message', data => {
  114. expect(data.message).toBe('hi there');
  115. p2.emit('send-message', {
  116. token: token,
  117. color: c2,
  118. message: 'hello!'
  119. });
  120. });
  121. p1.on('receive-message', data => {
  122. expect(data.message).toBe('hello!');
  123. done();
  124. });
  125. });
  126. it('should resign the game', done => {
  127. p2.emit('resign', {
  128. token: token,
  129. color: c2
  130. });
  131. p1.on('player-resigned', data => {
  132. expect(data.color).toBe(c2);
  133. done();
  134. });
  135. });
  136. it('should offer rematch and decline', done => {
  137. p1.emit('rematch-offer', {
  138. token: token
  139. });
  140. p2.on('rematch-offered', () => {
  141. p2.emit('rematch-decline', {
  142. token: token
  143. });
  144. });
  145. p1.on('rematch-declined', () => {
  146. done();
  147. });
  148. });
  149. it('should offer rematch and accept', done => {
  150. p1.emit('rematch-offer', {
  151. token: token
  152. });
  153. p2.on('rematch-offered', () => {
  154. p2.emit('rematch-accept', {
  155. token: token,
  156. time: 10 * 60,
  157. inc: 5
  158. });
  159. });
  160. p1.on('rematch-accepted', () => {
  161. getGames();
  162. expect(games.getIn([token, 'players', 0, 'time'])).toBe(600 - 5 + 1);
  163. expect(games.getIn([token, 'players', 1, 'time'])).toBe(600 - 5 + 1);
  164. done();
  165. });
  166. });
  167. it('should disconnect player and delete game', done => {
  168. p2.disconnect();
  169. p1.on('opponent-disconnected', () => {
  170. getGames();
  171. expect(games.size).toBe(0);
  172. done();
  173. });
  174. });
  175. it('should delete game with only one player on disconnect', done => {
  176. p3 = client.connect(url, options);
  177. p3.on('connect', () => p3.emit('start'));
  178. p3.on('created', data => p3.emit('join', {
  179. token: data.token,
  180. time: 10 * 60,
  181. inc: 5
  182. }));
  183. p3.on('joined', () => {
  184. getGames();
  185. expect(games.size).toBe(1);
  186. p3.disconnect();
  187. setTimeout(() => {
  188. getGames();
  189. expect(games.size).toBe(0);
  190. done();
  191. }, 100);
  192. });
  193. });
  194. });