Skip to content

Commit 30ba68c

Browse files
committed
Fix tests
1 parent 4115af3 commit 30ba68c

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

server/roomlogs.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ type RedisDriver = import('ioredis').Redis;
9090

9191
export class RedisScrollback implements Scrollback {
9292
room: BasicRoom;
93-
static driver = require('ioredis').createClient(
94-
Config.redis || Config.redislogs
95-
) as RedisDriver;
93+
static driver = RedisScrollback.getDriver()!;
9694
gettingLog: Promise<string[]> | null = null;
9795
logsWhileGetting: string[] | null = null;
9896
constructor(room: BasicRoom) {
9997
this.room = room;
10098
}
99+
static getDriver() {
100+
const config = Config.redis || Config.redislogs;
101+
if (!config) return;
102+
return require('ioredis').createClient(config) as RedisDriver;
103+
}
101104
async add(message: string) {
102105
await RedisScrollback.driver.lpush(`scrollback:${this.room.roomid}`, message);
103106
}

test/server/chat-plugins/chat-monitor.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ describe('Chat monitor', () => {
5050
});
5151

5252
describe('in-room tests', function () {
53-
before(() => {
53+
before(async () => {
5454
this.room = Rooms.get('lobby');
5555
this.user = makeUser("Unit Tester");
5656
this.connection = this.user.connections[0];
57-
this.user.joinRoom(this.room.roomid, this.connection);
57+
await this.user.joinRoom(this.room.roomid, this.connection);
5858

5959
Chat.loadPlugins();
6060
this.parse = async function (message) {
@@ -80,7 +80,8 @@ describe('Chat monitor', () => {
8080
await this.parse("haha autolock me pls");
8181

8282
assert(this.user.locked);
83-
assert.notEqual(this.room.log.log.pop(), "haha autolock me pls");
83+
const log = [...await this.room.log.get()];
84+
assert.notEqual(log.pop(), "haha autolock me pls");
8485
});
8586

8687
it('should lock users who evade evasion phrases', async () => {
@@ -92,7 +93,8 @@ describe('Chat monitor', () => {
9293

9394
await this.parse("sl ur");
9495
assert(this.user.locked);
95-
assert.notEqual(this.room.log.log.pop(), "sl ur");
96+
const log = [...await this.room.log.get()];
97+
assert.notEqual(log.pop(), "sl ur");
9698
});
9799

98100
it('should replace words filtered to other words', async () => {
@@ -104,8 +106,9 @@ describe('Chat monitor', () => {
104106
});
105107

106108
await this.parse("Hello! replace me pls! thanks, and remember to replace me.");
109+
const log = [...await this.room.log.get()];
107110
assert.equal(
108-
this.room.log.log.pop().replace(/^\|c:\|[0-9]+\| Unit Tester\|/, ''),
111+
log.pop().replace(/^\|c:\|[0-9]+\| Unit Tester\|/, ''),
109112
"Hello! i got replaced pls! thanks, and remember to i got replaced."
110113
);
111114
});
@@ -118,7 +121,9 @@ describe('Chat monitor', () => {
118121
});
119122

120123
await this.parse("mild slur");
121-
assert.notEqual(this.room.log.log.pop(), "mild slur");
124+
// note: None of the log tests work. these check for things it can literally never be.
125+
const log = [...await this.room.log.get()];
126+
assert.notEqual(log.pop(), "mild slur");
122127
});
123128

124129
it('should prevent banwords and evasion banwords from being used in usernames', () => {

test/server/chat-plugins/trivia.js

+29-31
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const FirstModeTrivia = trivia.FirstModeTrivia;
99
const TimerModeTrivia = trivia.TimerModeTrivia;
1010
const NumberModeTrivia = trivia.NumberModeTrivia;
1111

12-
function makeTriviaUser(name, ip) {
12+
async function makeTriviaUser(name, ip) {
1313
const user = makeUser(name, ip);
1414
assert.equal(Users.users.get(user.id), user);
15-
user.joinRoom('trivia');
15+
await user.joinRoom('trivia');
1616
return user;
1717
}
1818

@@ -29,10 +29,10 @@ describe('Trivia', function () {
2929
this.room = Rooms.get('trivia');
3030
});
3131

32-
beforeEach(function () {
32+
beforeEach(async function () {
3333
const questions = [{question: '', answers: ['answer'], category: 'ae'}];
34-
this.user = makeTriviaUser('Morfent', '127.0.0.1');
35-
this.tarUser = makeTriviaUser('ReallyNotMorfent', '127.0.0.2');
34+
this.user = await makeTriviaUser('Morfent', '127.0.0.1');
35+
this.tarUser = await makeTriviaUser('ReallyNotMorfent', '127.0.0.2');
3636
this.game = this.room.game = new Trivia(this.room, 'first', ['ae'], true, 'short', questions);
3737
});
3838

@@ -64,24 +64,24 @@ describe('Trivia', function () {
6464
assert.equal(this.game.playerCount, 1);
6565
});
6666

67-
it('should not add a player if another one on the same IP has joined', function () {
67+
it('should not add a player if another one on the same IP has joined', async function () {
6868
this.game.addTriviaPlayer(this.user);
6969

70-
const user2 = makeTriviaUser('Not Morfent', '127.0.0.1');
70+
const user2 = await makeTriviaUser('Not Morfent', '127.0.0.1');
7171
assert.throws(() => this.game.addTriviaPlayer(user2));
7272

7373
assert.equal(this.game.playerCount, 1);
7474
destroyUser(user2);
7575
});
7676

77-
it('should not add a player if another player had their username previously', function () {
77+
it('should not add a player if another player had their username previously', async function () {
7878
const userid = this.user.id;
7979
const name = this.user.name;
8080
this.game.addTriviaPlayer(this.user);
8181
this.user.forceRename('Not Morfent', true);
8282
this.user.previousIDs.push(userid);
8383

84-
const user2 = makeTriviaUser(name, '127.0.0.3');
84+
const user2 = await makeTriviaUser(name, '127.0.0.3');
8585
assert.throws(() => this.game.addTriviaPlayer(user2));
8686

8787
assert.equal(this.game.playerCount, 1);
@@ -117,14 +117,14 @@ describe('Trivia', function () {
117117
assert.equal(this.game.playerCount, 0);
118118
});
119119

120-
it('should not add users who were kicked under another IP', function () {
120+
it('should not add users who were kicked under another IP', async function () {
121121
this.game.addTriviaPlayer(this.tarUser);
122122
this.game.kick(this.tarUser, this.user);
123123

124124
const name = this.tarUser.name;
125125
this.tarUser.resetName();
126126

127-
const user2 = makeTriviaUser(name, '127.0.0.2');
127+
const user2 = await makeTriviaUser(name, '127.0.0.2');
128128
assert.throws(() => this.game.addTriviaPlayer(user2));
129129
assert.equal(this.game.playerCount, 0);
130130
destroyUser(user2);
@@ -159,16 +159,14 @@ describe('Trivia', function () {
159159
const questions = [null, null].fill({question: '', answers: ['answer'], category: 'ae'});
160160
const game = new FirstModeTrivia(this.room, 'first', ['ae'], true, 'short', questions);
161161

162-
this.user = makeTriviaUser('Morfent', '127.0.0.1');
163-
this.user2 = makeTriviaUser('user2', '127.0.0.2');
164-
this.user3 = makeTriviaUser('user3', '127.0.0.3');
162+
this.user = await makeTriviaUser('Morfent', '127.0.0.1');
163+
this.user2 = await makeTriviaUser('user2', '127.0.0.2');
164+
this.user3 = await makeTriviaUser('user3', '127.0.0.3');
165165

166-
this.user.joinRoom(this.room);
167-
game.addTriviaPlayer(this.user);
168-
this.user2.joinRoom(this.room);
169-
game.addTriviaPlayer(this.user2);
170-
this.user3.joinRoom(this.room);
171-
game.addTriviaPlayer(this.user3);
166+
for (const user of [this.user, this.user2, this.user3]) {
167+
await user.joinRoom(this.room);
168+
game.addTriviaPlayer(user);
169+
}
172170
game.start();
173171
await game.askQuestion();
174172
clearTimeout(game.phaseTimeout);
@@ -189,11 +187,11 @@ describe('Trivia', function () {
189187
}
190188
});
191189

192-
it('should mark a player absent on leave and unnmark them when they return', function () {
190+
it('should mark a player absent on leave and unnmark them when they return', async function () {
193191
this.user.leaveRoom(this.room);
194192
assert.equal(this.player.isAbsent, true);
195193

196-
this.user.joinRoom(this.room);
194+
await this.user.joinRoom(this.room);
197195
assert.equal(this.player.isAbsent, false);
198196
});
199197
});
@@ -203,9 +201,9 @@ describe('Trivia', function () {
203201
const questions = [{question: '', answers: ['answer'], category: 'ae'}];
204202
const game = new FirstModeTrivia(this.room, 'first', ['ae'], true, 'short', questions);
205203

206-
this.user = makeTriviaUser('Morfent', '127.0.0.1');
207-
this.user2 = makeTriviaUser('user2', '127.0.0.2');
208-
this.user3 = makeTriviaUser('user3', '127.0.0.3');
204+
this.user = await makeTriviaUser('Morfent', '127.0.0.1');
205+
this.user2 = await makeTriviaUser('user2', '127.0.0.2');
206+
this.user3 = await makeTriviaUser('user3', '127.0.0.3');
209207

210208
game.addTriviaPlayer(this.user);
211209
game.addTriviaPlayer(this.user2);
@@ -267,9 +265,9 @@ describe('Trivia', function () {
267265
const questions = [{question: '', answers: ['answer'], category: 'ae'}];
268266
const game = new TimerModeTrivia(this.room, 'first', ['ae'], true, 'short', questions);
269267

270-
this.user = makeTriviaUser('Morfent', '127.0.0.1');
271-
this.user2 = makeTriviaUser('user2', '127.0.0.2');
272-
this.user3 = makeTriviaUser('user3', '127.0.0.3');
268+
this.user = await makeTriviaUser('Morfent', '127.0.0.1');
269+
this.user2 = await makeTriviaUser('user2', '127.0.0.2');
270+
this.user3 = await makeTriviaUser('user3', '127.0.0.3');
273271

274272
game.addTriviaPlayer(this.user);
275273
game.addTriviaPlayer(this.user2);
@@ -342,9 +340,9 @@ describe('Trivia', function () {
342340
const questions = [{question: '', answers: ['answer'], category: 'ae'}];
343341
const game = new NumberModeTrivia(this.room, 'first', ['ae'], true, 'short', questions);
344342

345-
this.user = makeTriviaUser('Morfent', '127.0.0.1');
346-
this.user2 = makeTriviaUser('user2', '127.0.0.2');
347-
this.user3 = makeTriviaUser('user3', '127.0.0.3');
343+
this.user = await makeTriviaUser('Morfent', '127.0.0.1');
344+
this.user2 = await makeTriviaUser('user2', '127.0.0.2');
345+
this.user3 = await makeTriviaUser('user3', '127.0.0.3');
348346

349347
game.addTriviaPlayer(this.user);
350348
game.addTriviaPlayer(this.user2);

test/server/punishments.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ describe('broader, more integrated Punishments tests', function () {
8181
});
8282

8383
describe('room bans', () => {
84-
before(() => {
84+
before(async () => {
8585
this.user = makeUser("Roomban Me Please", '127.0.0.8');
8686
this.connection = this.user.connections[0];
87-
this.user.joinRoom(this.room.roomid, this.connection);
87+
await this.user.joinRoom(this.room.roomid, this.connection);
8888
});
8989

9090
beforeEach(async () => Punishments.roomBan(this.room, this.user, Date.now() + TEST_PUNISHMENT_DURATION, this.user.id, false, 'test'));
@@ -111,25 +111,25 @@ describe('broader, more integrated Punishments tests', function () {
111111
});
112112

113113
describe('locks (network) (slow)', () => {
114-
before(() => {
114+
before(async () => {
115115
this.user = makeUser("Lock Me Please", '127.0.0.3');
116116
this.connection = this.user.connections[0];
117-
this.user.joinRoom(this.room.roomid, this.connection);
117+
await this.user.joinRoom(this.room.roomid, this.connection);
118118
});
119119

120120
beforeEach(async () => Punishments.lock(this.user, Date.now() + TEST_PUNISHMENT_DURATION, this.user.id, false, 'test'));
121121
afterEach(() => Punishments.unlock(this.user.id));
122122

123123
it('should prevent users from chatting in rooms while they are locked', async () => {
124-
const initialLogLength = this.room.log.log.length;
124+
const initialLogLength = this.room.log.logLength;
125125

126126
await this.parse("Hi! I'm a locked user!");
127-
assert.equal(this.room.log.log.length, initialLogLength, `user should be unable to sucessfully chat while locked`);
127+
assert.equal(this.room.log.logLength, initialLogLength, `user should be unable to sucessfully chat while locked`);
128128

129129
Punishments.unlock(this.user.id);
130130
await this.parse("/msgroom lobby,Hi! I'm no longer locked!");
131131
// we can't just check the roomlog length because unlocking adds a |n| message to
132-
const lastMessage = this.room.log.log.pop();
132+
const lastMessage = (await this.room.log.get()).pop();
133133
assert(lastMessage.endsWith(` Lock Me Please|Hi! I'm no longer locked!`), `user should have sucessfuly sent a message after being locked`);
134134
});
135135

@@ -171,10 +171,10 @@ describe('broader, more integrated Punishments tests', function () {
171171
});
172172

173173
describe('namelocks (network) (slow)', () => {
174-
before(() => {
174+
before(async () => {
175175
this.user = makeUser("Namelock Me Please", '127.0.0.6');
176176
this.connection = this.user.connections[0];
177-
this.user.joinRoom(this.room.roomid, this.connection);
177+
await this.user.joinRoom(this.room.roomid, this.connection);
178178
});
179179

180180
beforeEach(async () => Punishments.namelock(this.user, Date.now() + TEST_PUNISHMENT_DURATION, this.user.id, false, 'test'));
@@ -202,10 +202,10 @@ describe('broader, more integrated Punishments tests', function () {
202202
});
203203

204204
describe('global bans (network) (slow)', () => {
205-
before(() => {
205+
before(async () => {
206206
this.user = makeUser("Ban Me Please", '127.0.0.7');
207207
this.connection = this.user.connections[0];
208-
this.user.joinRoom(this.room.roomid, this.connection);
208+
await this.user.joinRoom(this.room.roomid, this.connection);
209209
});
210210

211211
beforeEach(async () => Punishments.ban(this.user, Date.now() + TEST_PUNISHMENT_DURATION, this.user.id, false, 'test'));

test/server/rooms.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('Rooms features', function () {
8585
assert.equal(room.auth.get(makeUser().id), '%');
8686
});
8787

88-
it('should prevent overriding tournament room auth by a tournament player', function () {
88+
it('should prevent overriding tournament room auth by a tournament player', async () => {
8989
parent = Rooms.createChatRoom('parentroom2');
9090
parent.auth.get = () => '%';
9191
const p1 = makeUser();
@@ -110,8 +110,8 @@ describe('Rooms features', function () {
110110
room: parent,
111111
},
112112
});
113-
roomStaff.joinRoom(room);
114-
administrator.joinRoom(room);
113+
await roomStaff.joinRoom(room);
114+
await administrator.joinRoom(room);
115115
assert.equal(room.auth.get(roomStaff), '%', 'before promotion attempt');
116116
Chat.parse("/roomvoice Room auth", room, p1, p1.connections[0]);
117117
assert.equal(room.auth.get(roomStaff), '%', 'after promotion attempt');
@@ -154,7 +154,7 @@ describe('Rooms features', function () {
154154
it("should move the users and their connections", async function () {
155155
room = Rooms.createChatRoom("test", "Test");
156156
const user = makeUser();
157-
user.joinRoom(room);
157+
await user.joinRoom(room);
158158
await room.rename("Test2");
159159
assert.equal(user.inRooms.has("test"), false);
160160
assert.equal(user.inRooms.has("test2"), true);

test/server/users.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ describe('Users features', function () {
170170
target.tempGroup = '&';
171171
assert.equal(user.can('globalban', target), false, 'targeting higher rank');
172172
});
173-
it(`should not allow users to demote themselves`, function () {
173+
it(`should not allow users to demote themselves`, async function () {
174174
room = Rooms.createChatRoom("test");
175175
const user = makeUser("User");
176-
user.joinRoom(room);
176+
await user.joinRoom(room);
177177
for (const group of [' ', '+', '@']) {
178178
room.auth.set(user.id, group);
179179
assert.equal(room.auth.get(user.id), group, 'before demotion attempt');

0 commit comments

Comments
 (0)