use mediation::{
Batch, BatchReconciliation, ChargedParty, ChargingId, CorrelationKeys, DedupWindow,
Disposition, HoldReason, NotKeyable, Octets, ProcessConfig, ProcessState, RecordKey,
RecordType, RejectReason, SourceRef, SubscriberRef, SuppressReason, UnattributableReason,
UsageRecord, VolumePair, apportion, process, split_session,
};
use proptest::prelude::*;
use std::net::Ipv4Addr;
use time::{Duration, OffsetDateTime, macros::datetime};
const NOW: OffsetDateTime = datetime!(2026-07-28 12:00:00 UTC);
fn billable(charging_id: u32) -> UsageRecord {
UsageRecord::new(
RecordType::PduSession,
ChargedParty::Subscriber(SubscriberRef::new("imsi-262021234567890".parse().unwrap())),
"2026-07-28T10:00:00+02:00".parse().unwrap(),
SourceRef::new("pgw-fra-01".parse().unwrap(), 0),
)
.with_networks("262-02".parse().unwrap(), "262-02".parse().unwrap())
.with_closed("2026-07-28T10:30:00+02:00".parse().unwrap())
.with_volume(VolumePair::new(Octets::new(1_000), Octets::new(4_000)))
.with_correlation(
CorrelationKeys::default()
.with_charging_id(ChargingId::new(charging_id))
.with_gateway(Ipv4Addr::new(10, 0, 0, 1).into()),
)
}
fn any_disposition() -> impl Strategy<Value = Disposition> {
let key = RecordKey::RecordingEntity {
entity: "msc-01".parse().unwrap(),
local_sequence: 1,
};
prop_oneof![
Just(Disposition::Billable),
prop_oneof![
Just(SuppressReason::TestIdentity),
Just(SuppressReason::ZeroRated),
Just(SuppressReason::InternalTraffic),
Just(SuppressReason::ZeroUsage),
Just(SuppressReason::OperatorPolicy),
]
.prop_map(Disposition::Suppressed),
Just(Disposition::Duplicate(key)),
prop_oneof![
Just(RejectReason::BeyondDeadline),
Just(RejectReason::FutureTimestamp),
Just(RejectReason::Malformed),
Just(RejectReason::Unattributable(
UnattributableReason::IdentityAbsent
)),
Just(RejectReason::NotKeyable(NotKeyable::NoCorrelatorPresent)),
]
.prop_map(Disposition::Rejected),
prop_oneof![
Just(HoldReason::AwaitingPartial),
Just(HoldReason::AwaitingCorrelation),
Just(HoldReason::AwaitingReferenceData),
Just(HoldReason::SequenceGap),
]
.prop_map(Disposition::Held),
]
}
fn any_batch() -> impl Strategy<Value = Vec<Disposition>> {
proptest::collection::vec(any_disposition(), 0..200)
}
proptest! {
#[test]
fn every_batch_balances(batch in any_batch()) {
let counts: BatchReconciliation = batch.iter().collect();
prop_assert!(counts.balances(), "{counts}");
prop_assert_eq!(counts.ingested(), batch.len() as u64);
}
#[test]
fn the_counts_are_order_independent(batch in any_batch(), rotation in 0usize..200) {
let forward: BatchReconciliation = batch.iter().collect();
let mut shuffled = batch.clone();
let len = shuffled.len();
if len > 0 {
shuffled.rotate_left(rotation % len);
}
shuffled.reverse();
let rotated: BatchReconciliation = shuffled.iter().collect();
prop_assert_eq!(forward, rotated);
}
#[test]
fn splitting_and_merging_is_lossless(batch in any_batch(), at in 0usize..200) {
let whole: BatchReconciliation = batch.iter().collect();
let at = if batch.is_empty() { 0 } else { at % (batch.len() + 1) };
let (left, right) = batch.split_at(at);
let merged = BatchReconciliation::from_iter(left)
.checked_add(BatchReconciliation::from_iter(right))
.expect("counts this small cannot overflow");
prop_assert_eq!(whole, merged);
prop_assert!(merged.balances());
}
#[test]
fn merging_is_associative(a in any_batch(), b in any_batch(), c in any_batch()) {
let (ra, rb, rc) = (
BatchReconciliation::from_iter(&a),
BatchReconciliation::from_iter(&b),
BatchReconciliation::from_iter(&c),
);
let left = ra.checked_add(rb).unwrap().checked_add(rc).unwrap();
let right = ra.checked_add(rb.checked_add(rc).unwrap()).unwrap();
prop_assert_eq!(left, right);
prop_assert!(left.balances());
}
#[test]
fn the_empty_batch_is_the_identity(batch in any_batch()) {
let counts = BatchReconciliation::from_iter(&batch);
prop_assert_eq!(counts.checked_add(BatchReconciliation::EMPTY), Some(counts));
prop_assert_eq!(BatchReconciliation::EMPTY.checked_add(counts), Some(counts));
}
#[test]
fn finality_is_exactly_balanced_and_nothing_held(batch in any_batch()) {
let counts = BatchReconciliation::from_iter(&batch);
let held = batch.iter().filter(|d| d.may_return()).count() as u64;
prop_assert_eq!(counts.held(), held);
prop_assert_eq!(counts.is_final(), counts.balances() && held == 0);
}
#[test]
fn only_billable_records_are_released(batch in any_batch()) {
let counts = BatchReconciliation::from_iter(&batch);
let released = batch.iter().filter(|d| d.is_released()).count() as u64;
prop_assert_eq!(counts.released(), released);
for disposition in &batch {
prop_assert_eq!(
disposition.is_released(),
matches!(disposition, Disposition::Billable)
);
}
}
}
#[test]
fn each_outcome_is_final_or_returnable_and_not_both() {
let key = RecordKey::RecordingEntity {
entity: "msc-01".parse().unwrap(),
local_sequence: 1,
};
let all = [
Disposition::Billable,
Disposition::Suppressed(SuppressReason::ZeroRated),
Disposition::Duplicate(key),
Disposition::Rejected(RejectReason::Malformed),
Disposition::Held(HoldReason::SequenceGap),
];
let returnable = all.iter().filter(|d| d.may_return()).count();
assert_eq!(returnable, 1, "only a held record may return");
let mut kinds: Vec<&str> = all.iter().map(Disposition::kind).collect();
kinds.sort_unstable();
kinds.dedup();
assert_eq!(kinds.len(), all.len());
}
#[test]
fn a_zero_length_session_does_not_lose_its_usage() {
let record = UsageRecord::new(
RecordType::PduSession,
ChargedParty::Subscriber(SubscriberRef::new("imsi-262021234567890".parse().unwrap())),
"2026-07-28T10:00:00+02:00".parse().unwrap(),
SourceRef::new("pgw-01".parse().unwrap(), 0),
)
.with_closed("2026-07-28T10:00:00+02:00".parse().unwrap())
.with_volume(VolumePair::new(Octets::new(1_000), Octets::new(500)));
let split = split_session(&record, &[datetime!(2026-07-28 09:00 +2)]).unwrap();
assert_eq!(split.len(), 1);
assert_eq!(split.total_volume(), record.volume);
assert!(split.conserves(&record));
}
#[test]
fn apportionment_conserves_for_every_weighting() {
for total in [0u64, 1, 7, 1_000, u64::MAX] {
for weights in [
vec![0u64],
vec![0, 0],
vec![0, 0, 0, 0, 0],
vec![1, 0, 1],
vec![u64::MAX, u64::MAX],
vec![3, 1, 4, 1, 5, 9, 2, 6],
] {
let parts = apportion(total, &weights);
assert_eq!(parts.len(), weights.len());
assert_eq!(
parts.iter().map(|p| u128::from(*p)).sum::<u128>(),
u128::from(total),
"{total} over {weights:?}"
);
}
}
}
fn any_record() -> impl Strategy<Value = UsageRecord> {
(
0u32..4, 0u32..6, proptest::option::of(0u32..4), proptest::bool::ANY, 0u64..5_000, proptest::bool::ANY, 0i64..90, proptest::option::of(0u32..3), )
.prop_map(
|(shape, value, partial, fqpc, uplink, attributable, age, file)| {
let party = if attributable {
ChargedParty::Subscriber(SubscriberRef::new(
"imsi-262021234567890".parse().unwrap(),
))
} else {
ChargedParty::Unattributable(UnattributableReason::IdentityAbsent)
};
let opened = datetime!(2026-07-28 10:00:00 UTC) - time::Duration::days(age);
let opened =
mediation::CdrTimestamp::from_utc(opened, mediation::QuarterHourOffset::UTC);
let mut source = SourceRef::new("pgw-01".parse().unwrap(), 0);
if let Some(file) = file {
source = source.with_file_sequence(file);
}
let mut record = UsageRecord::new(RecordType::PduSession, party, opened, source)
.with_closed(opened)
.with_volume(VolumePair::new(Octets::new(uplink), Octets::ZERO));
let mut keys = mediation::CorrelationKeys::default();
match shape {
0 => {}
1 => {
keys = keys
.with_charging_id(mediation::ChargingId::new(value))
.with_gateway(std::net::Ipv4Addr::new(10, 0, 0, 1).into());
}
2 => {
keys = keys.with_icid(format!("icid-{value}").parse().unwrap());
}
_ => {
keys.recording_entity = Some("pgw-01".parse().unwrap());
keys.local_sequence_number = Some(value);
}
}
if let Some(sequence) = partial {
keys.record_sequence_number = Some(sequence + 1);
record.partial = Some(mediation::PartialInfo::new(
sequence + 1,
if fqpc {
mediation::PartialFormat::FullyQualified
} else {
mediation::PartialFormat::Reduced
},
mediation::RecordClosureCause::VolumeLimit,
));
}
record.with_correlation(keys)
},
)
}
proptest! {
#[test]
fn the_driver_conserves_over_any_batch(
records in proptest::collection::vec(any_record(), 0..24),
carried in 0usize..6,
) {
let carried = carried.min(records.len());
let batch = mediation::Batch::with_carry(&records, carried).expect("within the slice");
let mut state = mediation::ProcessState::new(
mediation::DedupWindow::from_bound(time::Duration::days(7)).unwrap(),
);
let outcome = mediation::process(
&batch,
&mediation::ProcessConfig::default(),
datetime!(2026-07-28 12:00:00 UTC),
&mut state,
);
prop_assert_eq!(outcome.dispositions().len(), records.len());
prop_assert_eq!(outcome.reconciliation().carried_in(), carried as u64);
prop_assert_eq!(
outcome.reconciliation().presented(),
Some(records.len() as u64)
);
prop_assert!(outcome.reconciliation().balances());
if let Ok(totals) = outcome.totals() {
prop_assert!(totals.is_consistent());
}
}
#[test]
fn the_driver_is_deterministic(
records in proptest::collection::vec(any_record(), 0..16),
) {
let run = || {
let mut state = mediation::ProcessState::new(
mediation::DedupWindow::from_bound(time::Duration::days(7)).unwrap(),
);
let outcome = mediation::process(
&mediation::Batch::new(&records),
&mediation::ProcessConfig::default(),
datetime!(2026-07-28 12:00:00 UTC),
&mut state,
);
(outcome.dispositions().to_vec(), *outcome.reconciliation())
};
prop_assert_eq!(run(), run());
}
}
#[test]
fn the_totals_carry_the_same_reconciliation_the_pass_reported() {
let records: Vec<UsageRecord> = (1..=4).map(billable).collect();
let batch = Batch::with_carry(&records, 2).expect("two carried");
let mut state = ProcessState::new(DedupWindow::from_bound(Duration::days(7)).unwrap());
let outcome = process(&batch, &ProcessConfig::default(), NOW, &mut state);
let totals = outcome.totals().expect("aggregable");
assert_eq!(&totals.reconciliation(), outcome.reconciliation());
assert_eq!(totals.reconciliation().carried_in(), 2);
assert_eq!(totals.reconciliation().ingested(), 2);
assert!(totals.is_consistent());
}