-
Notifications
You must be signed in to change notification settings - Fork 34
/
GameStateValidator.test.ts
142 lines (135 loc) 路 5.94 KB
/
GameStateValidator.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import GameBoard from '@bot/entity/GameBoard';
import MessagingTunnel from '@bot/messaging/MessagingTunnel';
import GameStateManager from '@bot/state/GameStateManager';
import GameStateValidator from '@bot/state/GameStateValidator';
import TicTacToeBot from '@bot/TicTacToeBot';
import Config from '@config/Config';
import Entity from '@tictactoe/Entity';
import {
Collection,
Guild,
GuildMember,
GuildMemberRoleManager,
Permissions,
PermissionString,
Role,
TextChannel
} from 'discord.js';
describe('GameStateValidator', () => {
let tunnel: MessagingTunnel;
let manager: GameStateManager;
let validator: GameStateValidator;
beforeEach(() => {
// default state is valid
tunnel = <MessagingTunnel>{
channel: <TextChannel>{
id: 'TC1',
guild: <Guild>{
me: <GuildMember>{
permissionsIn: _c => <Readonly<Permissions>>{ has: _p => true }
}
}
},
author: <GuildMember>{
id: 'GM1',
roles: <GuildMemberRoleManager>{
cache: new Collection()
},
permissions: <Readonly<Permissions>>{ has: _ => true }
}
};
manager = <GameStateManager>{
gameboards: [
{ tunnel: <MessagingTunnel>{}, entities: [] as Array<Entity> }
] as Array<GameBoard>,
memberCooldownEndTimes: new Map(),
bot: <TicTacToeBot>{
configuration: <Config>{}
}
};
validator = new GameStateValidator(manager);
});
it.each`
permissions | expected
${[]} | ${false}
${['ADD_REACTIONS']} | ${false}
${GameStateValidator['PERM_LIST']} | ${true}
`('should check for member permissions $permissions', ({ permissions, expected }) => {
const spyError = jest.spyOn(global.console, 'error').mockImplementation();
jest.spyOn(tunnel.channel.guild.me!, 'permissionsIn').mockReturnValue(<
Readonly<Permissions>
>{
has: list => (list as Array<PermissionString>).every(k => permissions.includes(k))
});
expect(validator.isInteractionValid(tunnel)).toBe(expected);
expect(spyError).toHaveBeenCalledTimes(expected ? 0 : 1);
spyError.mockRestore();
});
it.each`
allowedRoleIds | memberRoles | isAdmin | expected
${['R1']} | ${[]} | ${false} | ${false}
${['R1']} | ${['R2']} | ${false} | ${false}
${['R1', 'R2']} | ${['R1']} | ${false} | ${true}
${['R1']} | ${['R1']} | ${false} | ${true}
${['R1']} | ${['R1', 'R2']} | ${false} | ${true}
${['R1']} | ${[]} | ${true} | ${true}
${[]} | ${[]} | ${false} | ${true}
${[]} | ${[]} | ${true} | ${true}
`(
'should check with member roles $memberRoles and admin $isAdmin (allowed roles: $allowedRoleIds)',
({ allowedRoleIds, memberRoles, isAdmin, expected }) => {
jest.spyOn(tunnel.author.permissions, 'has').mockReturnValue(isAdmin);
jest.spyOn(tunnel.author.permissions, 'has').mockReturnValue(isAdmin);
Object.defineProperty(tunnel.author.roles, 'cache', {
value: new Collection(
memberRoles.map((roleId: string) => [roleId, <Role>{ id: roleId }])
)
});
manager.bot.configuration.allowedRoleIds = allowedRoleIds;
expect(validator.isInteractionValid(tunnel)).toBe(expected);
}
);
it.each`
cooldownTime | currentTime | expected
${10} | ${Number.MAX_VALUE} | ${false}
${20} | ${Date.now() * 2} | ${false}
${undefined} | ${0} | ${true}
${0} | ${undefined} | ${true}
${10} | ${undefined} | ${true}
${5} | ${Date.now() - 20} | ${true}
`(
'should check with cooldown time $cooldownTime and current time $currentTime',
({ cooldownTime, currentTime, expected }) => {
if (currentTime !== undefined) {
manager.memberCooldownEndTimes.set('GM1', currentTime);
}
manager.bot.configuration.requestCooldownTime = cooldownTime;
expect(validator.isInteractionValid(tunnel)).toBe(expected);
}
);
it.each`
sameChannel | sameAuthor | sameInvited | simultaneousGames | expected
${false} | ${true} | ${false} | ${false} | ${false}
${true} | ${true} | ${false} | ${true} | ${false}
${true} | ${false} | ${false} | ${false} | ${false}
${false} | ${false} | ${true} | ${false} | ${false}
${false} | ${false} | ${false} | ${false} | ${true}
${true} | ${false} | ${false} | ${true} | ${true}
`(
'should check gameboard states with sameChannel $sameChannel, sameAuthor $sameAuthor, simultaneous option $simultaneousGames and invited $sameInvited',
({ sameChannel, sameAuthor, sameInvited, simultaneousGames, expected }) => {
if (sameChannel) {
manager.gameboards[0] = <GameBoard>{ ...manager.gameboards[0], tunnel };
}
if (sameAuthor || sameInvited) {
manager.gameboards[0] = <GameBoard>{
...manager.gameboards[0],
entities: [tunnel.author as Entity]
};
}
manager.bot.configuration.simultaneousGames = simultaneousGames;
const invited = sameInvited ? tunnel.author : undefined;
expect(validator.isNewGamePossible(tunnel, invited)).toBe(expected);
}
);
});