Skip to content

test: add test coverage for subscription registry - #566

Merged
Naomi-Gift merged 1 commit into
HubDApp:mainfrom
amankoli09:feature/issue-493-subscription-registry-tests
Aug 20, 2026
Merged

test: add test coverage for subscription registry#566
Naomi-Gift merged 1 commit into
HubDApp:mainfrom
amankoli09:feature/issue-493-subscription-registry-tests

Conversation

@amankoli09

Copy link
Copy Markdown
Contributor

🚀 Feature: Comprehensive Unit Test Coverage for Subscription Registry

📝 Description

This Pull Request addresses Issue #493 by introducing a robust suite of inline unit tests directly within the SubscriptionRegistry module (dongle-smartcontract/src/subscription_registry.rs).

Closes #493

Previously, test coverage for the subscription logic was primarily driven by high-level integration tests (tests/subscriptions.rs). While valuable, those tests evaluated the contract's outermost layer (the Client Wrapper) and left the direct mechanics of SubscriptionRegistry untested in isolation.

This PR bridges that gap by directly validating the core mechanisms governing project follows, unfollows, and pagination limits at the struct level, bringing deep confidence to the SubscriptionRegistry implementation. Every logical pathway, including state transitions and authorization edge cases within the Soroban VM, is now explicitly tested.


🔍 Detailed Changes & Additions

1. State Mutation & Integrity Tests

  • test_follow_project: Validates the happy path of a user successfully following a project. Ensures that:
    • The follower_count correctly increments.
    • The internal is_following boolean check returns true.
    • The proper event (PROJECT_FOLLOWED) would be triggered implicitly by maintaining the expected state.
  • test_unfollow_project: Validates the happy path of a user removing a follow. Ensures that:
    • The follower_count safely decrements.
    • The is_following boolean correctly reflects the unfollow as false.

2. Error Boundary & Edge Case Tests

Ensures the contract safely halts execution and returns deterministic errors when bad state changes are attempted.

  • test_duplicate_follow_error: Asserts that a user attempting to follow a project they are already following triggers a graceful ContractError::AlreadyFollowing instead of appending duplicate addresses to persistent storage.
  • test_unfollow_not_following_error: Asserts that an attempt to unfollow a project that the user has never followed cleanly triggers ContractError::NotFollowing, preventing underflows or missing index panics.

3. Pagination & Limit Boundaries

Because smart contracts must minimize execution cost (gas/compute) on state iteration, pagination is mission-critical.

  • test_get_project_followers_pagination: Mocks 5 distinct users following a single project and tests retrieving them via the get_project_followers method in discrete chunks. Validates chunk sizing (limit=2), boundary conditions on the final page (length=1), and out-of-bounds start indices returning empty pages.
  • test_get_user_subscriptions_pagination: Mocks a single user subscribing to 4 distinct projects. Verifies that get_user_subscriptions correctly slices the array of project IDs and bounds-checks the maximum items returned against the internal MAX_PAGE_LIMIT.

4. Soroban Execution Context Fidelity

A major technical challenge addressed in this PR is the HostError: Error(Auth, ExistingValue) panic that arises from mock_all_auths() reusing the same invocation frame for multiple state mutations in Soroban.

  • Resolution: All storage modifications and state assertions inside the tests are now structurally wrapped in separate env.as_contract(&client.address, || { ... }) closures. This accurately mimics the distinct boundaries of external contract invocations, ensuring perfectly isolated authentication frames and preventing synthetic test crashes.

🔗 Related Issues


🧪 Testing and Verification

  • Local Execution: Successfully executed cargo test -p dongle-contract on the workspace.
  • Zero Regressions: Existing integration tests (e.g. tests/subscriptions.rs) pass flawlessly alongside the newly introduced inline unit tests.
  • No Linter Warnings: Code follows existing formatting (cargo fmt) and clippy requirements.

💡 Reviewer Notes

When reviewing, pay special attention to the env.as_contract boundaries within the new mod tests. These scopes are required because SubscriptionRegistry methods act directly on env.storage(), which requires an active contract host ID context—something the native test environment lacks unless explicitly supplied via closure execution.

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: no tests for follow/unfollow and subscription registry

2 participants