SeaBattle/sea_battle_backend/src/test/bot_client_bot_play.rs

170 lines
5.4 KiB
Rust
Raw Normal View History

2022-09-15 15:37:26 +00:00
use tokio::task;
2022-09-14 15:36:16 +00:00
use crate::args::Args;
2022-09-15 16:04:22 +00:00
use crate::data::{BoatsLayout, GameRules};
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-14 15:36:16 +00:00
use crate::test::network_utils::wait_for_port;
use crate::test::TestPort;
#[tokio::test]
async fn invalid_port() {
let _ = env_logger::builder().is_test(true).try_init();
2022-09-16 14:55:29 +00:00
bot_client::BotClient::new(TestPort::ClientInvalidPort.as_url())
.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;
task::spawn_local(start_server(Args::for_test(TestPort::ClientInvalidRules)));
wait_for_port(TestPort::ClientInvalidRules.port()).await;
2022-09-16 14:55:29 +00:00
bot_client::BotClient::new(TestPort::ClientInvalidRules.as_url())
.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-15 15:37:26 +00:00
task::spawn_local(start_server(Args::for_test(TestPort::FullGame)));
wait_for_port(TestPort::FullGame.port()).await;
2022-09-14 16:02:11 +00:00
2022-09-16 14:55:29 +00:00
let res = bot_client::BotClient::new(TestPort::FullGame.as_url())
.run_client()
.await
.unwrap();
2022-09-15 16:04:22 +00:00
assert_eq!(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(
TestPort::FullGameTouchingBoats,
)));
wait_for_port(TestPort::FullGameTouchingBoats.port()).await;
2022-09-16 14:55:29 +00:00
let res = bot_client::BotClient::new(TestPort::FullGameTouchingBoats.as_url())
.with_rules(rules)
.run_client()
.await
.unwrap();
2022-09-15 16:04:22 +00:00
assert_eq!(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(
TestPort::FullGameTouchingBoats,
)));
wait_for_port(TestPort::FullGameTouchingBoats.port()).await;
let mut rules_modified = rules.clone();
rules_modified.pop_boat();
let layout = BoatsLayout::gen_random_for_rules(&rules_modified).unwrap();
2022-09-16 14:55:29 +00:00
let res = bot_client::BotClient::new(&TestPort::FullGameTouchingBoats.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(
TestPort::FullGameTouchingBoats,
)));
wait_for_port(TestPort::FullGameTouchingBoats.port()).await;
let mut rules_modified = rules.clone();
let previous = rules_modified.pop_boat();
rules_modified.add_boat(previous - 1);
let layout = BoatsLayout::gen_random_for_rules(&rules_modified).unwrap();
2022-09-16 14:55:29 +00:00
let res = bot_client::BotClient::new(&TestPort::FullGameTouchingBoats.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 {
task::spawn_local(start_server(Args::for_test(TestPort::FullGame)));
wait_for_port(TestPort::FullGame.port()).await;
let res = bot_client::BotClient::new(TestPort::FullGame.as_url())
.with_number_plays(5)
.run_client()
.await
.unwrap();
assert_eq!(res, ClientEndResult::Finished);
})
.await;
}