fuel_core_e2e_client/
lib.rs

1use crate::{
2    config::SuiteConfig,
3    test_context::TestContext,
4};
5use libtest_mimic::{
6    Arguments,
7    Failed,
8    Trial,
9};
10use std::{
11    env,
12    fs,
13    future::Future,
14    time::Duration,
15};
16
17pub const CONFIG_FILE_KEY: &str = "FUEL_CORE_E2E_CONFIG";
18pub const SYNC_TIMEOUT: Duration = Duration::from_secs(20);
19
20pub mod config;
21pub mod test_context;
22pub mod tests;
23
24pub fn main_body(config: SuiteConfig, mut args: Arguments) {
25    fn with_cloned(
26        config: &SuiteConfig,
27        f: impl FnOnce(SuiteConfig) -> anyhow::Result<(), Failed>,
28    ) -> impl FnOnce() -> anyhow::Result<(), Failed> {
29        let config = config.clone();
30        move || f(config)
31    }
32
33    // If we run tests in parallel they may fail because try to use the same state like UTXOs.
34    args.test_threads = Some(1);
35
36    let tests = vec![
37        Trial::test(
38            "can transfer from alice to bob",
39            with_cloned(&config, |config| {
40                async_execute(async {
41                    let ctx = TestContext::new(config).await;
42                    tests::transfers::basic_transfer(&ctx).await
43                })
44            }),
45        ),
46        Trial::test(
47            "can transfer from alice to bob and back",
48            with_cloned(&config, |config| {
49                async_execute(async {
50                    let ctx = TestContext::new(config).await;
51                    tests::transfers::transfer_back(&ctx).await
52                })
53            }),
54        ),
55        Trial::test(
56            "can collect fee from alice",
57            with_cloned(&config, |config| {
58                async_execute(async {
59                    let ctx = TestContext::new(config).await;
60                    tests::collect_fee::collect_fee(&ctx).await
61                })
62            }),
63        ),
64        Trial::test(
65            "can execute script and get receipts",
66            with_cloned(&config, |config| {
67                async_execute(async {
68                    let ctx = TestContext::new(config).await;
69                    tests::transfers::transfer_back(&ctx).await
70                })
71            }),
72        ),
73        Trial::test(
74            "can dry run transfer script and get receipts",
75            with_cloned(&config, |config| {
76                async_execute(async {
77                    let ctx = TestContext::new(config).await;
78                    tests::script::dry_run(&ctx).await
79                })?;
80                Ok(())
81            }),
82        ),
83        Trial::test(
84            "can dry run multiple transfer scripts and get receipts",
85            with_cloned(&config, |config| {
86                async_execute(async {
87                    let ctx = TestContext::new(config).await;
88                    tests::script::dry_run_multiple_txs(&ctx).await
89                })?;
90                Ok(())
91            }),
92        ),
93        Trial::test(
94            "dry run script that touches the contract with large state",
95            with_cloned(&config, |config| {
96                async_execute(async {
97                    let ctx = TestContext::new(config).await;
98                    tests::script::run_contract_large_state(&ctx).await
99                })?;
100                Ok(())
101            }),
102        ),
103        Trial::test(
104            "dry run transaction from `arbitrary_tx.raw` file",
105            with_cloned(&config, |config| {
106                async_execute(async {
107                    let ctx = TestContext::new(config).await;
108                    tests::script::arbitrary_transaction(&ctx).await
109                })?;
110                Ok(())
111            }),
112        ),
113        Trial::test(
114            "can deploy a large contract",
115            with_cloned(&config, |config| {
116                async_execute(async {
117                    let ctx = TestContext::new(config).await;
118                    tests::contracts::deploy_large_contract(&ctx).await
119                })
120            }),
121        ),
122    ];
123
124    libtest_mimic::run(&args, tests).exit();
125}
126
127pub fn load_config_env() -> SuiteConfig {
128    // load from env var
129    env::var_os(CONFIG_FILE_KEY)
130        .map(|path| load_config(path.to_string_lossy().to_string()))
131        .unwrap_or_default()
132}
133
134pub fn load_config(path: String) -> SuiteConfig {
135    let file = fs::read(path).unwrap();
136    toml::from_slice(&file).unwrap()
137}
138
139fn async_execute<F: Future<Output = anyhow::Result<(), Failed>>>(
140    func: F,
141) -> Result<(), Failed> {
142    tokio::runtime::Builder::new_current_thread()
143        .enable_all()
144        .build()
145        .unwrap()
146        .block_on(func)
147}