Skip to content

Conversation

@Rcc999
Copy link
Contributor

@Rcc999 Rcc999 commented Dec 11, 2025

Description

Closes: #241

Summary by Sourcery

Add Bun-based test setup for redeem_solver storage layer using Miniflare-backed D1 database and shared migrations.

Build:

  • Update redeem_solver package test script to run Bun test suite instead of a placeholder command.

Tests:

  • Introduce integration-style tests for D1Storage methods covering redeem retrieval, UTXO selection, status transitions, and active network queries using a Miniflare D1 instance and shared migrations.

…package following the btcindexer approach. Setup the miniflare for redeem-solver with only the D1 db since it is the only one used across the package

Signed-off-by: Rayane Charif <rayane.charif@gonative.cc>
@Rcc999 Rcc999 requested a review from a team as a code owner December 11, 2025 13:51
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 11, 2025

Reviewer's Guide

Adds a Bun+Miniflare-based test suite for D1Storage SQL behavior in redeem_solver and wires the package test script to run Bun tests using the shared btcindexer migration schema.

Sequence diagram for D1Storage markRedeemProposed Bun+Miniflare test

sequenceDiagram
    actor Developer
    participant BunTestRunner
    participant Miniflare
    participant D1Database
    participant D1Storage

    Developer->>BunTestRunner: run bun test
    BunTestRunner->>Miniflare: beforeAll create Miniflare instance
    Miniflare-->>BunTestRunner: Miniflare with D1 DB DB

    BunTestRunner->>Miniflare: beforeEach getD1Database DB
    Miniflare-->>BunTestRunner: D1Database
    BunTestRunner->>D1Database: initDb(db) apply migrations

    BunTestRunner->>Miniflare: getD1Database DB
    Miniflare-->>BunTestRunner: D1Database
    BunTestRunner->>D1Storage: new D1Storage(db)

    BunTestRunner->>D1Database: INSERT nbtc_redeem_requests row
    BunTestRunner->>D1Database: INSERT nbtc_utxos row

    BunTestRunner->>D1Storage: markRedeemProposed(redeem1, [utxo1], lockTimeMs)
    D1Storage->>D1Database: UPDATE nbtc_redeem_requests SET status Proposed
    D1Storage->>D1Database: UPDATE nbtc_utxos SET status Locked, locked_until
    D1Storage-->>BunTestRunner: Promise resolved

    BunTestRunner->>D1Database: SELECT status FROM nbtc_redeem_requests
    D1Database-->>BunTestRunner: status Proposed
    BunTestRunner->>D1Database: SELECT status, locked_until FROM nbtc_utxos
    D1Database-->>BunTestRunner: status Locked, locked_until

    BunTestRunner-->>Developer: test assertion results

    BunTestRunner->>Miniflare: afterEach getD1Database DB
    Miniflare-->>BunTestRunner: D1Database
    BunTestRunner->>D1Database: DROP TABLE IF EXISTS for all tables

    BunTestRunner->>Miniflare: afterAll dispose
    Miniflare-->>BunTestRunner: resources cleaned up
Loading

File-Level Changes

Change Details Files
Wire redeem_solver package tests to Bun so the new test suite is executed.
  • Replace placeholder npm test script with Bun test invocation so tests actually run for this package
packages/redeem_solver/package.json
Introduce Miniflare+D1-backed integration tests validating D1Storage query behavior and side effects.
  • Set up a Miniflare instance with an in-memory D1 database for tests and manage lifecycle hooks to reset schema and data between tests
  • Seed base package and deposit address records required by D1Storage in a per-test beforeEach
  • Verify getPendingRedeems orders pending redeems by created_at and returns correct sui_network mapping
  • Verify getRedeemsReadyForSolving filters by status and created_at cutoff timestamp
  • Verify getAvailableUtxos ordering by amount descending and filtering by package and status, including locked UTXOs
  • Verify markRedeemProposed updates redeem status and locks referenced UTXOs with a future locked_until
  • Verify markRedeemSolved transitions a redeem to Solved status
  • Verify getActiveNetworks returns distinct active Sui networks from active packages
packages/redeem_solver/src/storage.test.ts
Add a shared DB initialization helper that applies btcindexer SQL migrations into a D1 test database.
  • Locate btcindexer migration directory relative to redeem_solver and read migration files from disk
  • Normalize and clean SQL migration text (strip comments, collapse whitespace) before execution for D1 compatibility
  • Execute all sorted .sql migrations against the provided D1Database to build the schema used by storage tests
packages/redeem_solver/src/db.test.ts

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 there - I've reviewed your changes - here's some feedback:

  • The tests rely on Date.now() for assertions (e.g., comparing locked_until and filtering redeems), which can introduce flakiness over time; consider injecting a fixed timestamp or mocking the clock so the expectations are deterministic.
  • In initDb, the regex-based stripping of -- comments and newlines from migration SQL risks altering valid SQL (e.g., -- inside string literals or needed line breaks); it would be safer to only strip line-leading -- comments and execute statements as-is or split on ; instead of flattening everything into one line.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The tests rely on `Date.now()` for assertions (e.g., comparing `locked_until` and filtering redeems), which can introduce flakiness over time; consider injecting a fixed timestamp or mocking the clock so the expectations are deterministic.
- In `initDb`, the regex-based stripping of `--` comments and newlines from migration SQL risks altering valid SQL (e.g., `--` inside string literals or needed line breaks); it would be safer to only strip line-leading `--` comments and execute statements as-is or split on `;` instead of flattening everything into one line.

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.

Signed-off-by: Rayane Charif <rayane.charif@gonative.cc>
Signed-off-by: Rayane Charif <rayane.charif@gonative.cc>
Signed-off-by: Rayane Charif <rayane.charif@gonative.cc>
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.

we should avoid repeating code, lets move some of this code to common functions

Rcc999 and others added 3 commits December 11, 2025 16:57
Signed-off-by: Rayane Charif <rayane.charif@gonative.cc>
Signed-off-by: Rayane Charif <rayane.charif@gonative.cc>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces comprehensive test coverage for the redeem_solver storage layer using Miniflare-backed D1 databases and the Bun test runner. The changes extract common database initialization logic into a shared library to promote code reuse across packages.

Key Changes:

  • Added integration-style tests for D1Storage methods including UTXO selection, redeem status transitions, and network queries
  • Extracted shared database test initialization logic to @gonative-cc/lib/common-setup/db.test for reusability
  • Exported the UTXO_LOCK_TIME_MS constant for use in tests

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
packages/redeem_solver/src/storage.ts Exports UTXO_LOCK_TIME_MS constant (2 minutes) for test usage
packages/redeem_solver/src/storage.test.ts Comprehensive test suite for D1Storage covering all storage methods with proper Miniflare setup
packages/redeem_solver/src/db.test.ts Wrapper that configures shared initDb function with btcindexer migrations path
packages/redeem_solver/package.json Updates test script from placeholder to actual Bun test execution
packages/lib/src/common-setup/db.test.ts Shared database initialization logic that reads and applies SQL migrations
packages/lib/package.json Adds export path for common-setup modules
packages/btcindexer/src/db.test.ts Refactored to use shared initDb function, removing duplicate code

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

.bind("utxo1")
.first<{ status: string; locked_until: number }>();
expect(utxo!.status).toBe(UtxoStatus.Locked);
expect(utxo!.locked_until).toBeGreaterThan(Date.now());
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

Potential flaky test due to timing. The assertion expects locked_until to be greater than Date.now(), but there's a race condition if the test executes slowly. Between the markRedeemProposed call and the assertion, time passes. Consider using a more specific assertion that checks locked_until is approximately equal to the expected value (current time + UTXO_LOCK_TIME_MS) with a tolerance window.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean these tests focuses more on just verifying the sql queries

Copy link
Contributor

Choose a reason for hiding this comment

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

then lets just assert if its defined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay done

Rcc999 and others added 2 commits December 23, 2025 13:56
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.

lets rename it to something more meaningful than common-setup

let storage: D1Storage;
let db: D1Database;

async function insertRedeemRequest(
Copy link
Contributor

Choose a reason for hiding this comment

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

dont we have those functions in storage.ts

}

async function insertUtxo(
utxoId: number,
Copy link
Contributor

Choose a reason for hiding this comment

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

lets reuse what we already have in the storage

.bind("utxo1")
.first<{ status: string; locked_until: number }>();
expect(utxo!.status).toBe(UtxoStatus.Locked);
expect(utxo!.locked_until).toBeGreaterThan(Date.now());
Copy link
Contributor

Choose a reason for hiding this comment

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

then lets just assert if its defined

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.

test: test to verify sql queries in redeem solver

3 participants