epoch_forge/lib_efo/tests/conflict.rs

141 lines
3.9 KiB
Rust

use std::collections::HashSet;
use lib_efo::{
conflict::{
ConflictKind, ConflictStatus, ParticipantRef, create_conflict, finish_round,
validate_conflicts,
},
entity::Entity,
epoch::EpochInfo,
stats::EntityType,
};
use uuid::Uuid;
fn entity(id: u128, kind: EntityType, name: &str) -> Entity {
let mut entity = Entity::default();
entity.id = Uuid::from_u128(id);
entity.entity_type = kind;
entity.name = name.into();
entity
}
fn world() -> EpochInfo {
let mut world = EpochInfo::default();
world.towns.push(entity(1, EntityType::Town, "Haven"));
world
.organizations
.push(entity(2, EntityType::Organization, "Guild"));
world
.characters
.push(entity(3, EntityType::Character, "Aria"));
world
}
fn participant(id: u128, kind: EntityType, side: &str) -> ParticipantRef {
ParticipantRef {
entity_type: kind,
id: Uuid::from_u128(id),
side: side.into(),
committed_weight: 1,
removed: false,
}
}
#[test]
fn mixed_two_and_three_side_conflicts_validate_independently_of_insertion_order() {
let world = world();
let a = create_conflict(
&world,
Uuid::from_u128(20),
"The Crossing".into(),
ConflictKind::War,
vec![
participant(3, EntityType::Character, "third"),
participant(1, EntityType::Town, "red"),
participant(2, EntityType::Organization, "blue"),
],
)
.expect("valid conflict");
let b = create_conflict(
&world,
Uuid::from_u128(20),
"The Crossing".into(),
ConflictKind::War,
vec![
participant(2, EntityType::Organization, "blue"),
participant(1, EntityType::Town, "red"),
participant(3, EntityType::Character, "third"),
],
)
.expect("valid reordered conflict");
assert_eq!(a, b);
}
#[test]
fn fights_stalemate_and_wars_continue_or_select_the_remaining_side() {
let world = world();
let participants = vec![
participant(1, EntityType::Town, "red"),
participant(2, EntityType::Organization, "blue"),
];
let mut fight = create_conflict(
&world,
Uuid::from_u128(20),
"Fight".into(),
ConflictKind::Fight,
participants.clone(),
)
.expect("fight");
finish_round(&mut fight, Vec::new(), &HashSet::new()).expect("round");
assert_eq!(fight.status, ConflictStatus::Stalemate);
let mut war = create_conflict(
&world,
Uuid::from_u128(21),
"War".into(),
ConflictKind::War,
participants,
)
.expect("war");
finish_round(&mut war, Vec::new(), &HashSet::new()).expect("round");
assert_eq!(war.status, ConflictStatus::Active);
finish_round(&mut war, Vec::new(), &HashSet::from([Uuid::from_u128(2)]))
.expect("decisive round");
assert_eq!(war.status, ConflictStatus::Won { side: "red".into() });
}
#[test]
fn malformed_participants_and_cross_references_are_rejected() {
let mut world = world();
let duplicate = vec![
participant(1, EntityType::Town, "red"),
participant(1, EntityType::Town, "blue"),
];
assert!(
create_conflict(
&world,
Uuid::from_u128(20),
"Bad".into(),
ConflictKind::Fight,
duplicate
)
.is_err()
);
let conflict = create_conflict(
&world,
Uuid::from_u128(20),
"Valid".into(),
ConflictKind::Fight,
vec![
participant(1, EntityType::Town, "red"),
participant(2, EntityType::Organization, "blue"),
],
)
.expect("valid");
world.conflicts.insert(conflict.id, conflict);
validate_conflicts(&world).expect("valid world conflicts");
world.organizations.clear();
assert!(validate_conflicts(&world).is_err());
}