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
use crate::jcli::command::rest::v0::UtxOCommand;
use assert_cmd::assert::OutputAssertExt;
use jormungandr_lib::{
    crypto::hash::Hash,
    interfaces::{UTxOInfo, UTxOOutputInfo},
};
use jortestkit::prelude::ProcessOutput;
use std::str::FromStr;
pub struct UtxO {
    utxo_command: UtxOCommand,
}

impl UtxO {
    pub fn new(utxo_command: UtxOCommand) -> Self {
        Self { utxo_command }
    }

    pub fn assert_contains<S: Into<String>>(self, utxo: &UTxOInfo, host: S) {
        assert_eq!(self.get_by_item(utxo, host), *utxo)
    }

    pub fn get_by_item<S: Into<String>>(self, utxo: &UTxOInfo, host: S) -> UTxOInfo {
        self.get(
            utxo.transaction_id().to_string(),
            utxo.index_in_transaction(),
            host.into(),
        )
    }

    pub fn get<S: Clone + Into<String>>(
        self,
        fragment_id: S,
        output_index: u8,
        host: S,
    ) -> UTxOInfo {
        let content = self
            .utxo_command
            .fragment_id(fragment_id.clone().into())
            .output_index(output_index)
            .get()
            .host(host.into())
            .build()
            .assert()
            .success()
            .get_output()
            .as_lossy_string();

        serde_yaml::from_str::<UTxOOutputInfo>(&content)
            .expect("JCLI returned malformed UTxO")
            .into_utxo_info(Hash::from_str(&fragment_id.into()).unwrap(), output_index)
    }

    pub fn expect_item_not_found<S: Into<String>>(self, utxo: &UTxOInfo, host: S) {
        self.expect_not_found(
            &utxo.transaction_id().to_string(),
            utxo.index_in_transaction(),
            &host.into(),
        )
    }

    pub fn expect_not_found<S: Into<String>>(self, fragment_id: S, output_index: u8, host: S) {
        self.utxo_command
            .fragment_id(fragment_id.into())
            .output_index(output_index)
            .get()
            .host(host.into())
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains("404 Not Found"));
    }
}