mod support;
use fours::app::Action;
use fours::app::App;
use fours::app::Headline;
use fours::app::Notice;
use fours::app::input;
use fours::bot::Bot;
use fours::bot::Difficulty;
use fours::bot::RandomBot;
use fours::game::BoardDimensions;
use fours::game::Game;
use fours::game::GameState;
use fours::game::Player;
use ratatui::crossterm::event::KeyCode;
use ratatui::crossterm::event::KeyEvent;
use ratatui::crossterm::event::KeyEventKind;
use ratatui::crossterm::event::KeyModifiers;
use support::ScriptedBot;
use support::app_with_script;
use support::check_invariants;
fn after(actions: &[Action]) -> App {
let mut app = app_with_script(&[]);
for action in actions {
app.handle(*action);
}
app
}
#[test]
fn a_new_game_starts_in_the_middle_of_the_board() {
let app = after(&[]);
assert_eq!(app.selected_column(), 3);
assert_eq!(app.human_player(), Player::Red);
assert_eq!(app.state().current_player(), Player::Red);
assert!(app.is_running());
assert!(app.is_waiting_for_player());
assert_eq!(app.notice(), None);
}
#[test]
fn the_selection_moves_one_column_at_a_time() {
assert_eq!(after(&[Action::Previous]).selected_column(), 2);
assert_eq!(after(&[Action::Next]).selected_column(), 4);
assert_eq!(after(&[Action::Next, Action::Next]).selected_column(), 5);
assert_eq!(
after(&[Action::Previous, Action::Next]).selected_column(),
3
);
}
#[test]
fn the_selection_stops_at_the_edges_of_the_board() {
let left = vec![Action::Previous; 10];
let right = vec![Action::Next; 10];
assert_eq!(after(&left).selected_column(), 0);
assert_eq!(after(&right).selected_column(), 6);
}
#[test]
fn a_column_can_be_selected_directly() {
for column in 0..7 {
assert_eq!(after(&[Action::Choose(column)]).selected_column(), column);
}
}
#[test]
fn a_column_outside_the_board_is_ignored() {
for column in [7, 8, 100, usize::MAX] {
assert_eq!(after(&[Action::Choose(column)]).selected_column(), 3);
}
}
#[test]
fn placing_a_disc_lets_the_opponent_answer() {
let mut app = app_with_script(&[6]);
app.handle(Action::Choose(2));
app.handle(Action::Confirm);
assert_eq!(app.game().move_history(), [2, 6]);
assert_eq!(app.state().current_player(), Player::Red);
assert!(app.is_waiting_for_player());
check_invariants(app.state());
}
#[test]
fn the_opponent_does_not_answer_a_winning_move() {
let mut app = app_with_script(&[6, 6, 6]);
for _ in 0..4 {
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
}
assert_eq!(app.state().winner(), Some(Player::Red));
assert_eq!(app.game().move_history(), [0, 6, 0, 6, 0, 6, 0]);
}
#[test]
fn placing_a_disc_in_a_full_column_is_refused() {
let mut app =
support::playing_against(support::play(&[0, 0, 0, 0, 0, 0]), ScriptedBot::new(&[1]));
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
assert_eq!(app.notice(), Some(Notice::ColumnFull));
assert_eq!(app.state().disc_count(), 6);
}
#[test]
fn placing_a_disc_after_the_game_ended_is_refused() {
let mut app = app_with_script(&[6, 6, 6]);
for _ in 0..4 {
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
}
let discs = app.state().disc_count();
app.handle(Action::Confirm);
assert_eq!(app.notice(), Some(Notice::GameFinished));
assert_eq!(app.state().disc_count(), discs);
}
#[test]
fn a_remark_lasts_until_the_next_action() {
let mut app =
support::playing_against(support::play(&[0, 0, 0, 0, 0, 0]), ScriptedBot::new(&[1]));
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
assert!(app.notice().is_some());
app.handle(Action::Next);
assert_eq!(app.notice(), None);
}
#[test]
fn restarting_clears_the_board_and_the_selection() {
let mut app = app_with_script(&[1, 2, 3]);
app.handle(Action::Choose(6));
app.handle(Action::Confirm);
app.handle(Action::Restart);
assert_eq!(app.state(), &GameState::new());
assert!(app.game().move_history().is_empty());
assert_eq!(app.selected_column(), 3);
assert_eq!(app.notice(), None);
assert!(app.is_running());
}
#[test]
fn quitting_stops_the_game() {
let app = after(&[Action::Quit]);
assert!(!app.is_running());
}
#[test]
fn the_headline_says_what_the_game_is_waiting_for() {
let mut app = app_with_script(&[6, 6, 6]);
assert_eq!(app.headline(), Headline::Turn(Player::Red));
assert_eq!(app.headline_text(), "Your turn");
for _ in 0..4 {
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
}
assert_eq!(app.headline(), Headline::Won(Player::Red));
assert_eq!(app.headline_text(), "You win!");
assert_eq!(app.headline().player(), Some(Player::Red));
}
#[test]
fn the_headline_names_the_opponent_when_it_wins() {
let almost_lost = support::play(&[3, 0, 3, 0, 4, 0]);
let mut app = support::playing_against(almost_lost, ScriptedBot::new(&[0]));
app.handle(Action::Choose(6));
app.handle(Action::Confirm);
assert_eq!(app.headline(), Headline::Won(Player::Yellow));
assert_eq!(app.headline_text(), "Scripted bot wins");
}
#[test]
fn the_headline_reports_a_draw() {
let dimensions = BoardDimensions::new(2, 2).expect("a two by two board is playable");
let mut app =
support::playing_against(Game::with_dimensions(dimensions), ScriptedBot::new(&[0, 1]));
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
app.handle(Action::Choose(1));
app.handle(Action::Confirm);
assert_eq!(app.headline(), Headline::Draw);
assert_eq!(app.headline_text(), "A draw, the board is full");
assert_eq!(app.headline().player(), None);
}
#[test]
fn a_whole_game_against_the_random_bot_stays_legal() {
for seed in 0..200 {
let mut app = App::seeded(BoardDimensions::STANDARD, seed);
app.set_difficulty(Difficulty::Easy);
let mut safety = 0;
while !app.state().is_over() {
app.handle(Action::Choose(
usize::try_from(seed).unwrap_or_default() % 7,
));
app.handle(Action::Confirm);
if app.notice().is_some() {
app.handle(Action::Next);
app.handle(Action::Previous);
app.handle(Action::Choose(
app.state()
.legal_moves()
.first()
.copied()
.unwrap_or_default(),
));
app.handle(Action::Confirm);
}
check_invariants(app.state());
safety += 1;
assert!(safety <= 42, "a game should not outlast the board");
}
assert!(app.state().is_over());
}
}
#[test]
fn the_random_bot_only_plays_legal_moves() {
for seed in 0..50 {
let mut bot = RandomBot::seeded(seed);
let mut state = GameState::new();
while !state.is_over() {
let column = bot.choose_move(&state).expect("the game is not over");
assert!(state.is_playable(column));
state.apply_move(column).expect("the move is legal");
}
assert_eq!(bot.choose_move(&state), None);
assert_eq!(bot.name(), "Random bot");
}
}
#[test]
fn a_seeded_bot_plays_the_same_game_twice() {
let play = |seed| {
let mut app = App::seeded(BoardDimensions::STANDARD, seed);
app.set_difficulty(Difficulty::Easy);
while !app.state().is_over() {
app.handle(Action::Choose(app.state().legal_moves()[0]));
app.handle(Action::Confirm);
}
app.game().move_history().to_vec()
};
assert_eq!(play(11), play(11));
assert_ne!(play(11), play(12));
}
#[test]
fn keys_ask_for_the_action_they_look_like() {
check_bindings(&[
(KeyCode::Left, Action::Previous),
(KeyCode::Char('h'), Action::Previous),
(KeyCode::Right, Action::Next),
(KeyCode::Char('l'), Action::Next),
(KeyCode::Enter, Action::Confirm),
(KeyCode::Char(' '), Action::Confirm),
(KeyCode::Down, Action::Confirm),
(KeyCode::Char('j'), Action::Confirm),
(KeyCode::Char('d'), Action::CycleDifficulty),
(KeyCode::Char('D'), Action::CycleDifficulty),
(KeyCode::Char('r'), Action::Restart),
(KeyCode::Char('R'), Action::Restart),
(KeyCode::Char('q'), Action::Quit),
(KeyCode::Esc, Action::Quit),
(KeyCode::Char('1'), Action::Choose(0)),
(KeyCode::Char('7'), Action::Choose(6)),
]);
}
#[track_caller]
fn check_bindings(bindings: &[(KeyCode, Action)]) {
for (code, expected) in bindings {
assert_eq!(
input::action_for(KeyEvent::from(*code)),
Some(*expected),
"{code:?} should ask for {expected:?}"
);
}
}
#[test]
fn keys_without_a_meaning_are_ignored() {
for code in [
KeyCode::Char('z'),
KeyCode::Char('0'),
KeyCode::F(5),
KeyCode::Home,
KeyCode::PageUp,
] {
assert_eq!(input::action_for(KeyEvent::from(code)), None);
}
}
#[test]
fn the_interrupt_key_leaves_the_game() {
let interrupt = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
assert_eq!(input::action_for(interrupt), Some(Action::Quit));
}
#[test]
fn releasing_a_key_does_nothing() {
let release =
KeyEvent::new_with_kind(KeyCode::Enter, KeyModifiers::NONE, KeyEventKind::Release);
assert_eq!(input::action_for(release), None);
}
#[test]
fn every_key_binding_can_be_pressed_at_any_time() {
let mut app = app_with_script(&[]);
let codes = [
KeyCode::Left,
KeyCode::Right,
KeyCode::Enter,
KeyCode::Char('r'),
KeyCode::Char('1'),
KeyCode::Char('7'),
KeyCode::Char('d'),
];
for _ in 0..20 {
for code in codes {
if let Some(action) = input::action_for(KeyEvent::from(code)) {
app.handle(action);
check_invariants(app.state());
}
}
}
assert!(app.is_running());
}
fn playing() -> App {
App::seeded(BoardDimensions::STANDARD, 4)
}
#[test]
fn a_game_is_under_way_from_the_start() {
let app = playing();
assert_eq!(app.difficulty(), Difficulty::default());
assert_eq!(app.state().disc_count(), 0);
assert!(app.is_waiting_for_player());
assert!(app.is_running());
}
#[test]
fn the_level_can_be_changed_without_disturbing_the_game() {
let mut app = playing();
app.handle(Action::Confirm);
let board = app.state().to_string();
let discs = app.state().disc_count();
app.handle(Action::SetDifficulty(Difficulty::Hard));
assert_eq!(app.difficulty(), Difficulty::Hard);
assert_eq!(app.opponent().difficulty(), Difficulty::Hard);
assert_eq!(
app.state().to_string(),
board,
"the board should be left alone"
);
assert_eq!(app.state().disc_count(), discs);
assert!(app.is_waiting_for_player());
}
#[test]
fn a_new_opponent_answers_the_next_move() {
let mut app = playing();
app.handle(Action::SetDifficulty(Difficulty::Easy));
let easy = app.opponent().name();
app.handle(Action::SetDifficulty(Difficulty::Hard));
assert_ne!(app.opponent().name(), easy);
app.handle(Action::Confirm);
assert_eq!(app.state().disc_count(), 2, "the new opponent answered");
check_invariants(app.state());
}
#[test]
fn the_levels_come_round_again() {
let mut app = playing();
app.handle(Action::SetDifficulty(Difficulty::Easy));
for expected in [
Difficulty::Medium,
Difficulty::Hard,
Difficulty::Easy,
Difficulty::Medium,
] {
app.handle(Action::CycleDifficulty);
assert_eq!(app.difficulty(), expected);
}
}
#[test]
fn a_level_can_be_changed_after_the_game_ended() {
let mut app = app_with_script(&[6, 6, 6]);
for _ in 0..4 {
app.handle(Action::Choose(0));
app.handle(Action::Confirm);
}
assert!(app.state().is_over());
app.handle(Action::CycleDifficulty);
assert!(app.state().is_over(), "changing level does not undo a win");
app.handle(Action::Restart);
assert!(!app.state().is_over());
}
#[test]
fn restarting_keeps_the_level() {
let mut app = playing();
app.handle(Action::SetDifficulty(Difficulty::Hard));
app.handle(Action::Confirm);
app.handle(Action::Restart);
assert_eq!(app.difficulty(), Difficulty::Hard);
assert_eq!(app.opponent().difficulty(), Difficulty::Hard);
assert_eq!(app.state().disc_count(), 0);
}