DummySocket.ts
1.22 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
import { expect } from "chai";
import {
RawMessage,
ServerOutboundMessage,
ServerOutboundMessageKey,
ServerResponse,
} from "../../../common";
import { SocketWrapper } from "../../connection/SocketWrapper";
export class DummySocket implements SocketWrapper {
public handler?: (raw: RawMessage) => ServerResponse<any>;
public receivedMessages: RawMessage[] = [];
public setHandler(handler: (raw: RawMessage) => ServerResponse<any>) {
this.handler = handler;
}
public send(raw: RawMessage): void {
this.receivedMessages.push(raw);
}
private findMessageIndex<T extends ServerOutboundMessageKey>(key: T): number {
for (let i = 0; i < this.receivedMessages.length; i++) {
if (this.receivedMessages[i].type == key) {
return i;
}
}
return -1;
}
public received<T extends ServerOutboundMessageKey>(
key: T
): ServerOutboundMessage<T> {
const index = this.findMessageIndex(key);
expect(index !== -1).eq(true);
const message = this.receivedMessages[index];
this.receivedMessages.splice(index, 1);
return message.message;
}
public notReceived<T extends ServerOutboundMessageKey>(key: T): void {
expect(this.findMessageIndex(key) === -1).eq(true);
}
}