-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from ruiqi7/qa/test-cases
Add tests for websocket events
- Loading branch information
Showing
27 changed files
with
477 additions
and
155 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.