Skip to main content

ic_testkit/pic/
standalone.rs

1use candid::Principal;
2use pocket_ic::PocketIc;
3
4use super::{CanisterInstallExt, InstallSpec, PocketIcInstance, StandaloneCanisterInstallError};
5
6/// One caller-built PocketIC instance/server owner and one installed canister.
7///
8/// This fixture retains the instance and its optional managed server together.
9/// It does not impose process-wide synchronization or hide PocketIC methods.
10pub struct StandaloneCanisterFixture {
11    instance: PocketIcInstance<PocketIc>,
12    canister_id: Principal,
13}
14
15impl StandaloneCanisterFixture {
16    /// Install one canister into a caller-configured PocketIC instance.
17    ///
18    /// On failure, the error retains both the caller's instance/server ownership record and
19    /// the structured install failure.
20    pub fn try_install(
21        instance: PocketIcInstance<PocketIc>,
22        spec: InstallSpec,
23    ) -> Result<Self, StandaloneCanisterInstallError> {
24        let canister_id = match instance.instance.try_create_and_install(spec) {
25            Ok(canister_id) => canister_id,
26            Err(error) => return Err(StandaloneCanisterInstallError::new(instance, error)),
27        };
28
29        Ok(Self {
30            instance,
31            canister_id,
32        })
33    }
34
35    /// Borrow the PocketIC instance that owns this standalone fixture.
36    #[must_use]
37    pub const fn pocket_ic(&self) -> &PocketIc {
38        &self.instance.instance
39    }
40
41    /// Read the installed canister id for this standalone fixture.
42    #[must_use]
43    pub const fn canister_id(&self) -> Principal {
44        self.canister_id
45    }
46
47    /// Consume the fixture and return the instance/server ownership record and canister id.
48    pub fn into_parts(self) -> (PocketIcInstance<PocketIc>, Principal) {
49        (self.instance, self.canister_id)
50    }
51}