Skip to main content

ic_testkit/
lib.rs

1//! Focused `PocketIC` test-harness utilities for Internet Computer canisters.
2//!
3//! Import native `PocketIC` clients and DTOs directly from `ic_platform::pocket_ic`.
4//! `pic` adds optional typed Wire calls, extension traits for
5//! installation, diagnostics and snapshots, startup errors, caller-owned
6//! managed-server startup and bounded readiness polling. It does not provide a
7//! simulator wrapper or a host-wide runtime lock.
8//!
9//! The crate also provides:
10//!
11//! - marker parsing, aggregation, comparison, and reports in [`benchmark`];
12//! - canister-side marker emission in `performance` (Wasm only);
13//! - deterministic test principals through [`fake_principal`].
14//!
15//! The `pic` helpers are unavailable
16//! when compiling for `wasm32`; benchmark data types and marker emission remain
17//! available to canister code.
18
19pub mod benchmark;
20
21#[cfg(not(target_arch = "wasm32"))]
22pub mod pic;
23
24#[cfg(target_arch = "wasm32")]
25pub mod performance;
26
27use ic_platform::principal::ExternalPrincipal as Principal;
28
29/// Deterministically derive a test [`Principal`] from `seed`.
30///
31/// Values are derived directly from a numeric seed, making fixtures stable
32/// without embedding textual principal literals.
33#[must_use]
34pub fn fake_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#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn fake_principal_is_deterministic_and_unique() {
47        let p1: Principal = fake_principal(7);
48        let p2 = fake_principal(7);
49        let q = fake_principal(8);
50
51        assert_eq!(p1, p2, "fake_principal should be deterministic");
52        assert_ne!(p1, q, "fake_principal should differ for different seeds");
53
54        let bytes = p1.as_slice();
55        assert_eq!(bytes.len(), 29, "Principal must be 29 bytes");
56    }
57}