Files
pomomon-garden/tests/harvest.rs

209 lines
6.1 KiB
Rust

use bevy::ecs::system::RunSystemOnce;
use pomomon_garden::features::config::components::{BerrySeedConfig, GameConfig};
use pomomon_garden::features::grid::components::{Grid, TileState};
use pomomon_garden::features::inventory::components::{Inventory, ItemStack, ItemType};
use pomomon_garden::features::phase::components::SessionTracker;
use pomomon_garden::features::pom::actions::InteractionAction;
use pomomon_garden::prelude::*;
mod common;
use common::setup_app;
#[test]
fn test_harvest_fully_grown() {
let seed_type = ItemType::BerrySeed {
name: "TestSeed".into(),
};
let initial_states = vec![(
0,
0,
TileState::Occupied {
seed: seed_type.clone(),
watered: false,
growth_stage: 2, // Max
withered: false,
dry_counter: 0,
},
)];
let seed_config = BerrySeedConfig {
name: "TestSeed".into(),
cost: 1,
grants: 5,
slice: "".into(),
growth_stages: 2,
};
let mut app = setup_app(1, 1, &initial_states, vec![], Some(vec![seed_config]));
let _ = app.world_mut().run_system_once(
|mut commands: Commands,
grid: Res<Grid>,
mut tile_query: Query<&mut TileState>,
mut inventory: ResMut<Inventory>,
mut item_stack_query: Query<&mut ItemStack>,
config: Res<GameConfig>,
mut session_tracker: ResMut<SessionTracker>| {
InteractionAction::Harvest.execute(
(0, 0),
&grid,
&mut tile_query,
&mut inventory,
&mut item_stack_query,
&mut commands,
&config,
&mut session_tracker,
);
},
);
// Check Tile State -> Empty
// Wait for commands to apply
app.update();
let grid = app.world().resource::<Grid>();
let tile_entity = grid.get_tile((0, 0)).unwrap();
let tile_state = app.world().entity(tile_entity).get::<TileState>().unwrap();
assert!(matches!(tile_state, TileState::Empty));
// Check Inventory -> 5 Berries
let inventory = app.world().resource::<Inventory>();
assert_eq!(inventory.items.len(), 1);
let stack_entity = inventory.items[0];
let stack = app.world().entity(stack_entity).get::<ItemStack>().unwrap();
assert_eq!(stack.item_type, ItemType::Berry);
assert_eq!(stack.amount, 5);
// Check Session Tracker
let tracker = app.world().resource::<SessionTracker>();
assert_eq!(tracker.total_berries_earned, 5);
}
#[test]
fn test_harvest_withered() {
let seed_type = ItemType::BerrySeed {
name: "TestSeed".into(),
};
let initial_states = vec![(
0,
0,
TileState::Occupied {
seed: seed_type.clone(),
watered: false,
growth_stage: 1,
withered: true, // Withered
dry_counter: 0,
},
)];
let seed_config = BerrySeedConfig {
name: "TestSeed".into(),
cost: 1,
grants: 5,
slice: "".into(),
growth_stages: 2,
};
let mut app = setup_app(1, 1, &initial_states, vec![], Some(vec![seed_config]));
let _ = app.world_mut().run_system_once(
|mut commands: Commands,
grid: Res<Grid>,
mut tile_query: Query<&mut TileState>,
mut inventory: ResMut<Inventory>,
mut item_stack_query: Query<&mut ItemStack>,
config: Res<GameConfig>,
mut session_tracker: ResMut<SessionTracker>| {
InteractionAction::Harvest.execute(
(0, 0),
&grid,
&mut tile_query,
&mut inventory,
&mut item_stack_query,
&mut commands,
&config,
&mut session_tracker,
);
},
);
app.update();
// Check Tile State -> Empty
let grid = app.world().resource::<Grid>();
let tile_entity = grid.get_tile((0, 0)).unwrap();
let tile_state = app.world().entity(tile_entity).get::<TileState>().unwrap();
assert!(matches!(tile_state, TileState::Empty));
// Check Inventory -> Empty (no berries for withered)
let inventory = app.world().resource::<Inventory>();
assert!(inventory.items.is_empty());
}
#[test]
fn test_cannot_harvest_growing() {
let seed_type = ItemType::BerrySeed {
name: "TestSeed".into(),
};
let initial_states = vec![(
0,
0,
TileState::Occupied {
seed: seed_type.clone(),
watered: false,
growth_stage: 1, // Growing (Max is 2)
withered: false,
dry_counter: 0,
},
)];
let seed_config = BerrySeedConfig {
name: "TestSeed".into(),
cost: 1,
grants: 5,
slice: "".into(),
growth_stages: 2,
};
let mut app = setup_app(1, 1, &initial_states, vec![], Some(vec![seed_config]));
let _ = app.world_mut().run_system_once(
|mut commands: Commands,
grid: Res<Grid>,
mut tile_query: Query<&mut TileState>,
mut inventory: ResMut<Inventory>,
mut item_stack_query: Query<&mut ItemStack>,
config: Res<GameConfig>,
mut session_tracker: ResMut<SessionTracker>| {
InteractionAction::Harvest.execute(
(0, 0),
&grid,
&mut tile_query,
&mut inventory,
&mut item_stack_query,
&mut commands,
&config,
&mut session_tracker,
);
},
);
app.update();
// Check Tile State -> Still Occupied
let grid = app.world().resource::<Grid>();
let tile_entity = grid.get_tile((0, 0)).unwrap();
let tile_state = app.world().entity(tile_entity).get::<TileState>().unwrap();
match tile_state {
TileState::Occupied { growth_stage, .. } => {
assert_eq!(*growth_stage, 1);
}
_ => panic!("Should still be occupied"),
}
// Check Inventory -> Empty
let inventory = app.world().resource::<Inventory>();
assert!(inventory.items.is_empty());
}