Lines
95.98 %
Functions
100 %
Branches
//! A mutation reply refreshes every window of its kind, whether or not the window that asked
//! is still open.
use super::*;
use crate::modal::{ConfirmAction, Modal};
use crate::route::{ScriptRoute, TemplateRoute};
use crate::tabs::list::{Balances, ListTab};
fn ok_wire(id: i64) -> String {
format!(r#"(:id {id} :value "new-id")"#)
}
fn failed_wire(id: i64) -> String {
format!(r#"(:id {id} :error (:code db :message "boom"))"#)
fn loading(app: &App, window: WindowId) -> bool {
app.win_by(window).is_some_and(|win| win.view.loading())
fn ask(app: &mut App, id: i64, window: WindowId, ctx: RouteCtx) {
app.pending_routes.insert(
id,
Route {
target: Some(window),
ctx,
},
);
/// One request of every kind of mutation, with the view it changes.
fn mutations() -> Vec<(ViewId, fn() -> RouteCtx)> {
vec![
(ViewId::Tags, || RouteCtx::Mutation {
refresh: ViewId::Tags,
}),
(ViewId::Tags, || RouteCtx::TagCreate),
(ViewId::Tags, || RouteCtx::TagDelete {
label: "k=v".into(),
(ViewId::Templates, || {
RouteCtx::Template(TemplateRoute::Created)
RouteCtx::Template(TemplateRoute::Delete { label: "t".into() })
(ViewId::Scripts, || RouteCtx::Script(ScriptRoute::Created)),
(ViewId::Scripts, || {
RouteCtx::Script(ScriptRoute::Toggled {
label: "s".into(),
enabled: true,
})
RouteCtx::Script(ScriptRoute::Delete { label: "s".into() })
(ViewId::SshKeys, || RouteCtx::SshKeyAdd),
(ViewId::SshKeys, || RouteCtx::SshKeyRemove {
fingerprint: "fp".into(),
(ViewId::Config, || RouteCtx::ConfigSet { key: "k".into() }),
(ViewId::Config, || RouteCtx::OptionsSave {
changes: vec![("tui-theme".into(), "turbo".into())],
(ViewId::Transactions, || RouteCtx::DeleteTransaction {
id: "x".into(),
]
#[tokio::test]
async fn a_mutation_refreshes_a_second_window_of_its_kind() {
for (kind, ctx) in mutations() {
let mut app = crate::test_support::app();
app.attach_echo();
let first = app.window_of(kind);
let second = app.open_extra(kind);
ask(&mut app, 900, first, ctx());
app.route_reply(900, &ok_wire(900));
assert!(loading(&app, first), "{kind:?}: the asking window");
assert!(loading(&app, second), "{kind:?}: the second window");
async fn a_mutation_refreshes_the_other_window_after_the_asking_one_closed() {
app.close_window(first);
assert!(loading(&app, second), "{kind:?}");
async fn a_failed_mutation_of_a_closed_window_refreshes_nothing() {
let first = app.window_of(ViewId::Tags);
let second = app.open_extra(ViewId::Tags);
ask(&mut app, 900, first, RouteCtx::TagCreate);
app.route_reply(900, &failed_wire(900));
assert!(!loading(&app, second));
assert!(app.status.is_empty(), "{}", app.status);
async fn a_closed_window_read_reply_is_still_dropped_whole() {
ask(&mut app, 900, first, RouteCtx::None);
assert!(app.pending_routes.is_empty());
fn bulk_of_closed_window(app: &mut App) -> (WindowId, Vec<i64>) {
let first = app.window_of(ViewId::Transactions);
let second = app.open_extra(ViewId::Transactions);
app.open_dialog_in(
first,
Modal::Confirm {
focus: None,
prompt: String::new(),
action: ConfirmAction::DeleteMarked(vec![]),
app.submit_delete_marked(vec!["a".into(), "b".into()]);
let mut ids: Vec<i64> = app
.pending_routes
.iter()
.filter(|(_, route)| matches!(route.ctx, RouteCtx::BulkDelete { .. }))
.map(|(id, _)| *id)
.collect();
ids.sort_unstable();
(second, ids)
async fn a_bulk_delete_of_a_closed_window_refreshes_after_its_last_reply() {
let (second, ids) = bulk_of_closed_window(&mut app);
let Some(ListTab {
state: Fetch::Loading { id: load },
..
}) = app.win::<ListTab>(second)
else {
panic!("closing the window fronts and loads the next one");
};
let load = *load;
app.route_reply(load, &failed_wire(load));
let &[first_reply, last_reply] = ids.as_slice() else {
panic!("two delete requests expected: {ids:?}");
app.route_reply(first_reply, &ok_wire(first_reply));
assert!(!loading(&app, second), "refreshed before the last reply");
app.route_reply(last_reply, &ok_wire(last_reply));
assert!(loading(&app, second), "no refresh after the last reply");
async fn a_transaction_mutation_feeds_the_balances_of_every_accounts_window() {
let first = app.window_of(ViewId::Accounts);
let second = app.open_extra(ViewId::Accounts);
for window in [first, second] {
if let Some(list) = app.win_mut::<ListTab>(window) {
list.state = Fetch::Loaded(Vec::new());
list.set_balances(Balances::Failed);
let transactions = app.window_of(ViewId::Transactions);
ask(
&mut app,
900,
transactions,
RouteCtx::Mutation {
refresh: ViewId::Transactions,
let balances = app
.win::<ListTab>(window)
.is_some_and(ListTab::balances_loading);
assert!(balances, "{window:?} was not fed");
async fn a_bulk_delete_of_a_closed_window_refreshes_when_only_the_last_delete_failed() {
app.route_reply(last_reply, &failed_wire(last_reply));
assert!(loading(&app, second));
async fn a_bulk_delete_of_a_closed_window_that_deleted_nothing_does_not_refresh() {
for id in ids {
app.route_reply(id, &failed_wire(id));
async fn a_mixed_bulk_delete_refreshes_and_keeps_its_summary() {
let window = app.window_of(ViewId::Transactions);
window,
let mut ids: Vec<i64> = app.pending_routes.keys().copied().collect();
let &[first, last] = ids.as_slice() else {
app.route_reply(first, &ok_wire(first));
app.route_reply(last, &failed_wire(last));
let summary = app.status.clone();
assert!(summary.starts_with("deleted 1 of 2"), "{summary}");
state: Fetch::Loading { id: page },
}) = app.win::<ListTab>(window)
panic!("the list is fetched again");
let page = *page;
app.route_reply(
page,
&format!("(:id {page} :value (:ROWS () :HAS-MORE nil :TOTAL 0))"),
assert_eq!(app.status, summary);