Skip to content

feat: add missing tests to btcindexer storage#354

Merged
rmeena840 merged 1 commit intomasterfrom
349-add-missing-tests-to-btcindexer-storage
Feb 13, 2026
Merged

feat: add missing tests to btcindexer storage#354
rmeena840 merged 1 commit intomasterfrom
349-add-missing-tests-to-btcindexer-storage

Conversation

@rmeena840
Copy link
Contributor

@rmeena840 rmeena840 commented Feb 11, 2026

Description

Closes: #349


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included doc comments for public functions
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary

Summary by Sourcery

Expand test coverage for btcindexer storage edge cases and helper functions.

Tests:

  • Add tests for NBTC transaction status queries, retries, and candidate selection edge cases.
  • Add tests for block operations including latest height, chain tip, block retrieval, and processing batch limits.
  • Add tests for helper functions fetching package configs and NBTC addresses with various setup states.

@rmeena840 rmeena840 requested a review from a team as a code owner February 11, 2026 12:25
@rmeena840 rmeena840 linked an issue Feb 11, 2026 that may be closed by this pull request
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 11, 2026

Reviewer's Guide

Adds comprehensive edge-case and behavior tests for the btcindexer Cloudflare storage layer, covering nBTC mint transaction lifecycle, block processing logic, and helper query functions, plus a minor type import addition.

Sequence diagram for nbtc mint tx lifecycle edge cases tested in PR

sequenceDiagram
    actor IndexerWorker
    participant CFStorage

    Note over IndexerWorker,CFStorage: Broadcast registration without block info
    IndexerWorker->>CFStorage: registerBroadcastedNbtcTx([NbtcBroadcastedDeposit])
    CFStorage-->>IndexerWorker: ok (status Broadcasting)

    Note over IndexerWorker,CFStorage: Later update with block info
    IndexerWorker->>CFStorage: insertOrUpdateNbtcTxs([txBase])
    CFStorage-->>IndexerWorker: ok (status Confirming)

    Note over IndexerWorker,CFStorage: Query single tx status
    IndexerWorker->>CFStorage: getTxStatus(txId)
    CFStorage-->>IndexerWorker: MintTxStatus.Confirming or null

    Note over IndexerWorker,CFStorage: Batch failure handling and retries
    loop for each failure
        IndexerWorker->>CFStorage: batchUpdateNbtcMintTxs([update MintFailed])
        CFStorage-->>IndexerWorker: ok (increment retry_count)
    end

    Note over IndexerWorker,CFStorage: Finalization and candidate selection
    IndexerWorker->>CFStorage: finalizeNbtcTxs([txId])
    CFStorage-->>IndexerWorker: ok

    IndexerWorker->>CFStorage: getNbtcMintCandidates(maxRetryCount)
    CFStorage-->>IndexerWorker: [NbtcMintTx] within retry limit
Loading

Class diagram for CFStorage nbtc mint and block operations tested in PR

classDiagram
    class CFStorage {
        +insertOrUpdateNbtcTxs(nbtcTxs)
        +getTxStatus(txId)
        +updateNbtcTxsStatus(txIds, status)
        +finalizeNbtcTxs(txIds)
        +batchUpdateNbtcMintTxs(updates)
        +getNbtcMintTx(txId)
        +getNbtcMintTxsBySuiAddr(suiAddress)
        +getNbtcMintTxsByBtcSender(btcSender, btcNetwork)
        +getNbtcMintCandidates(maxRetryCount)
        +registerBroadcastedNbtcTx(broadcastedDeposits)
        +getConfirmingBlocks()
        +getLatestBlockHeight(btcNetwork)
        +getChainTip(btcNetwork)
        +setChainTip(height, btcNetwork)
        +getBlock(hash)
        +getBlockHash(height, btcNetwork)
        +insertBlockInfo(blockInfo)
        +markBlockAsProcessed(hash, btcNetwork)
        +getBlocksToProcess(limit)
    }

    class NbtcMintTx {
        +tx_id
        +vout
        +status
        +retry_count
        +block_hash
        +block_height
        +sui_tx_digest
        +sui_recipient
        +btc_sender
        +amount
    }

    class NbtcBroadcastedDeposit {
        +txId
        +btcNetwork
        +suiNetwork
        +nbtcPkg
        +depositAddress
        +sender
        +vout
        +suiRecipient
        +amount
    }

    class BlockInfo {
        +hash
        +height
        +network
        +timestamp_ms
        +processed
    }

    class MintTxStatus {
        <<enumeration>>
        Confirming
        Broadcasting
        Minted
        MintFailed
    }

    class InsertBlockStatus {
        <<enumeration>>
        Inserted
        Skipped
    }

    class BtcNet {
        <<enumeration>>
        REGTEST
        TESTNET
        MAINNET
    }

    class D1Database {
        +prepare(sql)
        +run()
        +all()
    }

    class StorageHelpers {
        +fetchPackageConfigs(db)
        +fetchNbtcAddresses(db)
    }

    CFStorage "1" --> "*" NbtcMintTx : manages
    CFStorage "1" --> "*" BlockInfo : manages
    CFStorage ..> NbtcBroadcastedDeposit : registers
    CFStorage ..> MintTxStatus : uses
    CFStorage ..> InsertBlockStatus : returns
    CFStorage ..> BtcNet : filters_by

    StorageHelpers ..> D1Database : queries
    StorageHelpers ..> NbtcMintTx : returns_related_data
Loading

File-Level Changes

Change Details Files
Extend CFStorage nBTC mint transaction tests to cover status queries, empty-input handling, failure/retry semantics, duplicate broadcasts, multi-tx updates, and address/sender lookups.
  • Import the NbtcBroadcastedDeposit type from models for use in tests.
  • Add tests for getTxStatus for existing and non-existent transactions.
  • Add tests ensuring insertOrUpdateNbtcTxs, updateNbtcTxsStatus, and finalizeNbtcTxs behave correctly when given empty arrays.
  • Add tests for batchUpdateNbtcMintTxs handling MintFailed status, incrementing retry_count, and affecting getNbtcMintCandidates with and without exceeding retry limits.
  • Add tests validating insertOrUpdateNbtcTxs updates existing transactions with new block information.
  • Add tests for registerBroadcastedNbtcTx to ensure duplicate broadcasts are ignored while preserving Broadcasting status.
  • Add tests verifying updateNbtcTxsStatus correctly updates the status of multiple transactions at once.
  • Add tests for various getters (getNbtcMintTxsBySuiAddr, getNbtcMintTxsByBtcSender, getNbtcMintTx) returning empty or null for non-existent records.
packages/btcindexer/src/storage.test.ts
Add edge-case tests for block storage operations, including latest height queries, chain tips, block retrieval, processing queues, timestamp collisions, and per-network chain tips.
  • Add tests for getLatestBlockHeight, getChainTip, getBlock, and getBlockHash when the database has no relevant data.
  • Add tests for getBlocksToProcess to verify behavior when all blocks are processed, to enforce batch-size limits, and to ensure results are ordered by ascending height.
  • Add a test for insertBlockInfo that validates InsertBlockStatus.Skipped is returned when inserting a block with an already-used height and timestamp.
  • Add tests ensuring setChainTip and getChainTip work correctly and independently across multiple BTC networks.
packages/btcindexer/src/storage.test.ts
Add edge-case tests for helper functions fetchPackageConfigs and fetchNbtcAddresses around active/inactive setups and multiple active configurations.
  • Add tests inserting inactive setups to validate that fetchPackageConfigs still returns the expected configurations based on current data.
  • Add tests inserting setups and deposit addresses with specific active flags to validate fetchNbtcAddresses behavior and returned map size.
  • Add tests to ensure fetchPackageConfigs correctly handles and returns multiple active package configurations.
packages/btcindexer/src/storage.test.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#349 Add missing tests so that all SQL queries used by btcindexer storage (including edge cases) are covered by tests.
#349 Ensure btcindexer storage tests use the init_db test helper functions for database creation and cleanup.

Possibly linked issues

  • #: The PR adds the requested missing btcindexer storage tests, exercising SQL queries and using init_db helpers.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Some test names/descriptions don’t match their assertions (e.g. fetchPackageConfigs should return empty array when no active packages expects length 1, and fetchNbtcAddresses should return empty map when no active setups expects size 1), which makes the intended behavior unclear—either adjust the expectations or rename the tests to reflect what they actually verify.
  • A few of the new edge-case tests only assert not.toBeNull() on results for empty inputs (e.g. insertOrUpdateNbtcTxs / updateNbtcTxsStatus / finalizeNbtcTxs with empty arrays); consider tightening these to assert on more concrete behavior (such as no-op semantics or specific return values) to better document and lock in the intended contract.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Some test names/descriptions don’t match their assertions (e.g. `fetchPackageConfigs should return empty array when no active packages` expects length `1`, and `fetchNbtcAddresses should return empty map when no active setups` expects `size` `1`), which makes the intended behavior unclear—either adjust the expectations or rename the tests to reflect what they actually verify.
- A few of the new edge-case tests only assert `not.toBeNull()` on results for empty inputs (e.g. `insertOrUpdateNbtcTxs` / `updateNbtcTxsStatus` / `finalizeNbtcTxs` with empty arrays); consider tightening these to assert on more concrete behavior (such as no-op semantics or specific return values) to better document and lock in the intended contract.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@sczembor sczembor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@rmeena840 rmeena840 force-pushed the 349-add-missing-tests-to-btcindexer-storage branch 2 times, most recently from b6c8878 to 5ce1b19 Compare February 13, 2026 03:53
@rmeena840 rmeena840 requested a review from sczembor February 13, 2026 03:53
Signed-off-by: Ravindra Meena <rmeena840@gmail.com>
@rmeena840 rmeena840 force-pushed the 349-add-missing-tests-to-btcindexer-storage branch from 5ce1b19 to a6a6ff9 Compare February 13, 2026 09:53
@rmeena840 rmeena840 enabled auto-merge (squash) February 13, 2026 09:54
@rmeena840 rmeena840 merged commit c1e492f into master Feb 13, 2026
12 checks passed
@rmeena840 rmeena840 deleted the 349-add-missing-tests-to-btcindexer-storage branch February 13, 2026 09:54
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.

Add missing tests to btcindexer storage

2 participants