canic_testkit/
lib.rs

1use canic::cdk::types::{Account, Principal};
2
3pub mod pic;
4
5///
6/// Fake
7///
8
9pub struct Fake;
10
11impl Fake {
12    #[must_use]
13    pub fn account(seed: u32) -> Account {
14        let mut sub = [0u8; 32];
15        let bytes = seed.to_be_bytes();
16        sub[..4].copy_from_slice(&bytes);
17
18        Account {
19            owner: Self::principal(seed),
20            subaccount: Some(sub),
21        }
22    }
23
24    #[must_use]
25    pub fn principal(seed: u32) -> Principal {
26        let mut buf = [0u8; 29];
27        buf[..4].copy_from_slice(&seed.to_be_bytes());
28
29        Principal::from_slice(&buf)
30    }
31}
32
33///
34/// TESTS
35///
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn fake_account_is_deterministic_and_unique() {
43        let a1 = Fake::account(42);
44        let a2 = Fake::account(42);
45        let b = Fake::account(99);
46
47        // Deterministic: same seed => same account
48        assert_eq!(a1, a2, "Fake::account should be deterministic");
49
50        // Unique: different seeds => different account
51        assert_ne!(a1, b, "Fake::account should vary by seed");
52    }
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}