Skip to content

Commit

Permalink
Merge pull request #124 from ruiqi7/qa/test-cases
Browse files Browse the repository at this point in the history
Add tests for websocket events
  • Loading branch information
nicolelim02 authored Nov 13, 2024
2 parents 3fb91e0 + 74b8cd5 commit 614dde5
Show file tree
Hide file tree
Showing 27 changed files with 477 additions and 155 deletions.
102 changes: 102 additions & 0 deletions backend/collab-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/collab-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
"@types/express": "^5.0.0",
"@types/jest": "^29.5.14",
"@types/node": "^22.7.9",
"@types/socket.io": "^3.0.2",
"@types/socket.io-client": "^1.4.36",
"@types/swagger-ui-express": "^4.1.6",
"cross-env": "^7.0.3",
"eslint": "^9.13.0",
"globals": "^15.11.0",
"jest": "^29.7.0",
"socket.io-client": "^4.8.1",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"tsx": "^4.19.1",
Expand Down
2 changes: 1 addition & 1 deletion backend/collab-service/src/handlers/websocketHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import redisClient from "../config/redis";
import { Doc, applyUpdateV2, encodeStateAsUpdateV2 } from "yjs";
import { createQuestionHistory } from "../api/questionHistoryService";

enum CollabEvents {
export enum CollabEvents {
// Receive
JOIN = "join",
LEAVE = "leave",
Expand Down
131 changes: 128 additions & 3 deletions backend/collab-service/tests/webSocketHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,130 @@
describe("Test web socket", () => {
it("Test", () => {
expect(true);
import { createServer } from "node:http";
import { type AddressInfo } from "node:net";
import ioc from "socket.io-client";
import { Server, Socket } from "socket.io";
import { CollabEvents } from "../src/handlers/websocketHandler";

describe("Collab service web socket", () => {
let io: Server, serverSocket: Socket, clientSocket: SocketIOClient.Socket;

beforeAll((done) => {
const httpServer = createServer();
io = new Server(httpServer);
httpServer.listen(() => {
const port = (httpServer.address() as AddressInfo).port;
clientSocket = ioc(`http://localhost:${port}`);
io.on("connection", (socket) => {
serverSocket = socket;
});
clientSocket.on("connect", done);
});
});

afterAll(() => {
io.close();
clientSocket.close();
});

it("Room ready", (done) => {
const isRoomReady = true;
clientSocket.on(
CollabEvents.ROOM_READY,
({ ready }: { ready: boolean }) => {
expect(ready).toEqual(isRoomReady);
done();
}
);
serverSocket.emit(CollabEvents.ROOM_READY, {
ready: isRoomReady,
});
});

it("Document ready", (done) => {
const questionHistoryIdSent = "123";
clientSocket.on(
CollabEvents.DOCUMENT_READY,
({ questionHistoryId }: { questionHistoryId: string }) => {
expect(questionHistoryId).toEqual(questionHistoryIdSent);
done();
}
);
serverSocket.emit(CollabEvents.DOCUMENT_READY, {
questionHistoryId: questionHistoryIdSent,
});
});

it("Document not found", (done) => {
clientSocket.on(CollabEvents.DOCUMENT_NOT_FOUND, () => {
done();
});
serverSocket.emit(CollabEvents.DOCUMENT_NOT_FOUND);
});

it("Document update", (done) => {
const updateSent = new Uint8Array([1, 2, 3]);
clientSocket.on(
CollabEvents.UPDATE,
({ update }: { update: Uint8Array }) => {
expect(Array.from(update)).toEqual(Array.from(updateSent));
done();
}
);
serverSocket.emit(CollabEvents.UPDATE, {
update: updateSent,
});
});

it("Cursor update", (done) => {
const uidSent = "123";
const usernameSent = "user";
const fromSent = 1;
const toSent = 5;
clientSocket.on(
CollabEvents.UPDATE_CURSOR,
({
uid,
username,
from,
to,
}: {
uid: string;
username: string;
from: number;
to: number;
}) => {
expect(uid).toEqual(uidSent);
expect(username).toEqual(usernameSent);
expect(from).toEqual(fromSent);
expect(to).toEqual(toSent);
done();
}
);
serverSocket.emit(CollabEvents.UPDATE_CURSOR, {
uid: uidSent,
username: usernameSent,
from: fromSent,
to: toSent,
});
});

it("End session", (done) => {
const sessionDurationSent = 30;
clientSocket.on(
CollabEvents.END_SESSION,
({ sessionDuration }: { sessionDuration: number }) => {
expect(sessionDuration).toEqual(sessionDurationSent);
done();
}
);
serverSocket.emit(CollabEvents.END_SESSION, {
sessionDuration: sessionDurationSent,
});
});

it("Partner disconnected", (done) => {
clientSocket.on(CollabEvents.PARTNER_DISCONNECTED, () => {
done();
});
serverSocket.emit(CollabEvents.PARTNER_DISCONNECTED);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const handleWebsocketCommunicationEvents = (socket: Socket) => {
CommunicationEvents.JOIN,
async ({ roomId, username }: { roomId: string; username: string }) => {
connectUser(username);
console.log(username, roomId);
const room = io.sockets.adapter.rooms.get(roomId);
if (room?.has(socket.id)) {
socket.emit(CommunicationEvents.ALREADY_JOINED);
Expand Down
Loading

0 comments on commit 614dde5

Please sign in to comment.