use serde_json::{Value, json};
use super::*;
use crate::testing::{fixtures::model, json::fixture};
fn read_at() -> DateTime<Utc> {
"2026-11-14T12:00:00Z".parse().unwrap()
}
fn balance(patch: &Value) -> Balance {
let mut row = fixture("balance");
row.as_object_mut().unwrap().extend(patch.as_object().unwrap().clone());
model(&row)
}
#[test]
fn a_read_is_fresh_under_two_minutes() {
let now = read_at() + TimeDelta::milliseconds(FRESH_MS);
assert!(fresh(Some(read_at()), now - TimeDelta::milliseconds(1)));
assert!(!fresh(Some(read_at()), now));
assert!(!fresh(None, read_at()));
}
#[test]
fn a_take_prepays_from_a_fresh_successful_chain_read() {
let row = balance(&json!({"chain_balance": "250000", "escrow_balance": "50000"}));
assert_eq!(prefunded(&row, read_at()), Some(Amount::from(300_000_u128)));
let stale = read_at() + TimeDelta::milliseconds(FRESH_MS);
assert_eq!(prefunded(&row, stale), None);
let failed = balance(&json!({"chain_error": "rpc down"}));
assert_eq!(prefunded(&failed, read_at()), None);
let unread = balance(&json!({"chain_balance": null}));
assert_eq!(prefunded(&unread, read_at()), None);
}
#[test]
fn only_a_whole_unit_at_the_deposit_address_is_swept() {
let unit = balance(&json!({"deposit_balance": "1000000"}));
assert_eq!(sweepable(&unit), Amount::from(1_000_000_u128));
let dust = balance(&json!({"deposit_balance": "999999"}));
assert_eq!(sweepable(&dust), Amount::ZERO);
let aster = balance(&json!({"venue": "aster", "deposit_balance": "999999999999999999"}));
assert_eq!(sweepable(&aster), Amount::ZERO);
}
#[test]
fn a_withdrawal_keeps_its_fee_and_hyperliquids_gas_reserve() {
let hyperliquid = balance(&json!({"withdrawal_fee": "1000000", "withdrawal_reserve": "40000"}));
assert_eq!(withdrawal_budget(&hyperliquid), Some(Amount::from(1_040_000_u128)));
let no_reserve = balance(&json!({"withdrawal_reserve": null}));
assert_eq!(withdrawal_budget(&no_reserve), None);
let polymarket =
balance(&json!({"venue": "polymarket", "withdrawal_fee": "0", "withdrawal_reserve": null}));
assert_eq!(withdrawal_budget(&polymarket), Some(Amount::ZERO));
let unknown = balance(&json!({"withdrawal_fee": null}));
assert_eq!(withdrawal_budget(&unknown), None);
}