Skip to main content

ic_testkit/
lib.rs

1//! Reusable PocketIC-oriented test utilities for IC canister tests.
2//!
3//! This crate is intended for host-side test environments (for example via
4//! PocketIC) and provides generic helpers such as stable dummy principals,
5//! PocketIC re-exports, standalone canister fixtures, generic prebuilt wasm
6//! install helpers, retry helpers for PocketIC install throttling, and cached
7//! baseline primitives.
8
9pub mod benchmark;
10
11#[cfg(not(target_arch = "wasm32"))]
12pub mod artifacts;
13
14#[cfg(not(target_arch = "wasm32"))]
15pub mod pic;
16
17pub mod performance;
18use candid::Principal;
19
20///
21/// Deterministic dummy-value generator for tests.
22///
23/// Produces stable principals derived from a numeric seed, which makes tests
24/// reproducible without hardcoding raw byte arrays.
25///
26
27pub struct Fake;
28
29impl Fake {
30    ///
31    /// Deterministically derive a [`Principal`] from `seed`.
32    ///
33    #[must_use]
34    pub fn principal(seed: u32) -> Principal {
35        let mut buf = [0u8; 29];
36        buf[..4].copy_from_slice(&seed.to_be_bytes());
37
38        Principal::from_slice(&buf)
39    }
40}
41
42///
43/// TESTS
44///
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn fake_principal_is_deterministic_and_unique() {
52        let p1 = Fake::principal(7);
53        let p2 = Fake::principal(7);
54        let q = Fake::principal(8);
55
56        assert_eq!(p1, p2, "Fake::principal should be deterministic");
57        assert_ne!(p1, q, "Fake::principal should differ for different seeds");
58
59        let bytes = p1.as_slice();
60        assert_eq!(bytes.len(), 29, "Principal must be 29 bytes");
61    }
62}