1
//! Palette args go through one parser: unknown and repeated keys are errors,
2
//! an empty value is not given, and `account balance` resolves a label.
3

            
4
use super::edit::options_wire;
5
use crate::app::App;
6
use crate::event::{Intent, apply};
7
use crate::route::RouteCtx;
8
use crate::tabs::nms_eval::double::Sent;
9
use crate::view::Tab;
10

            
11
const CASH: &str = "550e8400-e29b-41d4-a716-446655440071";
12
const BANK: &str = "550e8400-e29b-41d4-a716-446655440072";
13

            
14
27
fn run(app: &mut App, command: &str) {
15
27
    apply(app, Intent::OpenCommandLine);
16
27
    command
17
27
        .chars()
18
723
        .for_each(|c| apply(app, Intent::InsertChar(c)));
19
27
    apply(app, Intent::Submit);
20
27
}
21

            
22
27
fn connected() -> (App, Sent) {
23
27
    let mut app = crate::test_support::app();
24
27
    let sent = app.attach_echo();
25
27
    (app, sent)
26
27
}
27

            
28
3
fn accounts() -> String {
29
3
    options_wire(&[(CASH, "Assets:Cash"), (BANK, "Assets:Bank")])
30
3
}
31

            
32
#[tokio::test]
33
1
async fn every_leaf_rejects_an_unknown_arg() {
34
1
    for line in [
35
1
        "version bogus=1",
36
1
        "account balance account=x bogus=1",
37
1
        "account create bogus=1",
38
1
        "account edit bogus=1",
39
1
        "account list bogus=1",
40
1
        "account merge bogus=1",
41
1
        "commodity create bogus=1",
42
1
        "commodity edit bogus=1",
43
1
        "commodity convert bogus=1",
44
1
        "commodity list bogus=1",
45
1
        "transaction create bogus=1",
46
1
        "transaction list bogus=1",
47
1
        "config get bogus=1",
48
1
        "config set bogus=1",
49
1
        "reports balance bogus=1",
50
1
    ] {
51
15
        let (mut app, _) = connected();
52
15
        run(&mut app, line);
53
15
        assert_eq!(
54
15
            app.status, "command error: Arguments error: unknown arg: bogus",
55
1
            "{line}"
56
1
        );
57
15
        assert!(app.overlays.is_empty(), "{line}");
58
15
        assert!(app.pending_routes.is_empty(), "{line}");
59
1
    }
60
1
}
61

            
62
#[tokio::test]
63
1
async fn every_parsed_leaf_rejects_a_repeated_arg() {
64
1
    for (line, key) in [
65
1
        ("account balance account=a account=b", "account"),
66
1
        ("account merge from=a from=b", "from"),
67
1
        ("transaction list from=2026-01-01 from=2026-02-01", "from"),
68
1
        ("config get key=a key=b", "key"),
69
1
        ("config set key=a key=b", "key"),
70
1
        ("reports activity from=2026-01-01 from=2026-02-01", "from"),
71
1
    ] {
72
6
        let (mut app, _) = connected();
73
6
        run(&mut app, line);
74
6
        assert_eq!(
75
6
            app.status,
76
6
            format!("command error: Arguments error: duplicate arg: {key}"),
77
1
            "{line}"
78
1
        );
79
6
        assert!(app.pending_routes.is_empty(), "{line}");
80
1
    }
81
1
}
82

            
83
#[tokio::test]
84
1
async fn an_empty_transaction_list_value_is_not_a_filter() {
85
1
    for line in ["transaction list from=", "transaction list account="] {
86
2
        let (mut app, _) = connected();
87
2
        run(&mut app, line);
88
2
        assert_eq!(app.front_kind(), Some(Tab::Transactions), "{line}");
89
2
        assert!(!app.status.contains("error"), "{line}: {}", app.status);
90
2
        assert!(
91
2
            !app.pending_routes
92
2
                .values()
93
2
                .any(|route| matches!(route.ctx, RouteCtx::Lookup { .. })),
94
1
            "{line}"
95
1
        );
96
1
    }
97
1
}
98

            
99
#[tokio::test]
100
1
async fn account_balance_resolves_a_label_into_the_console() {
101
1
    let (mut app, _) = connected();
102
1
    run(&mut app, "account balance account=Assets:Cash");
103
1
    let id = *app.pending_routes.keys().next().expect("account lookup");
104
1
    app.route_reply(id, &accounts());
105
1
    assert_eq!(app.front_kind(), Some(Tab::Console));
106
1
    let expected = format!(r#"> (get-balances "{CASH}")"#);
107
1
    assert_eq!(app.console().scrollback.last(), Some(&expected));
108
1
}
109

            
110
#[tokio::test]
111
1
async fn account_balance_resolves_an_id() {
112
1
    let (mut app, _) = connected();
113
1
    run(&mut app, &format!("account balance account={BANK}"));
114
1
    let id = *app.pending_routes.keys().next().expect("account lookup");
115
1
    app.route_reply(id, &accounts());
116
1
    assert!(app.console().scrollback.join("\n").contains(BANK));
117
1
}
118

            
119
#[tokio::test]
120
1
async fn account_balance_with_an_unknown_label_says_so() {
121
1
    let (mut app, _) = connected();
122
1
    run(&mut app, "account balance account=Nowhere");
123
1
    let id = *app.pending_routes.keys().next().expect("account lookup");
124
1
    app.route_reply(id, &accounts());
125
1
    assert!(app.status.contains("matches no entry"), "{}", app.status);
126
1
    assert!(!app.front_is(Tab::Console));
127
1
}
128

            
129
#[tokio::test]
130
1
async fn account_balance_without_an_account_says_so() {
131
1
    let (mut app, _) = connected();
132
1
    run(&mut app, "account balance");
133
1
    assert_eq!(
134
1
        app.status,
135
1
        "command error: Arguments error: missing required arg: account"
136
    );
137
1
    assert!(app.pending_routes.is_empty());
138
1
}