1
//! Tests for routing fetched account/commodity options into the open
2
//! transaction-create form's split rows (FormOptions source routing).
3

            
4
use super::{accounts_wire, make};
5
use crate::form::Form;
6
use crate::modal::Modal;
7
use crate::route::{FormOptionsSource, Route, RouteCtx};
8
use crate::view::ViewId;
9
use crate::widgets::{EditMode, Widget};
10

            
11
/// One-commodity list-commodities reply wire for the splits commodity fetch.
12
1
fn commodities_wire(id: u64, uuid: &str, symbol: &str) -> String {
13
1
    format!(r#"(:id {id} :value "((:id \"{uuid}\" :symbol \"{symbol}\" :name \"\"))")"#)
14
1
}
15

            
16
#[test]
17
1
fn deliver_reply_commodity_options_populates_transaction_splits() {
18
1
    let mut app = make();
19
1
    app.overlays
20
1
        .push(Modal::Form(Form::transaction_create(EditMode::Emacs)));
21
1
    app.form_options_seq = 1;
22
1
    let uuid = "550e8400-e29b-41d4-a716-446655440010";
23
1
    let wire = commodities_wire(7, uuid, "USD");
24
1
    app.deliver_reply(
25
1
        Route {
26
1
            target: ViewId::Commodities,
27
1
            ctx: RouteCtx::FormOptions {
28
1
                seq: 1,
29
1
                source: FormOptionsSource::Commodities,
30
1
            },
31
1
        },
32
1
        &wire,
33
    );
34
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
35
        panic!("transaction form must be open");
36
    };
37
1
    let Widget::Splits(sw) = &form.fields[2].widget else {
38
        panic!("field 2 must be Splits");
39
    };
40
1
    let row = &sw.rows()[0];
41
1
    assert_eq!(
42
1
        row.from_commodity.option_count(),
43
        1,
44
        "from-commodity populated"
45
    );
46
1
    assert_eq!(row.to_commodity.option_count(), 1, "to-commodity populated");
47
1
    assert_eq!(row.from_commodity.value(), uuid);
48
1
}
49

            
50
#[test]
51
1
fn deliver_reply_account_options_populates_transaction_splits() {
52
1
    let mut app = make();
53
1
    app.overlays
54
1
        .push(Modal::Form(Form::transaction_create(EditMode::Emacs)));
55
1
    app.form_options_seq = 1;
56
1
    let uuid = "550e8400-e29b-41d4-a716-446655440011";
57
1
    let wire = accounts_wire(8, uuid, "Cash");
58
1
    app.deliver_reply(
59
1
        Route {
60
1
            target: ViewId::Accounts,
61
1
            ctx: RouteCtx::FormOptions {
62
1
                seq: 1,
63
1
                source: FormOptionsSource::Accounts,
64
1
            },
65
1
        },
66
1
        &wire,
67
    );
68
1
    let Some(Modal::Form(form)) = app.overlays.top() else {
69
        panic!("transaction form must be open");
70
    };
71
1
    let Widget::Splits(sw) = &form.fields[2].widget else {
72
        panic!("field 2 must be Splits");
73
    };
74
1
    let row = &sw.rows()[0];
75
    // The (none) sentinel + the single account.
76
1
    assert_eq!(
77
1
        row.from.option_count(),
78
        2,
79
        "from-account populated with (none)+1"
80
    );
81
1
    assert_eq!(row.to.option_count(), 2, "to-account populated");
82
1
}