use mediation::{
BatchDiscrepancy, BatchReconciliation, BatchTotals, CdrTimestamp, ChargedParty, ChargingId,
CorrelationKeys, DedupDecision, DedupStore, DedupWindow, Disposition, Octets, PeriodLedger,
RapReturn, RecordKey, RecordType, ScaledAmount, SequenceSpace, SequenceTracker, SequenceValue,
SourceRef, SubscriberRef, TapBatch, TotalKind, UsageRecord, ValidationConfig, VolumePair,
aggregate_released, score_batch, validate_records,
};
use std::net::Ipv4Addr;
use time::{Duration, OffsetDateTime, macros::datetime};
const NOW: OffsetDateTime = datetime!(2026-07-28 12:00:00 UTC);
fn seq(value: u32) -> SequenceValue {
SequenceValue::new(SequenceSpace::TAP, value).expect("in space")
}
fn event(charging_id: u32, opened: &str, up: u64) -> UsageRecord {
UsageRecord::new(
RecordType::PduSession,
ChargedParty::Subscriber(SubscriberRef::new("imsi-262021234567890".parse().unwrap())),
opened.parse().unwrap(),
SourceRef::new("vplmn-gw-01".parse().unwrap(), 0).with_file_sequence(42),
)
.with_networks("234-15".parse().unwrap(), "262-02".parse().unwrap())
.with_closed("2026-07-25T10:30:00+02:00".parse().unwrap())
.with_volume(VolumePair::new(Octets::new(up), Octets::new(up * 4)))
.with_correlation(
CorrelationKeys::default()
.with_charging_id(ChargingId::new(charging_id))
.with_gateway(Ipv4Addr::new(10, 0, 0, 1).into()),
)
}
fn batch(declared_events: u64, declared_charge: ScaledAmount) -> TapBatch {
TapBatch {
sender: "DEUD1".parse().unwrap(),
recipient: "GBRVF".parse().unwrap(),
file_sequence: seq(42),
created: "2026-07-28T10:00:00+02:00".parse().unwrap(),
declared_events,
earliest_event: Some("2026-07-25T10:00:00+02:00".parse().unwrap()),
latest_event: Some("2026-07-25T12:00:00+02:00".parse().unwrap()),
declared_charge,
declared_discount: None,
declared_tax: None,
}
}
fn process(records: &[UsageRecord]) -> Vec<Disposition> {
let findings = validate_records(records, &ValidationConfig::default(), NOW);
let mut store = DedupStore::new(DedupWindow::from_bound(Duration::days(7)).unwrap());
records
.iter()
.enumerate()
.map(|(index, record)| {
let key = match RecordKey::of(record) {
Ok(key) => key,
Err(reason) => {
return Disposition::Rejected(mediation::RejectReason::NotKeyable(reason));
}
};
match store.observe(key.clone(), record.opened.to_utc()) {
DedupDecision::Duplicate { .. } => Disposition::Duplicate(key),
DedupDecision::First if findings.record_blocked(index) => {
Disposition::Rejected(mediation::RejectReason::Malformed)
}
DedupDecision::First => Disposition::Billable,
}
})
.collect()
}
#[test]
fn a_clean_roaming_batch_reconciles_end_to_end() {
let records: Vec<UsageRecord> = (1..=5)
.map(|i| event(i, "2026-07-25T10:00:00+02:00", 1_000))
.collect();
let dispositions = process(&records);
let released = aggregate_released(&records, &dispositions).unwrap();
assert_eq!(released.total().records(), 5);
assert_eq!(released.total().volume().uplink(), Octets::new(5_000));
assert!(released.is_consistent());
let charge = ScaledAmount::new(12_500, 3);
assert!(
batch(5, charge)
.audit(BatchTotals::new(released.total().records(), charge))
.is_empty()
);
let mut tracker = SequenceTracker::new(SequenceSpace::TAP);
tracker.observe_default(42);
let findings = validate_records(&records, &ValidationConfig::default(), NOW);
let quality = score_batch(&released.reconciliation(), &findings, tracker.is_complete());
assert!(!quality.blocks_release(), "{quality}");
assert!(quality.is_final());
}
#[test]
fn a_batch_that_miscounts_itself_is_rejected_with_every_reason() {
let records: Vec<UsageRecord> = (1..=5)
.map(|i| event(i, "2026-07-25T10:00:00+02:00", 1_000))
.collect();
let dispositions = process(&records);
let released = aggregate_released(&records, &dispositions).unwrap();
let declared = ScaledAmount::new(99_999, 3);
let actual = ScaledAmount::new(12_500, 3);
let found = batch(9, declared).audit(BatchTotals::new(released.total().records(), actual));
assert_eq!(found.len(), 2);
assert!(found.contains(&BatchDiscrepancy::EventCountMismatch {
declared: 9,
actual: 5,
}));
assert!(found.iter().all(|d| d.rejects_the_file()));
}
#[test]
fn a_partner_resend_inside_one_file_is_suppressed_without_breaking_the_audit() {
let mut records: Vec<UsageRecord> = (1..=3)
.map(|i| event(i, "2026-07-25T10:00:00+02:00", 1_000))
.collect();
records.push(event(1, "2026-07-25T10:00:00+02:00", 1_000));
let dispositions = process(&records);
let released = aggregate_released(&records, &dispositions).unwrap();
assert_eq!(released.reconciliation().ingested(), 4);
assert_eq!(released.reconciliation().released(), 3);
assert_eq!(released.reconciliation().duplicate(), 1);
assert!(released.is_consistent());
let charge = ScaledAmount::zero(3);
assert!(
batch(4, charge)
.audit(BatchTotals::new(4, charge))
.is_empty()
);
assert_eq!(released.total().records(), 3);
}
#[test]
fn an_event_past_the_inclusion_window_is_caught_at_both_levels() {
let stale = event(1, "2026-01-01T10:00:00+02:00", 1_000);
let findings = validate_records(
std::slice::from_ref(&stale),
&ValidationConfig::default(),
NOW,
);
assert!(findings.has(mediation::Rule::BeyondDeadline));
assert!(findings.blocks_billing());
let mut aged = batch(1, ScaledAmount::zero(3));
aged.earliest_event = Some("2026-01-01T10:00:00+02:00".parse().unwrap());
assert!(
aged.audit(BatchTotals::new(1, ScaledAmount::zero(3)))
.iter()
.any(|d| matches!(d, BatchDiscrepancy::EventBeyondInclusionWindow { .. }))
);
}
#[test]
fn a_fatal_return_removes_the_batch_from_the_period() {
let records: Vec<UsageRecord> = (1..=4)
.map(|i| event(i, "2026-07-25T10:00:00+02:00", 1_000))
.collect();
let original: BatchReconciliation = process(&records).into_iter().collect();
assert_eq!(original.released(), 4);
let after = RapReturn::Fatal.effect_on(original).unwrap();
assert_eq!(after, BatchReconciliation::EMPTY);
assert!(after.balances());
let mut period = PeriodLedger::new();
period.append(after).unwrap();
assert!(period.is_closable());
assert_eq!(period.totals().ingested(), 0);
let mut tracker = SequenceTracker::new(SequenceSpace::TAP);
tracker.observe_default(41);
tracker.observe_default(43);
assert_eq!(tracker.missing(), vec![42]);
assert!(!tracker.is_complete());
}
#[test]
fn a_severe_return_adjusts_the_batch_without_unwinding_it() {
let records: Vec<UsageRecord> = (1..=10)
.map(|i| event(i, "2026-07-25T10:00:00+02:00", 1_000))
.collect();
let original: BatchReconciliation = process(&records).into_iter().collect();
let after = RapReturn::Severe { returned_events: 3 }
.effect_on(original)
.unwrap();
assert_eq!(after.ingested(), 10, "the file still counted");
assert_eq!(after.released(), 7);
assert_eq!(after.rejected(), 3);
assert!(after.balances());
assert_eq!(
RapReturn::Severe { returned_events: 3 }.accepted_events(10),
Some(7)
);
let mut period = PeriodLedger::new();
period.append(after).unwrap();
assert!(period.is_closable());
}
#[test]
fn a_return_larger_than_the_batch_is_a_disagreement_not_a_correction() {
let records: Vec<UsageRecord> = (1..=2)
.map(|i| event(i, "2026-07-25T10:00:00+02:00", 1_000))
.collect();
let original: BatchReconciliation = process(&records).into_iter().collect();
assert_eq!(
RapReturn::Severe { returned_events: 5 }.effect_on(original),
None
);
assert_eq!(
RapReturn::Severe { returned_events: 5 }.accepted_events(2),
None
);
}
#[test]
fn charges_sum_exactly_at_one_scale() {
let charges = [
ScaledAmount::new(1_234, 3),
ScaledAmount::new(5, 3),
ScaledAmount::new(999_999, 3),
];
let total = charges
.iter()
.try_fold(ScaledAmount::zero(3), |acc, c| acc.checked_add(*c))
.unwrap();
assert_eq!(total, ScaledAmount::new(1_001_238, 3));
assert_eq!(total.to_string(), "1001.238");
assert!(total.checked_add(ScaledAmount::new(12, 2)).is_none());
}
#[test]
fn a_credit_nets_against_a_charge() {
let charge = ScaledAmount::new(1_234, 3);
let credit = charge.checked_negate().unwrap();
assert_eq!(credit.to_string(), "-1.234");
assert_eq!(charge.checked_add(credit).unwrap(), ScaledAmount::zero(3));
assert!(charge.checked_add(credit).unwrap().is_zero());
}
#[test]
fn a_severe_return_moves_only_the_returned_events() {
let mut batch = BatchReconciliation::EMPTY;
batch.observe_carried(&Disposition::Billable);
batch.observe(&Disposition::Billable);
batch.observe(&Disposition::Billable);
batch.observe(&Disposition::Duplicate(RecordKey::RecordingEntity {
entity: "msc-01".parse().unwrap(),
local_sequence: 1,
}));
batch.observe(&Disposition::Held(mediation::HoldReason::AwaitingPartial));
let after = RapReturn::Severe { returned_events: 1 }
.effect_on(batch)
.unwrap();
assert_eq!(after.carried_in(), 1, "the carry survives");
assert_eq!(after.ingested(), batch.ingested());
assert_eq!(after.released(), 2);
assert_eq!(after.rejected(), 1);
assert_eq!(after.duplicate(), 1, "a duplicate is not a suppression");
assert_eq!(after.held(), 1);
assert!(after.balances());
assert!(
RapReturn::Severe {
returned_events: 99
}
.effect_on(batch)
.is_none()
);
}
#[test]
fn a_fatal_return_leaves_the_carry_outstanding() {
let mut batch = BatchReconciliation::EMPTY;
batch.observe_carried(&Disposition::Billable);
batch.observe_carried(&Disposition::Billable);
batch.observe(&Disposition::Billable);
let after = RapReturn::Fatal.effect_on(batch).unwrap();
assert_eq!(after.ingested(), 0, "nothing this batch ingested counted");
assert_eq!(after.carried_in(), 2);
assert_eq!(after.held(), 2, "still owed by the period");
assert!(after.balances());
}
#[test]
fn each_declared_total_is_checked_and_names_itself() {
let charge = ScaledAmount::new(1_000, 3);
let mut batch = batch(2, charge);
batch.declared_discount = Some(ScaledAmount::new(100, 3));
batch.declared_tax = Some(ScaledAmount::new(190, 3));
let agreeing = BatchTotals::new(2, charge)
.with_discount(ScaledAmount::new(100, 3))
.with_tax(ScaledAmount::new(190, 3));
assert!(batch.audit(agreeing).is_empty());
let wrong_tax = BatchTotals::new(2, charge)
.with_discount(ScaledAmount::new(100, 3))
.with_tax(ScaledAmount::new(200, 3));
let found = batch.audit(wrong_tax);
assert_eq!(found.len(), 1);
assert!(matches!(
found[0],
BatchDiscrepancy::ChargeMismatch {
kind: TotalKind::Tax,
..
}
));
assert!(
found[0].to_string().contains("totalTaxValue"),
"{}",
found[0]
);
}
#[test]
fn an_undeclared_total_is_not_a_discrepancy() {
let charge = ScaledAmount::new(1_000, 3);
let batch = batch(2, charge);
let with_extras = BatchTotals::new(2, charge)
.with_discount(ScaledAmount::new(100, 3))
.with_tax(ScaledAmount::new(190, 3));
assert!(batch.audit(with_extras).is_empty());
}
#[test]
fn a_tap_timestamp_needs_both_of_its_fields() {
let ts = CdrTimestamp::from_tap("20260728103045", "+0545").unwrap();
assert_eq!(ts.to_string(), "2026-07-28T10:30:45+05:45");
assert_eq!(
ts.to_tap(),
("20260728103045".to_owned(), "+0545".to_owned())
);
let last_century = CdrTimestamp::from_tap("19990101000000", "+0000").unwrap();
assert_eq!(last_century.local_date().year(), 1999);
for offset in [
"-1200", "-0330", "+0000", "+0530", "+0545", "+0845", "+1400",
] {
let ts = CdrTimestamp::from_tap("20260728103045", offset).unwrap();
assert_eq!(ts.to_tap().1, offset);
}
}
#[test]
fn a_malformed_tap_timestamp_is_refused() {
for (local, offset) in [
("2026072810304", "+0200"), ("202607281030455", "+0200"), ("2026072810304X", "+0200"), ("20260230103045", "+0200"), ("20260728253045", "+0200"), ("20260728103045", "+02:00"), ("20260728103045", "+0220"), ("20260728103045", "+1500"), ] {
assert!(
CdrTimestamp::from_tap(local, offset).is_err(),
"{local:?}/{offset:?} must not decode"
);
}
}