index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { expect } from 'chai';
  2. import { Client } from '../../../../src/models/client';
  3. import { TransmissionHandler } from '../../../../src/messageHandler/handlers';
  4. import { Realm } from '../../../../src/models/realm';
  5. import { MessageType } from '../../../../src/enums';
  6. import { MyWebSocket } from '../../../../src/services/webSocketServer/webSocket';
  7. const createFakeSocket = (): MyWebSocket => {
  8. /* eslint-disable @typescript-eslint/no-empty-function */
  9. const sock = {
  10. send: (): void => { },
  11. close: (): void => { },
  12. on: (): void => { },
  13. };
  14. /* eslint-enable @typescript-eslint/no-empty-function */
  15. return (sock as unknown as MyWebSocket);
  16. };
  17. describe('Transmission handler', () => {
  18. it('should save message in queue when destination client not connected', () => {
  19. const realm = new Realm();
  20. const handleTransmission = TransmissionHandler({ realm });
  21. const clientFrom = new Client({ id: 'id1', token: '' });
  22. const idTo = 'id2';
  23. realm.setClient(clientFrom, clientFrom.getId());
  24. handleTransmission(clientFrom, { type: MessageType.OFFER, src: clientFrom.getId(), dst: idTo });
  25. expect(realm.getMessageQueueById(idTo)?.getMessages().length).to.be.eq(1);
  26. });
  27. it('should not save LEAVE and EXPIRE messages in queue when destination client not connected', () => {
  28. const realm = new Realm();
  29. const handleTransmission = TransmissionHandler({ realm });
  30. const clientFrom = new Client({ id: 'id1', token: '' });
  31. const idTo = 'id2';
  32. realm.setClient(clientFrom, clientFrom.getId());
  33. handleTransmission(clientFrom, { type: MessageType.LEAVE, src: clientFrom.getId(), dst: idTo });
  34. handleTransmission(clientFrom, { type: MessageType.EXPIRE, src: clientFrom.getId(), dst: idTo });
  35. expect(realm.getMessageQueueById(idTo)).to.be.undefined;
  36. });
  37. it('should send message to destination client when destination client connected', () => {
  38. const realm = new Realm();
  39. const handleTransmission = TransmissionHandler({ realm });
  40. const clientFrom = new Client({ id: 'id1', token: '' });
  41. const clientTo = new Client({ id: 'id2', token: '' });
  42. const socketTo = createFakeSocket();
  43. clientTo.setSocket(socketTo);
  44. realm.setClient(clientTo, clientTo.getId());
  45. let sent = false;
  46. socketTo.send = (): void => {
  47. sent = true;
  48. };
  49. handleTransmission(clientFrom, { type: MessageType.OFFER, src: clientFrom.getId(), dst: clientTo.getId() });
  50. expect(sent).to.be.true;
  51. });
  52. it('should send LEAVE message to source client when sending to destination client failed', () => {
  53. const realm = new Realm();
  54. const handleTransmission = TransmissionHandler({ realm });
  55. const clientFrom = new Client({ id: 'id1', token: '' });
  56. const clientTo = new Client({ id: 'id2', token: '' });
  57. const socketFrom = createFakeSocket();
  58. const socketTo = createFakeSocket();
  59. clientFrom.setSocket(socketFrom);
  60. clientTo.setSocket(socketTo);
  61. realm.setClient(clientFrom, clientFrom.getId());
  62. realm.setClient(clientTo, clientTo.getId());
  63. let sent = false;
  64. socketFrom.send = (data: string): void => {
  65. if (JSON.parse(data)?.type === MessageType.LEAVE) {
  66. sent = true;
  67. }
  68. };
  69. socketTo.send = (): void => {
  70. throw Error();
  71. };
  72. handleTransmission(clientFrom, { type: MessageType.OFFER, src: clientFrom.getId(), dst: clientTo.getId() });
  73. expect(sent).to.be.true;
  74. });
  75. });