SeaBattle/sea_battle_backend/src/test/client.rs

80 lines
2.2 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;
use crate::bot_client;
use crate::data::GameRules;
use crate::server::start_server;
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();
bot_client::run_client(
&TestPort::ClientInvalidPort.as_url(),
&GameRules::random_players_rules(),
)
.await
.unwrap_err();
}
#[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;
bot_client::run_client(&TestPort::ClientInvalidRules.as_url(), &rules)
.await
.unwrap_err();
})
.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 {
let rules = GameRules::random_players_rules();
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-15 15:37:26 +00:00
bot_client::run_client(&TestPort::FullGame.as_url(), &rules)
.await
.unwrap();
})
.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;
bot_client::run_client(&TestPort::FullGameTouchingBoats.as_url(), &rules)
2022-09-14 16:02:11 +00:00
.await
.unwrap();
})
.await;
}