clash/server/tests/tactics_card_tests.rs
coyotte508 e69d7148fb Fix tactical retreat, destroyed wonder draws, and Defiant Stand journal
Release viewer 0.1.222 and engine 0.4.155. Destroyed wonders are tracked publicly; saves from before this release cannot reconstruct Wonders already destroyed.
2026-11-18 17:13:57 +00:00

483 lines
15 KiB
Rust

use crate::common::{TestAction, move_action};
use common::JsonTest;
use server::action::Action;
use server::card::HandCard;
use server::content::persistent_events::EventResponse;
use server::position::Position;
mod common;
const JSON: JsonTest = JsonTest::new("tactics_cards");
#[test]
fn test_peltasts() {
JSON.test(
"peltasts",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
1,
)])),
),
],
);
}
#[test]
fn test_encircled() {
JSON.test(
"encircled",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
2,
)])),
),
],
);
}
#[test]
fn test_wedge_formation() {
JSON.test(
"wedge_formation",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))).skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
5,
)])),
),
],
);
}
#[test]
fn test_high_morale() {
JSON.test(
"high_morale",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))).skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
6,
)])),
),
],
);
}
#[test]
fn test_heavy_resistance() {
JSON.test(
"heavy_resistance",
vec![
TestAction::not_undoable(0, move_action(vec![0], Position::from_offset("C1")))
.skip_json(),
TestAction::not_undoable(
1,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
7,
)])),
),
],
);
}
#[test]
fn heavy_resistance_can_resolve_a_battle_at_a_wonder_city() {
use server::content::custom_actions::CustomActionType;
use server::game_api;
use server::wonder::Wonder;
let mut game = JSON.load_game("heavy_resistance");
let city = Position::from_offset("C1");
let wonders = vec![Wonder::GreatLibrary, Wonder::GreatLighthouse];
game.players[0].action_cards.clear();
game.players[1].action_cards = vec![7];
game.players[1].units.truncate(1);
game.players[1].get_city_mut(city).pieces.wonders = wonders.clone();
game.players[1].wonders_built = wonders.clone();
// Two strong attacker rolls, then a low defender roll. Heavy Resistance
// applies normally; resolving the round must also transfer the wonders.
game.dice_roll_outcomes = vec![0, 11, 11];
let game = game.clone(); // initialize ownership and wonder abilities
let game = game_api::execute(game, move_action(vec![0, 2], city), 0);
let game = game_api::execute(
game,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
7,
)])),
1,
);
assert!(game.player(1).try_get_city(city).is_none());
assert_eq!(game.player(0).get_city(city).pieces.wonders, wonders);
assert_eq!(
game.player(1).wonders_built,
wonders,
"builder credit is retained"
);
for wonder in &wonders {
assert!(game.player(0).wonders_owned.contains(*wonder));
assert!(!game.player(1).wonders_owned.contains(*wonder));
}
for ability in [CustomActionType::GreatLibrary] {
assert!(game.player(0).special_actions.contains_key(
&server::content::custom_actions::SpecialAction::Custom(ability)
));
assert!(!game.player(1).special_actions.contains_key(
&server::content::custom_actions::SpecialAction::Custom(ability)
));
}
let restored = game.clone();
assert_eq!(
restored.player(0).wonders_owned,
game.player(0).wonders_owned
);
assert_eq!(
restored.player(1).wonders_owned,
game.player(1).wonders_owned
);
}
#[test]
fn test_high_ground() {
JSON.test(
"high_ground",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))).skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
1,
)])),
)
.skip_json(),
TestAction::not_undoable(
1,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
9,
)])),
),
],
);
}
#[test]
fn test_surprise() {
JSON.test(
"surprise",
vec![
TestAction::not_undoable(0, move_action(vec![0], Position::from_offset("C1")))
.skip_json(),
TestAction::not_undoable(
1,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
10,
)])),
),
],
);
}
#[test]
fn test_siege() {
JSON.test(
"siege",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))).skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
11,
)])),
),
],
);
}
#[test]
fn test_scout() {
JSON.test(
"scout",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))).skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
1,
)])),
)
.skip_json(),
TestAction::not_undoable(
1,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
23,
)])),
),
],
);
}
#[test]
fn test_martyr() {
JSON.test(
"martyr",
vec![
TestAction::undoable(0, move_action(vec![7, 8], Position::from_offset("D2")))
.skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
6,
)])),
)
.skip_json(),
TestAction::not_undoable(
1,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
24,
)])),
)
.skip_json(),
TestAction::undoable(0, Action::Response(EventResponse::SelectUnits(vec![7])))
.skip_json(),
TestAction::not_undoable(0, Action::Response(EventResponse::SelectUnits(vec![2]))),
],
);
}
#[test]
fn test_martyr2() {
JSON.test(
"martyr2",
vec![
TestAction::not_undoable(0, Action::Response(EventResponse::SelectUnits(vec![10])))
.skip_json(),
TestAction::not_undoable(1, Action::Response(EventResponse::SelectUnits(vec![12]))),
],
);
}
#[test]
fn test_archers() {
JSON.test(
"archers",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))).skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
26,
)])),
)
.skip_json(),
TestAction::not_undoable(1, Action::Response(EventResponse::SelectUnits(vec![1]))),
],
);
}
#[test]
fn test_flanking() {
JSON.test(
"flanking",
vec![
TestAction::undoable(0, move_action(vec![0], Position::from_offset("C1"))).skip_json(),
TestAction::not_undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
42,
)])),
),
],
);
}
#[test]
fn fortress_without_army_cannot_use_tactical_retreat_even_from_a_saved_prompt() {
use server::content::persistent_events::PersistentEventRequest;
use server::game_api;
let city = Position::from_offset("C1");
let mut setup = JSON.load_game("heavy_resistance");
setup.players[0].action_cards.clear();
setup.players[1].action_cards = vec![19, 25];
setup.players[1].units.clear();
setup.players[1].get_city_mut(city).pieces.fortress = Some(1);
let mut game = game_api::execute(setup, move_action(vec![0], city), 0);
let view = server::web_view::view(&game, Some(1));
let options = view["decision"]["options"].as_array().unwrap();
assert_eq!(options.len(), 1);
assert_eq!(options[0]["value"]["ActionCard"], 25);
// An already pending request may still contain choices saved by the old engine.
let handler = game
.events
.last_mut()
.unwrap()
.player
.handler
.as_mut()
.unwrap();
let PersistentEventRequest::SelectHandCards(request) = &mut handler.request else {
panic!("Expected tactics choice");
};
request.choices.push(HandCard::ActionCard(19));
let view = server::web_view::view(&game, Some(1));
assert_eq!(view["decision"]["options"].as_array().unwrap().len(), 1);
assert!(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
game_api::execute(
game,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
19,
)])),
1,
)
}))
.is_err()
);
}
fn retreat_game(defender_army: usize) -> server::game::Game {
use server::unit::{Unit, UnitType};
let city = Position::from_offset("C1");
let destination = Position::from_offset("B1");
let mut setup = JSON.load_game("heavy_resistance");
setup.players[0].action_cards.clear();
setup.players[0]
.advances
.insert(server::advance::Advance::Engineering);
setup.players[0].cities.clear();
setup.players[1].civilization = setup.cache.get_civilization("Persia");
setup.players[1].action_cards = vec![19];
setup.players[1].units.clear();
for id in 0..defender_army as u32 {
setup.players[1]
.units
.push(Unit::new(1, city, UnitType::Infantry, id));
}
setup.players[1]
.units
.push(Unit::new(1, city, UnitType::Settler, 10));
setup.players[1]
.units
.push(Unit::new(1, destination, UnitType::Infantry, 11));
setup.players[1].next_unit_id = 12;
setup.clone()
}
fn retreat_view(game: &server::game::Game) -> serde_json::Value {
server::web_view::view(game, Some(1))
}
#[test]
fn tactical_retreat_respects_stack_limit_and_moves_settlers() {
use server::game_api;
use server::unit::UnitType;
let city = Position::from_offset("C1");
let destination = Position::from_offset("B1");
let setup = retreat_game(1);
let game = game_api::execute(setup, move_action(vec![0], city), 0);
let view = retreat_view(&game);
assert_eq!(view["decision"]["options"].as_array().unwrap().len(), 1);
let game = game_api::execute(
game,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
19,
)])),
1,
);
let text = serde_json::to_value(game.cloned_data()).unwrap()["log"].to_string();
assert!(text.contains("withdraws to B1"));
assert_eq!(game.player(1).get_units(destination).len(), 3);
assert!(game.player(1).get_units(city).is_empty());
assert!(
game.player(1)
.get_units(destination)
.iter()
.any(|u| u.unit_type == UnitType::Settler)
);
}
#[test]
fn tactical_retreat_is_unavailable_when_every_destination_would_overflow() {
use server::game_api;
use server::unit::{Unit, UnitType};
let city = Position::from_offset("C1");
let other = Position::from_offset("B2");
let mut setup = retreat_game(2);
setup.players[1]
.units
.push(Unit::new(1, other, UnitType::Infantry, 12));
setup.players[1]
.units
.push(Unit::new(1, other, UnitType::Infantry, 13));
setup.players[1]
.units
.push(Unit::new(1, other, UnitType::Infantry, 14));
let game = game_api::execute(setup, move_action(vec![0], city), 0);
let game = game_api::execute(
game,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
19,
)])),
1,
);
let text = serde_json::to_value(game.cloned_data()).unwrap()["log"].to_string();
assert!(text.contains("withdraws to"));
assert!(game.player(1).get_units(city).is_empty());
}
#[test]
fn high_ground_is_not_skipped_after_attacker_flanking() {
let mut game = JSON.load_game("high_ground");
game.players[0].action_cards = vec![42];
game.players[1].action_cards = vec![40];
for (player, action) in [
(0, move_action(vec![0], Position::from_offset("C1"))),
(
0,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
42,
)])),
),
(
1,
Action::Response(EventResponse::SelectHandCards(vec![HandCard::ActionCard(
40,
)])),
),
] {
game = server::game_api::execute(game, action, player);
}
let data = serde_json::to_value(game.data()).unwrap();
let text = data["log"].to_string();
assert!(text.contains("High Ground prevents opponent"));
assert!(text.contains("Flanking added 1 extra die"));
fn check(value: &serde_json::Value) {
if let Some(roll) = value.get("CombatRoll") {
if value["player"] == 0 {
assert!(
roll["rolls"]
.as_array()
.unwrap()
.iter()
.all(|r| r["bonus"] == false)
);
}
}
match value {
serde_json::Value::Array(a) => a.iter().for_each(check),
serde_json::Value::Object(o) => o.values().for_each(check),
_ => (),
}
}
check(&data["log"]);
}