Files
factorio-clusterio/test/mock.js
T
Hornwitser 677512512e Replace socket.io with plain WebSocket
Replace socket.io with plain WebSocket using the ws library as it
doesn't really provide any useful tools over plain WebSocket when it
comes to implementing reliable communication that works across
reconnects.  The widespread browser support for WebSocket also means
it has little value to add for the web interface over WebSocket.
2020-03-22 02:10:54 +01:00

88 lines
1.4 KiB
JavaScript

const events = require("events");
const link = require("lib/link");
class MockSocket {
constructor() {
this.sentMessages = [];
this.events = new Map();
}
send(message) {
this.sentMessages.push(message);
}
on(event, fn) {
this.events.set(event, fn);
}
terminate() {
this.terminateCalled = true;
}
close() {
this.closeCalled = true;
}
}
class MockConnector extends events.EventEmitter {
constructor() {
super();
this._seq = 1;
this.sentMessages = [];
this.events = new Map();
this.handshake = { address: "socket.test" };
}
send(type, data) {
let message = { seq: this._seq, type, data };
this.sentMessages.push(message);
setImmediate(() => this.emit('send', message));
return this._seq++;
}
close(reason) {
this.send('close', { reason });
this.disconnect();
}
disconnect() {
this.disconnectCalled = true;
}
}
class MockServer {
constructor() {
this.rconCommands = [];
this.rconCommandResults = new Map();
}
async sendRcon(command) {
this.rconCommands.push(command);
return this.rconCommandResults.get(command) || '';
}
}
class MockInstance extends link.Link {
constructor() {
super('instance', 'slave', new MockConnector());
this.server = new MockServer();
this.name = "test";
this.config = {
get: (name) => {
if (name === "instance.id") { return 7357 }
throw Error("Not implemented");
}
}
}
}
module.exports = {
MockSocket,
MockConnector,
MockInstance,
};