From a664963db9d77a01fbf9c89c3fabe1b21ccf1890 Mon Sep 17 00:00:00 2001 From: panditdhamdhere Date: Mon, 23 Mar 2026 23:34:17 +0530 Subject: [PATCH] Handle ElementsRegtest in test context instead of panicking - Add ElementsRegtest case to network match (maps to default_regtest()) - Return BadNetworkName error for invalid network values instead of panic - Add unit test for invalid network error handling - Align error message with CLI config validation Made-with: Cursor --- CHANGELOG.md | 4 ++++ crates/test/src/context.rs | 34 +++++++++++++++++++++++++++++++++- crates/test/src/error.rs | 3 +++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b08c67f..d2b2836 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] + +- Handle `ElementsRegtest` in test context instead of panicking; return clear error for invalid network values. + ## [0.0.2] - Implemented `simplex init` and `simplex clean` commands. diff --git a/crates/test/src/context.rs b/crates/test/src/context.rs index 1f61942..9b4be0c 100644 --- a/crates/test/src/context.rs +++ b/crates/test/src/context.rs @@ -71,7 +71,8 @@ impl TestContext { let network = match esplora.network.as_str() { "Liquid" => SimplicityNetwork::Liquid, "LiquidTestnet" => SimplicityNetwork::LiquidTestnet, - _ => panic!("Impossible branch reached, please report a bug"), + "ElementsRegtest" => SimplicityNetwork::default_regtest(), + other => return Err(TestError::BadNetworkName(other.to_string())), }; let provider = Box::new(EsploraProvider::new(esplora.url, network)); @@ -91,3 +92,34 @@ impl TestContext { Ok((signer, client)) } } + +#[cfg(test)] +mod tests { + use std::fs; + + use super::*; + + #[test] + fn invalid_network_returns_error() { + let config = r#" + mnemonic = "exist carry drive collect lend cereal occur much tiger just involve mean" + bitcoins = 10000 + + [esplora] + url = "http://localhost:3000" + network = "InvalidNetwork" + "#; + + let path = std::env::temp_dir().join("smplx_test_invalid_network.toml"); + fs::write(&path, config).unwrap(); + + let result = TestContext::new(path); + let Err(e) = result else { + panic!("expected BadNetworkName error") + }; + assert!( + matches!(e, TestError::BadNetworkName(ref s) if s == "InvalidNetwork"), + "expected BadNetworkName, got: {e}" + ); + } +} diff --git a/crates/test/src/error.rs b/crates/test/src/error.rs index 01f26c0..4b545e8 100644 --- a/crates/test/src/error.rs +++ b/crates/test/src/error.rs @@ -21,4 +21,7 @@ pub enum TestError { #[error("io error occurred: '{0}'")] Io(#[from] io::Error), + + #[error("Network name should either be `Liquid`, `LiquidTestnet` or `ElementsRegtest`, got: {0}")] + BadNetworkName(String), }