ic_test/icp/
mod.rs

1use std::{sync::Arc, time::Duration};
2
3use candid::Principal;
4use pocket_ic::{nonblocking::PocketIc, PocketIcBuilder, Time};
5use test_principals::TEST_PRINCIPALS;
6use user::IcpUser;
7
8pub mod caller;
9pub mod deployer;
10pub mod provider;
11pub mod user;
12
13#[cfg(feature = "evm")]
14pub(crate) mod http_outcalls;
15
16pub(crate) mod test_principals;
17
18pub struct Icp {
19    pub pic: Arc<PocketIc>,
20}
21
22impl Icp {
23    pub async fn new() -> Self {
24        let pic = PocketIcBuilder::new()
25            .with_nns_subnet()
26            .with_ii_subnet()
27            .with_log_level(slog::Level::Error)
28            .build_async()
29            .await;
30
31        let time = Time::from_nanos_since_unix_epoch(1_740_000_000_000_000_000);
32
33        pic.set_time(time).await;
34
35        Self { pic: Arc::new(pic) }
36    }
37
38    pub fn test_user_count(&self) -> usize {
39        TEST_PRINCIPALS.len()
40    }
41
42    pub fn test_user(&self, index: usize) -> IcpUser {
43        if index >= self.test_user_count() {
44            panic!(
45                "Reached maximum number of test users: {}",
46                self.test_user_count()
47            );
48        }
49        self.user_from(Principal::from_text(TEST_PRINCIPALS[index]).unwrap())
50    }
51
52    pub fn default_user(&self) -> IcpUser {
53        self.test_user(0)
54    }
55
56    pub fn user_from(&self, principal: Principal) -> IcpUser {
57        IcpUser {
58            principal,
59            pic: Arc::clone(&self.pic),
60        }
61    }
62
63    pub async fn tick(&self) {
64        self.pic.advance_time(Duration::from_secs(1)).await;
65        self.pic.tick().await;
66    }
67
68    pub fn pocket_ic(&self) -> &PocketIc {
69        &self.pic
70    }
71}