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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
|
//! Behaviour tests for the client core.
//!
//! These tests never touch a terminal or a socket. `Screen` applies the
//! logical `Effect`s that `ClientCore` emits, so assertions are about what a
//! player would see, not about ANSI sequences.
use zagan_bet::blackjack::BlackjackState;
use zagan_bet::client::core::{ClientCore, Effect, InteractiveCommand, Key};
use zagan_bet::client::games::response::{parse_blackjack_response, parse_spin_response};
use zagan_bet::client::ui::command_names;
const WIN3: &str = "CCCCLLCLODGSDGS";
const LOSS: &str = "CLOOGDSLCDCOGLS";
const ALL_C: &str = "CCCCCCCCCCCCCCC";
#[derive(Default)]
struct Screen {
lines: Vec<String>,
region_start: Option<usize>,
region_len: usize,
prompt: String,
prompt_cursor: u16,
}
impl Screen {
fn apply(
&mut self,
effects: &[Effect],
sent: &mut Vec<String>,
interactive: &mut Vec<InteractiveCommand>,
) {
for effect in effects {
match effect {
Effect::Print(lines) => self.lines.extend(lines.iter().cloned()),
Effect::ReplaceRegion(lines) => {
if let Some(start) = self.region_start {
self.lines.truncate(start);
} else {
self.region_start = Some(self.lines.len());
}
self.lines.extend(lines.iter().cloned());
self.region_len = lines.len();
}
Effect::ClearRegion => {
if let Some(start) = self.region_start {
self.lines.truncate(start);
}
self.region_start = None;
self.region_len = 0;
}
Effect::AnimateRoulette { final_lines, .. } => {
if let Some(start) = self.region_start {
self.lines.truncate(start);
} else {
self.region_start = Some(self.lines.len());
}
self.lines.extend(final_lines.iter().cloned());
self.region_len = final_lines.len();
}
Effect::SetPrompt { text, cursor_col } => {
self.prompt = text.clone();
self.prompt_cursor = *cursor_col;
}
Effect::SendLine(line) => sent.push(line.clone()),
Effect::Interactive(command) => interactive.push(command.clone()),
Effect::Quit
| Effect::Disconnect
| Effect::ListHosts
| Effect::ForgetHost(_) => {}
}
}
}
fn board(&self) -> &[String] {
match self.region_start {
Some(start) => &self.lines[start..start + self.region_len],
None => &[],
}
}
}
struct Harness {
core: ClientCore,
screen: Screen,
sent: Vec<String>,
interactive: Vec<InteractiveCommand>,
}
impl Harness {
fn new() -> Self {
let mut core = ClientCore::new();
core.set_connected(true);
core.set_logged_in(Some("alice".to_string()));
Self {
core,
screen: Screen::default(),
sent: Vec::new(),
interactive: Vec::new(),
}
}
fn apply(&mut self, effects: Vec<Effect>) {
self.screen
.apply(&effects, &mut self.sent, &mut self.interactive);
}
fn key(&mut self, key: Key) {
let effects = self.core.handle_key(key);
self.apply(effects);
if let Some(prompt) = self.core.prompt_effect() {
self.apply(vec![prompt]);
}
}
fn line(&mut self, line: &str) {
for c in line.chars() {
self.key(Key::Char(c));
}
self.key(Key::Enter);
}
fn reply(&mut self, reply: &str) {
let effects = self.core.handle_reply(reply);
self.apply(effects);
if let Some(prompt) = self.core.prompt_effect() {
self.apply(vec![prompt]);
}
}
fn board(&self) -> &[String] {
self.screen.board()
}
fn enter_slots(&mut self, bet: &str) {
self.line(&format!("play slots {bet}"));
self.reply("INFO alice 1 1000");
}
fn enter_roulette(&mut self) {
self.line("play roulette");
self.reply("INFO alice 1 1000");
}
fn enter_blackjack(&mut self) {
self.line("play blackjack");
self.reply("INFO alice 1 1000");
}
}
#[test]
fn play_shows_a_ten_line_board_and_fetches_the_balance() {
let mut h = Harness::new();
h.line("play slots 50");
assert_eq!(h.board().len(), 10);
assert!(h.board()[1].contains("Bet: 50 PLN"));
assert!(!h.board()[1].contains("Lines:"));
assert!(h.board()[5].contains("Pays bet x symbol x shape"));
assert!(h.board()[6].contains("Symbol C1 L2 O3 G4 D6 S10"));
assert!(h.board()[6].contains("Shape 3:1 4:3 5:14"));
assert!(h.sent.contains(&"INFO".to_string()));
h.reply("INFO alice 1 1000");
assert_eq!(h.board().len(), 10);
assert!(h.board()[1].contains("Balance: 1000 PLN"));
}
#[test]
fn spin_replaces_the_board_and_keeps_the_controls_line() {
let mut h = Harness::new();
h.enter_slots("50");
let lines_before_spin = h.screen.lines.len();
h.key(Key::Char(' '));
assert_eq!(h.board().len(), 10);
assert!(h.board()[7].contains("Spinning..."));
assert!(h.board()[9].contains("[Q] Quit"));
assert!(h.sent.iter().any(|line| line == "PLAY_START slots 50"));
assert_eq!(h.screen.lines.len(), lines_before_spin);
h.reply("GAME_RESULT session-1 50 50 1000 CCCCLLCLODGSDGS win 1 0,3,6");
assert_eq!(h.board().len(), 10);
assert!(h
.board()
.get(2)
.is_some_and(|line| line.contains("{ C } { C } { C } [ D ] [ D ]")));
assert!(h.board()[7].contains("Won 50 PLN"));
assert!(h.board()[8].contains("Winning lines: 1 (top)"));
assert!(h.board()[1].contains("Balance: 1000 PLN"));
assert!(h.board()[9].contains("[Q] Quit"));
assert_eq!(h.screen.lines.len(), lines_before_spin);
}
#[test]
fn equals_increases_the_bet_like_plus() {
let mut h = Harness::new();
h.enter_slots("10");
h.key(Key::Char('='));
assert_eq!(h.board().len(), 10);
assert!(h.board()[1].contains("Bet: 20 PLN"));
}
#[test]
fn changing_the_bet_after_a_spin_does_not_leave_stale_lines() {
let mut h = Harness::new();
h.enter_slots("50");
h.key(Key::Char(' '));
h.reply("GAME_RESULT session-1 50 50 1000 CCCCLLCLODGSDGS win 1 0,3,6");
let lines_before_bet = h.screen.lines.len();
h.key(Key::Char('+'));
assert_eq!(h.board().len(), 10);
assert!(h.board()[1].contains("Bet: 60 PLN"));
assert!(h
.board()
.get(2)
.is_some_and(|line| line.contains("{ C } { C } { C } [ D ] [ D ]")));
assert_eq!(h.screen.lines.len(), lines_before_bet);
assert_eq!(
h.screen
.lines
.iter()
.filter(|line| line.contains("=== SLOTS ==="))
.count(),
1
);
}
#[test]
fn a_losing_spin_shows_no_win_and_no_winning_lines() {
let mut h = Harness::new();
h.enter_slots("10");
h.key(Key::Char(' '));
h.reply(&format!("GAME_RESULT session-1 0 10 990 {LOSS} none -"));
assert_eq!(h.board().len(), 10);
assert!(h.board()[7].contains("No win."));
assert_eq!(h.board()[8], "");
assert!(!h.board()[2].contains('{'));
}
#[test]
fn a_mega_win_shows_all_winning_lines() {
let mut h = Harness::new();
h.enter_slots("10");
h.key(Key::Char(' '));
h.reply(&format!("GAME_RESULT session-1 700 10 1690 {ALL_C} mega 1,2,3,4,5 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14"));
assert_eq!(h.board().len(), 10);
assert!(h.board()[7].contains("MEGA WIN! 700 PLN"));
assert!(h.board()[8].contains("Winning lines: 1 (top), 2 (middle), 3 (bottom), 4 (V), 5 (inverted V)"));
assert_eq!(h.board()[2].matches("{ C }").count(), 5);
}
#[test]
fn quitting_clears_the_board_and_returns_the_prompt() {
let mut h = Harness::new();
h.enter_slots("50");
h.key(Key::Char('q'));
assert!(h.board().is_empty());
assert!(h.screen.lines.iter().any(|line| line == "Game exited."));
assert_eq!(h.screen.prompt, "");
}
#[test]
fn a_server_error_replaces_the_status_line() {
let mut h = Harness::new();
h.enter_slots("50");
h.key(Key::Char(' '));
h.reply("ERROR Insufficient balance");
assert_eq!(h.board().len(), 10);
assert!(h.board()[7].contains("ERROR: Insufficient balance"));
assert!(h.board()[2].contains("[ ? ]"));
assert!(!h.board().iter().any(|line| line.contains("Spinning")));
}
#[test]
fn an_empty_reply_is_an_error_not_a_silent_spin() {
let mut h = Harness::new();
h.enter_slots("50");
h.key(Key::Char(' '));
h.reply("");
assert_eq!(h.board().len(), 10);
assert!(h.board()[7].contains("No response from server"));
}
#[test]
fn empty_enter_does_not_print_help() {
let mut h = Harness::new();
h.line("games");
let lines_before = h.screen.lines.clone();
h.key(Key::Enter);
assert_eq!(h.screen.lines, lines_before);
assert_eq!(h.screen.prompt, "");
}
#[test]
fn history_does_not_duplicate_consecutive_commands() {
let mut h = Harness::new();
h.line("games");
h.line("games");
h.key(Key::Up);
assert_eq!(h.screen.prompt, "games");
h.key(Key::Up);
assert_eq!(h.screen.prompt, "games");
}
#[test]
fn history_recalls_previous_commands() {
let mut h = Harness::new();
h.line("games");
h.line("help");
h.key(Key::Up);
assert_eq!(h.screen.prompt, "help");
h.key(Key::Up);
assert_eq!(h.screen.prompt, "games");
}
#[test]
fn tab_completes_a_unique_command() {
let mut h = Harness::new();
for c in "con".chars() {
h.key(Key::Char(c));
}
h.key(Key::Tab);
assert_eq!(h.screen.prompt, "connect");
assert_eq!(h.screen.prompt_cursor, 9);
}
#[test]
fn tab_completion_list_wraps() {
let mut h = Harness::new();
h.key(Key::Tab);
assert!(h
.screen
.lines
.iter()
.any(|line| line == "Possible completions:"));
for line in h
.screen
.lines
.iter()
.skip_while(|line| line.as_str() != "Possible completions:")
.skip(1)
{
assert!(
line.len() <= 76,
"completion line is too wide ({}): {line:?}",
line.len()
);
}
assert_eq!(h.screen.prompt, "");
}
#[test]
fn play_without_a_bet_uses_the_default() {
let mut h = Harness::new();
h.line("play slots");
assert_eq!(h.board().len(), 10);
assert!(h.board()[1].contains("Bet: 10 PLN"));
}
#[test]
fn an_invalid_bet_does_not_start_a_game() {
let mut h = Harness::new();
h.line("play slots 5");
assert!(h.board().is_empty());
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("Bet must be at least 10 PLN")));
}
#[test]
fn help_lists_every_command() {
let mut h = Harness::new();
h.line("help");
let transcript = h.screen.lines.join("\n");
for command in command_names() {
assert!(
transcript.contains(command),
"help is missing '{command}'"
);
}
}
#[test]
fn send_keeps_the_whole_message() {
let mut h = Harness::new();
h.line("send hello world");
assert_eq!(h.sent.last().map(String::as_str), Some("hello world"));
}
#[test]
fn play_requires_a_connection_and_a_login() {
let mut h = Harness::new();
h.core.set_connected(false);
h.line("play slots");
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("Not connected")));
let mut h = Harness::new();
h.core.set_logged_in(None);
h.line("play slots");
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("Not logged in")));
}
#[test]
fn ctrl_c_quits_from_the_prompt() {
let mut core = ClientCore::new();
assert_eq!(core.handle_key(Key::CtrlC), vec![Effect::Quit]);
}
#[test]
fn ctrl_c_quits_from_a_game() {
let mut h = Harness::new();
h.enter_slots("50");
assert_eq!(h.core.handle_key(Key::CtrlC), vec![Effect::Quit]);
}
#[test]
fn prompt_cursor_tracks_the_input() {
let mut h = Harness::new();
h.key(Key::Char('a'));
assert_eq!(h.screen.prompt, "a");
assert_eq!(h.screen.prompt_cursor, 3);
h.key(Key::Backspace);
assert_eq!(h.screen.prompt, "");
assert_eq!(h.screen.prompt_cursor, 2);
}
#[test]
fn info_shows_the_host_and_the_server_fields() {
let mut h = Harness::new();
h.core.set_host_info(
Some("127.0.0.1:11525".to_string()),
Some("deadbeef".to_string()),
);
h.line("info");
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("Connected to: 127.0.0.1:11525")));
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("Fingerprint: deadbeef")));
h.reply("INFO alice 42 1234");
assert!(h.screen.lines.iter().any(|line| line == "User: alice"));
assert!(h.screen.lines.iter().any(|line| line == "Account: 42"));
assert!(h
.screen
.lines
.iter()
.any(|line| line == "Balance: 1234 PLN"));
}
#[test]
fn info_without_a_connection_does_not_send() {
let mut h = Harness::new();
h.core.set_connected(false);
h.line("info");
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("Not connected to any host")));
assert!(h.sent.is_empty());
}
#[test]
fn ping_prints_pong() {
let mut h = Harness::new();
h.line("ping");
assert_eq!(h.sent.last().map(String::as_str), Some("PING"));
h.reply("PONG");
assert!(h.screen.lines.iter().any(|line| line == "Pong"));
}
#[test]
fn send_prints_the_echo() {
let mut h = Harness::new();
h.line("send hello");
h.reply("hello");
assert!(h
.screen
.lines
.iter()
.any(|line| line == "Echo: hello"));
}
#[test]
fn disconnect_clears_connection_state() {
let mut h = Harness::new();
h.line("disconnect");
assert!(h
.screen
.lines
.iter()
.any(|line| line == "Disconnected"));
h.line("play slots");
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("Not connected")));
}
#[test]
fn parses_a_five_by_three_result_with_winning_lines() {
let response = format!("GAME_RESULT session-1 50 50 1000 {WIN3} win 1 0,3,6");
let result = parse_spin_response(&response).expect("valid 5x3 result");
assert_eq!(result.reels.chars().count(), 15);
assert_eq!(result.win_type, "win");
assert_eq!(result.win_lines, vec![1]);
assert_eq!(result.win_cells, vec![0, 3, 6]);
}
#[test]
fn accepts_a_legacy_three_symbol_result() {
let result = parse_spin_response("GAME_RESULT session-1 60 10 1050 OSS two")
.expect("legacy result should still parse");
assert_eq!(result.reels, "OSS");
assert_eq!(result.win_type, "two");
assert!(result.win_lines.is_empty());
assert!(result.win_cells.is_empty());
}
#[test]
fn rejects_a_grid_with_the_wrong_size() {
let response = "GAME_RESULT session-1 100 50 1050 CCCCCCCCCCCCCC win 1";
assert!(parse_spin_response(response).is_err());
}
#[test]
fn roulette_split_bets_spin_and_show_the_result() {
let mut h = Harness::new();
h.enter_roulette();
assert_eq!(h.board().len(), 8);
assert_eq!(h.board()[0], "=== ROULETTE ===");
assert!(h.board()[1].contains("Balance: 1000 PLN"));
assert!(h.board()[1].contains("Chip: 10 PLN"));
assert!(h.board()[2].contains("Bets: (none)"));
h.key(Key::Char('r'));
h.key(Key::Char('e'));
h.key(Key::Char('1'));
h.key(Key::Char('7'));
h.key(Key::Enter);
assert!(h.board()[2].contains("RED 10"));
assert!(h.board()[2].contains("EVEN 10"));
assert!(h.board()[2].contains("#17 10"));
assert!(h.board()[2].contains("Total: 30 PLN"));
h.key(Key::Char(' '));
let request = h
.sent
.iter()
.find(|line| line.starts_with("PLAY_START roulette"))
.expect("roulette request");
assert_eq!(
request,
"PLAY_START roulette 30 {\"red\":10,\"black\":0,\"even\":10,\"odd\":0,\"numbers\":[{\"number\":17,\"amount\":10}]}"
);
let bets =
r#"{"red":10,"black":0,"even":10,"odd":0,"numbers":[{"number":17,"amount":10}]}"#;
h.reply(&format!(
"ROULETTE_RESULT session-1 360 30 1330 17 black {bets}"
));
assert_eq!(h.board().len(), 8);
assert!(h.board()[1].contains("Balance: 1330 PLN"));
assert!(h.board()[4].contains("Winning: 17 BLACK ODD"));
assert!(h.board()[5].contains("Won 360 PLN"));
}
#[test]
fn roulette_rejects_an_empty_spin_and_accepts_the_equals_chip_key() {
let mut h = Harness::new();
h.enter_roulette();
h.key(Key::Char(' '));
assert!(h.board()[4].contains("ERROR: Place at least one bet"));
assert!(!h.sent.iter().any(|line| line.starts_with("PLAY_START roulette")));
h.key(Key::Char('='));
assert!(h.board()[1].contains("Chip: 20 PLN"));
h.key(Key::Char('r'));
assert!(h.board()[2].contains("RED 20"));
assert!(h.board()[2].contains("Total: 20 PLN"));
}
#[test]
fn roulette_rejects_an_out_of_range_number() {
let mut h = Harness::new();
h.enter_roulette();
h.key(Key::Char('9'));
h.key(Key::Char('9'));
h.key(Key::Enter);
assert!(h.board()[4].contains("ERROR: Number must be 0-36"));
assert!(h.board()[2].contains("Bets: (none)"));
}
fn bj_state(
session: &str,
phase: &str,
player: &[&str],
dealer: &[&str],
player_total: u8,
dealer_total: u8,
bet: u64,
base_bet: u64,
balance: i64,
actions: &[&str],
result: &str,
winnings: u64,
) -> BlackjackState {
BlackjackState {
session_id: session.to_string(),
phase: phase.to_string(),
player: player.iter().map(|card| card.to_string()).collect(),
dealer: dealer.iter().map(|card| card.to_string()).collect(),
player_total,
dealer_total,
bet,
base_bet,
balance,
actions: actions.iter().map(|action| action.to_string()).collect(),
result: result.to_string(),
winnings,
}
}
#[test]
fn blackjack_deals_hits_and_settles() {
let mut h = Harness::new();
h.enter_blackjack();
assert_eq!(h.board().len(), 6);
assert!(h.board()[4].contains("Press [SPACE] to deal."));
h.key(Key::Char(' '));
assert!(h
.sent
.iter()
.any(|line| line == "PLAY_START blackjack 10"));
let player = bj_state(
"bj1",
"player",
&["10S", "6H"],
&["9D", "?"],
16,
9,
10,
10,
990,
&["hit", "stand", "double"],
"playing",
0,
);
h.reply(&format!(
"BLACKJACK_STATE {}",
serde_json::to_string(&player).unwrap()
));
assert_eq!(h.board().len(), 6);
assert!(h.board()[2].contains("Dealer: [9♦] [ ? ] 9"));
assert!(h.board()[3].contains("You: [10♠] [6♥] 16"));
assert!(h.board()[4].contains("Hit, stand or double."));
assert!(h.board()[5].contains("[D]ouble"));
assert!(h.board()[5].contains("[Q] Forfeit"));
h.key(Key::Char('h'));
assert_eq!(
h.sent.last().map(String::as_str),
Some("BLACKJACK_ACTION bj1 hit")
);
let done = bj_state(
"bj1",
"done",
&["10S", "6H", "5D"],
&["9D", "8C"],
21,
17,
10,
10,
1010,
&[],
"win",
20,
);
h.reply(&format!(
"BLACKJACK_STATE {}",
serde_json::to_string(&done).unwrap()
));
assert!(h.board()[4].contains("Win! Won 20 PLN"));
assert!(h.board()[5].contains("[SPACE] New hand"));
h.key(Key::Char('q'));
assert!(h.board().is_empty());
assert!(h.screen.lines.iter().any(|line| line == "Game exited."));
}
#[test]
fn blackjack_forfeit_is_sent_when_leaving_an_active_hand() {
let mut h = Harness::new();
h.enter_blackjack();
h.key(Key::Char(' '));
let player = bj_state(
"bj1",
"player",
&["10S", "6H"],
&["9D", "?"],
16,
9,
10,
10,
990,
&["hit", "stand", "double"],
"playing",
0,
);
h.reply(&format!(
"BLACKJACK_STATE {}",
serde_json::to_string(&player).unwrap()
));
h.key(Key::Char('q'));
assert_eq!(
h.sent.last().map(String::as_str),
Some("BLACKJACK_ACTION bj1 forfeit")
);
let forfeited = bj_state(
"bj1",
"done",
&["10S", "6H"],
&["9D", "?"],
16,
9,
10,
10,
990,
&[],
"forfeit",
0,
);
h.reply(&format!(
"BLACKJACK_STATE {}",
serde_json::to_string(&forfeited).unwrap()
));
assert!(h.board().is_empty());
assert!(h
.screen
.lines
.iter()
.any(|line| line.contains("forfeited (10 PLN lost)")));
}
#[test]
fn blackjack_ready_board_can_change_the_next_bet() {
let mut h = Harness::new();
h.enter_blackjack();
h.key(Key::Char('='));
assert!(h.board()[1].contains("Bet: 20 PLN"));
h.key(Key::Char(' '));
assert!(h
.sent
.iter()
.any(|line| line == "PLAY_START blackjack 20"));
}
#[test]
fn parses_a_blackjack_state() {
let state = bj_state(
"bj1",
"player",
&["10S", "6H"],
&["9D", "?"],
16,
9,
10,
10,
990,
&["hit", "stand"],
"playing",
0,
);
let response = format!("BLACKJACK_STATE {}", serde_json::to_string(&state).unwrap());
assert_eq!(parse_blackjack_response(&response).unwrap(), state);
assert!(parse_blackjack_response("ERROR Insufficient balance").is_err());
}
|