236 lines
8.1 KiB
Rust
236 lines
8.1 KiB
Rust
use lib_efo::{
|
|
adjudication::{
|
|
AdjudicationConfig, AdjudicationDisposition as Disposition, AdjudicationLevel as Level,
|
|
AdjudicationSpec, ChosenEffect, StatName,
|
|
},
|
|
entity::Entity,
|
|
epoch::{EpochId, EpochInfo},
|
|
epoch_resolution::{self as resolution, apply, preview},
|
|
stats::{EntityType, ListStats, Modification, StatPair},
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
fn world(health: isize) -> EpochInfo {
|
|
let mut world = EpochInfo::default();
|
|
for id in [1, 2] {
|
|
let mut event = Entity::default();
|
|
event.id = Uuid::from_u128(id);
|
|
event.name = format!("event {id}");
|
|
event.entity_type = EntityType::Event;
|
|
world.events.push(event);
|
|
}
|
|
let mut target = Entity::default();
|
|
target.id = Uuid::from_u128(3);
|
|
target.name = "town".into();
|
|
target.entity_type = EntityType::Town;
|
|
target.change_stat_strict(
|
|
StatPair {
|
|
stat: ListStats::Health,
|
|
value: health.into(),
|
|
},
|
|
Modification::Replace,
|
|
);
|
|
target.update_metastats();
|
|
world.towns.push(target);
|
|
world
|
|
}
|
|
|
|
fn spec(source: u128, delta: isize) -> AdjudicationSpec {
|
|
AdjudicationSpec {
|
|
level: Level::Apocalypse,
|
|
disposition: Disposition::Applied,
|
|
source: Uuid::from_u128(source),
|
|
external_result: "GM ruling".into(),
|
|
note: String::new(),
|
|
effects: vec![ChosenEffect {
|
|
stat: StatName::Health,
|
|
delta,
|
|
}],
|
|
policy_id: AdjudicationConfig::embedded().expect("config").policy_id,
|
|
}
|
|
}
|
|
|
|
fn health(world: &EpochInfo) -> isize {
|
|
**world.towns[0].get_stats().get_field(&ListStats::Health)
|
|
}
|
|
|
|
#[test]
|
|
fn independent_clamps_are_order_independent_in_batches_and_separate_invocations() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
for (base, plus, minus, expected) in [
|
|
(110, 20, -20, 100),
|
|
(5, 10, -10, 10),
|
|
(100, 10, 10, 120),
|
|
(100, 100, 100, 120),
|
|
] {
|
|
let initial = world(base);
|
|
let a = preview(&initial, &config, &spec(1, plus), Uuid::from_u128(3), 0).expect("a");
|
|
let b = preview(&initial, &config, &spec(2, minus), Uuid::from_u128(3), 0).expect("b");
|
|
let first = apply(&initial, &config, &[(EntityType::Town, a.clone())]).expect("first");
|
|
// Simulate another process opening persisted state before previewing b.
|
|
let first: EpochInfo =
|
|
serde_json::from_slice(&serde_json::to_vec(&first).expect("encode")).expect("decode");
|
|
assert_eq!(
|
|
preview(&first, &config, &spec(2, minus), Uuid::from_u128(3), 0)
|
|
.expect("same baseline"),
|
|
b
|
|
);
|
|
let ab = apply(&first, &config, &[(EntityType::Town, b.clone())]).expect("second");
|
|
let ba = apply(
|
|
&initial,
|
|
&config,
|
|
&[(EntityType::Town, b), (EntityType::Town, a)],
|
|
)
|
|
.expect("reversed batch");
|
|
assert_eq!(health(&ab), expected);
|
|
assert_eq!(
|
|
serde_json::to_value(&ab).expect("ab"),
|
|
serde_json::to_value(&ba).expect("ba")
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn next_epoch_compounds_and_identical_ruling_has_new_identity() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let initial = world(100);
|
|
let a = preview(&initial, &config, &spec(1, -10), Uuid::from_u128(3), 0).expect("a");
|
|
let mut next = apply(&initial, &config, &[(EntityType::Town, a.clone())]).expect("apply");
|
|
next.epoch_id = EpochId::new(1);
|
|
let b = preview(&next, &config, &spec(1, -10), Uuid::from_u128(3), 0).expect("next");
|
|
assert_ne!(a.id, b.id);
|
|
assert_eq!(b.changes[0].before, 90);
|
|
let next = apply(&next, &config, &[(EntityType::Town, b)]).expect("compound");
|
|
assert_eq!(health(&next), 80);
|
|
assert!(
|
|
next.epoch_resolution
|
|
.as_ref()
|
|
.expect("state")
|
|
.baseline
|
|
.epoch_resolution
|
|
.is_none()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn duplicate_and_stale_reports_are_atomic_and_do_not_overwrite_edits() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let initial = world(100);
|
|
let a = preview(&initial, &config, &spec(1, -10), Uuid::from_u128(3), 0).expect("a");
|
|
assert!(
|
|
apply(
|
|
&initial,
|
|
&config,
|
|
&[(EntityType::Town, a.clone()), (EntityType::Town, a.clone())]
|
|
)
|
|
.is_err()
|
|
);
|
|
assert_eq!(health(&initial), 100);
|
|
let mut updated = apply(&initial, &config, &[(EntityType::Town, a.clone())]).expect("apply");
|
|
assert!(apply(&updated, &config, &[(EntityType::Town, a)]).is_err());
|
|
updated.towns[0].change_stat_strict(
|
|
StatPair {
|
|
stat: ListStats::Health,
|
|
value: 40.into(),
|
|
},
|
|
Modification::Replace,
|
|
);
|
|
assert!(preview(&updated, &config, &spec(2, -10), Uuid::from_u128(3), 0).is_err());
|
|
assert_eq!(health(&updated), 40);
|
|
}
|
|
|
|
#[test]
|
|
fn source_state_reduces_by_minimum_and_destruction_dominates() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let initial = world(100);
|
|
let mut destroy = spec(1, -10);
|
|
destroy.disposition = Disposition::Destroyed;
|
|
let a = preview(&initial, &config, &destroy, Uuid::from_u128(3), 0).expect("destroy");
|
|
let mut defaults = spec(1, 0);
|
|
defaults.effects.clear();
|
|
defaults.level = Level::Bad;
|
|
let b = preview(&initial, &config, &defaults, Uuid::from_u128(3), 1).expect("defaults");
|
|
let first = apply(&initial, &config, &[(EntityType::Town, a.clone())]).expect("destroy first");
|
|
assert_eq!(
|
|
preview(&first, &config, &defaults, Uuid::from_u128(3), 1).expect("same-epoch default"),
|
|
b
|
|
);
|
|
let ab = apply(&first, &config, &[(EntityType::Town, b.clone())]).expect("default next");
|
|
let ba = apply(
|
|
&initial,
|
|
&config,
|
|
&[(EntityType::Town, b), (EntityType::Town, a)],
|
|
)
|
|
.expect("batch");
|
|
assert_eq!(
|
|
serde_json::to_value(&ab).expect("ab"),
|
|
serde_json::to_value(&ba).expect("ba")
|
|
);
|
|
assert_eq!(health(&ab), 85);
|
|
assert_eq!(
|
|
ab.event_adjudications[&Uuid::from_u128(1)].potency_basis_points,
|
|
0
|
|
);
|
|
assert!(ab.event_adjudications[&Uuid::from_u128(1)].destroyed);
|
|
let mut next = ab;
|
|
next.epoch_id = EpochId::new(1);
|
|
assert!(preview(&next, &config, &defaults, Uuid::from_u128(3), 1).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_fixture_and_corrupt_state_are_handled_explicitly() {
|
|
let legacy: EpochInfo =
|
|
serde_json::from_str(include_str!("fixtures/epoch-resolution/legacy.json"))
|
|
.expect("old epoch");
|
|
assert!(legacy.epoch_resolution.is_none());
|
|
resolution::validate(&legacy).expect("legacy validation");
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let initial = world(100);
|
|
let a = preview(&initial, &config, &spec(1, -10), Uuid::from_u128(3), 0).expect("a");
|
|
let updated = apply(&initial, &config, &[(EntityType::Town, a)]).expect("apply");
|
|
let mut bad = updated.clone();
|
|
bad.epoch_resolution.as_mut().expect("state").ruleset = "future".into();
|
|
assert!(resolution::validate(&bad).is_err());
|
|
let mut bad = updated.clone();
|
|
bad.adjudications.clear();
|
|
assert!(resolution::validate(&bad).is_err());
|
|
let mut bad = updated.clone();
|
|
bad.epoch_resolution
|
|
.as_mut()
|
|
.expect("state")
|
|
.baseline
|
|
.epoch_resolution = updated.epoch_resolution;
|
|
assert!(resolution::validate(&bad).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn extreme_deltas_clamp_without_intermediate_overflow() {
|
|
let initial = world(100);
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let a = preview(
|
|
&initial,
|
|
&config,
|
|
&spec(1, isize::MAX),
|
|
Uuid::from_u128(3),
|
|
0,
|
|
)
|
|
.expect("positive");
|
|
let b = preview(
|
|
&initial,
|
|
&config,
|
|
&spec(2, isize::MIN),
|
|
Uuid::from_u128(3),
|
|
0,
|
|
)
|
|
.expect("negative");
|
|
assert_eq!(a.changes[0].applied_delta, isize::MAX);
|
|
assert_eq!(b.changes[0].applied_delta, isize::MIN);
|
|
let updated = apply(
|
|
&initial,
|
|
&config,
|
|
&[(EntityType::Town, a), (EntityType::Town, b)],
|
|
)
|
|
.expect("exact sum");
|
|
assert_eq!(health(&updated), 20);
|
|
}
|