-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathFishCommandTest.java
More file actions
498 lines (391 loc) · 13.5 KB
/
FishCommandTest.java
File metadata and controls
498 lines (391 loc) · 13.5 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package oakbot.command;
import static oakbot.bot.ChatActionsUtils.assertMessage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import oakbot.Database;
import oakbot.bot.IBot;
import oakbot.bot.PostMessage;
import oakbot.util.ChatCommandBuilder;
import oakbot.util.Now;
import oakbot.util.Rng;
class FishCommandTest {
@AfterEach
void after() {
Now.restore();
Rng.restore();
}
@Test
void loadInventories_no_data() {
var db = mock(Database.class);
new FishCommand(db, Duration.ofSeconds(1), Duration.ofSeconds(1), Duration.ofSeconds(1));
verify(db).getMap("fish.caught");
}
@Test
void loadInventories_empty() {
var db = mock(Database.class);
when(db.getMap("fish.caught")).thenReturn(new HashMap<String, Object>());
new FishCommand(db, Duration.ofSeconds(1), Duration.ofSeconds(1), Duration.ofSeconds(1));
verify(db).getMap("fish.caught");
}
@Test
void loadInventories_no_entry_for_user() {
//@formatter:off
Map<String, Object> map = Map.of(
"123456", Map.of(
"Hellfish", 1
)
);
//@formatter:on
var db = mock(Database.class);
when(db.getMap("fish.caught")).thenReturn(map);
var bot = mock(IBot.class);
var command = new FishCommand(db, Duration.ofSeconds(1), Duration.ofSeconds(1), Duration.ofSeconds(1));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.userId(789012)
.content("inv")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Your inventory is empty.*", 10, actual);
}
@Test
void loadInventories_test_sort_order() {
//@formatter:off
Map<String, Object> map = Map.of(
"123456", Map.of(
"Hellfish", 1,
"Chlam", 1,
"Slavug", 2
)
);
//@formatter:on
var db = mock(Database.class);
when(db.getMap("fish.caught")).thenReturn(map);
var bot = mock(IBot.class);
var command = new FishCommand(db, Duration.ofSeconds(1), Duration.ofSeconds(1), Duration.ofSeconds(1));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.userId(123456)
.content("inv")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Your inventory: Slavug (x2), Chlam, Hellfish*", 10, actual);
}
@Test
void fish_username_does_not_end_in_s() throws Exception {
var db = mock(Database.class);
var bot = mock(IBot.class);
var rand = mock(Random.class);
when(rand.nextInt(15 * 60, 30 * 60)).thenReturn(20 * 60); //line will quiver after (15+5) minutes
when(rand.nextDouble()).thenReturn(0.1234);
Rng.inject(rand);
var command = new FishCommand(db, Duration.ofMinutes(15), Duration.ofMinutes(30), Duration.ofMinutes(15));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Michael")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Michael throws in a line.*", actual);
Now.fastForward(Duration.ofMinutes(21)); //1 extra minute to ensure the time is right
command.run(bot);
verify(bot).sendMessage(1, new PostMessage("🐟 *Michael's line quivers.*"));
verify(db, never()).set(eq("fish.caught"), any(Map.class));
}
@Test
void fish_too_soon() throws Exception {
var db = mock(Database.class);
var bot = mock(IBot.class);
var rand = mock(Random.class);
when(rand.nextInt(15 * 60, 30 * 60)).thenReturn(20 * 60); //line will quiver after (15+5) minutes
when(rand.nextDouble()).thenReturn(0.1234);
Rng.inject(rand);
var command = new FishCommand(db, Duration.ofMinutes(15), Duration.ofMinutes(30), Duration.ofMinutes(15));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Zagreus throws in a line.*", actual);
Now.fastForward(Duration.ofMinutes(15));
command.run(bot);
verify(bot, never()).sendMessage(anyInt(), any(PostMessage.class));
//@formatter:off
actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
var expected = List.of(
new PostMessage("🐟 *Zagreus pulls up nothing.*")
);
//@formatter:on
assertEquals(expected, actual.getActions());
/*
* Line won't quiver because it has been pulled.
*/
Now.fastForward(Duration.ofMinutes(6)); //1 extra minute to ensure the time is right
command.run(bot);
verify(bot, never()).sendMessage(anyInt(), any(PostMessage.class));
verify(db, never()).set(eq("fish.caught"), any(Map.class));
}
@Test
void fish_too_late() throws Exception {
var db = mock(Database.class);
var bot = mock(IBot.class);
var rand = mock(Random.class);
when(rand.nextInt(15 * 60, 30 * 60)).thenReturn(20 * 60, 20 * 60); //line will quiver after (15+5) minutes
when(rand.nextDouble()).thenReturn(0.1234);
Rng.inject(rand);
var command = new FishCommand(db, Duration.ofMinutes(15), Duration.ofMinutes(30), Duration.ofMinutes(15));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Zagreus throws in a line.*", actual);
Now.fastForward(Duration.ofMinutes(15));
command.run(bot);
verify(bot, never()).sendMessage(anyInt(), any(PostMessage.class));
Now.fastForward(Duration.ofMinutes(6)); //1 extra minute to ensure the time is right
command.run(bot);
verify(bot).sendMessage(1, new PostMessage("🐟 *Zagreus' line quivers.*"));
/*
* Waited too long.
*/
Now.fastForward(Duration.ofMinutes(20));
//@formatter:off
actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
var expected = List.of(
new PostMessage("🐟 *Zagreus pulls up nothing. They weren't quick enough.*")
);
//@formatter:on
assertEquals(expected, actual.getActions());
verify(db, never()).set(eq("fish.caught"), any(Map.class));
}
@Test
void fish_quiver_again() throws Exception {
var db = mock(Database.class);
var bot = mock(IBot.class);
var rand = mock(Random.class);
when(rand.nextInt(15 * 60, 30 * 60)).thenReturn(20 * 60, 25 * 60); //line will quiver after (15+5) and (15+10) minutes
when(rand.nextDouble()).thenReturn(0.1234);
Rng.inject(rand);
var command = new FishCommand(db, Duration.ofMinutes(15), Duration.ofMinutes(30), Duration.ofMinutes(15));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Zagreus throws in a line.*", actual);
Now.fastForward(Duration.ofMinutes(21)); //1 extra minute to ensure the time is right
command.run(bot);
verify(bot).sendMessage(1, new PostMessage("🐟 *Zagreus' line quivers.*"));
Now.fastForward(Duration.ofMinutes(26)); //1 extra minute to ensure the time is right
command.run(bot);
verify(bot).sendMessage(1, new PostMessage("🐟 *Zagreus' line quivers.*"));
verify(db, never()).set(eq("fish.caught"), any(Map.class));
}
@Test
void fish() throws Exception {
var db = mock(Database.class);
var bot = mock(IBot.class);
var rand = mock(Random.class);
when(rand.nextInt(15 * 60, 30 * 60)).thenReturn(20 * 60); //line will quiver after (15+5) minutes
when(rand.nextDouble()).thenReturn(0.1234);
Rng.inject(rand);
var command = new FishCommand(db, Duration.ofMinutes(15), Duration.ofMinutes(30), Duration.ofMinutes(15));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Zagreus throws in a line.*", actual);
Now.fastForward(Duration.ofMinutes(15));
command.run(bot);
verify(bot, never()).sendMessage(anyInt(), any(PostMessage.class));
Now.fastForward(Duration.ofMinutes(6)); //1 extra minute to ensure the time is right
command.run(bot);
verify(bot).sendMessage(1, new PostMessage("🐟 *Zagreus' line quivers.*"));
//@formatter:off
actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
var expected = List.of(
new PostMessage("🐟 *Zagreus caught a **Hellfish**!*"),
new PostMessage("https://static.wikia.nocookie.net/hades_gamepedia_en/images/3/3d/Hellfish.png")
);
//@formatter:on
assertEquals(expected, actual.getActions());
//@formatter:off
Map<String, Object> map = Map.of(
"123456", Map.of(
"Hellfish", 1
)
);
//@formatter:on
verify(db).set("fish.caught", map);
}
@Test
void fish_again() throws Exception {
var db = mock(Database.class);
var bot = mock(IBot.class);
var rand = mock(Random.class);
when(rand.nextInt(15 * 60, 30 * 60)).thenReturn(20 * 60); //line will quiver after (15+5) minutes
when(rand.nextDouble()).thenReturn(0.1234);
Rng.inject(rand);
var command = new FishCommand(db, Duration.ofMinutes(15), Duration.ofMinutes(30), Duration.ofMinutes(15));
verify(db).getMap("fish.caught");
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.content("again")
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Zagreus throws in a line.*", actual);
Now.fastForward(Duration.ofMinutes(15));
command.run(bot);
verify(bot, never()).sendMessage(anyInt(), any(PostMessage.class));
Now.fastForward(Duration.ofMinutes(6)); //1 extra minute to ensure the time is right
command.run(bot);
verify(bot).sendMessage(1, new PostMessage("🐟 *Zagreus' line quivers.*"));
//@formatter:off
actual = command.onMessage(new ChatCommandBuilder(command)
.content("again")
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
var expected = List.of(
new PostMessage("🐟 *Zagreus caught a **Hellfish**!*"),
new PostMessage("https://static.wikia.nocookie.net/hades_gamepedia_en/images/3/3d/Hellfish.png"),
new PostMessage("🐟 *Zagreus throws in a line.*")
);
//@formatter:on
assertEquals(expected, actual.getActions());
//@formatter:off
Map<String, Object> map = Map.of(
"123456", Map.of(
"Hellfish", 1
)
);
//@formatter:on
verify(db).set("fish.caught", map);
}
@Test
void fish_status() {
var db = mock(Database.class);
var bot = mock(IBot.class);
var rand = mock(Random.class);
when(rand.nextInt(15 * 60, 30 * 60)).thenReturn(20 * 60); //line will quiver after (15+5) minutes
when(rand.nextDouble()).thenReturn(0.1234);
Rng.inject(rand);
var command = new FishCommand(db, Duration.ofMinutes(15), Duration.ofMinutes(30), Duration.ofMinutes(15));
verify(db).getMap("fish.caught");
/*
* Before throwing out the line.
*/
//@formatter:off
var actual = command.onMessage(new ChatCommandBuilder(command)
.content("status")
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *You don't have any lines out.*", 10, actual);
/*
* Throw out the line.
*/
//@formatter:off
actual = command.onMessage(new ChatCommandBuilder(command)
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Zagreus throws in a line.*", actual);
/*
* Check status before quiver.
*/
//@formatter:off
actual = command.onMessage(new ChatCommandBuilder(command)
.content("status")
.messageId(10)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
//@formatter:on
assertMessage("🐟 *Your line doesn't have anything. You should wait until it quivers.*", 10, actual);
/*
* Check status after quiver.
*/
Now.fastForward(Duration.ofMinutes(21));
//@formatter:off
actual = command.onMessage(new ChatCommandBuilder(command)
.content("status")
.messageId(11)
.roomId(1)
.userId(123456)
.username("Zagreus")
.build(), bot);
var expected = List.of(
new PostMessage("🐟 *Your line is quivering. Better pull it up.*").parentId(11)
);
//@formatter:on
assertEquals(expected, actual.getActions());
verify(db, never()).set(eq("fish.caught"), any(Map.class));
}
}