|
1 |
| -// todo: add test cases |
| 1 | +import { createServer } from "node:http"; |
| 2 | +import { type AddressInfo } from "node:net"; |
| 3 | +import ioc from "socket.io-client"; |
| 4 | +import { Server, Socket } from "socket.io"; |
2 | 5 |
|
3 |
| -describe("Test web socket", () => { |
4 |
| - it("Test", () => { |
5 |
| - expect(true); |
| 6 | +import { BOT_NAME } from "../src/utils/constants"; |
| 7 | +import { CommunicationEvents, MessageTypes } from "../src/utils/types"; |
| 8 | + |
| 9 | +describe("Communication service web socket", () => { |
| 10 | + let io: Server, serverSocket: Socket, clientSocket: SocketIOClient.Socket; |
| 11 | + |
| 12 | + beforeAll((done) => { |
| 13 | + const httpServer = createServer(); |
| 14 | + io = new Server(httpServer); |
| 15 | + httpServer.listen(() => { |
| 16 | + const port = (httpServer.address() as AddressInfo).port; |
| 17 | + clientSocket = ioc(`http://localhost:${port}`); |
| 18 | + io.on("connection", (socket) => { |
| 19 | + serverSocket = socket; |
| 20 | + }); |
| 21 | + clientSocket.on("connect", done); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + afterAll(() => { |
| 26 | + io.close(); |
| 27 | + clientSocket.close(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("User joined acknowledgement", (done) => { |
| 31 | + const createdAt = Date.now(); |
| 32 | + const messageSent = "User has joined the chat"; |
| 33 | + clientSocket.on( |
| 34 | + CommunicationEvents.USER_JOINED, |
| 35 | + ({ |
| 36 | + from, |
| 37 | + type, |
| 38 | + message, |
| 39 | + createdTime, |
| 40 | + }: { |
| 41 | + from: string; |
| 42 | + type: string; |
| 43 | + message: string; |
| 44 | + createdTime: number; |
| 45 | + }) => { |
| 46 | + expect(from).toBe(BOT_NAME); |
| 47 | + expect(type).toBe(MessageTypes.BOT_GENERATED); |
| 48 | + expect(message).toBe(messageSent); |
| 49 | + expect(createdTime).toBe(createdAt); |
| 50 | + done(); |
| 51 | + } |
| 52 | + ); |
| 53 | + serverSocket.emit(CommunicationEvents.USER_JOINED, { |
| 54 | + from: BOT_NAME, |
| 55 | + type: MessageTypes.BOT_GENERATED, |
| 56 | + message: messageSent, |
| 57 | + createdTime: createdAt, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("Message received", (done) => { |
| 62 | + const createdAt = Date.now(); |
| 63 | + const messageSent = "Hello"; |
| 64 | + const username = "user"; |
| 65 | + clientSocket.on( |
| 66 | + CommunicationEvents.TEXT_MESSAGE_RECEIVED, |
| 67 | + ({ |
| 68 | + from, |
| 69 | + type, |
| 70 | + message, |
| 71 | + createdTime, |
| 72 | + }: { |
| 73 | + from: string; |
| 74 | + type: string; |
| 75 | + message: string; |
| 76 | + createdTime: number; |
| 77 | + }) => { |
| 78 | + expect(from).toBe(username); |
| 79 | + expect(type).toBe(MessageTypes.USER_GENERATED); |
| 80 | + expect(message).toBe(messageSent); |
| 81 | + expect(createdTime).toBe(createdAt); |
| 82 | + done(); |
| 83 | + } |
| 84 | + ); |
| 85 | + serverSocket.emit(CommunicationEvents.TEXT_MESSAGE_RECEIVED, { |
| 86 | + from: username, |
| 87 | + type: MessageTypes.USER_GENERATED, |
| 88 | + message: messageSent, |
| 89 | + createdTime: createdAt, |
| 90 | + }); |
6 | 91 | });
|
7 | 92 | });
|
0 commit comments