-
Notifications
You must be signed in to change notification settings - Fork 34
/
GameStateManager.test.ts
163 lines (138 loc) 路 6.72 KB
/
GameStateManager.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import DuelRequest from '@bot/entity/DuelRequest';
import GameBoard from '@bot/entity/GameBoard';
import EventHandler from '@bot/EventHandler';
import MessagingTunnel from '@bot/messaging/MessagingTunnel';
import GameStateManager from '@bot/state/GameStateManager';
import GameStateValidator from '@bot/state/GameStateValidator';
import TicTacToeBot from '@bot/TicTacToeBot';
import AI from '@tictactoe/AI';
import Entity from '@tictactoe/Entity';
import { GuildMember } from 'discord.js';
jest.mock('@bot/entity/DuelRequest');
jest.mock('@bot/entity/GameBoard');
jest.mock('@bot/state/GameStateValidator');
jest.mock('@tictactoe/AI');
describe('GameStateManager', () => {
const duelRequest = jest.mocked(DuelRequest);
const gameBoard = jest.mocked(GameBoard);
let manager: GameStateManager;
let validator: GameStateValidator;
let bot: TicTacToeBot;
let tunnel: MessagingTunnel;
beforeEach(() => {
bot = <TicTacToeBot>{
configuration: {},
eventHandler: <EventHandler>{
emitEvent: (_type, _data) => {
// empty
}
}
};
tunnel = <MessagingTunnel>{
author: { id: 'A1', displayName: 'username' },
replyWith: _a => Promise.resolve({})
};
manager = new GameStateManager(bot);
validator = manager['validator'];
});
describe('Method: requestDuel', () => {
it('should check if the interaction is valid', async () => {
const spyValidate = jest.spyOn(validator, 'isInteractionValid');
const invited = <GuildMember>{};
await manager.requestDuel(tunnel, invited);
expect(spyValidate).toHaveBeenCalledTimes(1);
expect(spyValidate).toHaveBeenCalledWith(tunnel);
});
it('should check if a new game is possible', async () => {
jest.spyOn(validator, 'isInteractionValid').mockReturnValue(true);
const spyValidate = jest.spyOn(validator, 'isNewGamePossible');
const invited = <GuildMember>{};
await manager.requestDuel(tunnel, invited);
expect(spyValidate).toHaveBeenCalledTimes(1);
expect(spyValidate).toHaveBeenCalledWith(tunnel, invited);
});
it('should create a duel request and send it into the messaging tunnel', async () => {
jest.spyOn(validator, 'isInteractionValid').mockReturnValue(true);
jest.spyOn(validator, 'isNewGamePossible').mockReturnValue(true);
const spyReplyWith = jest.spyOn(tunnel, 'replyWith');
await manager.requestDuel(tunnel, <GuildMember>{});
expect(duelRequest).toHaveBeenCalledTimes(1);
expect(spyReplyWith).toHaveBeenCalledTimes(1);
});
it('should setup user cooldown if enabled in configuration', async () => {
jest.spyOn(validator, 'isInteractionValid').mockReturnValue(true);
jest.spyOn(validator, 'isNewGamePossible').mockReturnValue(true);
// by default, no cooldown
await manager.requestDuel(tunnel, <GuildMember>{});
expect(manager.memberCooldownEndTimes.size).toBe(0);
// setup fake cooldown
bot.configuration.requestCooldownTime = 60;
await manager.requestDuel(tunnel, <GuildMember>{});
expect(manager.memberCooldownEndTimes.size).toBe(1);
expect(manager.memberCooldownEndTimes.get('A1')).toBeGreaterThan(Date.now());
});
});
describe('Method: createGame', () => {
it('should check if the interaction is valid', async () => {
const spyValidate = jest.spyOn(validator, 'isInteractionValid');
await manager.createGame(tunnel);
expect(spyValidate).toHaveBeenCalledTimes(1);
expect(spyValidate).toHaveBeenCalledWith(tunnel);
});
it('should check if a new game is possible', async () => {
jest.spyOn(validator, 'isInteractionValid').mockReturnValue(true);
const spyValidate = jest.spyOn(validator, 'isNewGamePossible');
const invited = <GuildMember>{};
await manager.createGame(tunnel, invited);
expect(spyValidate).toHaveBeenCalledTimes(1);
expect(spyValidate).toHaveBeenCalledWith(tunnel, invited);
});
it('should create a game board and send it into the messaging tunnel', async () => {
jest.spyOn(validator, 'isInteractionValid').mockReturnValue(true);
jest.spyOn(validator, 'isNewGamePossible').mockReturnValue(true);
const spyReplyWith = jest.spyOn(tunnel, 'replyWith');
const invited = <GuildMember>{};
await manager.createGame(tunnel, invited);
expect(manager.gameboards).toHaveLength(1);
expect(gameBoard).toHaveBeenCalledTimes(1);
expect(gameBoard).toHaveBeenCalledWith(manager, tunnel, invited, expect.anything());
expect(spyReplyWith).toHaveBeenCalledTimes(1);
});
it('should create a game board with AI if no invited member', async () => {
jest.spyOn(validator, 'isInteractionValid').mockReturnValue(true);
jest.spyOn(validator, 'isNewGamePossible').mockReturnValue(true);
await manager.createGame(tunnel);
expect(gameBoard).toHaveBeenCalledTimes(1);
expect(gameBoard).toHaveBeenCalledWith(
manager,
tunnel,
expect.any(AI),
expect.anything()
);
});
});
describe('Method: endGame', () => {
const entity1 = <Entity>{ id: 'E1' };
const entity2 = <Entity>{ id: 'E2' };
const gameboard = <GameBoard>{ entities: [entity1, entity2] };
it('should remove gameboard from list when ended', () => {
manager.gameboards.push(gameboard);
manager.endGame(gameboard);
expect(manager.gameboards).toHaveLength(0);
});
it('should emit win event if ended with a winner', () => {
const spyEmitEvent = jest.spyOn(bot.eventHandler, 'emitEvent');
// entity 1 = winner
manager.endGame(gameboard, entity1);
expect(spyEmitEvent).toHaveBeenCalledWith('win', { winner: entity1, loser: entity2 });
// entity 2 = winner
manager.endGame(gameboard, entity2);
expect(spyEmitEvent).toHaveBeenCalledWith('win', { winner: entity2, loser: entity1 });
});
it('should emit tie event if ended without winner', () => {
const spyEmitEvent = jest.spyOn(bot.eventHandler, 'emitEvent');
manager.endGame(gameboard, null);
expect(spyEmitEvent).toHaveBeenCalledWith('tie', { players: gameboard.entities });
});
});
});