2022-09-15 15:37:26 +00:00
|
|
|
use tokio::task;
|
|
|
|
|
2022-09-14 15:36:16 +00:00
|
|
|
use crate::args::Args;
|
2022-10-17 06:35:49 +00:00
|
|
|
use crate::consts::MIN_STRIKE_TIMEOUT;
|
2022-09-15 16:04:22 +00:00
|
|
|
use crate::data::{BoatsLayout, GameRules};
|
2022-10-17 06:35:49 +00:00
|
|
|
use crate::human_player_ws::ServerMessage;
|
2022-09-14 15:36:16 +00:00
|
|
|
use crate::server::start_server;
|
2022-09-16 14:55:29 +00:00
|
|
|
use crate::test::bot_client;
|
|
|
|
use crate::test::bot_client::ClientEndResult;
|
2022-09-19 17:29:11 +00:00
|
|
|
use crate::test::play_utils::check_no_replay_on_hit;
|
2022-09-14 15:36:16 +00:00
|
|
|
use crate::test::TestPort;
|
2022-10-01 18:44:01 +00:00
|
|
|
use crate::utils::network_utils::wait_for_port;
|
2022-09-14 15:36:16 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn invalid_port() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
bot_client::BotClient::new(TestPort::RandomBotClientInvalidPort.as_url())
|
2022-09-16 14:55:29 +00:00
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap_err();
|
2022-09-14 15:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn invalid_rules() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
|
|
|
let mut rules = GameRules::random_players_rules();
|
|
|
|
rules.map_width = 0;
|
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
task::spawn_local(start_server(Args::for_test(
|
|
|
|
TestPort::RandomBotClientInvalidRules,
|
|
|
|
)));
|
|
|
|
wait_for_port(TestPort::RandomBotClientInvalidRules.port()).await;
|
2022-09-14 15:36:16 +00:00
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
bot_client::BotClient::new(TestPort::RandomBotClientInvalidRules.as_url())
|
2022-09-16 14:55:29 +00:00
|
|
|
.with_rules(rules.clone())
|
|
|
|
.with_layout(
|
|
|
|
BoatsLayout::gen_random_for_rules(&GameRules::random_players_rules()).unwrap(),
|
|
|
|
)
|
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap_err();
|
2022-09-14 15:36:16 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
2022-09-14 16:02:11 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn full_game() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
2022-09-19 17:29:11 +00:00
|
|
|
task::spawn_local(start_server(Args::for_test(TestPort::RandomBotFullGame)));
|
|
|
|
wait_for_port(TestPort::RandomBotFullGame.port()).await;
|
2022-09-14 16:02:11 +00:00
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
let res = bot_client::BotClient::new(TestPort::RandomBotFullGame.as_url())
|
2022-09-16 14:55:29 +00:00
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-22 16:12:23 +00:00
|
|
|
assert!(matches!(res, ClientEndResult::Finished { .. }));
|
2022-09-15 15:37:26 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn full_game_no_touching_boats() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
|
|
|
let mut rules = GameRules::random_players_rules();
|
|
|
|
rules.boats_can_touch = false;
|
|
|
|
task::spawn_local(start_server(Args::for_test(
|
2022-09-19 17:29:11 +00:00
|
|
|
TestPort::RandomBotFullGameNoTouchingBoats,
|
2022-09-15 15:37:26 +00:00
|
|
|
)));
|
2022-09-19 17:29:11 +00:00
|
|
|
wait_for_port(TestPort::RandomBotFullGameNoTouchingBoats.port()).await;
|
2022-09-15 15:37:26 +00:00
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
let res =
|
|
|
|
bot_client::BotClient::new(TestPort::RandomBotFullGameNoTouchingBoats.as_url())
|
|
|
|
.with_rules(rules)
|
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-15 16:04:22 +00:00
|
|
|
|
2022-09-22 16:12:23 +00:00
|
|
|
assert!(matches!(res, ClientEndResult::Finished { .. }));
|
2022-09-14 16:02:11 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
2022-09-15 17:56:11 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn invalid_boats_layout_number_of_boats() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
|
|
|
let mut rules = GameRules::random_players_rules();
|
|
|
|
rules.boats_can_touch = false;
|
|
|
|
task::spawn_local(start_server(Args::for_test(
|
2022-09-19 17:29:11 +00:00
|
|
|
TestPort::RandomBotInvalidBoatsLayoutNumberOfBoats,
|
2022-09-15 17:56:11 +00:00
|
|
|
)));
|
2022-09-19 17:29:11 +00:00
|
|
|
wait_for_port(TestPort::RandomBotInvalidBoatsLayoutNumberOfBoats.port()).await;
|
2022-09-15 17:56:11 +00:00
|
|
|
|
|
|
|
let mut rules_modified = rules.clone();
|
2022-09-21 14:37:34 +00:00
|
|
|
rules_modified.remove_last_boat();
|
2022-09-15 17:56:11 +00:00
|
|
|
let layout = BoatsLayout::gen_random_for_rules(&rules_modified).unwrap();
|
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
let res = bot_client::BotClient::new(
|
|
|
|
&TestPort::RandomBotInvalidBoatsLayoutNumberOfBoats.as_url(),
|
|
|
|
)
|
|
|
|
.with_rules(rules)
|
|
|
|
.with_layout(layout)
|
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-15 17:56:11 +00:00
|
|
|
|
|
|
|
assert_eq!(res, ClientEndResult::InvalidBoatsLayout);
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn invalid_boats_layout_len_of_a_boat() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
|
|
|
let mut rules = GameRules::random_players_rules();
|
|
|
|
rules.boats_can_touch = false;
|
|
|
|
task::spawn_local(start_server(Args::for_test(
|
2022-09-19 17:29:11 +00:00
|
|
|
TestPort::RandomBotInvalidBoatsLayoutLenOfABoat,
|
2022-09-15 17:56:11 +00:00
|
|
|
)));
|
2022-09-19 17:29:11 +00:00
|
|
|
wait_for_port(TestPort::RandomBotInvalidBoatsLayoutLenOfABoat.port()).await;
|
2022-09-15 17:56:11 +00:00
|
|
|
|
|
|
|
let mut rules_modified = rules.clone();
|
2022-09-21 14:37:34 +00:00
|
|
|
let previous = rules_modified.remove_last_boat();
|
2022-09-15 17:56:11 +00:00
|
|
|
rules_modified.add_boat(previous - 1);
|
|
|
|
let layout = BoatsLayout::gen_random_for_rules(&rules_modified).unwrap();
|
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
let res = bot_client::BotClient::new(
|
|
|
|
&TestPort::RandomBotInvalidBoatsLayoutLenOfABoat.as_url(),
|
|
|
|
)
|
|
|
|
.with_rules(rules)
|
|
|
|
.with_layout(layout)
|
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-15 17:56:11 +00:00
|
|
|
|
|
|
|
assert_eq!(res, ClientEndResult::InvalidBoatsLayout);
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
2022-09-16 14:55:29 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn full_game_multiple_rematches() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
2022-09-17 10:02:13 +00:00
|
|
|
task::spawn_local(start_server(Args::for_test(
|
2022-09-19 17:29:11 +00:00
|
|
|
TestPort::RandomBotFullGameMultipleRematch,
|
|
|
|
)));
|
|
|
|
wait_for_port(TestPort::RandomBotFullGameMultipleRematch.port()).await;
|
|
|
|
|
|
|
|
let res =
|
|
|
|
bot_client::BotClient::new(TestPort::RandomBotFullGameMultipleRematch.as_url())
|
|
|
|
.with_number_plays(5)
|
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-22 16:12:23 +00:00
|
|
|
assert!(matches!(res, ClientEndResult::Finished { .. }));
|
2022-09-19 17:29:11 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn full_game_no_replay_on_hit() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
|
|
|
task::spawn_local(start_server(Args::for_test(
|
|
|
|
TestPort::RandomBotNoReplayOnHit,
|
2022-09-17 10:02:13 +00:00
|
|
|
)));
|
2022-09-19 17:29:11 +00:00
|
|
|
wait_for_port(TestPort::RandomBotNoReplayOnHit.port()).await;
|
2022-09-16 14:55:29 +00:00
|
|
|
|
2022-09-19 17:29:11 +00:00
|
|
|
let res = bot_client::BotClient::new(TestPort::RandomBotNoReplayOnHit.as_url())
|
|
|
|
.with_rules(GameRules::random_players_rules().with_player_continue_on_hit(false))
|
|
|
|
.with_server_msg_callback(check_no_replay_on_hit)
|
2022-09-16 14:55:29 +00:00
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-09-22 16:12:23 +00:00
|
|
|
assert!(matches!(res, ClientEndResult::Finished { .. }));
|
2022-09-16 14:55:29 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
2022-10-17 06:35:49 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn check_fire_time_out() {
|
|
|
|
let _ = env_logger::builder().is_test(true).try_init();
|
|
|
|
|
|
|
|
let local_set = task::LocalSet::new();
|
|
|
|
local_set
|
|
|
|
.run_until(async move {
|
|
|
|
task::spawn_local(start_server(Args::for_test(TestPort::RandomCheckTimeout)));
|
|
|
|
wait_for_port(TestPort::RandomCheckTimeout.port()).await;
|
|
|
|
|
|
|
|
let mut did_skip_one = false;
|
|
|
|
let res = bot_client::BotClient::new(TestPort::RandomCheckTimeout.as_url())
|
|
|
|
.with_rules(
|
|
|
|
GameRules::random_players_rules().with_strike_timeout(MIN_STRIKE_TIMEOUT),
|
|
|
|
)
|
|
|
|
.with_server_msg_callback(move |msg| {
|
|
|
|
if matches!(msg, ServerMessage::RequestFire { .. }) && !did_skip_one {
|
|
|
|
*msg = ServerMessage::OpponentReplacedByBot;
|
|
|
|
did_skip_one = true;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.run_client()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(matches!(res, ClientEndResult::Finished { .. }));
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|