1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use chain_addr::AddressReadable;
use chain_impl_mockchain::key::Hash;
use jormungandr_lib::interfaces::{Initial, InitialUTxO, PrivateTallyState, Tally, VotePlanStatus};
use std::str::FromStr;

pub trait VotePlanStatusAssert {
    fn assert_proposal_tally(&self, vote_plan_id: String, index: u8, expected: Vec<u64>);
}

impl VotePlanStatusAssert for Vec<VotePlanStatus> {
    fn assert_proposal_tally(&self, vote_plan_id: String, index: u8, expected: Vec<u64>) {
        let vote_plan_status = self
            .iter()
            .find(|c_vote_plan| c_vote_plan.id == Hash::from_str(&vote_plan_id).unwrap().into())
            .unwrap();

        let tally = &vote_plan_status
            .proposals
            .iter()
            .find(|x| x.index == index)
            .unwrap()
            .tally;

        match tally {
            Tally::Public { result } => assert_eq!(expected, result.results()),
            Tally::Private { state } => match state {
                PrivateTallyState::Encrypted { .. } => {
                    panic!("expected decrypted private tally state")
                }
                PrivateTallyState::Decrypted { result, .. } => {
                    assert_eq!(expected, result.results())
                }
            },
        }
    }
}

pub trait InitialsAssert {
    fn assert_contains(&self, entry: InitialUTxO);
    fn assert_not_contain(&self, entry: InitialUTxO);
}

impl InitialsAssert for Vec<Initial> {
    fn assert_contains(&self, expected: InitialUTxO) {
        let address_readable =
            AddressReadable::from_address("ca", &expected.address.clone().into()).to_string();
        for initial in self.iter() {
            if let Initial::Fund(initial_utxos) = initial {
                if let Some(entry) = initial_utxos.iter().find(|x| x.address == expected.address) {
                    assert_eq!(
                        entry.value, expected.value,
                        "Address {} found but value is different",
                        address_readable
                    );
                    return;
                }
            }
        }
        panic!("Address {} not found", address_readable);
    }

    fn assert_not_contain(&self, entry: InitialUTxO) {
        let address_readable =
            AddressReadable::from_address("ca", &entry.address.clone().into()).to_string();
        for initial in self.iter() {
            if let Initial::Fund(initial_utxos) = initial {
                if initial_utxos.iter().any(|x| x.address == entry.address) {
                    panic!("Address {} found, while it shouldn't", address_readable);
                }
            }
        }
    }
}