SocketTester.ts
1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { expect } from "chai";
import {
RawMessage,
ServerInboundMessage,
ServerInboundMessageKey,
ServerResponse,
} from "../../../common";
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { DummySocket } from "./DummySocket";
export class SocketTester {
public readonly connection: Connection;
public readonly socket: DummySocket;
constructor(roomManager: RoomManager) {
this.socket = new DummySocket();
this.connection = new Connection(this.socket, roomManager);
}
public test<T extends ServerInboundMessageKey>(
type: T,
message: ServerInboundMessage<T>
): ServerResponse<any> {
return this.connection.handleRaw({
type: type as string,
message: message,
});
}
public testOk<T extends ServerInboundMessageKey>(
type: T,
message: ServerInboundMessage<T>
): void {
expect(this.test(type, message).ok).eq(true);
}
public testNotOk<T extends ServerInboundMessageKey>(
type: T,
message: ServerInboundMessage<T>
): void {
expect(this.test(type, message).ok).eq(false);
}
public testRaw(raw: RawMessage): ServerResponse<any> {
return this.connection.handleRaw(raw);
}
public testAny(obj: any): ServerResponse<any> {
return this.connection.handleRaw(obj);
}
public login(username: string): void {
this.testOk("login", { username });
expect(this.connection.user !== undefined).eq(true);
}
}