|
1 |
| -describe("Test web socket", () => { |
2 |
| - it("Test", () => { |
3 |
| - expect(true); |
| 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"; |
| 5 | +import { MatchEvents } from "../src/handlers/websocketHandler"; |
| 6 | +import { MatchUser } from "../src/handlers/matchHandler"; |
| 7 | + |
| 8 | +describe("Matching service web socket", () => { |
| 9 | + let io: Server, serverSocket: Socket, clientSocket: SocketIOClient.Socket; |
| 10 | + |
| 11 | + beforeAll((done) => { |
| 12 | + const httpServer = createServer(); |
| 13 | + io = new Server(httpServer); |
| 14 | + httpServer.listen(() => { |
| 15 | + const port = (httpServer.address() as AddressInfo).port; |
| 16 | + clientSocket = ioc(`http://localhost:${port}`); |
| 17 | + io.on("connection", (socket) => { |
| 18 | + serverSocket = socket; |
| 19 | + }); |
| 20 | + clientSocket.on("connect", done); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + afterAll(() => { |
| 25 | + io.close(); |
| 26 | + clientSocket.close(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("Match found", (done) => { |
| 30 | + const matchIdSent = "123"; |
| 31 | + const user1Sent = { |
| 32 | + id: "456", |
| 33 | + username: "user1", |
| 34 | + }; |
| 35 | + const user2Sent = { |
| 36 | + id: "789", |
| 37 | + username: "user2", |
| 38 | + }; |
| 39 | + clientSocket.on( |
| 40 | + MatchEvents.MATCH_FOUND, |
| 41 | + ({ |
| 42 | + matchId, |
| 43 | + user1, |
| 44 | + user2, |
| 45 | + }: { |
| 46 | + matchId: string; |
| 47 | + user1: MatchUser; |
| 48 | + user2: MatchUser; |
| 49 | + }) => { |
| 50 | + expect(matchId).toEqual(matchIdSent); |
| 51 | + expect(user1).toEqual(user1Sent); |
| 52 | + expect(user2).toEqual(user2Sent); |
| 53 | + done(); |
| 54 | + } |
| 55 | + ); |
| 56 | + serverSocket.emit(MatchEvents.MATCH_FOUND, { |
| 57 | + matchId: matchIdSent, |
| 58 | + user1: user1Sent, |
| 59 | + user2: user2Sent, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("Match successful", (done) => { |
| 64 | + const questionIdSent = "123"; |
| 65 | + const questionTitleSent = "456"; |
| 66 | + clientSocket.on( |
| 67 | + MatchEvents.MATCH_SUCCESSFUL, |
| 68 | + ({ |
| 69 | + questionId, |
| 70 | + questionTitle, |
| 71 | + }: { |
| 72 | + questionId: string; |
| 73 | + questionTitle: string; |
| 74 | + }) => { |
| 75 | + expect(questionId).toEqual(questionIdSent); |
| 76 | + expect(questionTitle).toEqual(questionTitleSent); |
| 77 | + done(); |
| 78 | + } |
| 79 | + ); |
| 80 | + serverSocket.emit(MatchEvents.MATCH_SUCCESSFUL, { |
| 81 | + questionId: questionIdSent, |
| 82 | + questionTitle: questionTitleSent, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("Match unsuccessful", (done) => { |
| 87 | + clientSocket.on(MatchEvents.MATCH_UNSUCCESSFUL, () => { |
| 88 | + done(); |
| 89 | + }); |
| 90 | + serverSocket.emit(MatchEvents.MATCH_UNSUCCESSFUL); |
| 91 | + }); |
| 92 | + |
| 93 | + it("Match request exists", (done) => { |
| 94 | + clientSocket.on(MatchEvents.MATCH_REQUEST_EXISTS, () => { |
| 95 | + done(); |
| 96 | + }); |
| 97 | + serverSocket.emit(MatchEvents.MATCH_REQUEST_EXISTS); |
| 98 | + }); |
| 99 | + |
| 100 | + it("Match request error", (done) => { |
| 101 | + clientSocket.on(MatchEvents.MATCH_REQUEST_ERROR, () => { |
| 102 | + done(); |
| 103 | + }); |
| 104 | + serverSocket.emit(MatchEvents.MATCH_REQUEST_ERROR); |
4 | 105 | });
|
5 | 106 | });
|
0 commit comments