Skip to main content

ic_testkit/pic/
standalone.rs

1use ic_platform::pocket_ic::PocketIc;
2use ic_platform::principal::ExternalPrincipal as Principal;
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    ///
21    /// # Errors
22    /// Returns the installation failure with the owned instance for caller recovery.
23    pub fn try_install(
24        instance: PocketIcInstance<PocketIc>,
25        spec: InstallSpec,
26    ) -> Result<Self, StandaloneCanisterInstallError> {
27        let canister_id = match instance.instance().try_create_and_install(spec) {
28            Ok(canister_id) => canister_id,
29            Err(error) => return Err(StandaloneCanisterInstallError::new(instance, error)),
30        };
31
32        Ok(Self {
33            instance,
34            canister_id,
35        })
36    }
37
38    /// Borrow the `PocketIC` instance that owns this standalone fixture.
39    #[must_use]
40    pub const fn pocket_ic(&self) -> &PocketIc {
41        self.instance.instance()
42    }
43
44    /// Read the installed canister id for this standalone fixture.
45    #[must_use]
46    pub const fn canister_id(&self) -> Principal {
47        self.canister_id
48    }
49
50    /// Consume the fixture and return the instance/server ownership record and canister id.
51    pub fn into_parts(self) -> (PocketIcInstance<PocketIc>, Principal) {
52        (self.instance, self.canister_id)
53    }
54}