Lines
96.27 %
Functions
90 %
Branches
100 %
//! The Scripts tab through the real key dispatch: list, delete and the tab key.
use crate::app::App;
use crate::modal::Modal;
use crate::route::{RouteCtx, ScriptRoute};
use crate::tabs::fetch::Fetch;
use crate::tabs::nms_eval::double::Sent;
use crate::tabs::select_list::SelectRow;
use crate::test_support::{key, press};
use crate::view::Tab;
use ratatui::crossterm::event::KeyCode;
pub(super) const SYNC: &str = "aaaaaaaa-0000-0000-0000-000000000001";
pub(super) const OLD: &str = "bbbbbbbb-0000-0000-0000-000000000002";
pub(super) fn scripts_app() -> (App, Sent) {
let mut app = crate::test_support::app();
let sent = app.attach_echo();
app.switch_tab(Tab::Scripts);
(app, sent)
}
fn loading_id(app: &App) -> anyhow::Result<i64> {
match app.scripts().state {
Fetch::Loading { id } => Ok(id),
ref other => anyhow::bail!("not loading: {other:?}"),
/// A `list-scripts` reply with a row of `(id, name, enabled, has_source)` each.
pub(super) fn load_states(
app: &mut App,
scripts: &[(&str, &str, bool, bool)],
) -> anyhow::Result<()> {
let id = loading_id(app)?;
let flag = |on: bool| if on { "t" } else { "nil" };
let rows: Vec<String> = scripts
.iter()
.map(|(id, name, enabled, has_source)| {
format!(
r#"(:id \"{id}\" :name \"{name}\" :enabled {} :has-source {})"#,
flag(*enabled),
flag(*has_source)
)
})
.collect();
let wire = format!(r#"(:id {id} :value "({})")"#, rows.join(" "));
app.route_reply(id, &wire);
Ok(())
pub(super) fn load(app: &mut App, scripts: &[(&str, &str)]) -> anyhow::Result<()> {
let rows: Vec<_> = scripts
.map(|(id, name)| (*id, *name, true, true))
load_states(app, &rows)
pub(super) fn pending(app: &App, wanted: fn(&ScriptRoute) -> bool) -> Option<i64> {
app.pending_routes
.find_map(|(id, route)| match &route.ctx {
RouteCtx::Script(route) if wanted(route) => Some(*id),
_ => None,
fn shown(app: &App) -> Vec<String> {
app.scripts()
.rows()
.map(|row| row.cells().join(" | "))
.collect()
#[tokio::test]
async fn opening_the_tab_lists_the_scripts_with_their_states() -> anyhow::Result<()> {
let (mut app, sent) = scripts_app();
load_states(
&mut app,
&[(SYNC, "sync", true, true), (OLD, "old", false, false)],
)?;
assert_eq!(shown(&app), ["sync | enabled", "old | disabled, no source"]);
tokio::task::yield_now().await;
app.drain_eval();
let echoed = sent.frames().join("\n");
assert!(echoed.contains(r#"(list-scripts)"#), "{echoed}");
async fn zero_opens_the_tab_and_console_stays_on_nine() {
press(&mut app, key(KeyCode::Char('0')));
assert_eq!(app.front_kind(), Some(Tab::Scripts));
press(&mut app, key(KeyCode::Char('9')));
assert_eq!(app.front_kind(), Some(Tab::Console));
async fn a_list_error_shows_in_the_tab() -> anyhow::Result<()> {
let (mut app, _) = scripts_app();
let id = loading_id(&app)?;
app.route_reply(
id,
&format!(r#"(:id {id} :error (:code db :message "connection refused"))"#),
);
assert!(matches!(&app.scripts().state, Fetch::Error(m) if m.contains("connection refused")));
async fn keys_without_a_selection_say_so() {
for letter in ['d', 'e', 't'] {
app.status.clear();
press(&mut app, key(KeyCode::Char(letter)));
assert_eq!(app.status, "no script selected", "{letter}");
assert!(app.overlays.is_empty());
async fn d_asks_naming_the_script_and_n_cancels() -> anyhow::Result<()> {
load(&mut app, &[(SYNC, "sync"), (OLD, "old")])?;
press(&mut app, key(KeyCode::Char('j')));
press(&mut app, key(KeyCode::Char('d')));
let Some(Modal::Confirm { prompt, .. }) = app.overlays.top() else {
anyhow::bail!("no confirm");
};
assert_eq!(prompt, "Delete script old?");
press(&mut app, key(KeyCode::Char('n')));
assert!(pending(&app, |r| matches!(r, ScriptRoute::Delete { .. })).is_none());
async fn enter_confirms_the_delete_over_delete_script_and_refreshes() -> anyhow::Result<()> {
press(&mut app, key(KeyCode::Enter));
let id = pending(&app, |r| matches!(r, ScriptRoute::Delete { .. }))
.ok_or_else(|| anyhow::anyhow!("delete not sent"))?;
assert!(
echoed.contains(&format!(r#"(delete-script "{SYNC}")"#)),
"{echoed}"
app.route_reply(id, &format!("(:id {id} :value t)"));
assert_eq!(app.status, "deleted sync");
load(&mut app, &[(OLD, "old")])?;
assert_eq!(shown(&app), ["old | enabled"]);
async fn a_failed_delete_shows_the_servers_message_and_does_not_refetch() -> anyhow::Result<()> {
load(&mut app, &[(SYNC, "sync")])?;
press(&mut app, key(KeyCode::Char('y')));
&format!(r#"(:id {id} :error (:code not-found :message "no such script"))"#),
assert!(app.status.contains("no such script"), "{}", app.status);
assert!(!app.scripts().any_loading());