145 lines
4.7 KiB
Rust
145 lines
4.7 KiB
Rust
use lib_efo::{
|
|
entity::Entity,
|
|
epoch::EpochInfo,
|
|
simulation::{
|
|
MobilityState, Road, begin_diffusion, begin_travel, plan_diffusion, plan_travel,
|
|
tick_diffusion, tick_travel, validate_roads,
|
|
},
|
|
stats::EntityType,
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
fn entity(id: u128, kind: EntityType, position: &str) -> Entity {
|
|
let mut entity = Entity::default();
|
|
entity.id = Uuid::from_u128(id);
|
|
entity.name = format!("entity {id}");
|
|
entity.entity_type = kind;
|
|
entity.modify_position(position.parse().expect("position"));
|
|
entity
|
|
}
|
|
|
|
fn world() -> EpochInfo {
|
|
let mut world = EpochInfo {
|
|
towns: vec![
|
|
entity(1, EntityType::Town, "0,0"),
|
|
entity(2, EntityType::Town, "10,0"),
|
|
entity(3, EntityType::Town, "20,0"),
|
|
],
|
|
characters: vec![entity(4, EntityType::Character, "0,0")],
|
|
events: vec![entity(5, EntityType::Event, "0,0")],
|
|
..EpochInfo::default()
|
|
};
|
|
world.mobility.insert(
|
|
Uuid::from_u128(4),
|
|
MobilityState::Resident {
|
|
place: Uuid::from_u128(1),
|
|
},
|
|
);
|
|
world.roads = vec![
|
|
Road {
|
|
id: Uuid::from_u128(10),
|
|
from: Uuid::from_u128(1),
|
|
to: Uuid::from_u128(2),
|
|
speed_percent: 200,
|
|
tags: vec!["paved".into()],
|
|
},
|
|
Road {
|
|
id: Uuid::from_u128(11),
|
|
from: Uuid::from_u128(2),
|
|
to: Uuid::from_u128(3),
|
|
speed_percent: 200,
|
|
tags: Vec::new(),
|
|
},
|
|
];
|
|
world
|
|
}
|
|
|
|
#[test]
|
|
fn fastest_route_is_deterministic_and_travel_arrives_reversibly() {
|
|
let mut world = world();
|
|
let plan =
|
|
plan_travel(&world, Uuid::from_u128(4), Uuid::from_u128(3), 50).expect("travel plan");
|
|
assert!(plan.uses_roads);
|
|
assert_eq!(
|
|
plan.route,
|
|
[Uuid::from_u128(1), Uuid::from_u128(2), Uuid::from_u128(3)]
|
|
);
|
|
assert_eq!(plan.total_ticks, 10);
|
|
|
|
begin_travel(&mut world, Uuid::from_u128(4), Uuid::from_u128(3), &plan).expect("begin");
|
|
assert!(tick_travel(&mut world, 9).is_empty());
|
|
assert_eq!(tick_travel(&mut world, 1), [Uuid::from_u128(4)]);
|
|
assert_eq!(
|
|
world.mobility[&Uuid::from_u128(4)],
|
|
MobilityState::Resident {
|
|
place: Uuid::from_u128(3)
|
|
}
|
|
);
|
|
assert_eq!(world.characters[0].position().to_inner(), (20, 0));
|
|
}
|
|
|
|
#[test]
|
|
fn direct_fallback_and_invalid_topology_are_explicit() {
|
|
let mut world = world();
|
|
world.roads.clear();
|
|
let plan =
|
|
plan_travel(&world, Uuid::from_u128(4), Uuid::from_u128(3), 50).expect("direct plan");
|
|
assert!(!plan.uses_roads);
|
|
assert_eq!(plan.total_ticks, 40);
|
|
|
|
world.roads.push(Road {
|
|
id: Uuid::from_u128(12),
|
|
from: Uuid::from_u128(1),
|
|
to: Uuid::from_u128(1),
|
|
speed_percent: 100,
|
|
tags: Vec::new(),
|
|
});
|
|
assert!(validate_roads(&world).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn old_snapshots_default_new_spatial_fields() {
|
|
let mut value = serde_json::to_value(EpochInfo::default()).expect("serialize");
|
|
value.as_object_mut().expect("object").remove("roads");
|
|
value.as_object_mut().expect("object").remove("mobility");
|
|
value.as_object_mut().expect("object").remove("diffusions");
|
|
let decoded: EpochInfo = serde_json::from_value(value).expect("old snapshot");
|
|
assert!(decoded.roads.is_empty());
|
|
assert!(decoded.mobility.is_empty());
|
|
assert!(decoded.diffusions.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn diffusion_uses_roads_once_converges_and_expires() {
|
|
let mut world = world();
|
|
let event = Uuid::from_u128(5);
|
|
let state = plan_diffusion(&world, event, 100, 25, Some(11)).expect("frontier");
|
|
assert_eq!(state.frontier.len(), 3);
|
|
assert!(state.frontier.iter().all(|target| !target.route.is_empty()));
|
|
begin_diffusion(&mut world, event, state);
|
|
|
|
let first = tick_diffusion(&mut world, 5);
|
|
assert_eq!(first.reached.len(), 2);
|
|
assert!(first.expired.is_empty());
|
|
assert!(tick_diffusion(&mut world, 0).reached.is_empty());
|
|
let last = tick_diffusion(&mut world, 6);
|
|
assert_eq!(last.reached.len(), 1);
|
|
assert_eq!(last.expired, [event]);
|
|
assert!(tick_diffusion(&mut world, 100).reached.is_empty());
|
|
assert_eq!(world.diffusions[&event].affected.len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn diffusion_replay_is_deterministic_across_varied_worlds() {
|
|
for offset in 0..64_u128 {
|
|
let mut world = world();
|
|
world.events[0].id = Uuid::from_u128(100 + offset);
|
|
let event = world.events[0].id;
|
|
let first =
|
|
plan_diffusion(&world, event, 75 + offset as u32, 20, None).expect("first plan");
|
|
let second =
|
|
plan_diffusion(&world, event, 75 + offset as u32, 20, None).expect("replayed plan");
|
|
assert_eq!(first, second, "variation {offset}");
|
|
}
|
|
}
|