Skip to content

Commit 9a7aa53

Browse files
committed
fixup! simln-lib/test: Add unit tests for validate_activity(), validate_node_network()
1 parent 8b6b0d7 commit 9a7aa53

File tree

2 files changed

+44
-64
lines changed

2 files changed

+44
-64
lines changed

simln-lib/src/lib.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub mod lnd;
3333
mod random_activity;
3434
pub mod serializers;
3535
pub mod sim_node;
36-
#[cfg(test)]
3736
mod test_utils;
3837

3938
#[derive(Serialize, Debug, Clone)]
@@ -1417,7 +1416,6 @@ async fn track_payment_result(
14171416

14181417
#[cfg(test)]
14191418
mod tests {
1420-
// use crate::test_utils::TestNetworkBuilder;
14211419
use crate::{
14221420
get_payment_delay, test_utils, test_utils::LightningTestNodeBuilder, LightningError,
14231421
LightningNode, MutRng, PaymentGenerationError, PaymentGenerator,
@@ -1527,13 +1525,11 @@ mod tests {
15271525
);
15281526
}
15291527

1530-
// Tests for validate_activity()
1531-
15321528
/// Verifies that an empty activity (for random generation) is valid when two nodes with keysend
15331529
/// support are present, expecting an `Ok` result.
15341530
#[tokio::test]
15351531
async fn test_validate_activity_empty_with_sufficient_nodes() {
1536-
let (_, clients) = LightningTestNodeBuilder::setup_test_nodes(2, &[0, 1, 2]);
1532+
let (_, clients) = LightningTestNodeBuilder::new(3).build_full();
15371533
let simulation = test_utils::create_simulation(clients, vec![]);
15381534
let result = simulation.validate_activity().await;
15391535
assert!(result.is_ok());
@@ -1543,7 +1539,7 @@ mod tests {
15431539
/// expecting a `ValidationError` with "At least two nodes required".
15441540
#[tokio::test]
15451541
async fn test_validate_activity_empty_with_insufficient_nodes() {
1546-
let (_, clients) = LightningTestNodeBuilder::setup_test_nodes(1, &[0]);
1542+
let (_, clients) = LightningTestNodeBuilder::new(1).build_full();
15471543
let simulation = test_utils::create_simulation(clients, vec![]);
15481544
let result = simulation.validate_activity().await;
15491545

@@ -1556,7 +1552,9 @@ mod tests {
15561552
/// expecting a `ValidationError` with "must support keysend".
15571553
#[tokio::test]
15581554
async fn test_validate_activity_empty_with_non_keysend_node() {
1559-
let (_, clients) = LightningTestNodeBuilder::setup_test_nodes(2, &[0]);
1555+
let (_, clients) = LightningTestNodeBuilder::new(2)
1556+
.with_keysend_nodes(vec![0])
1557+
.build_full();
15601558
let simulation = test_utils::create_simulation(clients, vec![]);
15611559
let result = simulation.validate_activity().await;
15621560

@@ -1569,7 +1567,7 @@ mod tests {
15691567
/// expecting a `ValidationError` with "Source node not found".
15701568
#[tokio::test]
15711569
async fn test_validate_activity_with_missing_source_node() {
1572-
let (nodes, clients) = LightningTestNodeBuilder::setup_test_nodes(1, &[0]);
1570+
let (nodes, clients) = LightningTestNodeBuilder::new(1).build_full();
15731571
let missing_nodes = test_utils::create_nodes(1, 100_000);
15741572
let missing_node = missing_nodes.first().unwrap().0.clone();
15751573
let dest_node = nodes[0].clone();
@@ -1587,7 +1585,7 @@ mod tests {
15871585
/// expecting a `ValidationError` with "does not support keysend".
15881586
#[tokio::test]
15891587
async fn test_validate_activity_with_non_keysend_destination() {
1590-
let (nodes, clients) = LightningTestNodeBuilder::setup_test_nodes(1, &[0]);
1588+
let (nodes, clients) = LightningTestNodeBuilder::new(1).build_full();
15911589
let dest_nodes = test_utils::create_nodes(1, 100_000);
15921590
let dest_node = dest_nodes.first().unwrap().0.clone();
15931591

@@ -1604,7 +1602,7 @@ mod tests {
16041602
/// passes validation, expecting an `Ok` result.
16051603
#[tokio::test]
16061604
async fn test_validate_activity_valid_payment_flow() {
1607-
let (nodes, clients) = LightningTestNodeBuilder::setup_test_nodes(1, &[0]);
1605+
let (nodes, clients) = LightningTestNodeBuilder::new(1).build_full();
16081606
let dest_nodes = test_utils::create_nodes(1, 100_000);
16091607
let mut dest_node = dest_nodes.first().unwrap().0.clone();
16101608
dest_node.features.set_keysend_optional();
@@ -1620,7 +1618,7 @@ mod tests {
16201618
/// expecting a `ValidationError` with "zero values".
16211619
#[tokio::test]
16221620
async fn test_validate_zero_amount_no_valid() {
1623-
let (nodes, clients) = LightningTestNodeBuilder::setup_test_nodes(2, &[0, 1]);
1621+
let (nodes, clients) = LightningTestNodeBuilder::new(2).build_full();
16241622

16251623
let activity = test_utils::create_activity(nodes[0].clone(), nodes[1].clone(), 0);
16261624
let simulation = test_utils::create_simulation(clients, vec![activity]);
@@ -1631,8 +1629,6 @@ mod tests {
16311629
Err(LightningError::ValidationError(msg)) if msg.contains("zero values")));
16321630
}
16331631

1634-
// tests for validate_node_network()
1635-
16361632
/// Verifies that validation fails with no nodes, expecting a `ValidationError` with
16371633
/// "we don't control any nodes".
16381634
#[tokio::test]
@@ -1651,7 +1647,9 @@ mod tests {
16511647
/// with "mainnet is not supported".
16521648
#[tokio::test]
16531649
async fn test_validate_node_network_mainnet_not_supported() {
1654-
let clients = LightningTestNodeBuilder::setup_network_test_nodes(1, vec![Network::Bitcoin]);
1650+
let clients = LightningTestNodeBuilder::new(1)
1651+
.with_networks(vec![Network::Bitcoin])
1652+
.build_clients_only();
16551653

16561654
let simulation = test_utils::create_simulation(clients, vec![]);
16571655
let result = simulation.validate_node_network().await;
@@ -1665,10 +1663,9 @@ mod tests {
16651663
/// `ValidationError` with "nodes are not on the same network".
16661664
#[tokio::test]
16671665
async fn test_validate_node_network_mixed_networks() {
1668-
let clients = LightningTestNodeBuilder::setup_network_test_nodes(
1669-
2,
1670-
vec![Network::Testnet, Network::Regtest],
1671-
);
1666+
let clients = LightningTestNodeBuilder::new(2)
1667+
.with_networks(vec![Network::Testnet, Network::Regtest])
1668+
.build_clients_only();
16721669

16731670
let simulation = test_utils::create_simulation(clients, vec![]);
16741671
let result = simulation.validate_node_network().await;
@@ -1681,10 +1678,9 @@ mod tests {
16811678
/// Verifies that three Testnet nodes pass validation, expecting an `Ok` result.
16821679
#[tokio::test]
16831680
async fn test_validate_node_network_multiple_nodes_same_network() {
1684-
let clients = LightningTestNodeBuilder::setup_network_test_nodes(
1685-
3,
1686-
vec![Network::Testnet, Network::Testnet, Network::Testnet],
1687-
);
1681+
let clients = LightningTestNodeBuilder::new(3)
1682+
.with_networks(vec![Network::Testnet, Network::Testnet, Network::Testnet])
1683+
.build_clients_only();
16881684

16891685
let simulation = test_utils::create_simulation(clients, vec![]);
16901686
let result = simulation.validate_node_network().await;
@@ -1694,7 +1690,9 @@ mod tests {
16941690

16951691
#[tokio::test]
16961692
async fn test_validate_node_network_single_node_valid_network() {
1697-
let clients = LightningTestNodeBuilder::setup_network_test_nodes(1, vec![Network::Testnet]);
1693+
let clients = LightningTestNodeBuilder::new(1)
1694+
.with_networks(vec![Network::Testnet])
1695+
.build_clients_only();
16981696

16991697
let simulation = test_utils::create_simulation(clients, vec![]);
17001698
let result = simulation.validate_node_network().await;

simln-lib/src/test_utils.rs

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(test)]
1+
#![cfg(test)]
22
use async_trait::async_trait;
33
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
44
use bitcoin::Network;
@@ -91,7 +91,7 @@ mock! {
9191
}
9292
}
9393

94-
/// Type alias for the result of setup_test_nodes
94+
/// Type alias for the result of setup_test_nodes.
9595
type TestNodesResult = (
9696
Vec<NodeInfo>,
9797
HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
@@ -104,71 +104,52 @@ type TestNodesResult = (
104104
/// node info and clients) and client-only setups for network testing.
105105
///
106106
/// # Examples
107-
/// Basic setup with keysend on specific nodes:
108-
/// let (nodes, clients) = TestNetworkBuilder::setup_test_nodes(5, &[0, 2]);
109107
///
110108
/// Using the builder for more control:
111-
/// let (nodes, clients) = TestNetworkBuilder::new(5)
109+
/// let (nodes, clients) = LightningTestNodeBuilder::new(5)
112110
/// .with_keysend_nodes(vec![0, 2, 4])
113111
/// .build_full();
114112
///
115113
/// Building clients with specific networks:
116-
/// let clients = TestNetworkBuilder::new(3)
114+
/// let clients = LightningTestNodeBuilder::new(3)
117115
/// .with_networks(vec![Network::Bitcoin, Network::Testnet, Network::Regtest])
118116
/// .build_clients_only();
119117
pub struct LightningTestNodeBuilder {
120-
node_count: usize, // Required - must be provided at creation
121-
initial_balance: u64, // Always has a value (default: 100,000)
122-
keysend_indices: Vec<usize>, // Always a vector (default: empty)
123-
networks: Option<Vec<Network>>, // Can be None (default) or Some(networks)
118+
node_count: usize, // Required - must be provided at creation.
119+
initial_balance: u64, // Always has a value (default: 100,000).
120+
keysend_indices: Vec<usize>, // Always a vector (default: Vec filled with 0..node_count).
121+
networks: Option<Vec<Network>>, // Can be None (default) or Some(networks).
124122
}
125123

126124
impl LightningTestNodeBuilder {
127-
/// Creates test nodes with optional keysend support on specified nodes.
128-
/// A convenience method equivalent to `new(node_count).with_keysend_nodes(indices).build_full()`.
129-
pub fn setup_test_nodes(node_count: usize, keysend_indices: &[usize]) -> TestNodesResult {
130-
Self::new(node_count)
131-
.with_keysend_nodes(keysend_indices.to_vec())
132-
.build_full()
133-
}
134-
135-
/// Creates test nodes with specified networks.
136-
/// A convenience method equivalent to `new(node_count).with_networks(networks).build_clients_only()`.
137-
pub fn setup_network_test_nodes(
138-
node_count: usize,
139-
networks: Vec<Network>,
140-
) -> HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>> {
141-
Self::new(node_count)
142-
.with_networks(networks)
143-
.build_clients_only()
144-
}
145-
146125
/// Creates a new builder instance with the specified number of nodes.
147126
/// The default configuration includes a balance of 100,000 units per node,
148-
/// no keysend support, and no specific networks.
127+
/// keysend support ON, and Regtest network.
149128
pub fn new(node_count: usize) -> Self {
150129
Self {
151130
node_count,
152-
initial_balance: 100_000, // Default 100k sats
153-
keysend_indices: Vec::new(), // No keysend by default
154-
networks: None, // No specific networks by default
131+
initial_balance: 100_000, // Default 100k sats.
132+
keysend_indices: (0..node_count).collect(), // Keysend for all nodes ON by default.
133+
networks: Some(vec![Network::Regtest; node_count]), // Regtest network for all nodes by default.
155134
}
156135
}
157136

158-
/// Specifies which nodes should have the keysend feature enabled
137+
/// Specifies which nodes should have the keysend feature enabled.
159138
pub fn with_keysend_nodes(mut self, indices: Vec<usize>) -> Self {
160139
self.keysend_indices = indices;
161140
self
162141
}
163142

164-
/// Sets specific networks for each node
165-
/// Will panic if the number of networks doesn't match node_count
166-
/// Returns self for method chaining
143+
/// Sets specific networks for each node.
144+
/// Checks whether the number of networks matches node_count.
145+
/// Returns self for method chaining.
167146
pub fn with_networks(mut self, networks: Vec<Network>) -> Self {
168147
// Validate that we have the correct number of networks
169-
if networks.len() != self.node_count {
170-
panic!("Must specify a network for each node");
171-
}
148+
assert_eq!(
149+
networks.len(),
150+
self.node_count,
151+
"Must specify a network for each node"
152+
);
172153
self.networks = Some(networks);
173154
self
174155
}
@@ -212,6 +193,8 @@ impl LightningTestNodeBuilder {
212193
}
213194
}
214195

196+
/// Creates a new simulation with the given clients and activity definitions.
197+
/// Note: This sets a runtime for the simulation of 0, so run() will exit immediately.
215198
pub fn create_simulation(
216199
clients: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
217200
activity: Vec<ActivityDefinition>,
@@ -223,7 +206,6 @@ pub fn create_simulation(
223206
TaskTracker::new(),
224207
)
225208
}
226-
227209
pub fn create_activity(
228210
source: NodeInfo,
229211
destination: NodeInfo,

0 commit comments

Comments
 (0)