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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::jcli::command::votes::TallyCommand;
use assert_cmd::assert::OutputAssertExt;
use jcli_lib::vote::MergedVotePlan;
use jortestkit::prelude::ProcessOutput;
use std::path::Path;

pub struct Tally {
    tally_command: TallyCommand,
}

impl Tally {
    pub fn new(tally_command: TallyCommand) -> Self {
        Self { tally_command }
    }

    pub fn decryption_shares<P: AsRef<Path>, Q: AsRef<Path>, S: Into<String>>(
        self,
        vote_plan: Q,
        vote_plan_id: S,
        member_key: P,
    ) -> String {
        self.tally_command
            .decryption_shares(vote_plan, vote_plan_id, member_key)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line()
    }

    pub fn decrypt_results<P: AsRef<Path>, R: AsRef<Path>, S: Into<String>>(
        self,
        vote_plan: P,
        vote_plan_id: S,
        shares: R,
        threshold: u32,
    ) -> String {
        self.tally_command
            .decrypt_results(vote_plan, vote_plan_id, shares, threshold)
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string()
    }

    pub fn decrypt_results_expect_fail<P: AsRef<Path>, R: AsRef<Path>, S: Into<String>>(
        self,
        vote_plan: P,
        vote_plan_id: S,
        shares: R,
        threshold: u32,
        expected_msg: &str,
    ) {
        self.tally_command
            .decrypt_results(vote_plan, vote_plan_id, shares, threshold)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn merge_shares<P: AsRef<Path>>(self, shares_to_merge: Vec<P>) -> String {
        self.tally_command
            .merge_shares(shares_to_merge)
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string()
    }

    pub fn merge_results<P: AsRef<Path>>(
        self,
        vote_plans: P,
    ) -> Result<Vec<MergedVotePlan>, serde_json::Error> {
        serde_json::from_str(
            &self
                .tally_command
                .merge_results(vote_plans)
                .build()
                .assert()
                .success()
                .get_output()
                .as_lossy_string(),
        )
    }
}