Skip to content

feat(sdk): implement mock RPC server for unit tests#264

Open
mandyslovestories-sudo wants to merge 1 commit into
Fundable-Protocol:mainfrom
mandyslovestories-sudo:feat/mock-rpc-server-sdk-182
Open

feat(sdk): implement mock RPC server for unit tests#264
mandyslovestories-sudo wants to merge 1 commit into
Fundable-Protocol:mainfrom
mandyslovestories-sudo:feat/mock-rpc-server-sdk-182

Conversation

@mandyslovestories-sudo

@mandyslovestories-sudo mandyslovestories-sudo commented Jun 26, 2026

Copy link
Copy Markdown

Summary

Closes #182

Replaces the minimal 5-method stub in test-utils/mockRpcServer.ts with a fully-typed, reusable mock that covers every Soroban RPC method used across the SDK. Unit tests no longer have any network dependency.

Changes

src/test-utils/mockRpcServer.ts (rewrite)

  • Mock all 9 RPC methods as vi.fn(): getAccount, simulateTransaction, sendTransaction, getTransaction, getNetwork, getLatestLedger, getLedger, getFeeStats, getEvents
  • Add 12 pre-canned rpc.scenarios.* helpers for common situations: success, pendingThenSuccess, notFoundThenSuccess, transactionFailed, simulationError, simulationNetworkError, sendTransactionError, accountNotFound, networkError, highCongestionFeeStats, contractEvents, getEventsError
  • Intercept both @stellar/stellar-sdk/rpc and @stellar/stellar-sdk import paths via vi.mock() so all SDK modules are covered
  • resetMockRpcServer() clears call history and return values for clean beforeEach isolation
  • Fully typed exported interfaces for every response shape

src/__tests__/mockRpcServer.test.ts (new)

  • 49 tests covering the mock's own contract: constructor wiring, Api namespace, all scenario helpers, reset behaviour, and per-test override composition

Bug fixes

  • DistributorClient.test.ts — add missing mockTxNone helper (3 tests were calling it but it was never defined, would have thrown ReferenceError at runtime)
  • PaymentStreamClient.test.ts — same fix (1 test affected)

Testing

pnpm --filter @fundable/sdk test

All existing tests continue to pass. The new mockRpcServer.test.ts adds 49 additional tests.

Summary by CodeRabbit

  • Tests
    • Expanded SDK test coverage with a more complete mock RPC server.
    • Added support for simulating more response scenarios, including success, pending, failed, network errors, and no-result cases.
    • Improved test reliability by resetting mocks more consistently between runs.

…l#182)

Replaces the minimal stub in test-utils/mockRpcServer.ts with a
fully-typed, reusable mock that covers every Soroban RPC method
used by the SDK. Tests no longer depend on any network connection.

Changes:
- Expand mockRpcServer.ts to mock all 9 RPC methods: getAccount,
  simulateTransaction, sendTransaction, getTransaction, getNetwork,
  getLatestLedger, getLedger, getFeeStats, getEvents
- Add 12 pre-canned scenario helpers (rpc.scenarios.*) for the most
  common test situations (success, pendingThenSuccess, simulationError,
  transactionFailed, accountNotFound, networkError, etc.)
- Intercept both @stellar/stellar-sdk/rpc and @stellar/stellar-sdk
  import paths via vi.mock() so all SDK modules are covered
- Add resetMockRpcServer() that clears call history and return values
  for clean test isolation in beforeEach
- Add mockRpcServer.test.ts with 49 tests covering the mock contract
- Fix missing mockTxNone helper in DistributorClient.test.ts and
  PaymentStreamClient.test.ts (caused ReferenceError at runtime)

Closes Fundable-Protocol#182
@drips-wave

drips-wave Bot commented Jun 26, 2026

Copy link
Copy Markdown

@mandyslovestories-sudo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a reusable mock RPC server for SDK unit tests, with typed response shapes, preset scenarios, module-level SDK/RPC mocks, and coverage for those behaviors. Two client tests also gain helpers for transaction mocks that return undefined results.

Changes

Undefined transaction result helpers

Layer / File(s) Summary
No-result transaction mocks
packages/sdk/src/__tests__/DistributorClient.test.ts, packages/sdk/src/__tests__/PaymentStreamClient.test.ts
Adds mockTxNone helpers that keep signAndSend mocked while returning result: undefined.

Reusable mock RPC server

Layer / File(s) Summary
Typed mock shapes
packages/sdk/src/test-utils/mockRpcServer.ts
Defines typed response shapes, default fixtures, and the shared mock server type and constructor surface.
Scenario helpers and reset
packages/sdk/src/test-utils/mockRpcServer.ts
Adds the scenario helpers and rewrites reset logic to clear nested Vitest mocks while leaving scenarios in place.
SDK and RPC mocks
packages/sdk/src/test-utils/mockRpcServer.ts
Mocks @stellar/stellar-sdk/rpc and @stellar/stellar-sdk to expose the shared server, API helpers, and transaction status constants.
Base mock server tests
packages/sdk/src/__tests__/mockRpcServer.test.ts
Adds coverage for shared instance wiring, API constants, reset behavior, and mock function identity.
Scenario tests
packages/sdk/src/__tests__/mockRpcServer.test.ts
Adds coverage for default scenarios, transaction status sequences, failure cases, event responses, and overrides.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Poem

Hop-hop, I chewed through RPC strings,
and spun up mocky moonlit things.
No network sniffs, just static stars,
with tiny tests in carrot jars. 🥕
Now my whiskers twitch: success!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding a mock RPC server for SDK unit tests.
Linked Issues check ✅ Passed The PR implements a reusable mock RPC server to remove network dependence from SDK unit tests, matching issue #182.
Out of Scope Changes check ✅ Passed The added tests and helper fixes support the mock server work and do not appear unrelated to the stated objective.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/sdk/src/__tests__/DistributorClient.test.ts

Parsing error: Cannot read file '/tsconfig.json'.

packages/sdk/src/__tests__/PaymentStreamClient.test.ts

Parsing error: Cannot read file '/tsconfig.json'.

packages/sdk/src/__tests__/mockRpcServer.test.ts

Parsing error: Cannot read file '/tsconfig.json'.

  • 1 others

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/sdk/src/test-utils/mockRpcServer.ts`:
- Around line 444-452: resetMockRpcServer() is skipping vi.fn RPC method mocks
because Object.values(mock) returns functions, but the current guard only resets
objects. Update the resetMockRpcServer logic in mockRpcServer to also detect
function values (the RPC method mocks like getAccount and getTransaction) and
call mockReset on them, while still excluding the scenarios container so call
history and return values are cleared between tests.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fd56a0aa-ba2b-479e-ae7e-2a7fa9339058

📥 Commits

Reviewing files that changed from the base of the PR and between c085d63 and 47c7201.

📒 Files selected for processing (4)
  • packages/sdk/src/__tests__/DistributorClient.test.ts
  • packages/sdk/src/__tests__/PaymentStreamClient.test.ts
  • packages/sdk/src/__tests__/mockRpcServer.test.ts
  • packages/sdk/src/test-utils/mockRpcServer.ts

Comment thread packages/sdk/src/test-utils/mockRpcServer.ts
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.

[SDK] Implement mock RPC server for SDK unit tests

1 participant