Skip to content

Commit 3eba78f

Browse files
committed
cleanup
1 parent e39431c commit 3eba78f

File tree

9 files changed

+63
-56
lines changed

9 files changed

+63
-56
lines changed

crates/icp-cli/src/commands/mod.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl Context {
254254
artifacts: Arc::new(crate::store_artifact::MockInMemoryArtifactStore::new()),
255255
project: Arc::new(icp::MockProjectLoader::minimal()),
256256
identity: Arc::new(icp::identity::MockIdentityLoader::anonymous()),
257-
network: Arc::new(icp::network::MockNetworkAccessor::localhost()),
257+
network: Arc::new(icp::network::MockNetworkAccessor::new()),
258258
agent: Arc::new(icp::agent::Creator),
259259
builder: Arc::new(icp::canister::build::UnimplementedMockBuilder),
260260
syncer: Arc::new(icp::canister::sync::UnimplementedMockSyncer),
@@ -543,7 +543,7 @@ mod context_tests {
543543
let ctx = Context {
544544
project: Arc::new(MockProjectLoader::complex()),
545545
network: Arc::new(
546-
MockNetworkAccessor::localhost()
546+
MockNetworkAccessor::new()
547547
.with_network(
548548
"local",
549549
NetworkAccess {
@@ -588,6 +588,27 @@ mod context_tests {
588588
));
589589
}
590590

591+
#[tokio::test]
592+
async fn test_get_agent_for_env_network_not_configured() {
593+
// Environment exists in project but its network not configured in MockNetworkAccessor
594+
let ctx = Context {
595+
project: Arc::new(MockProjectLoader::complex()),
596+
// MockNetworkAccessor has no networks configured
597+
..Context::mocked()
598+
};
599+
600+
let result = ctx
601+
.get_agent_for_env(&IdentitySelection::Anonymous, "nonexistent")
602+
.await;
603+
604+
assert!(matches!(
605+
result,
606+
Err(GetAgentForEnvError::NetworkAccess {
607+
source: icp::network::AccessError::Unexpected(_)
608+
})
609+
));
610+
}
611+
591612
#[tokio::test]
592613
async fn test_get_agent_for_network_success() {
593614
use icp::network::access::NetworkAccess;
@@ -596,7 +617,7 @@ mod context_tests {
596617

597618
let ctx = Context {
598619
project: Arc::new(MockProjectLoader::complex()),
599-
network: Arc::new(MockNetworkAccessor::localhost().with_network(
620+
network: Arc::new(MockNetworkAccessor::new().with_network(
600621
"local",
601622
NetworkAccess {
602623
default_effective_canister_id: None,
@@ -631,6 +652,27 @@ mod context_tests {
631652
));
632653
}
633654

655+
#[tokio::test]
656+
async fn test_get_agent_for_network_not_configured() {
657+
// Network exists in project but not configured in MockNetworkAccessor
658+
let ctx = Context {
659+
project: Arc::new(MockProjectLoader::complex()),
660+
// MockNetworkAccessor has no networks configured
661+
..Context::mocked()
662+
};
663+
664+
let result = ctx
665+
.get_agent_for_network(&IdentitySelection::Anonymous, "nonexistent")
666+
.await;
667+
668+
assert!(matches!(
669+
result,
670+
Err(GetAgentForNetworkError::NetworkAccess {
671+
source: icp::network::AccessError::Unexpected(_)
672+
})
673+
));
674+
}
675+
634676
#[tokio::test]
635677
async fn test_get_agent_for_url_success() {
636678
let ctx = Context::mocked();

crates/icp-cli/src/store_artifact.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ impl Access for ArtifactStore {
8989
}
9090

9191
#[cfg(test)]
92-
/// In-memory mock implementation of `Access` for testing purposes.
93-
///
94-
/// This mock stores artifacts (WASM bytes) in a HashMap instead of on disk,
95-
/// making it suitable for tests that need a working artifact store without
96-
/// file system interactions.
92+
/// In-memory mock implementation of `Access`.
9793
pub(crate) struct MockInMemoryArtifactStore {
9894
store: Mutex<HashMap<String, Vec<u8>>>,
9995
}

crates/icp-cli/src/store_id.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,7 @@ impl Access for IdStore {
182182
}
183183

184184
#[cfg(test)]
185-
/// In-memory mock implementation of `Access` for testing purposes.
186-
///
187-
/// This mock stores canister IDs in a HashMap instead of on disk,
188-
/// making it suitable for tests that need a working ID store without
189-
/// file system interactions.
185+
/// In-memory mock implementation of `Access`.
190186
pub(crate) struct MockInMemoryIdStore {
191187
store: Mutex<HashMap<Key, Principal>>,
192188
}

crates/icp/src/canister/build.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,8 @@ impl Build for Builder {
109109
}
110110

111111
#[cfg(any(test, feature = "test-features"))]
112-
/// Unimplemented mock implementation of `Build` for testing purposes.
113-
///
112+
/// Unimplemented mock implementation of `Build`.
114113
/// All methods panic with `unimplemented!()` when called.
115-
/// This is useful for tests that need to construct a context but don't
116-
/// actually use the build functionality.
117114
pub struct UnimplementedMockBuilder;
118115

119116
#[cfg(any(test, feature = "test-features"))]

crates/icp/src/canister/sync.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,8 @@ impl Synchronize for Syncer {
101101
}
102102

103103
#[cfg(any(test, feature = "test-features"))]
104-
/// Unimplemented mock implementation of `Synchronize` for testing purposes.
105-
///
104+
/// Unimplemented mock implementation of `Synchronize`.
106105
/// All methods panic with `unimplemented!()` when called.
107-
/// This is useful for tests that need to construct a context but don't
108-
/// actually use the sync functionality.
109106
pub struct UnimplementedMockSyncer;
110107

111108
#[cfg(any(test, feature = "test-features"))]

crates/icp/src/directories.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,8 @@ impl Access for Directories {
146146
}
147147

148148
#[cfg(feature = "test-features")]
149-
/// Unimplemented mock implementation of `Access` for testing purposes.
150-
///
149+
/// Unimplemented mock implementation of `Access`.
151150
/// All methods panic with `unimplemented!()` when called.
152-
/// This is useful for tests that need to construct a `Context` but don't
153-
/// actually use the directory functionality.
154151
#[derive(Debug, Clone)]
155152
pub struct UnimplementedMockDirs;
156153

crates/icp/src/identity/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ impl Load for Loader {
116116
use std::collections::HashMap;
117117

118118
#[cfg(any(test, feature = "test-features"))]
119-
/// Mock identity loader for testing.
120-
///
121-
/// Allows configuring multiple identities that can be selected by name.
122-
/// Supports default, anonymous, and named identity selections.
123119
pub struct MockIdentityLoader {
124120
/// The default identity to return when IdentitySelection::Default is used
125121
default: Arc<dyn Identity>,

crates/icp/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ impl<T: Load> Load for Lazy<T, Project> {
156156

157157
#[cfg(any(test, feature = "test-features"))]
158158
/// Mock project loader for testing.
159-
///
160159
/// Returns a pre-configured `Project` when `load()` is called.
161-
/// This is useful for tests that need a project but don't want to
162-
/// deal with file system interactions or project manifest parsing.
163160
pub struct MockProjectLoader {
164161
project: Project,
165162
}

crates/icp/src/network/mod.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -143,54 +143,43 @@ impl Access for Accessor {
143143
use std::collections::HashMap;
144144

145145
#[cfg(any(test, feature = "test-features"))]
146-
/// Mock network access provider for testing.
147-
///
148-
/// Allows configuring network access details for specific networks.
149-
/// Supports a default fallback for networks not explicitly configured.
150146
pub struct MockNetworkAccessor {
151-
/// Default network access to return for unconfigured networks
152-
default: NetworkAccess,
153-
154147
/// Network-specific access configurations by network name
155148
networks: HashMap<String, NetworkAccess>,
156149
}
157150

158151
#[cfg(any(test, feature = "test-features"))]
159152
impl MockNetworkAccessor {
160-
/// Creates a new mock network accessor with the given default.
161-
pub fn new(default: NetworkAccess) -> Self {
153+
/// Creates a new empty mock network accessor.
154+
pub fn new() -> Self {
162155
Self {
163-
default,
164156
networks: HashMap::new(),
165157
}
166158
}
167159

168-
/// Creates a mock with localhost:8000 as the default.
169-
pub fn localhost() -> Self {
170-
Self::new(NetworkAccess::new("http://localhost:8000"))
171-
}
172-
173160
/// Adds a network-specific access configuration.
174161
pub fn with_network(mut self, name: impl Into<String>, access: NetworkAccess) -> Self {
175162
self.networks.insert(name.into(), access);
176163
self
177164
}
165+
}
178166

179-
/// Sets the default network access.
180-
pub fn with_default(mut self, access: NetworkAccess) -> Self {
181-
self.default = access;
182-
self
167+
#[cfg(any(test, feature = "test-features"))]
168+
impl Default for MockNetworkAccessor {
169+
fn default() -> Self {
170+
Self::new()
183171
}
184172
}
185173

186174
#[cfg(any(test, feature = "test-features"))]
187175
#[async_trait]
188176
impl Access for MockNetworkAccessor {
189177
async fn access(&self, network: &Network) -> Result<NetworkAccess, AccessError> {
190-
Ok(self
191-
.networks
192-
.get(&network.name)
193-
.cloned()
194-
.unwrap_or_else(|| self.default.clone()))
178+
self.networks.get(&network.name).cloned().ok_or_else(|| {
179+
AccessError::Unexpected(anyhow::anyhow!(
180+
"network '{}' not configured in mock",
181+
network.name
182+
))
183+
})
195184
}
196185
}

0 commit comments

Comments
 (0)