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
use crate::jcli::command::GenesisCommand;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::{assert::PathAssert, fixture::ChildPath};
use jormungandr_lib::crypto::hash::Hash;
use jortestkit::prelude::ProcessOutput;
use std::{path::Path, str::FromStr};
pub struct Genesis {
    genesis_command: GenesisCommand,
}

impl Genesis {
    pub fn new(genesis_command: GenesisCommand) -> Self {
        Self { genesis_command }
    }

    pub fn decode<P: AsRef<Path>>(self, input: P, output: &ChildPath) {
        self.genesis_command
            .decode()
            .input(input)
            .output(output.path())
            .build()
            .assert()
            .success();
        output.assert(jortestkit::prelude::file_exists_and_not_empty());
    }

    pub fn encode<P: AsRef<Path>>(self, input: P, output: &ChildPath) {
        self.genesis_command
            .encode()
            .input(input)
            .output(output.path())
            .build()
            .assert()
            .success();

        output.assert(jortestkit::prelude::file_exists_and_not_empty());
    }

    pub fn encode_expect_fail<P: AsRef<Path>>(self, input: P, expected_msg: &str) {
        self.genesis_command
            .encode()
            .input(input)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn hash<P: AsRef<Path>>(self, input: P) -> Hash {
        let hash = self
            .genesis_command
            .hash()
            .input(input)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line();

        Hash::from_str(&hash).unwrap()
    }

    pub fn hash_expect_fail<P: AsRef<Path>>(self, input: P, expected_msg: &str) {
        self.genesis_command
            .hash()
            .input(input)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn init(self) -> String {
        self.genesis_command
            .init()
            .assert()
            .success()
            .get_output()
            .as_lossy_string()
    }
}