clash/server/tests/wonder_tests.rs
coyotte508 6a6829ec92 Fix native CI and keep snapshot checks read-only
Handle private queue actions in the native log renderer's exhaustive match. Keep CI fixtures read-only to prevent parallel readers seeing truncated files and to fail snapshot mismatches directly.

Refresh the stale Epic-length and Conversion-log expectations, and strengthen Great Gardens coverage with legal-target and positive-control assertions.
2026-11-02 20:54:13 +00:00

408 lines
15 KiB
Rust

use crate::common::{
JsonTest, TestAction, advance_action, custom_action, move_action, payment_response,
};
use server::action::{Action, execute_without_undo};
use server::advance::Advance;
use server::card::HandCard;
use server::content::custom_actions::{CustomAction, CustomActionType};
use server::content::persistent_events::EventResponse;
use server::playing_actions::PlayingAction;
use server::playing_actions::PlayingAction::WonderCard;
use server::position::Position;
use server::resource_pile::ResourcePile;
use server::wonder::Wonder;
mod common;
const JSON: JsonTest = JsonTest::new("wonders");
#[test]
fn test_colosseum() {
JSON.test(
"colosseum",
vec![
TestAction::undoable(0, Action::Playing(WonderCard(Wonder::Colosseum))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::new(3, 4, 5, 0, 0, 0, 5)))
.skip_json(),
TestAction::undoable(
0,
Action::Playing(PlayingAction::Custom(CustomAction::new(
CustomActionType::Sports,
Some(Position::from_offset("C2")),
))),
)
.skip_json(),
TestAction::undoable(
0,
// mood payment is only possible because of colosseum
payment_response(ResourcePile::mood_tokens(1)),
)
.skip_json(),
TestAction::not_undoable(
0,
move_action(vec![0, 1, 2, 3], Position::from_offset("C1")),
)
.skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::culture_tokens(1))).skip_json(),
TestAction::undoable(0, Action::Response(EventResponse::SelectUnits(vec![0]))),
],
);
}
#[test]
fn test_pyramids() {
JSON.test(
"pyramids",
vec![
TestAction::undoable(0, Action::Playing(WonderCard(Wonder::Pyramids))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::new(2, 3, 6, 0, 1, 0, 5)))
.skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectHandCards(vec![
HandCard::ObjectiveCard(32),
])),
),
],
);
}
#[test]
fn test_library() {
JSON.test(
"library",
vec![
TestAction::undoable(0, custom_action(CustomActionType::GreatLibrary)).skip_json(),
TestAction::undoable(
0,
Action::Response(EventResponse::SelectAdvance(Advance::Engineering)),
)
.skip_json(),
// can use effect to build a wonder - but don't draw a wonder card (one time ability)
TestAction::undoable(0, Action::Playing(WonderCard(Wonder::Pyramids))),
],
);
}
#[test]
fn test_library_borrowed_effect_survives_reload() {
let mut game = JSON.load_game("library");
game.player_mut(0).resources.mood_tokens = 0;
let game = server::game_api::execute(game, custom_action(CustomActionType::GreatLibrary), 0);
let game = server::game_api::execute(
game,
Action::Response(EventResponse::SelectAdvance(Advance::FreeEducation)),
0,
);
// Clone rebuilds runtime listeners from saved data, just like a new request.
let game = game.clone();
assert_eq!(game.player(0).resources.mood_tokens, 0);
let game = server::game_api::execute(
game,
advance_action(Advance::Storage, ResourcePile::ideas(2)),
0,
);
assert!(game.current_event_handler().is_some());
let game = server::game_api::execute(game.clone(), payment_response(ResourcePile::ideas(1)), 0);
assert_eq!(game.player(0).resources.mood_tokens, 2);
let game = server::game_api::execute(game.clone(), Action::Playing(PlayingAction::EndTurn), 0);
assert_eq!(game.player(0).great_library_advance, None);
assert_ne!(game.current_player_index, 0);
}
#[test]
fn test_lighthouse() {
let game = JSON.load_game("lighthouse");
let game = server::game_api::execute(
game,
Action::Playing(WonderCard(Wonder::GreatLighthouse)),
0,
);
let game = server::game_api::execute(
game,
payment_response(ResourcePile::new(3, 5, 4, 0, 0, 0, 5)),
0,
);
let city = game
.player(0)
.cities
.iter()
.find(|c| c.pieces.wonders.contains(&Wonder::GreatLighthouse))
.unwrap();
let activations = city.activations;
let position = city.position;
let actions = game.actions_left;
let ships = game
.player(0)
.units
.iter()
.filter(|u| u.unit_type.is_ship())
.count();
let game = server::game_api::execute(
game,
Action::Response(EventResponse::SelectPositions(vec![Position::from_offset(
"C3",
)])),
0,
);
assert_eq!(game.player(0).get_city(position).activations, activations);
assert_eq!(game.actions_left, actions);
assert_eq!(
game.player(0)
.units
.iter()
.filter(|u| u.unit_type.is_ship())
.count(),
ships + 1
);
assert!(!game.player(0).special_actions.contains_key(
&server::content::custom_actions::SpecialAction::Custom(CustomActionType::GreatLighthouse)
));
}
#[test]
fn test_great_gardens() {
JSON.test(
"great_gardens",
vec![
TestAction::undoable(0, Action::Playing(WonderCard(Wonder::GreatGardens))).skip_json(),
TestAction::undoable(0, payment_response(ResourcePile::new(5, 5, 2, 0, 0, 0, 5)))
.skip_json(),
TestAction::not_undoable(0, Action::Playing(PlayingAction::EndTurn)).skip_json(),
TestAction::undoable(1, move_action(vec![0, 1], Position::from_offset("B1")))
.with_post_assert(|mut game| {
use server::movement::{MovementRestriction, possible_move_routes};
let start = Position::from_offset("B1");
let destination = Position::from_offset("A1");
let can_attack = |game: &server::game::Game| {
possible_move_routes(game.player(1), game, &[0, 1], start, None)
.unwrap()
.iter()
.any(|route| route.destination == destination)
};
assert!(
!can_attack(&game),
"Great Gardens must not be a legal target"
);
let error = execute_without_undo(
&mut game.clone(),
move_action(vec![0, 1], destination),
1,
)
.unwrap_err();
// Route selection now wraps the specific rejection with
// destination/payment diagnostics. Keep checking the rule.
assert!(error.contains("fertile movement attack great gardens restriction"));
// Positive control: remove only the Grassland restriction,
// leaving the same units, target, route and payment.
for id in [0, 1] {
let unit = game.player_mut(1).get_unit_mut(id);
assert!(
unit.movement_restrictions
.contains(&MovementRestriction::Fertile)
);
unit.movement_restrictions
.retain(|restriction| *restriction != MovementRestriction::Fertile);
}
assert!(can_attack(&game));
execute_without_undo(&mut game, move_action(vec![0, 1], destination), 1)
.unwrap();
for id in [0, 1] {
assert_eq!(game.player(1).get_unit(id).position, destination);
}
}),
],
);
}
#[test]
fn great_gardens_collection_is_scoped_to_its_city() {
use server::cache::Cache;
use server::collect::{
PositionCollection, get_total_collection, possible_resource_collections,
};
use server::events::{EventOrigin, check_event_origin};
use server::game::{Game, GameContext};
use server::game_data::GameData;
use server::player::CostTrigger;
use std::collections::HashSet;
// Both cities can collect the same Plains tile. Owning the wonder elsewhere
// must not change that city's choices or allow an invalid submitted action.
for wonder_city in [0, 1] {
let mut data: serde_json::Value =
serde_json::from_str(include_str!("test_games/wonders/great_gardens.json")).unwrap();
data["players"][0]["cities"][wonder_city]["city_pieces"] =
serde_json::json!({"wonders": ["GreatGardens"]});
let data: GameData = serde_json::from_value(data).unwrap();
let cache = Cache::new(&data.options);
let mut game = Game::from_data(data, cache, GameContext::Play);
game.map
.tiles
.insert(Position::from_offset("A2"), server::map::Terrain::Forest);
let plains = Position::from_offset("B1");
let resources = [
ResourcePile::food(1),
ResourcePile::wood(1),
ResourcePile::ore(1),
ResourcePile::ideas(1),
ResourcePile::gold(1),
];
for (index, name) in ["A1", "C2"].iter().enumerate() {
let city = Position::from_offset(name);
let options = possible_resource_collections(
&game,
city,
0,
&check_event_origin(),
CostTrigger::WithModifiers,
);
let expected = if index == wonder_city {
resources.clone().into_iter().collect()
} else {
HashSet::from([ResourcePile::food(1)])
};
assert_eq!(options.choices[&plains], expected, "Collecting with {name}");
assert_eq!(
options
.modifiers
.contains(&EventOrigin::Wonder(Wonder::GreatGardens)),
index == wonder_city,
);
// Forest still provides only wood, even for the wonder's city.
assert_eq!(
options.choices[&Position::from_offset(if index == 0 { "A2" } else { "B2" })],
HashSet::from([ResourcePile::wood(1)])
);
for resource in &resources {
let result = get_total_collection(
&game,
0,
&check_event_origin(),
city,
&[PositionCollection::new(plains, resource.clone())],
CostTrigger::NoModifiers,
);
assert_eq!(
result.is_ok(),
index == wonder_city || *resource == ResourcePile::food(1),
"{name}: {resource:?}"
);
}
}
}
}
#[test]
fn great_gardens_applies_to_plains_reached_with_husbandry() {
use server::cache::Cache;
use server::collect::{PositionCollection, get_total_collection};
use server::events::check_event_origin;
use server::game::{Game, GameContext};
use server::game_data::GameData;
use server::map::Terrain;
use server::player::CostTrigger;
let mut data: serde_json::Value =
serde_json::from_str(include_str!("test_games/wonders/great_gardens.json")).unwrap();
data["players"][0]["cities"][0]["city_pieces"] =
serde_json::json!({"wonders": ["GreatGardens"]});
data["players"][0]["advances"]
.as_array_mut()
.unwrap()
.push(serde_json::json!("Husbandry"));
let data: GameData = serde_json::from_value(data).unwrap();
let cache = Cache::new(&data.options);
let mut game = Game::from_data(data, cache, GameContext::Play);
game.map
.tiles
.insert(Position::from_offset("A2"), Terrain::Forest);
game.map
.tiles
.insert(Position::from_offset("A3"), Terrain::Fertile);
assert!(
get_total_collection(
&game,
0,
&check_event_origin(),
Position::from_offset("A1"),
&[PositionCollection::new(
Position::from_offset("A3"),
ResourcePile::gold(1)
)],
CostTrigger::NoModifiers,
)
.is_ok()
);
}
#[test]
fn test_great_mausoleum() {
JSON.test(
"great_mausoleum",
vec![
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(123))).skip_json(),
TestAction::undoable(0, Action::Response(EventResponse::Bool(true))).skip_json(),
TestAction::not_undoable(0, Action::Response(EventResponse::Bool(false))).skip_json(),
TestAction::undoable(0, advance_action(Advance::Storage, ResourcePile::ideas(2)))
.skip_json(),
TestAction::undoable(0, Action::Response(EventResponse::Bool(true))),
],
);
}
#[test]
fn test_great_statue() {
JSON.test(
"great_statue",
vec![
TestAction::undoable(0, Action::Playing(WonderCard(Wonder::GreatStatue))).skip_json(),
TestAction::not_undoable(0, payment_response(ResourcePile::new(3, 4, 5, 0, 0, 0, 5)))
.skip_json(),
TestAction::undoable(0, custom_action(CustomActionType::GreatStatue)),
],
);
}
#[test]
fn test_great_wall() {
JSON.test(
"great_wall",
vec![
TestAction::not_undoable(1, move_action(vec![0, 1], Position::from_offset("A1")))
.skip_json(),
TestAction::not_undoable(1, Action::Playing(PlayingAction::EndTurn)).skip_json(),
TestAction::not_undoable(0, advance_action(Advance::Storage, ResourcePile::ideas(2)))
.skip_json(),
TestAction::undoable(0, Action::Playing(PlayingAction::ActionCard(5))),
],
);
}
#[test]
fn great_mausoleum_previews_visible_discards() {
let game = JSON.load_game("great_mausoleum");
let game = server::game_api::execute(game, Action::Playing(PlayingAction::ActionCard(123)), 0);
let game = server::game_api::execute(game, Action::Response(EventResponse::Bool(true)), 0);
let displayed = server::web_view::view(&game, Some(0));
let preview = &displayed["choiceDecision"]["preview"];
assert!(preview["name"].is_string());
assert!(preview["rules"].as_array().unwrap().len() >= 2);
let game = server::game_api::execute(game, Action::Response(EventResponse::Bool(false)), 0);
let game = server::game_api::execute(
game,
advance_action(Advance::Storage, ResourcePile::ideas(2)),
0,
);
let displayed = server::web_view::view(&game, Some(0));
assert!(displayed["choiceDecision"]["preview"]["name"].is_string());
assert!(
displayed["choiceDecision"]["preview"]["rules"]
.as_array()
.unwrap()
.len()
>= 2
);
}