use moonpool_core::{Providers, TimeProvider};
use std::time::Duration;
pub(crate) async fn time_contract<P: Providers>(p: &P) {
let time = p.time();
let first = time.now();
let second = time.now();
assert!(second >= first, "now() must be monotonic non-decreasing");
let sampled = time.now();
assert!(
time.timer() >= sampled,
"timer() must be >= a now() sampled immediately before"
);
let before = time.now();
time.sleep(Duration::from_millis(5))
.await
.expect("sleep should succeed");
let elapsed = time.now().saturating_sub(before);
assert!(
elapsed >= Duration::from_millis(5),
"sleep(5ms) must advance now() by >= 5ms, got {elapsed:?}"
);
let ok = time
.timeout(Duration::from_millis(50), async { 42u32 })
.await;
assert_eq!(ok.expect("fast future must not time out"), 42);
let elapsed = time
.timeout(Duration::from_millis(5), std::future::pending::<()>())
.await;
assert!(
elapsed.is_err(),
"timeout over a never-completing future must elapse"
);
}