use std::path::PathBuf;
use std::time::Duration;
use astrs_tui::{App, ClusterView, CoordinatorSource, ReplaySource};
use crate::command::client::Endpoint;
use crate::error::CliError;
const TICK_RATE: Duration = Duration::from_millis(250);
#[derive(Debug, Clone, Default)]
pub struct TopArgs {
pub replay: Option<PathBuf>,
}
pub fn run(endpoint: &Endpoint, args: &TopArgs) -> Result<(), CliError> {
let view: Box<dyn ClusterView> = match &args.replay {
Some(path) => Box::new(ReplaySource::open(path)?),
None => Box::new(CoordinatorSource::connect(
endpoint.addr,
endpoint.token.clone(),
)?),
};
let app = App::new(view);
astrs_tui::run(app, TICK_RATE)?;
Ok(())
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use astrs_wire::AuthToken;
use super::*;
fn endpoint() -> Endpoint {
Endpoint::new(
std::net::SocketAddr::from(([127, 0, 0, 1], 1)),
AuthToken::ZERO,
)
}
#[test]
fn a_dead_coordinator_is_reported_rather_than_entering_the_terminal() {
let args = TopArgs::default();
let error = run(&endpoint(), &args).unwrap_err();
assert!(matches!(error, CliError::CoordinatorSource(_)));
}
#[test]
fn a_missing_replay_file_is_reported_rather_than_entering_the_terminal() {
let args = TopArgs {
replay: Some(std::env::temp_dir().join(format!(
"astrs-cli-top-test-does-not-exist-{}.arec",
std::process::id()
))),
};
let error = run(&endpoint(), &args).unwrap_err();
assert!(matches!(error, CliError::Replay(_)));
}
}