feat(persistence): add high score saving/loading
Add HighScore resource, persistence module with save/load, and integration tests. Dependencies: serde, serde_json, dirs.
This commit is contained in:
parent
459e8a2353
commit
060a9a2a14
6 changed files with 189 additions and 3 deletions
60
tests/high_score.rs
Normal file
60
tests/high_score.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
use bevy::ecs::world::World;
|
||||
use bglga::persistence;
|
||||
use bglga::resources::{HighScore, Score};
|
||||
use bglga::systems::check_high_score;
|
||||
|
||||
// ── check_high_score system ──
|
||||
|
||||
#[test]
|
||||
fn check_high_score_updates_when_score_exceeds() {
|
||||
let save_path = std::env::temp_dir().join("bglga_test_integration.json");
|
||||
let mut world = World::default();
|
||||
world.insert_resource(Score { value: 7000 });
|
||||
world.insert_resource(HighScore { value: 5000 });
|
||||
persistence::set_save_path_override(save_path.clone());
|
||||
world.register_system(check_high_score);
|
||||
world
|
||||
.run_system_cached(check_high_score)
|
||||
.expect("system should run");
|
||||
let hs = world.resource::<HighScore>();
|
||||
assert_eq!(hs.value, 7000);
|
||||
// Assert file was persisted to disk
|
||||
let content = std::fs::read_to_string(&save_path).expect("high score file should exist");
|
||||
assert!(
|
||||
content.contains("7000"),
|
||||
"persisted file should contain score 7000: {content}"
|
||||
);
|
||||
std::fs::remove_file(&save_path).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_high_score_does_not_update_when_score_equal() {
|
||||
let mut world = bevy::ecs::world::World::default();
|
||||
world.insert_resource(Score { value: 75 });
|
||||
world.insert_resource(HighScore { value: 75 });
|
||||
world.register_system(check_high_score);
|
||||
world
|
||||
.run_system_cached(check_high_score)
|
||||
.expect("system should run");
|
||||
let high_score = world.resource::<HighScore>();
|
||||
assert_eq!(
|
||||
high_score.value, 75,
|
||||
"HighScore should remain unchanged when score equals high score"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_high_score_does_not_update_when_score_lower() {
|
||||
let mut world = bevy::ecs::world::World::default();
|
||||
world.insert_resource(Score { value: 30 });
|
||||
world.insert_resource(HighScore { value: 99 });
|
||||
world.register_system(check_high_score);
|
||||
world
|
||||
.run_system_cached(check_high_score)
|
||||
.expect("system should run");
|
||||
let high_score = world.resource::<HighScore>();
|
||||
assert_eq!(
|
||||
high_score.value, 99,
|
||||
"HighScore should remain unchanged when score is lower"
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue