Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mock mainnet btc blockstream requests #5642

Merged
merged 1 commit into from
Jul 12, 2024
Merged

feat: mock mainnet btc blockstream requests #5642

merged 1 commit into from
Jul 12, 2024

Conversation

alter-eggo
Copy link
Contributor

@alter-eggo alter-eggo commented Jul 11, 2024

Try out Leather build 02f4f65Extension build, Test report, Storybook, Chromatic

This pr mocks btc blockstream mainnet requests

Summary by CodeRabbit

  • New Features

    • Enhanced onboarding test to verify 'No activity yet' text visibility under specific conditions.
  • Bug Fixes

    • Standardized API base URLs in BTC transaction tests to use constants for improved maintainability.
  • Tests

    • Introduced mock API requests for better simulation in test environments.
    • Added new async functions and routes to streamline test execution and accuracy.

Copy link

coderabbitai bot commented Jul 11, 2024

Walkthrough

Recent updates mainly focus on enhancing asynchronous operations, refining mock data setups, and using constants for API base URLs in tests. Key changes include introducing concurrency in setupMockApis, adding new mock request functions in mock-utxos.ts, and switching hardcoded URLs to constants in send-btc.spec.ts. Additionally, the onboarding.spec.ts test now intercepts a specific route to simulate no activity in the Activity tab.

Changes

Files Change Summary
tests/mocks/mock-apis.ts Utilized Promise.all for concurrent async operations; added mock API setup for mockMainnetTestAccountBlockstreamRequests.
tests/mocks/mock-utxos.ts Added imports for Page and TEST_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS; added mockMainnetNsTransactionsTestAccount array; defined mockMainnetTestAccountBlockstreamRequests async function.
tests/specs/send/send-btc.spec.ts Updated imports to include BESTINSLOT_API_BASE_URL_TESTNET; replaced hardcoded URLs with constants.
tests/specs/onboarding/onboarding.spec.ts Added page parameter to Activity tab test; intercepted blockstream.info/api/address/**/txs route to return an empty JSON array.

Sequence Diagrams

sequenceDiagram
    participant Test as Test
    participant MockAPIs as Mock APIs
    participant MockUTXOs as Mock UTXOs
    participant Blockstream as Blockstream API

    Test ->> MockAPIs: setupMockApis()
    MockAPIs ->> MockUTXOs: mockMainnetTestAccountBlockstreamRequests(page)
    MockUTXOs ->> Blockstream: GET /api/address/**/txs
    Blockstream -->> MockUTXOs: Return empty JSON array
    MockUTXOs -->> MockAPIs: Mock responses configured
    MockAPIs -->> Test: setup complete
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.


export async function setupMockApis(page: Page) {
await page.route(/chrome-extension/, route => route.continue());
await page.route(/github/, route => route.fulfill(json({})));
await page.route('https://api.hiro.so/', route => route.fulfill());
await page.route('https://api.testnet.hiro.so/', route => route.fulfill());
await mockStacksFeeRequests(page);
await Promise.all([mockMainnetTestAccountBlockstreamRequests(page), mockStacksFeeRequests(page)]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the Promise.all really needed here, given it's just for the definition of the mock? You could equally await the 5 lines above too, but haven't seen this done in docs

@fbwoolf
Copy link
Contributor

fbwoolf commented Jul 11, 2024

When I mocked just there it also didn't fix the test, I actually had to put it directly in signInWithTestAccount, but still not sure why.

@alter-eggo alter-eggo force-pushed the fix/test-btc branch 2 times, most recently from b963ac1 to 414e4b8 Compare July 12, 2024 11:38
@alter-eggo alter-eggo marked this pull request as ready for review July 12, 2024 11:45
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 71f2565 and 9722533.

Files selected for processing (3)
  • tests/mocks/mock-apis.ts (1 hunks)
  • tests/mocks/mock-utxos.ts (2 hunks)
  • tests/specs/send/send-btc.spec.ts (3 hunks)
Additional context used
Gitleaks
tests/mocks/mock-utxos.ts

53-53: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


57-57: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


72-72: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


75-75: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


90-90: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


93-93: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


97-97: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


100-100: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Additional comments not posted (8)
tests/mocks/mock-apis.ts (2)

5-5: Approved: New import for mockMainnetTestAccountBlockstreamRequests.

The new import aligns with the added functionality for mocking mainnet BTC requests.


8-15: Efficient use of Promise.all, but reconsider necessity.

The use of Promise.all is efficient for concurrent execution of asynchronous operations. However, reconsider if it's necessary given the context of defining mocks.

tests/mocks/mock-utxos.ts (3)

1-3: Approved: New imports for Page and TEST_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS.

The new imports are necessary for the added functionality.


43-114: Approved: New array mockMainnetNsTransactionsTestAccount.

The array structure is well-defined and includes detailed transaction data necessary for mocking responses.

Tools
Gitleaks

53-53: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


57-57: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


72-72: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


75-75: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


90-90: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


93-93: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


97-97: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


100-100: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


116-131: Approved: New async function mockMainnetTestAccountBlockstreamRequests.

The function correctly sets up request routes and uses Promise.all for concurrent execution of asynchronous operations.

tests/specs/send/send-btc.spec.ts (3)

7-7: Approved: Updated import statement to include BESTINSLOT_API_BASE_URL_TESTNET.

The updated import statement is correct and aligns with the usage of the new constant.


130-130: Approved: Replaced hardcoded API base URL with BESTINSLOT_API_BASE_URL_TESTNET.

The replacement improves maintainability by using a constant instead of hardcoded values.


143-143: Approved: Replaced hardcoded API base URL with BESTINSLOT_API_BASE_URL_TESTNET.

The replacement improves maintainability by using a constant instead of hardcoded values.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
tests/mocks/mock-apis.ts (1)

8-15: Asynchronous handling is acceptable, but consider simplifying.

Using Promise.all is efficient for handling multiple asynchronous operations concurrently. However, if the operations are not interdependent, you might consider awaiting them sequentially for better readability.

-  await Promise.all([
-    page.route(/chrome-extension/, route => route.continue()),
-    page.route(/github/, route => route.fulfill(json({}))),
-    page.route('https://api.hiro.so/', route => route.fulfill()),
-    page.route('https://api.testnet.hiro.so/', route => route.fulfill()),
-    mockMainnetTestAccountBlockstreamRequests(page),
-    mockStacksFeeRequests(page),
-  ]);
+  await page.route(/chrome-extension/, route => route.continue());
+  await page.route(/github/, route => route.fulfill(json({})));
+  await page.route('https://api.hiro.so/', route => route.fulfill());
+  await page.route('https://api.testnet.hiro.so/', route => route.fulfill());
+  await mockMainnetTestAccountBlockstreamRequests(page);
+  await mockStacksFeeRequests(page);
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9722533 and 02f4f65.

Files selected for processing (4)
  • tests/mocks/mock-apis.ts (1 hunks)
  • tests/mocks/mock-utxos.ts (2 hunks)
  • tests/specs/onboarding/onboarding.spec.ts (1 hunks)
  • tests/specs/send/send-btc.spec.ts (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/specs/send/send-btc.spec.ts
Additional context used
Gitleaks
tests/mocks/mock-utxos.ts

53-53: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


57-57: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


72-72: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


75-75: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


90-90: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


93-93: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


97-97: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


100-100: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Additional comments not posted (6)
tests/mocks/mock-apis.ts (1)

5-5: Import is appropriate.

The new import for mockMainnetTestAccountBlockstreamRequests is correctly added.

tests/specs/onboarding/onboarding.spec.ts (2)

64-64: Function signature update is acceptable.

Adding the page parameter to the function signature ensures that the new route can be set up correctly.


67-71: Correctly setting up the new route.

The route for blockstream.info/api/address/**/txs is correctly set up to fulfill with an empty JSON array. This will mock the response as intended.

tests/mocks/mock-utxos.ts (3)

1-3: Imports are appropriate.

The new imports for Page and TEST_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS are correctly added.


43-114: Mock transactions array is well-defined.

The mockMainnetNsTransactionsTestAccount array is correctly defined with detailed transaction data. However, ensure that no sensitive data is included.

Tools
Gitleaks

53-53: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


57-57: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


72-72: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


75-75: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


90-90: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


93-93: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


97-97: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


100-100: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


116-130: Function to handle BTC Blockstream requests is correctly implemented.

The mockMainnetTestAccountBlockstreamRequests function correctly sets up routes to mock responses for BTC Blockstream requests. Using Promise.all here is efficient.

@alter-eggo alter-eggo added this pull request to the merge queue Jul 12, 2024
Merged via the queue into dev with commit 16d751c Jul 12, 2024
30 checks passed
@alter-eggo alter-eggo deleted the fix/test-btc branch July 12, 2024 12:57
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.

3 participants