pokemon-showdown/test/application/rooms.js
Ben Davies 5031794f97 Sockets: fix unit tests (#3281)
* Sockets: fix Sockets.killWorker not disconnecting connections

* Sockets: fix unit tests

- Fix crash when constructing mock sockets in certain cases
- Properly prevent workers from writing to stdout
- Fix race conditions in workers-related tests that were causing false
  positives

* Tests: mock workers now more closely imitate sockets' workers

This helps catch cases where messages are being sent in the wrong order
to the workers, e.g. messages sent to sockets that no longer exist.
2017-02-24 05:38:37 -06:00

95 lines
2.8 KiB
JavaScript

'use strict';
const assert = require('assert');
let userUtils = require('./../../dev-tools/users-utils');
let User = userUtils.User;
describe('Rooms features', function () {
describe('Rooms', function () {
describe('Rooms.get', function () {
it('should be a function', function () {
assert.strictEqual(typeof Rooms.get, 'function');
});
it('should be equal to `Rooms`', function () {
assert.strictEqual(Rooms.get, Rooms);
});
});
describe('Rooms.rooms', function () {
it('should be a Map', function () {
assert.ok(Rooms.rooms instanceof Map);
});
});
});
describe('BattleRoom', function () {
const packedTeam = 'Weavile||lifeorb||swordsdance,knockoff,iceshard,iciclecrash|Jolly|,252,,,4,252|||||';
let room;
afterEach(function () {
Users.users.forEach(user => {
room.onLeave(user);
user.disconnectAll();
user.destroy();
});
if (room) room.destroy();
});
it('should allow two users to join the battle', function () {
let p1 = new User();
let p2 = new User();
let options = [{rated: false, tour: false}, {rated: false, tour: {onBattleWin() {}}}, {rated: true, tour: false}, {rated: true, tour: {onBattleWin() {}}}];
for (let option of options) {
room = Rooms.global.startBattle(p1, p2, 'customgame', packedTeam, packedTeam, option);
assert.ok(room.battle.p1 && room.battle.p2); // Automatically joined
}
});
it('should copy auth from tournament', function () {
const p1 = new User();
const p2 = new User();
const options = {
rated: false,
auth: {},
tour: {
onBattleWin() {},
room: {getAuth() {
return '%';
}},
},
};
room = Rooms.global.startBattle(p1, p2, 'customgame', packedTeam, packedTeam, options);
assert.strictEqual(room.getAuth(new User()), '%');
});
it('should prevent overriding tournament room auth by a tournament player', function () {
const p1 = new User();
const p2 = new User();
const roomStaff = new User();
roomStaff.forceRename("Room auth", true);
const administrator = new User();
administrator.forceRename("Admin", true);
administrator.group = '~';
const options = {
rated: false,
auth: {},
tour: {
onBattleWin() {},
room: {getAuth(user) {
return '%';
}},
},
};
room = Rooms.global.startBattle(p1, p2, 'customgame', packedTeam, packedTeam, options);
roomStaff.joinRoom(room);
administrator.joinRoom(room);
assert.strictEqual(room.getAuth(roomStaff), '%', 'before promotion attempt');
Chat.parse("/roomvoice Room auth", room, p1, p1.connections[0]);
assert.strictEqual(room.getAuth(roomStaff), '%', 'after promotion attempt');
Chat.parse("/roomvoice Room auth", room, administrator, administrator.connections[0]);
assert.strictEqual(room.getAuth(roomStaff), '+', 'after being promoted by an administrator');
});
});
});