pokemon-showdown/test/server/rooms.js
Guangcong Luo f6f4467265 Turn on prefer-const for .js files
Not having prefer-const on the JS side makes JS -> TS refactors really
unreadable. This commit just auto-fixes it so we're using
`prefer-const` everywhere.
2020-04-23 11:37:47 -07:00

177 lines
5.5 KiB
JavaScript

'use strict';
const assert = require('assert').strict;
const {User} = require('../users-utils');
describe('Rooms features', function () {
describe('Rooms', function () {
describe('Rooms.get', function () {
it('should be a function', function () {
assert.equal(typeof Rooms.get, 'function');
});
});
describe('Rooms.rooms', function () {
it('should be a Map', function () {
assert.ok(Rooms.rooms instanceof Map);
});
});
});
describe('BasicRoom', function () {
describe('getGame', function () {
it('should return the game only when the gameids match', function () {
const Hangman = require('../../.server-dist/chat-plugins/hangman').Hangman;
const Uno = require('../../.server-dist/chat-plugins/uno').UnoGame;
const room = Rooms.createChatRoom('r/relationshipadvice');
const game = new Hangman(room, new User(), 'There\'s a lot of red flags here');
room.game = game;
assert.equal(room.getGame(Hangman), game);
assert.equal(room.getGame(Uno), null);
});
});
});
describe('GameRoom', function () {
const packedTeam = 'Weavile||lifeorb||swordsdance,knockoff,iceshard,iciclecrash|Jolly|,252,,,4,252|||||';
let room;
let parent;
afterEach(function () {
for (const user of Users.users.values()) {
user.disconnectAll();
user.destroy();
}
if (room) room.destroy();
if (parent) parent.destroy();
});
it('should allow two users to join the battle', function () {
const p1 = new User();
const p2 = new User();
const options = [{rated: false, tour: false}, {rated: false, tour: {onBattleWin() {}}}, {rated: true, tour: false}, {rated: true, tour: {onBattleWin() {}}}];
for (const option of options) {
room = Rooms.createBattle('customgame', Object.assign({
p1,
p2,
p1team: packedTeam,
p2team: packedTeam,
}, option));
assert.ok(room.battle.p1 && room.battle.p2); // Automatically joined
}
});
it('should copy auth from tournament', function () {
parent = Rooms.createChatRoom('parentroom', '', {});
parent.getAuth = () => '%';
const p1 = new User();
const p2 = new User();
const options = {
p1,
p2,
p1team: packedTeam,
p2team: packedTeam,
rated: false,
auth: {},
tour: {
onBattleWin() {},
room: parent,
},
};
room = Rooms.createBattle('customgame', options);
assert.equal(room.getAuth(new User()), '%');
});
it('should prevent overriding tournament room auth by a tournament player', function () {
parent = Rooms.createChatRoom('parentroom2', '', {});
parent.getAuth = () => '%';
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 = {
p1,
p2,
p1team: packedTeam,
p2team: packedTeam,
rated: false,
auth: {},
tour: {
onBattleWin() {},
room: parent,
},
};
room = Rooms.createBattle('customgame', options);
roomStaff.joinRoom(room);
administrator.joinRoom(room);
assert.equal(room.getAuth(roomStaff), '%', 'before promotion attempt');
Chat.parse("/roomvoice Room auth", room, p1, p1.connections[0]);
assert.equal(room.getAuth(roomStaff), '%', 'after promotion attempt');
Chat.parse("/roomvoice Room auth", room, administrator, administrator.connections[0]);
assert.equal(room.getAuth(roomStaff), '+', 'after being promoted by an administrator');
});
});
describe("ChatRoom", function () {
describe("#rename", function () {
let room;
let parent;
let subroom;
afterEach(function () {
for (const user of Users.users.values()) {
user.disconnectAll();
user.destroy();
}
const rooms = [room, parent, subroom];
for (const room of rooms) {
if (room) {
room.destroy();
}
}
});
it("should rename its roomid and title", async function () {
room = Rooms.createChatRoom("test", "Test");
await room.rename("Test2");
assert.strictEqual(room.roomid, "test2");
assert.strictEqual(room.title, "Test2");
});
it("should rename its key in Rooms.rooms", async function () {
room = Rooms.createChatRoom("test", "Test");
await room.rename("Test2");
assert.strictEqual(Rooms.rooms.has("test"), false);
assert.strictEqual(Rooms.rooms.has("test2"), true);
});
it("should move the users and their connections", async function () {
room = Rooms.createChatRoom("test", "Test");
const user = new User();
user.joinRoom(room);
await room.rename("Test2");
assert.strictEqual(user.inRooms.has("test"), false);
assert.strictEqual(user.inRooms.has("test2"), true);
assert.strictEqual(user.connections[0].inRooms.has("test"), false);
assert.strictEqual(user.connections[0].inRooms.has("test2"), true);
});
it("should rename their parents subroom reference", async function () {
parent = Rooms.createChatRoom("parent", "Parent");
subroom = Rooms.createChatRoom("subroom", "Subroom", {parentid: "parent"});
await subroom.rename("TheSubroom");
assert.strictEqual(parent.subRooms.has("subroom"), false);
assert.strictEqual(parent.subRooms.has("thesubroom"), true);
});
it("should rename their subrooms parent reference", async function () {
parent = Rooms.createChatRoom("parent", "Parent");
subroom = Rooms.createChatRoom("subroom", "Subroom", {parentid: "parent"});
await parent.rename("TheParent");
assert.strictEqual(subroom.parent, parent);
});
});
});
});