Skip to content

Commit 33554ec

Browse files
committed
Add tests
1 parent 5f4834e commit 33554ec

File tree

3 files changed

+189
-4
lines changed

3 files changed

+189
-4
lines changed

backend/communication-service/package-lock.json

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

backend/communication-service/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
"@types/express": "^5.0.0",
2525
"@types/jest": "^29.5.14",
2626
"@types/socket.io": "^3.0.1",
27+
"@types/socket.io-client": "^1.4.36",
2728
"cross-env": "^7.0.3",
2829
"eslint": "^9.13.0",
2930
"globals": "^15.11.0",
3031
"jest": "^29.7.0",
32+
"socket.io-client": "^4.8.1",
3133
"ts-jest": "^29.2.5",
3234
"ts-node": "^10.9.2",
3335
"tsx": "^4.19.2",
Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,92 @@
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";
25

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+
});
691
});
792
});

0 commit comments

Comments
 (0)