use mirui::core::log::sinks::RingBufferSink;
use mirui::core::log::{Level, clear_sinks, install_sink, set_max_level};
use mirui::core::time::{clock_now_ns, is_clock_installed, mock};
#[test]
fn std_auto_anchors_first_log_and_stays_monotonic() {
let _serial = mock::install();
drop(_serial);
clear_sinks();
set_max_level(Level::Info);
let sink = RingBufferSink::new(4);
let handle = sink.handle();
install_sink(Box::new(sink));
assert!(is_clock_installed(), "std auto-anchors");
mirui::info!("first");
std::thread::sleep(std::time::Duration::from_millis(2));
mirui::info!("second");
let recs = handle.records();
assert_eq!(recs.len(), 2);
assert!(
recs[1].time_ns > recs[0].time_ns,
"second time_ns {} must exceed first {}",
recs[1].time_ns,
recs[0].time_ns,
);
assert!(
recs[1].time_ns - recs[0].time_ns > 500_000,
"delta {} ns too small — clock likely stuck",
recs[1].time_ns - recs[0].time_ns,
);
}
#[test]
fn mock_install_routes_clock_reads_through_mock_buffer() {
let _guard = mock::install();
mock::set_ns(42_000);
assert_eq!(clock_now_ns(), 42_000);
mock::advance_ms(10);
assert_eq!(clock_now_ns(), 42_000 + 10_000_000);
}
#[test]
fn mock_drop_restores_real_clock_source() {
let baseline = clock_now_ns();
{
let _guard = mock::install();
mock::set_ns(1);
assert_eq!(clock_now_ns(), 1);
}
let after = clock_now_ns();
assert!(
after >= baseline,
"real clock non-monotonic across mock lifetime: baseline={baseline} after={after}"
);
assert_ne!(after, 1, "mock leaked past its guard");
}