1use std::path::PathBuf;
10use std::time::Duration;
11
12use astrs_tui::{App, ClusterView, CoordinatorSource, ReplaySource};
13
14use crate::command::client::Endpoint;
15use crate::error::CliError;
16
17const TICK_RATE: Duration = Duration::from_millis(250);
21
22#[derive(Debug, Clone, Default)]
24pub struct TopArgs {
25 pub replay: Option<PathBuf>,
27}
28
29pub fn run(endpoint: &Endpoint, args: &TopArgs) -> Result<(), CliError> {
41 let view: Box<dyn ClusterView> = match &args.replay {
42 Some(path) => Box::new(ReplaySource::open(path)?),
43 None => Box::new(CoordinatorSource::connect(
44 endpoint.addr,
45 endpoint.token.clone(),
46 )?),
47 };
48 let app = App::new(view);
49 astrs_tui::run(app, TICK_RATE)?;
50 Ok(())
51}
52
53#[cfg(test)]
54mod tests {
55 #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
56
57 use astrs_wire::AuthToken;
58
59 use super::*;
60
61 fn endpoint() -> Endpoint {
62 Endpoint::new(
64 std::net::SocketAddr::from(([127, 0, 0, 1], 1)),
65 AuthToken::ZERO,
66 )
67 }
68
69 #[test]
70 fn a_dead_coordinator_is_reported_rather_than_entering_the_terminal() {
71 let args = TopArgs::default();
72 let error = run(&endpoint(), &args).unwrap_err();
73 assert!(matches!(error, CliError::CoordinatorSource(_)));
74 }
75
76 #[test]
77 fn a_missing_replay_file_is_reported_rather_than_entering_the_terminal() {
78 let args = TopArgs {
79 replay: Some(std::env::temp_dir().join(format!(
80 "astrs-cli-top-test-does-not-exist-{}.arec",
81 std::process::id()
82 ))),
83 };
84 let error = run(&endpoint(), &args).unwrap_err();
85 assert!(matches!(error, CliError::Replay(_)));
86 }
87}