1
//! The View menu opens a new window of a view, and every window of a view acts alone.
2
use super::open::app_on;
3
use super::*;
4
use crate::event::{Intent, apply};
5
use crate::tabs::list::ListTab;
6
use crate::test_support::{key, press};
7
use ratatui::crossterm::event::KeyCode;
8

            
9
2
fn page_wire(ids: &[&str]) -> String {
10
2
    let rows: String = ids
11
2
        .iter()
12
4
        .map(|i| format!(r#"(:ID "{i}" :DATE "2026-10-01T00:00:00Z" :DATE-TEXT "2026-10-01 00:00" :NOTE "n" :AMOUNT "5")"#))
13
        .collect::<Vec<_>>()
14
2
        .join(" ");
15
2
    format!(r#"(:id 1 :value (:ROWS ({rows}) :HAS-MORE nil :TOTAL 2))"#)
16
2
}
17

            
18
2
fn deliver(app: &mut App, window: WindowId, wire: &str) {
19
2
    let route = Route {
20
2
        target: Some(window),
21
2
        ctx: RouteCtx::None,
22
2
    };
23
2
    app.deliver_reply(route, wire);
24
2
}
25

            
26
13
fn open_from_menu(app: &mut App, kind: ViewId) {
27
13
    press(app, key(KeyCode::F(10)));
28
13
    app.select_menu_item(kind.index());
29
13
    press(app, key(KeyCode::Enter));
30
13
}
31

            
32
2
fn loading(app: &App, window: WindowId) -> bool {
33
2
    app.win_by(window).is_some_and(|win| win.view.loading())
34
2
}
35

            
36
4
fn marked(app: &mut App, window: WindowId) -> Vec<String> {
37
4
    app.win_mut::<ListTab>(window)
38
4
        .map(|list| list.marked_ids())
39
        .unwrap_or_default()
40
4
}
41

            
42
/// Two Transactions windows, the older raised and unzoomed so that both are drawn.
43
3
fn two_transactions() -> (App, WindowId, WindowId) {
44
3
    let mut app = app_on(120, 36);
45
3
    app.attach_echo();
46
3
    app.switch_tab(ViewId::Transactions);
47
3
    app.toggle_zoom();
48
3
    let first = app.window_of(ViewId::Transactions);
49
3
    open_from_menu(&mut app, ViewId::Transactions);
50
3
    let second = app.window_of(ViewId::Transactions);
51
3
    (app, first, second)
52
3
}
53

            
54
#[tokio::test]
55
1
async fn the_view_menu_opens_a_second_window_and_both_load() {
56
1
    let (app, first, second) = two_transactions();
57
1
    assert_ne!(first, second);
58
1
    assert_eq!(app.window_ids(ViewId::Transactions), [second, first]);
59
1
    assert_eq!(app.front_window(), Some(second));
60
1
    assert!(loading(&app, first));
61
1
    assert!(loading(&app, second));
62
1
}
63

            
64
#[tokio::test]
65
1
async fn a_key_acts_on_the_front_window_only() {
66
1
    let (mut app, first, second) = two_transactions();
67
1
    deliver(&mut app, first, &page_wire(&["a", "b"]));
68
1
    deliver(&mut app, second, &page_wire(&["a", "b"]));
69
1
    apply(&mut app, Intent::ListMark);
70
1
    assert_eq!(marked(&mut app, second), ["a"]);
71
1
    assert!(marked(&mut app, first).is_empty());
72
1
    app.activate_window(first);
73
1
    apply(&mut app, Intent::ListMark);
74
1
    apply(&mut app, Intent::ListMark);
75
1
    assert_eq!(marked(&mut app, first), ["a", "b"]);
76
1
    assert_eq!(marked(&mut app, second), ["a"]);
77
1
}
78

            
79
#[tokio::test]
80
1
async fn a_dialog_belongs_to_the_window_it_was_opened_in() {
81
1
    let (mut app, first, second) = two_transactions();
82
1
    for window in [second, first, second] {
83
3
        app.activate_window(window);
84
3
        apply(&mut app, Intent::ListFilter);
85
3
        assert_eq!(app.overlays.origin().map(|o| o.window), Some(window));
86
3
        apply(&mut app, Intent::CloseTopmost);
87
3
        assert!(app.overlays.is_empty());
88
1
    }
89
1
}
90

            
91
#[tokio::test]
92
1
async fn every_view_opens_a_second_window_from_the_menu() {
93
1
    let mut app = app_on(120, 36);
94
1
    app.attach_echo();
95
1
    for kind in ViewId::ALL {
96
10
        let before = app.window_ids(kind);
97
10
        open_from_menu(&mut app, kind);
98
10
        let after = app.window_ids(kind);
99
10
        assert_eq!(after.len(), before.len() + 1, "{kind:?}");
100
10
        assert_eq!(app.front_window(), after.first().copied(), "{kind:?}");
101
10
        assert_eq!(app.window_view(after[0]), Some(kind));
102
1
    }
103
1
}
104

            
105
#[test]
106
1
fn a_view_is_marked_while_one_of_its_windows_is_open() {
107
1
    let mut app = crate::test_support::app();
108
1
    let tags = ViewId::Tags.index();
109
1
    app.open_menu();
110
1
    assert!(app.menu_entries()[tags].marked);
111
1
    app.close_menu();
112
1
    app.close_window(app.window_of(ViewId::Tags));
113
1
    app.open_menu();
114
1
    assert!(!app.menu_entries()[tags].marked);
115
1
    app.select_menu_item(tags);
116
1
    apply(&mut app, Intent::MenuSelect);
117
1
    app.open_menu();
118
1
    assert!(app.menu_entries()[tags].marked);
119
1
}
120

            
121
#[test]
122
1
fn a_digit_raises_the_most_recent_window_of_its_view_and_opens_none() {
123
1
    let mut app = crate::test_support::app();
124
1
    let older = app.window_of(ViewId::Transactions);
125
1
    let newer = app.open_extra(ViewId::Transactions);
126
1
    app.switch_tab(ViewId::Config);
127
1
    let count = app.windows.len();
128
1
    press(&mut app, key(KeyCode::Char('2')));
129
1
    assert_eq!(app.front_window(), Some(newer));
130
1
    assert_eq!(app.windows.len(), count);
131
1
    assert_ne!(Some(older), app.front_window());
132
1
}