Skip to content

test(profile): add credits.rs coverage for spend/earn/refund/admin_grant#48

Open
Sendi0011 wants to merge 1 commit into
boundlessfi:testnetfrom
Sendi0011:test/profile-credits-coverage
Open

test(profile): add credits.rs coverage for spend/earn/refund/admin_grant#48
Sendi0011 wants to merge 1 commit into
boundlessfi:testnetfrom
Sendi0011:test/profile-credits-coverage

Conversation

@Sendi0011

@Sendi0011 Sendi0011 commented Jun 23, 2026

Copy link
Copy Markdown

Summary

Adds tests/credits.rs for contracts/profile/src/credits.rs, covering
bootstrap, spend, earn, refund, and admin_grant.

Closes #26 (part of #25).

Coverage

  • Happy path for each function
  • Every Error variant actually reachable from credits.rs:
    EventsContractNotConfigured, OpAlreadySeen, Paused,
    ProfileNotFound, InsufficientCredits, ReasonRequired
  • Edge cases: zero-amount spend, exact-balance spend to zero, u32::MAX
    saturation on earn/refund/admin_grant, admin_grant bootstrapping
    a fresh profile for an unknown user
  • Idempotency: replayed op_id rejected on every function, including the
    zero-amount spend no-op path
  • Auth rejection: bootstrap/spend/earn/refund reject a caller that
    isn't the configured events contract; admin_grant rejects a caller
    that isn't admin. These use mock_auths + #[should_panic] since
    require_auth() panics rather than returning an Error — there was no
    existing auth-rejection test anywhere in the suite to follow, so this
    establishes the pattern.

Note: Error::InvalidAmount is declared in errors.rs but is never
returned by any path in credits.rs, so it isn't tested here — happy to
add if it's meant to be wired in as part of this issue.

Testing

```
cargo test -p boundless-profile
cargo test
```

Screenshot

Screenshot 2026-06-23 at 22 49 51

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for the credits system, including bootstrap, spend, earn, refund, and admin grant operations with edge case handling and error scenarios.

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 72b7199a-6840-41e8-825e-eff0700d4a82

📥 Commits

Reviewing files that changed from the base of the PR and between d3a32bd and 03dd912.

📒 Files selected for processing (2)
  • contracts/profile/src/tests/credits.rs
  • contracts/profile/src/tests/mod.rs

📝 Walkthrough

Walkthrough

Adds contracts/profile/src/tests/credits.rs, a 762-line test suite covering bootstrap, spend, earn, refund, and admin_grant operations in the profile credits contract. Each operation is tested for happy paths, edge cases (saturation, zero-amount), every error variant, and auth-rejection panics. The module is registered in tests/mod.rs.

Changes

Credits Contract Test Suite

Layer / File(s) Summary
Test scaffolding and bootstrap tests
contracts/profile/src/tests/mod.rs, contracts/profile/src/tests/credits.rs
Registers the new credits submodule and adds imports, shared op_id helper, and bootstrap tests covering default credit initialization, bootstrapped_at assignment, idempotency, replay rejection, missing events config, paused state, and non-events-contract panic.
Spend operation tests
contracts/profile/src/tests/credits.rs
Covers credit deduction, zero-amount no-op with op_id still marked as seen (replay rejected), insufficient-credit revert with no state change, unbootstrapped-user revert, replay rejection, paused and missing-events-config reverts, and non-events-contract panic.
Earn, refund, and admin_grant tests
contracts/profile/src/tests/credits.rs
Covers earn (saturation to u32::MAX, error variants, auth panic), refund via spend-then-refund workflow (saturation, same error variants), and admin_grant (bootstrap-on-unknown-user, saturation, empty-reason revert without profile creation, replay rejection, paused revert, non-admin panic).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • #26 (test(profile): credits — spend / earn / refund / admin_grant): This PR directly implements the acceptance criteria by adding tests/credits.rs covering all five operations with happy paths, every Error variant, edge cases, and auth-rejection tests, and wires it into tests/mod.rs.
  • #30: The PR's earn tests in credits.rs overlap with the requested earnings-specific coverage, though the organization differs (a combined file rather than a dedicated earnings.rs).

Poem

🐇 Hoppity-hop through the credit lanes,
Bootstrap and spend with no replay pains,
Earn to the max where u32 ends,
Refund the credits the contract defends,
Admin may grant but must know the reason—
All tests now pass in every season! ✨

🚥 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 accurately summarizes the main change: adding comprehensive test coverage for the credits module's spend/earn/refund/admin_grant functions.
Linked Issues check ✅ Passed The PR fully satisfies issue #26 requirements: new tests/credits.rs covers all functions (bootstrap, spend, earn, refund, admin_grant) with happy paths, error variants, edge cases, idempotency, and auth-rejection testing; all 49 tests pass.
Out of Scope Changes check ✅ Passed All changes are in-scope: adding tests/credits.rs test suite and a single mod declaration in tests/mod.rs, both directly supporting the test coverage objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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(profile): credits — spend / earn / refund / admin_grant

1 participant