pokemon-showdown/test/server/sockets.js
Guangcong Luo a65faf263f
Stop using assert.strict.strictEqual (#7515)
It turns out that when I switched us from `assert` to `assert.strict`,
I didn't actually update any existing tests or tell anyone:

0df0d234f2

So apparently everyone else just kept on using `strictEqual`.

This will be a PR and also throw an error if people continue trying to
use it, which should make it much clearer what PS policy is on this.

A lot of the problem may be that TypeScript marks assert.strict.equal
as deprecated when it's not. This was fixed 4 days ago:

https://github.com/DefinitelyTyped/DefinitelyTyped/pull/48452

But this probably hasn't made it to a thing yet. Until then, you'll
have to deal with TS marking your tests as deprecated, but it shouldn't
be too long.

Accidentally using `assert` instead of `assert.strict` should now show
an error. This protects against the probably much worse mistake of
accidentally using `assert.equal` rather than `assert.strict.equal`.

`assert.ok` is also deprecated now.
2020-10-14 01:19:03 -07:00

220 lines
6.4 KiB
JavaScript

'use strict';
// These tests have not been updated to reflect the latest version of Sockets
// Anyone should feel free to try to rewrite them to work
/*
const assert = require('assert').strict;
const cluster = require('cluster');
describe('Sockets', function () {
const spawnWorker = () => (
new Promise(resolve => {
const worker = Sockets.spawnWorker();
worker.removeAllListeners('message');
resolve(worker);
})
);
before(function () {
cluster.settings.silent = true;
cluster.removeAllListeners('disconnect');
});
afterEach(function () {
for (const [workerid, worker] of Sockets.workers) {
worker.kill();
Sockets.workers.delete(workerid);
}
});
describe('master', function () {
it('should be able to spawn workers', function () {
Sockets.spawnWorker();
assert.equal(Sockets.workers.size, 1);
});
it('should be able to spawn workers on listen', function () {
Sockets.listen(0, '127.0.0.1', 1);
assert.equal(Sockets.workers.size, 1);
});
it('should be able to kill workers', function () {
return spawnWorker().then(worker => {
Sockets.killWorker(worker);
assert.equal(Sockets.workers.size, 0);
});
});
it('should be able to kill workers by PID', function () {
return spawnWorker().then(worker => {
Sockets.killPid(worker.process.pid);
assert.equal(Sockets.workers.size, 0);
});
});
});
describe('workers', function () {
// This composes a sequence of HOFs that send a message to a worker,
// wait for its response, then return the worker for the next function
// to use.
const chain = (eventHandler, msg) => worker => {
worker.once('message', eventHandler(worker));
msg = msg || `$
const {Session} = require('sockjs/lib/transport');
const socket = new Session('aaaaaaaa', server);
socket.remoteAddress = '127.0.0.1';
if (!('headers' in socket)) socket.headers = {};
socket.headers['x-forwarded-for'] = '';
socket.protocol = 'websocket';
socket.write = msg => process.send(msg);
server.emit('connection', socket);`;
worker.send(msg);
return worker;
};
const spawnSocket = eventHandler => spawnWorker().then(chain(eventHandler));
it('should allow sockets to connect', function () {
return spawnSocket(worker => data => {
const cmd = data.charAt(0);
const [sid, ip, protocol] = data.substr(1).split('\n');
assert.equal(cmd, '*');
assert.equal(sid, '1');
assert.equal(ip, '127.0.0.1');
assert.equal(protocol, 'websocket');
});
});
it('should allow sockets to disconnect', function () {
let querySocket;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
querySocket = `$
let socket = sockets.get(${sid});
process.send(!socket);`;
Sockets.socketDisconnect(worker, sid);
}).then(chain(worker => data => {
assert(data);
}, querySocket));
});
it('should allow sockets to send messages', function () {
const msg = 'ayy lmao';
let socketSend;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
socketSend = `>${sid}\n${msg}`;
}).then(chain(worker => data => {
assert.equal(data, msg);
}, socketSend));
});
it('should allow sockets to receive messages', function () {
let sid;
let msg;
let mockReceive;
return spawnSocket(worker => data => {
sid = data.substr(1, data.indexOf('\n'));
msg = '|/cmd rooms';
mockReceive = `$
let socket = sockets.get(${sid});
socket.emit('data', ${msg});`;
}).then(chain(worker => data => {
const cmd = data.charAt(0);
const params = data.substr(1).split('\n');
assert.equal(cmd, '<');
assert.equal(sid, params[0]);
assert.equal(msg, params[1]);
}, mockReceive));
});
it('should create a room for the first socket to get added to it', function () {
let queryChannel;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
const cid = 'global';
queryChannel = `$
let room = rooms.get(${cid});
process.send(room && room.has(${sid}));`;
Sockets.roomAdd(worker, cid, sid);
}).then(chain(worker => data => {
assert(data);
}, queryChannel));
});
it('should remove a room if the last socket gets removed from it', function () {
let queryChannel;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
const cid = 'global';
queryChannel = `$
process.send(!sockets.has(${sid}) && !rooms.has(${cid}));`;
Sockets.roomAdd(worker, cid, sid);
Sockets.roomRemove(worker, cid, sid);
}).then(chain(worker => data => {
assert(data);
}, queryChannel));
});
it('should send to all sockets in a room', function () {
const msg = 'ayy lmao';
const cid = 'global';
const roomSend = `#${cid}\n${msg}`;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
Sockets.roomAdd(worker, cid, sid);
}).then(chain(worker => data => {
assert.equal(data, msg);
}, roomSend));
});
it('should create a channel when moving a socket to it', function () {
let queryChannel;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
const cid = 'battle-ou-1';
const scid = '1';
queryChannel = `$
let channel = roomChannels[${cid}];
process.send(!!channel && (channel.get(${sid}) === ${scid}));`;
Sockets.channelMove(worker, cid, scid, sid);
}).then(chain(worker => data => {
assert(data);
}, queryChannel));
});
it('should remove a channel when removing its last socket', function () {
let queryChannel;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
const cid = 'battle-ou-1';
const scid = '1';
queryChannel = `$
let channel = roomChannels.get(${cid});
process.send(!!channel && (channel.get(${sid}) === ${scid}));`;
Sockets.channelMove(worker, cid, scid, sid);
Sockets.roomRemove(worker, cid, sid);
}).then(chain(worker => data => {
assert(data);
}, queryChannel));
});
it('should send to sockets in a channel', function () {
const cid = 'battle-ou-1';
const msg = 'ayy lmao';
const buf = `.${cid}\n\n|split\n\n${msg}\n\n`;
return spawnSocket(worker => data => {
const sid = data.substr(1, data.indexOf('\n'));
const scid = '1';
Sockets.channelMove(worker, cid, scid, sid);
}).then(chain(worker => data => {
assert.equal(data, msg);
}, buf));
});
});
});
*/