Skip to content

Commit 2a64d0a

Browse files
committed
Merge branch 'development' into chore/build
2 parents ac083cd + 614dde5 commit 2a64d0a

File tree

27 files changed

+476
-157
lines changed

27 files changed

+476
-157
lines changed

backend/collab-service/package-lock.json

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

backend/collab-service/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
"@types/express": "^5.0.0",
3434
"@types/jest": "^29.5.14",
3535
"@types/node": "^22.7.9",
36+
"@types/socket.io": "^3.0.2",
37+
"@types/socket.io-client": "^1.4.36",
3638
"@types/swagger-ui-express": "^4.1.6",
3739
"cross-env": "^7.0.3",
3840
"eslint": "^9.13.0",
3941
"globals": "^15.11.0",
4042
"jest": "^29.7.0",
43+
"socket.io-client": "^4.8.1",
4144
"ts-jest": "^29.2.5",
4245
"ts-node": "^10.9.2",
4346
"tsx": "^4.19.1",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import redisClient from "../config/redis";
44
import { Doc, applyUpdateV2, encodeStateAsUpdateV2 } from "yjs";
55
import { createQuestionHistory } from "../api/questionHistoryService";
66

7-
enum CollabEvents {
7+
export enum CollabEvents {
88
// Receive
99
JOIN = "join",
1010
LEAVE = "leave",
Lines changed: 128 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,130 @@
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 { CollabEvents } from "../src/handlers/websocketHandler";
6+
7+
describe("Collab service web socket", () => {
8+
let io: Server, serverSocket: Socket, clientSocket: SocketIOClient.Socket;
9+
10+
beforeAll((done) => {
11+
const httpServer = createServer();
12+
io = new Server(httpServer);
13+
httpServer.listen(() => {
14+
const port = (httpServer.address() as AddressInfo).port;
15+
clientSocket = ioc(`http://localhost:${port}`);
16+
io.on("connection", (socket) => {
17+
serverSocket = socket;
18+
});
19+
clientSocket.on("connect", done);
20+
});
21+
});
22+
23+
afterAll(() => {
24+
io.close();
25+
clientSocket.close();
26+
});
27+
28+
it("Room ready", (done) => {
29+
const isRoomReady = true;
30+
clientSocket.on(
31+
CollabEvents.ROOM_READY,
32+
({ ready }: { ready: boolean }) => {
33+
expect(ready).toEqual(isRoomReady);
34+
done();
35+
}
36+
);
37+
serverSocket.emit(CollabEvents.ROOM_READY, {
38+
ready: isRoomReady,
39+
});
40+
});
41+
42+
it("Document ready", (done) => {
43+
const questionHistoryIdSent = "123";
44+
clientSocket.on(
45+
CollabEvents.DOCUMENT_READY,
46+
({ questionHistoryId }: { questionHistoryId: string }) => {
47+
expect(questionHistoryId).toEqual(questionHistoryIdSent);
48+
done();
49+
}
50+
);
51+
serverSocket.emit(CollabEvents.DOCUMENT_READY, {
52+
questionHistoryId: questionHistoryIdSent,
53+
});
54+
});
55+
56+
it("Document not found", (done) => {
57+
clientSocket.on(CollabEvents.DOCUMENT_NOT_FOUND, () => {
58+
done();
59+
});
60+
serverSocket.emit(CollabEvents.DOCUMENT_NOT_FOUND);
61+
});
62+
63+
it("Document update", (done) => {
64+
const updateSent = new Uint8Array([1, 2, 3]);
65+
clientSocket.on(
66+
CollabEvents.UPDATE,
67+
({ update }: { update: Uint8Array }) => {
68+
expect(Array.from(update)).toEqual(Array.from(updateSent));
69+
done();
70+
}
71+
);
72+
serverSocket.emit(CollabEvents.UPDATE, {
73+
update: updateSent,
74+
});
75+
});
76+
77+
it("Cursor update", (done) => {
78+
const uidSent = "123";
79+
const usernameSent = "user";
80+
const fromSent = 1;
81+
const toSent = 5;
82+
clientSocket.on(
83+
CollabEvents.UPDATE_CURSOR,
84+
({
85+
uid,
86+
username,
87+
from,
88+
to,
89+
}: {
90+
uid: string;
91+
username: string;
92+
from: number;
93+
to: number;
94+
}) => {
95+
expect(uid).toEqual(uidSent);
96+
expect(username).toEqual(usernameSent);
97+
expect(from).toEqual(fromSent);
98+
expect(to).toEqual(toSent);
99+
done();
100+
}
101+
);
102+
serverSocket.emit(CollabEvents.UPDATE_CURSOR, {
103+
uid: uidSent,
104+
username: usernameSent,
105+
from: fromSent,
106+
to: toSent,
107+
});
108+
});
109+
110+
it("End session", (done) => {
111+
const sessionDurationSent = 30;
112+
clientSocket.on(
113+
CollabEvents.END_SESSION,
114+
({ sessionDuration }: { sessionDuration: number }) => {
115+
expect(sessionDuration).toEqual(sessionDurationSent);
116+
done();
117+
}
118+
);
119+
serverSocket.emit(CollabEvents.END_SESSION, {
120+
sessionDuration: sessionDurationSent,
121+
});
122+
});
123+
124+
it("Partner disconnected", (done) => {
125+
clientSocket.on(CollabEvents.PARTNER_DISCONNECTED, () => {
126+
done();
127+
});
128+
serverSocket.emit(CollabEvents.PARTNER_DISCONNECTED);
4129
});
5130
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const handleWebsocketCommunicationEvents = (socket: Socket) => {
99
CommunicationEvents.JOIN,
1010
async ({ roomId, username }: { roomId: string; username: string }) => {
1111
connectUser(username);
12-
console.log(username, roomId);
1312
const room = io.sockets.adapter.rooms.get(roomId);
1413
if (room?.has(socket.id)) {
1514
socket.emit(CommunicationEvents.ALREADY_JOINED);

0 commit comments

Comments
 (0)