use std::time::{Duration, Instant};
use nmbrs_metrics::labels::Labels;
use nmbrs_metrics::snapshot::MetricSet;
use nmbrs_tui::reporter::TuiReporter;
use nmbrs_tui::state::RunState;
#[test]
fn run_state_tracks_phase_lifecycle() {
let mut s = RunState::new("test.yaml", "repro", "stdout");
s.set_phase_running(0, "schema", "", 4);
assert_eq!(s.phases.len(), 1);
assert_eq!(s.phases[0].name, "schema");
assert!(matches!(
s.phases[0].status,
nmbrs_tui::state::PhaseStatus::Running
));
s.set_phase_completed(
0,
"schema",
"",
1.5,
nmbrs_tui::state::PhaseSummary::default(),
);
assert!(matches!(
s.phases[0].status,
nmbrs_tui::state::PhaseStatus::Completed
));
let e = &s.phases[0];
let (ss, se) = (
e.session_started.expect("start observed"),
e.session_elapsed.expect("completion stamped"),
);
let d = e.duration_secs.expect("duration set");
assert!(
(ss + d - se).abs() < 1e-9,
"session_started({ss}) + duration({d}) must equal session_elapsed({se})"
);
}
#[test]
fn actor_publishes_snapshots() {
use nmbrs_tui::run_state_actor::{RunStateCmd, spawn_run_state_actor};
let (handle, _join) = spawn_run_state_actor(RunState::new("test.yaml", "repro", "stdout"));
handle.send(RunStateCmd::PhaseStarting {
exec_id: 1,
scene_node_id: 0,
name: "schema".into(),
labels: "".into(),
op_templates: 4,
total_cycles: 4,
concurrency: 1,
});
let deadline = Instant::now() + Duration::from_secs(2);
while handle.load().phases.is_empty() && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(5));
}
let snap = handle.load();
assert_eq!(snap.phases.len(), 1, "phase should be visible on snapshot");
assert_eq!(snap.phases[0].name, "schema");
}
fn progress(
exec_id: u64,
name: &str,
labels: &str,
ops_finished: u64,
) -> nmbrs_runtime::observer::PhaseProgressUpdate {
nmbrs_runtime::observer::PhaseProgressUpdate {
exec_id,
name: name.into(),
labels: labels.into(),
cursor_name: "cycle".into(),
cursor_extent: ops_finished,
daemon: false,
rows_consumed: 0,
rows_total: 0,
fibers: 1,
ops_started: ops_finished,
ops_finished,
ops_ok: ops_finished,
skips: 0,
errors: 0,
retries: 0,
ops_per_sec: 0.0,
adapter_counters: Vec::new(),
rows_per_batch: 0.0,
relevancy: Vec::new(),
}
}
#[test]
fn actor_attributes_concurrent_same_name_cells_to_correct_nodes() {
use nmbrs_runtime::scene_tree::SceneTree;
use nmbrs_tui::run_state_actor::{RunStateCmd, spawn_run_state_actor};
use nmbrs_tui::state::{EntryKind, PhaseStatus};
let mut tree = SceneTree::new();
let s1 = tree.push(tree.root(), EntryKind::Scope, "x=1", "");
let a = tree.push(s1, EntryKind::Phase, "p", "x=1");
let s2 = tree.push(tree.root(), EntryKind::Scope, "x=2", "");
let b = tree.push(s2, EntryKind::Phase, "p", "x=2");
assert_ne!(a, b);
let (handle, _join) = spawn_run_state_actor(RunState::new("", "", ""));
handle.send(RunStateCmd::InstallTree(tree));
handle.send(RunStateCmd::PhaseStarting {
exec_id: 1,
scene_node_id: a,
name: "p".into(),
labels: "x=1".into(),
op_templates: 1,
total_cycles: 10,
concurrency: 1,
});
handle.send(RunStateCmd::PhaseStarting {
exec_id: 1,
scene_node_id: b,
name: "p".into(),
labels: "x=2".into(),
op_templates: 1,
total_cycles: 20,
concurrency: 1,
});
handle.send(RunStateCmd::PhaseProgress(progress(1, "p", "x=1", 10)));
handle.send(RunStateCmd::PhaseProgress(progress(1, "p", "x=2", 20)));
handle.send(RunStateCmd::PhaseCompleted {
exec_id: 1,
scene_node_id: b,
name: "p".into(),
labels: "x=2".into(),
duration_secs: 20.0,
});
handle.send(RunStateCmd::PhaseCompleted {
exec_id: 1,
scene_node_id: a,
name: "p".into(),
labels: "x=1".into(),
duration_secs: 10.0,
});
let deadline = Instant::now() + Duration::from_secs(2);
loop {
let snap = handle.load();
let done = snap
.phases
.iter()
.filter(|e| e.name == "p" && matches!(e.status, PhaseStatus::Completed))
.count();
if done == 2 || Instant::now() >= deadline {
break;
}
std::thread::sleep(Duration::from_millis(5));
}
let snap = handle.load();
let row = |id| {
snap.phases
.iter()
.find(|e| e.node_id == id)
.expect("row for id")
};
for (id, tag) in [(a, "x=1"), (b, "x=2")] {
let r = row(id);
let (ss, se) = (
r.session_started.expect("start observed"),
r.session_elapsed.expect("completion stamped"),
);
let d = r.duration_secs.expect("duration set");
assert!(
(ss + d - se).abs() < 1e-9,
"{tag} node's duration must reconcile with its own session stamps"
);
}
assert_eq!(
row(a).summary.as_ref().map(|s| s.ops_finished),
Some(10),
"x=1 node keeps its own summary (ops_finished)"
);
assert_eq!(
row(b).summary.as_ref().map(|s| s.ops_finished),
Some(20),
"x=2 node keeps its own summary (ops_finished)"
);
assert!(matches!(row(a).status, PhaseStatus::Completed));
assert!(matches!(row(b).status, PhaseStatus::Completed));
}
#[test]
fn sparkline_history_caps_at_120() {
let mut state = RunState::new("test.yaml", "repro", "stdout");
for i in 0..200 {
state.push_ops_sample(i as f64);
}
assert_eq!(state.ops_history.len(), 120);
assert_eq!(state.ops_history[0] as u64, 80);
}
#[test]
fn reporter_channel_delivers_frames() {
let (mut reporter, rx) = TuiReporter::channel();
let mut h = hdrhistogram::Histogram::new_with_bounds(1, 3_600_000_000_000, 3).unwrap();
for i in 1..=100 {
let _ = h.record(i * 1_000_000);
}
let mut snapshot = MetricSet::new(Duration::from_secs(1));
snapshot.insert_histogram("cycles_servicetime", Labels::default(), h, Instant::now());
use nmbrs_metrics::scheduler::Reporter;
reporter.report(&snapshot);
let received = rx.try_recv();
assert!(received.is_ok(), "snapshot should be received on channel");
}