From 8c1ba2654bdda716d80f67aae7030c91090db73a Mon Sep 17 00:00:00 2001 From: demenik Date: Tue, 9 Dec 2025 15:22:02 +0100 Subject: [PATCH] test: Added a test for triggering wonder events --- tests/wonderevent.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/wonderevent.rs diff --git a/tests/wonderevent.rs b/tests/wonderevent.rs new file mode 100644 index 0000000..3680e45 --- /dev/null +++ b/tests/wonderevent.rs @@ -0,0 +1,85 @@ +use bevy::prelude::*; +use pomomon_garden::features::config::components::GameConfig; +use pomomon_garden::features::core::states::AppState; +use pomomon_garden::features::grid::components::Grid; +use pomomon_garden::features::phase::components::{CurrentPhase, Phase, TimerSettings}; +use pomomon_garden::features::wonderevent::{RequestWonderEvent, WonderEventPlugin}; + +#[test] +fn test_wonder_event_triggers() { + let mut app = App::new(); + app.add_plugins(MinimalPlugins); + app.add_plugins(bevy::state::app::StatesPlugin); + app.add_plugins(WonderEventPlugin); + + app.insert_resource(GameConfig::default()); + app.insert_resource(TimerSettings { + focus_duration: 60, // 60 seconds + short_break_duration: 5, + long_break_duration: 15, + long_break_interval: 4, + }); + + app.insert_resource(Grid { + width: 10, + height: 10, + tiles: vec![], + }); + + app.init_state::(); + let mut next_state = app.world_mut().resource_mut::>(); + next_state.set(AppState::GameScreen); + + app.insert_resource(CurrentPhase(Phase::Focus { duration: 60.0 })); + + // 1. Full Duration (60.0). No trigger. + app.update(); + verify_event_count(&mut app, 0); + + // 2. Half Duration (30.0). Trigger! + *app.world_mut().resource_mut::() = CurrentPhase(Phase::Focus { duration: 30.0 }); + app.update(); + verify_event_count(&mut app, 1); + + // 3. Less than half (29.0). Already triggered. No new trigger. + *app.world_mut().resource_mut::() = CurrentPhase(Phase::Focus { duration: 29.0 }); + app.update(); + verify_event_count(&mut app, 0); // New events should be 0 + + // 4. Pause (Focus). Logic should NOT reset trigger. + let prev = Box::new(Phase::Focus { duration: 29.0 }); + *app.world_mut().resource_mut::() = CurrentPhase(Phase::Paused { + previous_phase: prev, + }); + app.update(); + verify_event_count(&mut app, 0); + + // 5. Resume Focus. Logic should remember it was triggered. + *app.world_mut().resource_mut::() = CurrentPhase(Phase::Focus { duration: 29.0 }); + app.update(); + verify_event_count(&mut app, 0); + + // 6. Break. Logic should reset trigger. + *app.world_mut().resource_mut::() = CurrentPhase(Phase::Break { duration: 5.0 }); + app.update(); + verify_event_count(&mut app, 0); + + // 7. New Focus (Half Duration). Logic should trigger again. + *app.world_mut().resource_mut::() = CurrentPhase(Phase::Focus { duration: 30.0 }); + app.update(); + verify_event_count(&mut app, 1); +} + +fn verify_event_count(app: &mut App, expected: usize) { + let count = app + .world_mut() + .resource_mut::>() + .drain() + .count(); + + assert_eq!( + count, expected, + "Expected {} events, found {}", + expected, count + ); +}