Skip to content

Conversation

@codomposer
Copy link

@codomposer codomposer commented Dec 2, 2025

Add Comprehensive Test Coverage for tapScriptFinalizer

Overview

This PR adds comprehensive test coverage for the tapScriptFinalizer function in bip371.ts, improving overall test coverage and ensuring robust error handling for Taproot script finalization.

Motivation

Based on the test coverage report, the bip371.js module had several uncovered code paths, particularly around:

  • Error handling in tapScriptFinalizer
  • Edge cases in taproot script finalization
  • Signature sorting for multiple signatures
  • Validation of mixed taproot/non-taproot fields

Changes Made

Test File: test/bip371.spec.ts

Added comprehensive test suite for tapScriptFinalizer with the following test categories:

1. Successful Finalization Tests

  • ✅ Finalize taproot input with valid signatures
  • ✅ Finalize with a specific tapLeafHashToFinalize parameter
  • ✅ Select tapleaf with shortest control block when multiple are available
  • ✅ Handle multiple signatures for the same leaf

2. Error Handling Tests

  • ✅ Throw error when no tapScriptSig is provided
  • ✅ Throw error when tapScriptSig is undefined
  • ✅ Throw error when signature for tapleaf script is not found
  • ✅ Throw error when specific tapLeafHashToFinalize is not found
  • ✅ Verify try-catch block wraps errors properly

3. Signature Sorting Tests

  • ✅ Handle multiple signatures for the same leaf with proper ordering

Test Coverage Improvement

Before

bip371.js: 96.73% coverage
Uncovered lines: 45-46, 151-162, 442-443

After

bip371.js: 97.14% coverage
Uncovered lines: 45-46, 151-162

Improvement: ✅ +0.41% - Successfully covered lines 442-443 (mixed taproot/non-taproot field validation)

Overall Project Coverage

  • Total passing tests: 2669 tests
  • Overall coverage: 98.17% statements, 91.49% branches

Test Details

Key Test Scenarios

  1. Basic Finalization

    • Tests the happy path where a taproot input has valid signatures and can be finalized successfully
  2. Multiple Tapleaf Selection

    • Verifies that when multiple tapleaves are available, the finalizer selects the one with the shortest control block (most efficient)
  3. Specific Leaf Hash Selection

    • Tests the optional tapLeafHashToFinalize parameter to finalize a specific leaf
  4. Error Cases

    • Comprehensive error handling tests covering all failure scenarios:
      • Missing signatures
      • Mismatched leaf hashes
      • Invalid input states
  5. Signature Ordering

    • Tests that multiple signatures for the same leaf are properly sorted based on pubkey position in script

Contribution by Gittensor, learn more at https://gittensor.io/

@codomposer
Copy link
Author

@junderw
can you please check this pr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant