-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
2,769 additions
and
1,417 deletions.
There are no files selected for viewing
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 was deleted.
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
117 changes: 117 additions & 0 deletions
117
__test__/messageHandler/handlers/transmission/index.spec.ts
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
|
||
import { Client } from "../../../../src/models/client"; | ||
import { TransmissionHandler } from "../../../../src/messageHandler/handlers"; | ||
import { Realm } from "../../../../src/models/realm"; | ||
import { MessageType } from "../../../../src/enums"; | ||
import type WebSocket from "ws"; | ||
|
||
const createFakeSocket = (): WebSocket => { | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
const sock = { | ||
send: (): void => {}, | ||
close: (): void => {}, | ||
on: (): void => {}, | ||
}; | ||
/* eslint-enable @typescript-eslint/no-empty-function */ | ||
|
||
return sock as unknown as WebSocket; | ||
}; | ||
|
||
describe("Transmission handler", () => { | ||
it("should save message in queue when destination client not connected", () => { | ||
const realm = new Realm(); | ||
const handleTransmission = TransmissionHandler({ realm }); | ||
|
||
const clientFrom = new Client({ id: "id1", token: "" }); | ||
const idTo = "id2"; | ||
realm.setClient(clientFrom, clientFrom.getId()); | ||
|
||
handleTransmission(clientFrom, { | ||
type: MessageType.OFFER, | ||
src: clientFrom.getId(), | ||
dst: idTo, | ||
}); | ||
|
||
expect(realm.getMessageQueueById(idTo)?.getMessages().length).toBe(1); | ||
}); | ||
|
||
it("should not save LEAVE and EXPIRE messages in queue when destination client not connected", () => { | ||
const realm = new Realm(); | ||
const handleTransmission = TransmissionHandler({ realm }); | ||
|
||
const clientFrom = new Client({ id: "id1", token: "" }); | ||
const idTo = "id2"; | ||
realm.setClient(clientFrom, clientFrom.getId()); | ||
|
||
handleTransmission(clientFrom, { | ||
type: MessageType.LEAVE, | ||
src: clientFrom.getId(), | ||
dst: idTo, | ||
}); | ||
handleTransmission(clientFrom, { | ||
type: MessageType.EXPIRE, | ||
src: clientFrom.getId(), | ||
dst: idTo, | ||
}); | ||
|
||
expect(realm.getMessageQueueById(idTo)).toBeUndefined(); | ||
}); | ||
|
||
it("should send message to destination client when destination client connected", () => { | ||
const realm = new Realm(); | ||
const handleTransmission = TransmissionHandler({ realm }); | ||
|
||
const clientFrom = new Client({ id: "id1", token: "" }); | ||
const clientTo = new Client({ id: "id2", token: "" }); | ||
const socketTo = createFakeSocket(); | ||
clientTo.setSocket(socketTo); | ||
realm.setClient(clientTo, clientTo.getId()); | ||
|
||
let sent = false; | ||
socketTo.send = (): void => { | ||
sent = true; | ||
}; | ||
|
||
handleTransmission(clientFrom, { | ||
type: MessageType.OFFER, | ||
src: clientFrom.getId(), | ||
dst: clientTo.getId(), | ||
}); | ||
|
||
expect(sent).toBe(true); | ||
}); | ||
|
||
it("should send LEAVE message to source client when sending to destination client failed", () => { | ||
const realm = new Realm(); | ||
const handleTransmission = TransmissionHandler({ realm }); | ||
|
||
const clientFrom = new Client({ id: "id1", token: "" }); | ||
const clientTo = new Client({ id: "id2", token: "" }); | ||
const socketFrom = createFakeSocket(); | ||
const socketTo = createFakeSocket(); | ||
clientFrom.setSocket(socketFrom); | ||
clientTo.setSocket(socketTo); | ||
realm.setClient(clientFrom, clientFrom.getId()); | ||
realm.setClient(clientTo, clientTo.getId()); | ||
|
||
let sent = false; | ||
socketFrom.send = (data: string): void => { | ||
if (JSON.parse(data)?.type === MessageType.LEAVE) { | ||
sent = true; | ||
} | ||
}; | ||
|
||
socketTo.send = (): void => { | ||
throw Error(); | ||
}; | ||
|
||
handleTransmission(clientFrom, { | ||
type: MessageType.OFFER, | ||
src: clientFrom.getId(), | ||
dst: clientTo.getId(), | ||
}); | ||
|
||
expect(sent).toBe(true); | ||
}); | ||
}); |
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
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
Oops, something went wrong.