Skip to content

Commit 1a5d219

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): refactor PSBT fixture test macro to include format parameter
Updates the test_psbt_fixtures macro to include the transaction format as an explicit parameter instead of hardcoding it within each test function. This reduces code duplication and makes the test infrastructure more flexible. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent 3f744bd commit 1a5d219

File tree

2 files changed

+62
-110
lines changed

2 files changed

+62
-110
lines changed

packages/wasm-utxo/src/bitgo_psbt/mod.rs

Lines changed: 29 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ mod tests {
184184
}
185185
}
186186

187-
crate::test_psbt_fixtures!(test_parse_network_mainnet_only, network, {
188-
test_parse_with_format(fixtures::TxFormat::Psbt, network);
189-
test_parse_with_format(fixtures::TxFormat::PsbtLite, network);
187+
crate::test_psbt_fixtures!(test_parse_network_mainnet_only, network, format, {
188+
test_parse_with_format(format, network);
190189
});
191190

192191
#[test]
@@ -242,9 +241,8 @@ mod tests {
242241
}
243242
}
244243

245-
crate::test_psbt_fixtures!(test_round_trip_mainnet_only, network, {
246-
test_round_trip_with_format(fixtures::TxFormat::Psbt, network);
247-
test_round_trip_with_format(fixtures::TxFormat::PsbtLite, network);
244+
crate::test_psbt_fixtures!(test_round_trip_mainnet_only, network, format, {
245+
test_round_trip_with_format(format, network);
248246
});
249247

250248
fn parse_derivation_path(path: &str) -> Result<(u32, u32), String> {
@@ -281,26 +279,6 @@ mod tests {
281279
Ok((chain, index))
282280
}
283281

284-
fn find_input_with_script_type(
285-
fixture: &fixtures::PsbtFixture,
286-
script_type: fixtures::ScriptType,
287-
) -> Result<(usize, &fixtures::PsbtInputFixture), String> {
288-
let result = fixture
289-
.psbt_inputs
290-
.iter()
291-
.enumerate()
292-
.filter(|(_, input)| script_type.matches_fixture(input))
293-
.collect::<Vec<_>>();
294-
if result.len() != 1 {
295-
return Err(format!(
296-
"Expected 1 input with script type {}, got {}",
297-
script_type.as_str(),
298-
result.len()
299-
));
300-
}
301-
Ok(result[0])
302-
}
303-
304282
fn get_output_script_from_non_witness_utxo(
305283
input: &fixtures::P2shInput,
306284
index: usize,
@@ -343,7 +321,7 @@ mod tests {
343321

344322
// Check if the script type is supported by the network
345323
let output_script_support = network.output_script_support();
346-
let input_fixture = find_input_with_script_type(&fixture, script_type);
324+
let input_fixture = fixture.find_input_with_script_type(script_type);
347325
if !script_type.is_supported_by(&output_script_support) {
348326
// Script type not supported by network - skip test (no fixture expected)
349327
assert!(
@@ -441,101 +419,47 @@ mod tests {
441419
Ok(())
442420
}
443421

444-
crate::test_psbt_fixtures!(test_p2sh_script_generation_from_fixture, network, {
445-
test_wallet_script_type(
446-
fixtures::ScriptType::P2sh,
447-
network,
448-
fixtures::TxFormat::Psbt,
449-
)
450-
.unwrap();
451-
test_wallet_script_type(
452-
fixtures::ScriptType::P2sh,
453-
network,
454-
fixtures::TxFormat::PsbtLite,
455-
)
456-
.unwrap();
422+
crate::test_psbt_fixtures!(test_p2sh_script_generation_from_fixture, network, format, {
423+
test_wallet_script_type(fixtures::ScriptType::P2sh, network, format).unwrap();
457424
});
458425

459-
crate::test_psbt_fixtures!(test_p2sh_p2wsh_script_generation_from_fixture, network, {
460-
test_wallet_script_type(
461-
fixtures::ScriptType::P2shP2wsh,
462-
network,
463-
fixtures::TxFormat::Psbt,
464-
)
465-
.unwrap();
466-
test_wallet_script_type(
467-
fixtures::ScriptType::P2shP2wsh,
468-
network,
469-
fixtures::TxFormat::PsbtLite,
470-
)
471-
.unwrap();
472-
});
426+
crate::test_psbt_fixtures!(
427+
test_p2sh_p2wsh_script_generation_from_fixture,
428+
network,
429+
format,
430+
{
431+
test_wallet_script_type(fixtures::ScriptType::P2shP2wsh, network, format).unwrap();
432+
}
433+
);
473434

474-
crate::test_psbt_fixtures!(test_p2wsh_script_generation_from_fixture, network, {
475-
test_wallet_script_type(
476-
fixtures::ScriptType::P2wsh,
477-
network,
478-
fixtures::TxFormat::Psbt,
479-
)
480-
.unwrap();
481-
test_wallet_script_type(
482-
fixtures::ScriptType::P2wsh,
483-
network,
484-
fixtures::TxFormat::PsbtLite,
485-
)
486-
.unwrap();
487-
});
435+
crate::test_psbt_fixtures!(
436+
test_p2wsh_script_generation_from_fixture,
437+
network,
438+
format,
439+
{
440+
test_wallet_script_type(fixtures::ScriptType::P2wsh, network, format).unwrap();
441+
}
442+
);
488443

489-
crate::test_psbt_fixtures!(test_p2tr_script_generation_from_fixture, network, {
490-
test_wallet_script_type(
491-
fixtures::ScriptType::P2tr,
492-
network,
493-
fixtures::TxFormat::Psbt,
494-
)
495-
.unwrap();
496-
test_wallet_script_type(
497-
fixtures::ScriptType::P2tr,
498-
network,
499-
fixtures::TxFormat::PsbtLite,
500-
)
501-
.unwrap();
444+
crate::test_psbt_fixtures!(test_p2tr_script_generation_from_fixture, network, format, {
445+
test_wallet_script_type(fixtures::ScriptType::P2tr, network, format).unwrap();
502446
});
503447

504448
crate::test_psbt_fixtures!(
505449
test_p2tr_musig2_script_path_generation_from_fixture,
506450
network,
451+
format,
507452
{
508-
test_wallet_script_type(
509-
fixtures::ScriptType::P2trMusig2,
510-
network,
511-
fixtures::TxFormat::Psbt,
512-
)
513-
.unwrap();
514-
test_wallet_script_type(
515-
fixtures::ScriptType::P2trMusig2,
516-
network,
517-
fixtures::TxFormat::PsbtLite,
518-
)
519-
.unwrap();
453+
test_wallet_script_type(fixtures::ScriptType::P2trMusig2, network, format).unwrap();
520454
}
521455
);
522456

523457
crate::test_psbt_fixtures!(
524458
test_p2tr_musig2_key_path_spend_script_generation_from_fixture,
525459
network,
460+
format,
526461
{
527-
test_wallet_script_type(
528-
fixtures::ScriptType::TaprootKeypath,
529-
network,
530-
fixtures::TxFormat::Psbt,
531-
)
532-
.unwrap();
533-
test_wallet_script_type(
534-
fixtures::ScriptType::TaprootKeypath,
535-
network,
536-
fixtures::TxFormat::PsbtLite,
537-
)
538-
.unwrap();
462+
test_wallet_script_type(fixtures::ScriptType::TaprootKeypath, network, format).unwrap();
539463
}
540464
);
541465

packages/wasm-utxo/src/fixed_script_wallet/test_utils/fixtures.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,26 @@ impl PsbtFixture {
269269
)?;
270270
Ok(psbt)
271271
}
272+
273+
pub fn find_input_with_script_type(
274+
&self,
275+
script_type: ScriptType,
276+
) -> Result<(usize, &PsbtInputFixture), String> {
277+
let result = self
278+
.psbt_inputs
279+
.iter()
280+
.enumerate()
281+
.filter(|(_, input)| script_type.matches_fixture(input))
282+
.collect::<Vec<_>>();
283+
if result.len() != 1 {
284+
return Err(format!(
285+
"Expected 1 input with script type {}, got {}",
286+
script_type.as_str(),
287+
result.len()
288+
));
289+
}
290+
Ok(result[0])
291+
}
272292
}
273293

274294
// Output script fixture types
@@ -729,20 +749,22 @@ impl ScriptType {
729749
}
730750

731751
/// Macro for testing PSBT fixtures across all mainnet networks (excluding testnets and BSV)
752+
/// and both transaction formats (Psbt and PsbtLite)
732753
///
733754
/// This macro generates test cases for mainnet networks only: Bitcoin, BitcoinCash, Ecash,
734-
/// BitcoinGold, Dash, Dogecoin, Litecoin, and Zcash.
755+
/// BitcoinGold, Dash, Dogecoin, Litecoin, and Zcash, combined with both TxFormat variants.
756+
/// This creates a cartesian product of 8 networks × 2 formats = 16 test cases.
735757
///
736758
/// # Example
737759
/// ```rust,no_run
738-
/// test_psbt_fixtures!(test_my_feature, network, {
760+
/// test_psbt_fixtures!(test_my_feature, network, format, {
739761
/// let fixture = load_psbt_fixture_with_network(network, SignatureState::Fullsigned).unwrap();
740-
/// // ... test logic here
762+
/// // ... test logic using both network and format
741763
/// });
742764
/// ```
743765
#[macro_export]
744766
macro_rules! test_psbt_fixtures {
745-
($test_name:ident, $network:ident, $body:block) => {
767+
($test_name:ident, $network:ident, $format:ident, $body:block) => {
746768
#[rstest::rstest]
747769
#[case::bitcoin($crate::Network::Bitcoin)]
748770
#[case::bitcoin_cash($crate::Network::BitcoinCash)]
@@ -752,7 +774,13 @@ macro_rules! test_psbt_fixtures {
752774
#[case::dogecoin($crate::Network::Dogecoin)]
753775
#[case::litecoin($crate::Network::Litecoin)]
754776
#[case::zcash($crate::Network::Zcash)]
755-
fn $test_name(#[case] $network: $crate::Network) $body
777+
fn $test_name(
778+
#[case] $network: $crate::Network,
779+
#[values(
780+
$crate::fixed_script_wallet::test_utils::fixtures::TxFormat::Psbt,
781+
$crate::fixed_script_wallet::test_utils::fixtures::TxFormat::PsbtLite
782+
)] $format: $crate::fixed_script_wallet::test_utils::fixtures::TxFormat
783+
) $body
756784
};
757785
}
758786

0 commit comments

Comments
 (0)