index.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { describe, expect, it } from "@jest/globals";
  2. import { Client } from "../../../../src/models/client.ts";
  3. import { TransmissionHandler } from "../../../../src/messageHandler/handlers/index.ts";
  4. import { Realm } from "../../../../src/models/realm.ts";
  5. import { MessageType } from "../../../../src/enums.ts";
  6. import type WebSocket from "ws";
  7. const createFakeSocket = (): WebSocket => {
  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 WebSocket;
  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, {
  25. type: MessageType.OFFER,
  26. src: clientFrom.getId(),
  27. dst: idTo,
  28. });
  29. expect(realm.getMessageQueueById(idTo)?.getMessages().length).toBe(1);
  30. });
  31. it("should not save LEAVE and EXPIRE messages in queue when destination client not connected", () => {
  32. const realm = new Realm();
  33. const handleTransmission = TransmissionHandler({ realm });
  34. const clientFrom = new Client({ id: "id1", token: "" });
  35. const idTo = "id2";
  36. realm.setClient(clientFrom, clientFrom.getId());
  37. handleTransmission(clientFrom, {
  38. type: MessageType.LEAVE,
  39. src: clientFrom.getId(),
  40. dst: idTo,
  41. });
  42. handleTransmission(clientFrom, {
  43. type: MessageType.EXPIRE,
  44. src: clientFrom.getId(),
  45. dst: idTo,
  46. });
  47. expect(realm.getMessageQueueById(idTo)).toBeUndefined();
  48. });
  49. it("should send message to destination client when destination client connected", () => {
  50. const realm = new Realm();
  51. const handleTransmission = TransmissionHandler({ realm });
  52. const clientFrom = new Client({ id: "id1", token: "" });
  53. const clientTo = new Client({ id: "id2", token: "" });
  54. const socketTo = createFakeSocket();
  55. clientTo.setSocket(socketTo);
  56. realm.setClient(clientTo, clientTo.getId());
  57. let sent = false;
  58. socketTo.send = (): void => {
  59. sent = true;
  60. };
  61. handleTransmission(clientFrom, {
  62. type: MessageType.OFFER,
  63. src: clientFrom.getId(),
  64. dst: clientTo.getId(),
  65. });
  66. expect(sent).toBe(true);
  67. });
  68. it("should send LEAVE message to source client when sending to destination client failed", () => {
  69. const realm = new Realm();
  70. const handleTransmission = TransmissionHandler({ realm });
  71. const clientFrom = new Client({ id: "id1", token: "" });
  72. const clientTo = new Client({ id: "id2", token: "" });
  73. const socketFrom = createFakeSocket();
  74. const socketTo = createFakeSocket();
  75. clientFrom.setSocket(socketFrom);
  76. clientTo.setSocket(socketTo);
  77. realm.setClient(clientFrom, clientFrom.getId());
  78. realm.setClient(clientTo, clientTo.getId());
  79. let sent = false;
  80. socketFrom.send = (data: string): void => {
  81. if (JSON.parse(data)?.type === MessageType.LEAVE) {
  82. sent = true;
  83. }
  84. };
  85. socketTo.send = (): void => {
  86. throw Error();
  87. };
  88. handleTransmission(clientFrom, {
  89. type: MessageType.OFFER,
  90. src: clientFrom.getId(),
  91. dst: clientTo.getId(),
  92. });
  93. expect(sent).toBe(true);
  94. });
  95. });