open-standards/openpay/crates/op-fraud-graph/tests/ring_detection.rs
Transaction Science 8180ee8c71 ingest wave D: 3 crates · op-fraud-graph + smart-byte-ilp + eoc-quantize
OpenPay (1 crate, 2487 LOC, 33 tests)
  op-fraud-graph    Graph-based fraud detection. EntityKind (card/email/
                    device/IP/address/phone/BIN+last4/account) with
                    canonicalization + hashed EntityKey + billing↔shipping
                    mismatch detection. Adjacency-list FraudGraph with
                    idempotent upsert + edge-merge. Double-hashing Bloom
                    filter (capacity+FPR-sized). Union-find connected
                    components. PageRank with dangling-sink handling.
                    Louvain community detection. Shared-instrument hub
                    rings. Sliding ring-buffer velocity counters. Synthetic
                    identity heuristics (freshness + heavy-use + mail-drop).
                    Structuring / smurfing / mule-chain DFS. EntityRisk +
                    TransactionRisk + RiskBand combiner.

Smart Byte (1 crate, ~1844 LOC, 44 tests)
  smart-byte-ilp    Interledger Protocol v4. OER codec (length-determinant,
                    var-octet-string, BE u32/u64). Prepare/Fulfill/Reject
                    packet roundtrip with all 19 IL-RFC-27 reject codes.
                    SHA-256 Condition + Fulfillment with constant-time
                    verify. ILP address validator (7 schemes, 1-128 char
                    segments, 1023-byte cap, segment-aligned prefix match).
                    STREAM (15 frame types). BTP. SPSP payment-pointer
                    resolver. Open Payments API. Longest-prefix routing
                    Connector with fixed-point quote conversion + balance +
                    rate-limit checks.

EOC (1 crate, 1921 LOC, 36 tests)
  eoc-quantize      Quantization primitives. int8 (symmetric/asymmetric
                    with zero-inclusion fix), int4 (GPTQ + AWQ group quant),
                    int2 (BitNet), fp8 (E4M3/E5M2), nf4 (QLoRA). GGUF v3
                    header parse. safetensors read sketch. Calibration
                    datasets + per-channel scales. KV-cache quant. Energy
                    tradeoff curves (joules-per-token across schemes).

Co-Authored-By: Claude Opus 4.7 <[email protected]>
2026-05-24 16:10:10 -04:00

81 lines
2.4 KiB
Rust

//! Integration tests for [`op_fraud_graph::RingDetector`].
use op_fraud_graph::{
EdgeKind, Entity, EntityKind, FraudGraph, RingDetector,
};
#[test]
fn detects_shared_card_across_many_accounts() {
let mut g = FraudGraph::new();
let card = g.upsert_entity(Entity::new(EntityKind::CardHash, "PAN-1"), 0, true);
let mut accounts = Vec::new();
for i in 0..7 {
let a = g.upsert_entity(
Entity::new(EntityKind::Account, &format!("acc-{i}")),
0,
true,
);
g.add_edge(card, a, EdgeKind::SharesInstrument, 1.0).expect("ok");
accounts.push(a);
}
let rings = RingDetector::default().detect(&g);
let ring = rings
.iter()
.find(|r| r.hub == card)
.expect("card-hub ring detected");
assert_eq!(ring.size(), 7);
assert!(ring.score() > 0.0);
}
#[test]
fn ignores_card_below_threshold() {
let mut g = FraudGraph::new();
let card = g.upsert_entity(Entity::new(EntityKind::CardHash, "PAN-2"), 0, true);
for i in 0..2 {
let a = g.upsert_entity(
Entity::new(EntityKind::Account, &format!("a-{i}")),
0,
true,
);
g.add_edge(card, a, EdgeKind::SharesInstrument, 1.0).expect("ok");
}
let rings = RingDetector::default().detect(&g);
assert!(rings.iter().all(|r| r.hub != card));
}
#[test]
fn ignores_wrong_edge_kind() {
let mut g = FraudGraph::new();
let card = g.upsert_entity(Entity::new(EntityKind::CardHash, "PAN-3"), 0, true);
for i in 0..10 {
let a = g.upsert_entity(
Entity::new(EntityKind::Account, &format!("x-{i}")),
0,
true,
);
g.add_edge(card, a, EdgeKind::CoTransaction, 1.0).expect("ok");
}
let rings = RingDetector::default().detect(&g);
assert!(rings.iter().all(|r| r.hub != card));
}
#[test]
fn respects_min_edge_weight() {
let mut g = FraudGraph::new();
let card = g.upsert_entity(Entity::new(EntityKind::CardHash, "PAN-4"), 0, true);
for i in 0..10 {
let a = g.upsert_entity(
Entity::new(EntityKind::Account, &format!("y-{i}")),
0,
true,
);
// Weight 0.1 < default min_edge_weight (1.0)
g.add_edge(card, a, EdgeKind::SharesInstrument, 0.1).expect("ok");
}
let rings = RingDetector::default().detect(&g);
assert!(rings.iter().all(|r| r.hub != card));
}