use canic::{
Error,
cdk::types::Principal,
dto::{topology::SubnetRegistryEntry, topology::SubnetRegistryResponse},
protocol,
};
use canic_internal::canister::SCALE;
use canic_testkit::pic::Pic;
pub fn create_worker(pic: &Pic, hub_pid: Principal) -> Result<Principal, Error> {
let result: Result<Result<Principal, Error>, Error> =
pic.update_call(hub_pid, "create_worker", ());
result.map_err(|err| Error::internal(format!("create_worker transport failed: {err}")))?
}
pub fn count_workers(pic: &Pic, root_id: Principal, parent_pid: Principal) -> usize {
let registry: Result<SubnetRegistryResponse, Error> = pic
.query_call(root_id, protocol::CANIC_SUBNET_REGISTRY, ())
.expect("query subnet registry transport");
let SubnetRegistryResponse(registry): SubnetRegistryResponse =
registry.expect("query subnet registry application");
registry
.iter()
.filter(|entry: &&SubnetRegistryEntry| {
entry.role == SCALE && entry.record.parent_pid == Some(parent_pid)
})
.count()
}