epoch_forge/lib_efo/tests/influx.rs

273 lines
8.6 KiB
Rust

use std::{fs, path::Path};
use lib_efo::{
entity::Entity,
influx::{
InfluxLevel, InfluxSpec, OutcomeThresholds, RepeatPolicy, ResolutionOutcome,
SimulationConfig, StatName, StatWeight, resolve,
},
stats::{EntityType, ListStats, Modification, StatPair},
};
use uuid::Uuid;
fn entity(id: u128, kind: EntityType, stats: &[(ListStats, isize)]) -> Entity {
let mut entity = Entity::default();
entity.id = Uuid::from_u128(id);
entity.name = format!("entity-{id}");
entity.entity_type = kind;
for (stat, value) in stats {
entity.change_stat_strict(
StatPair {
stat: *stat,
value: (*value).into(),
},
Modification::Replace,
);
}
entity.update_metastats();
entity
}
fn spec(config: &SimulationConfig, level: InfluxLevel, seed: u64) -> InfluxSpec {
InfluxSpec {
level,
seed,
source: Uuid::from_u128(1),
ruleset_id: config.ruleset_id.clone(),
repeat_policy: RepeatPolicy::Once,
}
}
#[test]
fn embedded_config_covers_every_level_and_external_files_are_complete_replacements() {
let config = SimulationConfig::embedded().expect("embedded config");
for level in [
InfluxLevel::Excellent,
InfluxLevel::Good,
InfluxLevel::Mid,
InfluxLevel::Bad,
InfluxLevel::Worsen,
InfluxLevel::Apocalypse,
] {
assert_eq!(
config
.levels
.iter()
.filter(|band| band.level == level)
.count(),
1
);
}
let directory = tempfile::tempdir().expect("tempdir");
let path = directory.path().join("simulation.json");
fs::write(&path, serde_json::to_vec_pretty(&config).expect("encode")).expect("write");
assert_eq!(SimulationConfig::load(&path).expect("external"), config);
assert!(SimulationConfig::load(Path::new("/definitely/missing/config.json")).is_err());
}
#[test]
fn semantic_validation_rejects_every_unsafe_configuration_family() {
let config = SimulationConfig::embedded().expect("config");
let mut invalid = config.clone();
invalid.levels.pop();
assert!(invalid.validate().is_err());
let mut invalid = config.clone();
invalid.levels[0].max = invalid.levels[1].min;
assert!(invalid.validate().is_err());
let mut invalid = config.clone();
invalid.roll.min = 5;
invalid.roll.max = -5;
assert!(invalid.validate().is_err());
let mut invalid = config.clone();
invalid.thresholds.avoided_max = invalid.thresholds.destroyed_max;
assert!(invalid.validate().is_err());
let mut invalid = config.clone();
invalid.source_weights[0].basis_points = isize::MIN;
assert!(invalid.validate().is_err());
let mut invalid = config.clone();
invalid.source_weights.push(invalid.source_weights[0]);
assert!(invalid.validate().is_err());
let mut invalid = config;
invalid.limits.max_targets = 0;
assert!(invalid.validate().is_err());
}
#[test]
fn reports_are_serializable_and_mismatched_inputs_fail_before_resolution() {
let config = SimulationConfig::embedded().expect("config");
let source = entity(1, EntityType::Event, &[]);
let target = entity(2, EntityType::Town, &[]);
let report = resolve(
&config,
&spec(&config, InfluxLevel::Good, 12),
&source,
&target,
4,
)
.expect("report");
let encoded = serde_json::to_vec(&report).expect("serialize");
let decoded: lib_efo::influx::ResolutionReport =
serde_json::from_slice(&encoded).expect("deserialize");
assert_eq!(decoded, report);
let mut wrong_source = spec(&config, InfluxLevel::Good, 12);
wrong_source.source = Uuid::from_u128(99);
assert!(resolve(&config, &wrong_source, &source, &target, 4).is_err());
let mut wrong_rules = spec(&config, InfluxLevel::Good, 12);
wrong_rules.ruleset_id = "unknown".into();
assert!(resolve(&config, &wrong_rules, &source, &target, 4).is_err());
}
#[test]
fn every_level_stays_inside_its_pressure_band_and_replays_exactly() {
let config = SimulationConfig::embedded().expect("config");
let source = entity(1, EntityType::Event, &[]);
let target = entity(2, EntityType::Town, &[]);
for band in &config.levels {
for seed in 0..128 {
let first = resolve(
&config,
&spec(&config, band.level, seed),
&source,
&target,
7,
)
.expect("resolution");
let replay = resolve(
&config,
&spec(&config, band.level, seed),
&source,
&target,
7,
)
.expect("replay");
assert_eq!(first, replay);
assert!((band.min..=band.max).contains(&first.base_pressure));
}
}
}
#[test]
fn stable_target_subseeds_are_independent_of_resolution_order() {
let config = SimulationConfig::embedded().expect("config");
let source = entity(1, EntityType::Event, &[(ListStats::Threat, 40)]);
let targets = [
entity(2, EntityType::Town, &[(ListStats::Health, 10)]),
entity(3, EntityType::Town, &[(ListStats::Health, 80)]),
];
let forward = targets
.iter()
.map(|target| {
resolve(
&config,
&spec(&config, InfluxLevel::Bad, 42),
&source,
target,
3,
)
.expect("forward")
})
.collect::<Vec<_>>();
let reverse = targets
.iter()
.rev()
.map(|target| {
resolve(
&config,
&spec(&config, InfluxLevel::Bad, 42),
&source,
target,
3,
)
.expect("reverse")
})
.collect::<Vec<_>>();
assert_eq!(forward[0], reverse[1]);
assert_eq!(forward[1], reverse[0]);
assert_ne!(forward[0].id, forward[1].id);
}
#[test]
fn thresholds_produce_all_four_outcomes_and_typed_effects() {
let base = SimulationConfig::embedded().expect("config");
let source = entity(1, EntityType::Event, &[]);
let target = entity(2, EntityType::Town, &[]);
for (expected, attenuation) in [
(ResolutionOutcome::Destroyed, 0),
(ResolutionOutcome::Avoided, 0),
(ResolutionOutcome::Dampened, 5_000),
(ResolutionOutcome::Affected, 10_000),
] {
let mut config = base.clone();
config.roll.min = 0;
config.roll.max = 0;
config.source_weights.clear();
config.resistance_weights.clear();
config.effects = vec![StatWeight {
stat: StatName::Health,
basis_points: 10_000,
}];
let pressure = resolve(
&config,
&spec(&config, InfluxLevel::Mid, 9),
&source,
&target,
0,
)
.expect("pressure")
.base_pressure;
config.thresholds = match expected {
ResolutionOutcome::Destroyed => OutcomeThresholds {
destroyed_max: pressure,
avoided_max: pressure + 1,
dampened_max: pressure + 2,
dampened_basis_points: 5_000,
},
ResolutionOutcome::Avoided => OutcomeThresholds {
destroyed_max: pressure - 1,
avoided_max: pressure,
dampened_max: pressure + 1,
dampened_basis_points: 5_000,
},
ResolutionOutcome::Dampened => OutcomeThresholds {
destroyed_max: pressure - 2,
avoided_max: pressure - 1,
dampened_max: pressure,
dampened_basis_points: 5_000,
},
ResolutionOutcome::Affected => OutcomeThresholds {
destroyed_max: pressure - 3,
avoided_max: pressure - 2,
dampened_max: pressure - 1,
dampened_basis_points: 5_000,
},
};
let report = resolve(
&config,
&spec(&config, InfluxLevel::Mid, 9),
&source,
&target,
0,
)
.expect("outcome");
assert_eq!(report.outcome, expected);
assert_eq!(report.attenuation_basis_points, attenuation);
if attenuation == 0 {
assert!(report.proposed_changes.is_empty());
} else {
assert_eq!(report.proposed_changes[0].stat, StatName::Health);
assert_eq!(
report.proposed_changes[0].delta,
pressure * attenuation / 10_000
);
}
}
}