ic_testkit/lib.rs
1//! Focused PocketIC test-harness utilities for Internet Computer canisters.
2//!
3//! `ic-testkit` keeps PocketIC itself visible: `pocket_ic` re-exports the
4//! complete upstream crate, while `pic` retains convenient runtime type
5//! re-exports and adds extension traits for typed Candid calls, generic
6//! installation, diagnostics, snapshots, startup errors, caller-owned
7//! managed-server startup and bounded readiness polling. It does not provide a
8//! simulator wrapper or a host-wide runtime lock.
9//!
10//! The crate also provides:
11//!
12//! - marker parsing, aggregation, comparison, and reports in [`benchmark`];
13//! - canister-side marker emission in `performance` (Wasm only);
14//! - deterministic test principals through [`Fake`].
15//!
16//! The `pocket_ic` and `pic` exports are unavailable
17//! when compiling for `wasm32`; benchmark data types and marker emission remain
18//! available to canister code.
19
20pub mod benchmark;
21
22#[cfg(not(target_arch = "wasm32"))]
23pub use pocket_ic;
24
25#[cfg(not(target_arch = "wasm32"))]
26pub mod pic;
27
28#[cfg(target_arch = "wasm32")]
29pub mod performance;
30
31use candid::Principal;
32
33/// Deterministic principal generator for tests.
34///
35/// Values are derived directly from a numeric seed, making fixtures stable
36/// without embedding textual principal literals.
37pub struct Fake;
38
39impl Fake {
40 /// Deterministically derive a [`Principal`] from `seed`.
41 #[must_use]
42 pub fn principal(seed: u32) -> Principal {
43 let mut buf = [0u8; 29];
44 buf[..4].copy_from_slice(&seed.to_be_bytes());
45
46 Principal::from_slice(&buf)
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn fake_principal_is_deterministic_and_unique() {
56 let p1 = Fake::principal(7);
57 let p2 = Fake::principal(7);
58 let q = Fake::principal(8);
59
60 assert_eq!(p1, p2, "Fake::principal should be deterministic");
61 assert_ne!(p1, q, "Fake::principal should differ for different seeds");
62
63 let bytes = p1.as_slice();
64 assert_eq!(bytes.len(), 29, "Principal must be 29 bytes");
65 }
66}