refactor: Split interaction tests into multiple files
This commit is contained in:
157
tests/watering.rs
Normal file
157
tests/watering.rs
Normal file
@@ -0,0 +1,157 @@
|
||||
use bevy::ecs::system::RunSystemOnce;
|
||||
use pomomon_garden::features::config::components::BerrySeedConfig;
|
||||
use pomomon_garden::features::grid::components::{Grid, Tile, TileState};
|
||||
use pomomon_garden::features::inventory::components::{Inventory, ItemStack, ItemType};
|
||||
use pomomon_garden::features::pom::actions::InteractionAction;
|
||||
use pomomon_garden::prelude::*;
|
||||
|
||||
fn setup_interaction_app(
|
||||
grid_width: u32,
|
||||
grid_height: u32,
|
||||
initial_tile_states: &[(u32, u32, TileState)],
|
||||
initial_inventory: Vec<(ItemType, u32)>,
|
||||
) -> App {
|
||||
let mut app = App::new();
|
||||
app.add_plugins(MinimalPlugins);
|
||||
app.add_plugins(AssetPlugin::default()); // Needed for asset server if used, though we mock or avoid visuals here
|
||||
|
||||
// Grid Setup
|
||||
let mut grid_tiles = Vec::with_capacity(grid_width as usize);
|
||||
for x in 0..grid_width {
|
||||
let mut column = Vec::with_capacity(grid_height as usize);
|
||||
for y in 0..grid_height {
|
||||
let entity = app
|
||||
.world_mut()
|
||||
.spawn((Tile { x, y }, TileState::Unclaimed))
|
||||
.id();
|
||||
column.push(entity);
|
||||
}
|
||||
grid_tiles.push(column);
|
||||
}
|
||||
app.world_mut().insert_resource(Grid {
|
||||
width: grid_width,
|
||||
height: grid_height,
|
||||
tiles: grid_tiles,
|
||||
});
|
||||
|
||||
for &(x, y, ref state) in initial_tile_states {
|
||||
if let Ok(entity) = app.world().resource::<Grid>().get_tile((x, y)) {
|
||||
*app.world_mut().get_mut::<TileState>(entity).unwrap() = state.clone();
|
||||
}
|
||||
}
|
||||
|
||||
// Inventory Setup
|
||||
let mut inventory_items = Vec::new();
|
||||
for (item_type, amount) in initial_inventory {
|
||||
let id = app.world_mut().spawn(ItemStack { item_type, amount }).id();
|
||||
inventory_items.push(id);
|
||||
}
|
||||
app.world_mut().insert_resource(Inventory {
|
||||
items: inventory_items,
|
||||
});
|
||||
|
||||
let mut game_config = GameConfig::default();
|
||||
game_config.berry_seeds.push(BerrySeedConfig {
|
||||
name: "TestSeed".to_string(),
|
||||
cost: 1,
|
||||
grants: 1,
|
||||
slice: "seed.aseprite".to_string(),
|
||||
growth_stages: 2,
|
||||
});
|
||||
app.insert_resource(game_config);
|
||||
|
||||
app
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_water_crop() {
|
||||
let seed_type = ItemType::BerrySeed {
|
||||
name: "TestSeed".into(),
|
||||
};
|
||||
let mut app = setup_interaction_app(
|
||||
3,
|
||||
3,
|
||||
&[(
|
||||
1,
|
||||
1,
|
||||
TileState::Occupied {
|
||||
seed: seed_type.clone(),
|
||||
watered: false,
|
||||
growth_stage: 0,
|
||||
withered: false,
|
||||
dry_counter: 0,
|
||||
},
|
||||
)],
|
||||
vec![],
|
||||
);
|
||||
|
||||
// Verify Water option is available
|
||||
let _ = app.world_mut().run_system_once(
|
||||
move |grid: Res<Grid>,
|
||||
tile_query: Query<&TileState>,
|
||||
inventory: Res<Inventory>,
|
||||
item_query: Query<&ItemStack>,
|
||||
game_config: Res<GameConfig>| {
|
||||
let tile_entity = grid.get_tile((1, 1)).unwrap();
|
||||
let tile_state = tile_query.get(tile_entity).unwrap();
|
||||
let options = InteractionAction::list_options(tile_state, &inventory, item_query, &game_config);
|
||||
|
||||
assert!(
|
||||
options.contains(&InteractionAction::Water),
|
||||
"Water option should be available"
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
// Execute Water
|
||||
let _ = app.world_mut().run_system_once(
|
||||
move |grid: Res<Grid>,
|
||||
mut tile_query: Query<&mut TileState>,
|
||||
mut inventory: ResMut<Inventory>,
|
||||
mut item_stack_query: Query<&mut ItemStack>,
|
||||
mut commands: Commands,
|
||||
game_config: Res<GameConfig>| {
|
||||
let action = InteractionAction::Water;
|
||||
action.execute(
|
||||
(1, 1),
|
||||
&grid,
|
||||
&mut tile_query,
|
||||
&mut inventory,
|
||||
&mut item_stack_query,
|
||||
&mut commands,
|
||||
&game_config,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.update();
|
||||
|
||||
// Assert Tile State Watered
|
||||
let grid = app.world().resource::<Grid>();
|
||||
let tile_entity = grid.get_tile((1, 1)).unwrap();
|
||||
let tile_state = app.world().entity(tile_entity).get::<TileState>().unwrap();
|
||||
|
||||
if let TileState::Occupied { watered, .. } = tile_state {
|
||||
assert!(watered, "Tile should be watered");
|
||||
} else {
|
||||
panic!("Tile should be Occupied, found {:?}", tile_state);
|
||||
}
|
||||
|
||||
// Verify Water option is NOT available
|
||||
let _ = app.world_mut().run_system_once(
|
||||
move |grid: Res<Grid>,
|
||||
tile_query: Query<&TileState>,
|
||||
inventory: Res<Inventory>,
|
||||
item_query: Query<&ItemStack>,
|
||||
game_config: Res<GameConfig>| {
|
||||
let tile_entity = grid.get_tile((1, 1)).unwrap();
|
||||
let tile_state = tile_query.get(tile_entity).unwrap();
|
||||
let options = InteractionAction::list_options(tile_state, &inventory, item_query, &game_config);
|
||||
|
||||
assert!(
|
||||
!options.contains(&InteractionAction::Water),
|
||||
"Water option should NOT be available"
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user