Skip to content

Add contract test enhancements and lightweight view function#1236

Merged
Olowodarey merged 2 commits into
Arena1X:mainfrom
designsage8:contract-test-enhancements
Jun 27, 2026
Merged

Add contract test enhancements and lightweight view function#1236
Olowodarey merged 2 commits into
Arena1X:mainfrom
designsage8:contract-test-enhancements

Conversation

@designsage8

Copy link
Copy Markdown
Contributor

Contract Test Enhancements and Lightweight View Function

Overview

This PR adds comprehensive test coverage for platform statistics, prediction distribution, and market cancellation, plus a new lightweight view function for counting user-joined events. These changes improve test coverage, add performance optimizations for dashboard queries, and ensure refund correctness in the open-market contract.

Closes #1036
Closes #1038
Closes #1035
Closes #1042


Changes

🧪 Task 1: Add comprehensive test for get_platform_statistics (#1036)

Problem:
views::get_platform_statistics returns a PlatformStatistics struct with counters for total events, matches, predictions, participants, and fees. There was no integration test that drives all of these counters through multiple operations and asserts each field.

Solution:

  • Added test_get_platform_statistics_comprehensive_counter_test() in views_tests.rs
  • Test exercises all counters through a complete workflow:
    • Initial state: all counters at 0
    • Create 2 events: assert total_events == 2
    • Add 2 matches to each event (4 total): assert total_matches == 4
    • Have 3 users join both events and submit predictions for each match
    • Assert total_predictions == 12 (3 users × 2 events × 2 matches)
    • Assert unique_participants == 3
    • Assert total_fees_collected == FEE * 2

Files Changed:

  • contracts/creator-event-manager/tests/views_tests.rs - Added comprehensive counter test

Acceptance Criteria Met:

  • ✅ Each counter matches the expected cumulative value after each operation
  • ✅ All counters (events, matches, predictions, participants, fees) tested

✨ Task 2: Add get_user_joined_events_count lightweight view function (#1038)

Problem:
get_user_events returns the full Vec of event IDs a user has joined. For dashboards that display only a "X events joined" badge, loading the entire vector is wasteful. A lightweight count function is needed.

Solution:

  • Added get_user_joined_events_count(env, user) -> u32 in src/views.rs
  • Wired the function into src/lib.rs as a public contract method
  • Added comprehensive tests in tests/views_tests.rs:
    • Returns 0 for unknown users
    • Consistent with get_user_events().len()
    • Increments with each join

Files Changed:

  • contracts/creator-event-manager/src/views.rs - Added lightweight count function
  • contracts/creator-event-manager/src/lib.rs - Wired function into contract interface
  • contracts/creator-event-manager/tests/views_tests.rs - Added 3 comprehensive tests

Acceptance Criteria Met:

  • ✅ Returns 0 for unknown users
  • ✅ Consistent with get_user_events().len()
  • ✅ Lightweight alternative for dashboard badges

🧪 Task 3: Add test for get_prediction_distribution with all three outcomes (#1035)

Problem:
prediction::get_prediction_distribution returns (team_a_count, draw_count, team_b_count). The existing tests did not exercise a scenario where users predict all three outcomes (TEAM_A, TEAM_B, DRAW) for the same match and verify each count independently.

Solution:

  • Added test_get_prediction_distribution_all_three_outcomes() with 5 users:
    • 2 users predict TEAM_A, 1 predicts DRAW, 2 predict TEAM_B
    • Assert distribution: (2, 2, 1)
  • Added test_get_prediction_distribution_zero_predictions_all_zero():
    • Call on a match with no predictions
    • Assert all counts are 0
  • Added test_get_prediction_distribution_single_outcome_saturation():
    • All 3 users predict TEAM_A
    • Assert distribution: (3, 0, 0)

Files Changed:

  • contracts/creator-event-manager/tests/prediction_tests.rs - Added 3 comprehensive tests

Acceptance Criteria Met:

  • ✅ Distribution is accurate for mixed predictions
  • ✅ All-zero for empty match
  • ✅ Single-outcome saturation works

🧪 Task 4: Add test for cancel_market refunds all stakers (#1042)

Problem:
market::cancel_market should refund every staker their original stake in full. The existing market tests did not have an end-to-end test that stakes from multiple users with different amounts, cancels the market, and verifies each user's balance is restored exactly.

Solution:

  • Added cancel_market_refunds_exact_stake_amounts() in market_tests.rs
  • Test creates a market and funds 3 users with different amounts (100, 250, 500 XLM)
  • Each user submits prediction with their respective stake
  • Records each balance before cancellation
  • Admin calls cancel_market
  • Asserts each user's balance is restored exactly
  • Asserts further predictions fail with MarketAlreadyCancelled

Files Changed:

  • contracts/open-market/tests/market_tests.rs - Added comprehensive refund verification test

Acceptance Criteria Met:

  • ✅ Every staker receives their exact stake back
  • ✅ No XLM is left in the contract after cancellation
  • ✅ Further predictions fail after cancellation

Testing

All changes include comprehensive unit tests:

Test Coverage:

  • ✅ Task 1: Platform statistics counter increments across multiple operations
  • ✅ Task 2: User joined events count returns 0 for unknown users and matches get_user_events length
  • ✅ Task 3: Prediction distribution for mixed outcomes, zero predictions, and single-outcome saturation
  • ✅ Task 4: Market cancellation refunds exact stake amounts to all stakers

Run Tests:

# Test creator-event-manager contract
cd contracts/creator-event-manager
cargo test

# Test open-market contract
cd contracts/open-market
cargo test

Breaking Changes

None. All changes are additive (new tests and new view function).


Migration Notes

No database migrations or contract upgrades required. All changes are test additions and a new read-only view function.


Performance Impact

  • Task 2: Improves dashboard performance by providing a lightweight count alternative to loading full event ID vectors

Security Considerations

  • Task 2: get_user_joined_events_count is a read-only view function with no authentication requirements, consistent with existing view functions

Future Improvements

  • Consider adding similar lightweight count functions for other dashboard metrics (e.g., user prediction counts)

Checklist

  • Code follows project style guidelines
  • All tests pass locally
  • New tests added for all changes
  • No breaking changes introduced
  • Documentation updated where necessary
  • PR description clearly explains changes

@vercel

vercel Bot commented Jun 27, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
insight-arena-4rll Ready Ready Preview, Comment Jun 27, 2026 10:56am

@drips-wave

drips-wave Bot commented Jun 27, 2026

Copy link
Copy Markdown

@designsage8 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

- Add comprehensive test for get_platform_statistics (Arena1X#1036)
- Add get_user_joined_events_count lightweight view function (Arena1X#1038)
- Add tests for get_prediction_distribution with all three outcomes (Arena1X#1035)
- Add test for cancel_market refunds all stakers (Arena1X#1042)

Closes Arena1X#1036, Arena1X#1038, Arena1X#1035, Arena1X#1042
@designsage8 designsage8 force-pushed the contract-test-enhancements branch from 348d2a6 to e66b1b5 Compare June 27, 2026 10:54
@Olowodarey Olowodarey merged commit 48155ba into Arena1X:main Jun 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment