Skip to content

Commit e98e697

Browse files
committed
Add tests for matching socket
1 parent 0d744bf commit e98e697

File tree

4 files changed

+205
-11
lines changed

4 files changed

+205
-11
lines changed

backend/matching-service/package-lock.json

Lines changed: 98 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/matching-service/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
"@types/jest": "^29.5.13",
3232
"@types/node": "^22.7.5",
3333
"@types/socket.io": "^3.0.2",
34+
"@types/socket.io-client": "^1.4.36",
3435
"@types/supertest": "^6.0.2",
3536
"@types/swagger-ui-express": "^4.1.6",
3637
"@types/uuid": "^10.0.0",
3738
"cross-env": "^7.0.3",
3839
"eslint": "^9.12.0",
3940
"globals": "^15.11.0",
4041
"jest": "^29.7.0",
42+
"socket.io-client": "^4.8.1",
4143
"supertest": "^7.0.0",
4244
"ts-jest": "^29.2.5",
4345
"ts-node": "^10.9.2",

backend/matching-service/src/handlers/websocketHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { io } from "../server";
1313
import { v4 as uuidv4 } from "uuid";
1414
import { getRandomQuestion } from "../api/questionService";
1515

16-
enum MatchEvents {
16+
export enum MatchEvents {
1717
// Receive
1818
MATCH_REQUEST = "match_request",
1919
CANCEL_MATCH_REQUEST = "cancel_match_request",
Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,106 @@
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);
4105
});
5106
});

0 commit comments

Comments
 (0)