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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#![allow(dead_code)]

use assert_fs::TempDir;
use jormungandr_automation::jormungandr::{
    JormungandrBootstrapper, JormungandrProcess, LegacyNodeConfig, SecretModelFactory, Starter,
    StartupError, Version,
};
use jormungandr_lib::interfaces::{Block0Configuration, NodeConfig};
use std::path::PathBuf;
use thor::Wallet;

pub struct TestContext {
    pub block0_config: Block0Configuration,
    pub node_config: NodeConfig,
    pub secret_factory: SecretModelFactory,
    pub reward_history: bool,
}

impl TestContext {
    pub fn restart_node(
        &self,
        mut jormungandr: JormungandrProcess,
    ) -> Result<JormungandrProcess, StartupError> {
        let temp_dir = jormungandr.steal_temp_dir().unwrap().try_into().unwrap();
        self.start_node(temp_dir)
    }

    pub fn node_config(&self) -> NodeConfig {
        self.node_config.clone()
    }

    pub fn start_node(&self, temp_dir: TempDir) -> Result<JormungandrProcess, StartupError> {
        let mut bootstrapper =
            JormungandrBootstrapper::default_with_config(self.node_config.clone())
                .with_block0_configuration(self.block0_config.clone())
                .with_secret(self.secret_factory.clone());
        if self.reward_history {
            bootstrapper = bootstrapper.with_rewards_history();
        }
        bootstrapper.start(temp_dir)
    }

    pub(crate) fn starter(&self, temp_dir: TempDir) -> Result<Starter, StartupError> {
        let mut bootstrapper =
            JormungandrBootstrapper::default_with_config(self.node_config.clone())
                .with_block0_configuration(self.block0_config.clone())
                .with_secret(self.secret_factory.clone());
        if self.reward_history {
            bootstrapper = bootstrapper.with_rewards_history();
        }
        bootstrapper.into_starter(temp_dir).map_err(Into::into)
    }

    pub(crate) fn block0_config(&self) -> Block0Configuration {
        self.block0_config.clone()
    }
}

pub struct ActorsTestContext {
    pub(crate) test_context: TestContext,
    pub(crate) alice: Option<Wallet>,
    pub(crate) bob: Option<Wallet>,
}

impl ActorsTestContext {
    pub(crate) fn start_node(&self, temp_dir: TempDir) -> Result<JormungandrProcess, StartupError> {
        self.test_context.start_node(temp_dir)
    }

    pub(crate) fn block0_config(&self) -> Block0Configuration {
        self.test_context.block0_config()
    }

    pub fn alice(&self) -> Wallet {
        self.alice.clone().expect("alice not defined")
    }

    pub(crate) fn bob(&self) -> Wallet {
        self.bob.clone().expect("bob not defined")
    }
}

pub struct LegacyTestContext {
    pub test_context: TestContext,
    pub legacy_node_config: LegacyNodeConfig,
    pub jormungandr_app: Option<PathBuf>,
    pub version: Version,
}

impl LegacyTestContext {
    pub(crate) fn start_node(&self, temp_dir: TempDir) -> Result<JormungandrProcess, StartupError> {
        let mut bootstrapper = JormungandrBootstrapper::default()
            .with_block0_configuration(self.block0_config())
            .with_legacy_node_config(self.legacy_node_config.clone())
            .with_secret(self.test_context.secret_factory.clone());
        if self.test_context.reward_history {
            bootstrapper = bootstrapper.with_rewards_history();
        }
        bootstrapper.start(temp_dir)
    }

    pub(crate) fn starter(&self, temp_dir: TempDir) -> Result<Starter, StartupError> {
        let mut bootstrapper = JormungandrBootstrapper::default()
            .with_block0_configuration(self.block0_config())
            .with_legacy_node_config(self.legacy_node_config.clone())
            .with_secret(self.test_context.secret_factory.clone());
        if self.test_context.reward_history {
            bootstrapper = bootstrapper.with_rewards_history();
        }
        let mut starter = bootstrapper.into_starter(temp_dir)?;
        if let Some(jormungandr) = &self.jormungandr_app {
            starter = starter.jormungandr_app(jormungandr.to_path_buf());
        }
        Ok(starter.verbose(true))
    }

    pub(crate) fn block0_config(&self) -> Block0Configuration {
        self.test_context.block0_config()
    }
}