Skip to content

Conversation

@Villarley
Copy link

test: add comprehensive tests for minimum and maximum bet limits

Closes #244

Summary

This PR adds comprehensive test coverage for bet limit validation in the Predictify Hybrid contract. The implementation includes 31 new test cases covering boundary values, rejection scenarios, edge cases, integration flows, security checks, and comprehensive coverage scenarios.

Changes Made

Files Modified

  • contracts/predictify-hybrid/src/bet_tests.rs - Added 31 comprehensive bet limit validation tests

Test Coverage Added

1. Boundary Value Tests (4 tests)

  • test_place_bet_exactly_minimum() - Successful bet at MIN_BET_AMOUNT
  • test_place_bet_exactly_maximum() - Successful bet at MAX_BET_AMOUNT
  • test_validate_bet_amount_at_minimum() - Validator accepts minimum
  • test_validate_bet_amount_at_maximum() - Validator accepts maximum

2. Rejection Tests (8 tests)

  • test_place_bet_below_minimum_one_stroop() - Rejects MIN_BET_AMOUNT - 1
  • test_place_bet_below_minimum_half() - Rejects MIN_BET_AMOUNT / 2
  • test_place_bet_zero_amount() - Rejects zero amount
  • test_place_bet_negative_amount() - Rejects negative amounts
  • test_place_bet_above_maximum_one_stroop() - Rejects MAX_BET_AMOUNT + 1
  • test_place_bet_above_maximum_double() - Rejects MAX_BET_AMOUNT * 2
  • test_validate_bet_amount_below_minimum() - Validator rejects below min
  • test_validate_bet_amount_above_maximum() - Validator rejects above max

3. Edge Case Tests (7 tests)

  • test_validate_bet_amount_zero() - Zero validation
  • test_validate_bet_amount_negative() - Negative validation
  • test_validate_bet_amount_very_small() - Very small values (1 stroop)
  • test_validate_bet_amount_very_large() - Very large values (i128::MAX)
  • test_validate_bet_amount_boundary_minus_one() - MIN - 1
  • test_validate_bet_amount_boundary_plus_one() - MAX + 1
  • test_validate_bet_amount_within_range() - Valid values in range

4. Integration Tests (5 tests)

  • test_place_bet_minimum_with_sufficient_balance() - Full flow with min
  • test_place_bet_maximum_with_sufficient_balance() - Full flow with max
  • test_place_bet_below_minimum_rejects_with_error() - Error code verification
  • test_place_bet_above_maximum_rejects_with_error() - Error code verification
  • test_place_bet_valid_amounts_in_range() - Multiple valid amounts

5. Security and Error Code Tests (3 tests)

  • test_bet_below_minimum_returns_insufficient_stake() - Error 107 verification
  • test_bet_above_maximum_returns_invalid_input() - Error 401 verification
  • test_error_codes_match_constants() - All error codes verified

6. Comprehensive Coverage Tests (4 tests)

  • test_multiple_bets_at_different_limits() - Various users at boundaries
  • test_bet_limit_validation_in_isolation() - Direct validator calls
  • test_bet_limit_validation_in_place_bet_flow() - Through BetManager
  • test_bet_amount_edge_cases_comprehensive() - All edge cases

Test Coverage

image
  • Total Tests: 59 (28 existing + 31 new)
  • Coverage Target: ✅ Minimum 95% achieved
  • Code Paths Covered: All paths in BetValidator::validate_bet_amount()
  • Error Cases: All error scenarios tested (InsufficientStake=107, InvalidInput=401)
  • Success Cases: All success scenarios tested (min, max, and values in between)

Test Results

$ cargo test --lib -- bet
test result: ok. 75 passed; 0 failed; 0 ignored; 0 measured; 366 filtered out

All tests compile and pass successfully.

Implementation Details

Bet Limits Tested

  • MIN_BET_AMOUNT: 1_000_000 stroops (0.1 XLM)
  • MAX_BET_AMOUNT: 100_000_000_000 stroops (10,000 XLM)

Error Codes Verified

  • Error::InsufficientStake (107) - For amounts < MIN_BET_AMOUNT
  • Error::InvalidInput (401) - For amounts > MAX_BET_AMOUNT

Test Approach

  • Direct validator testing via BetValidator::validate_bet_amount()
  • Integration testing through BetManager::place_bet()
  • Boundary value analysis (exact min/max, min-1, max+1)
  • Edge case coverage (zero, negative, very small/large values)

Notes

Features Not Yet Implemented

The requirements mentioned testing for:

  • Admin-only limit updates: This feature doesn't exist yet (limits are currently hardcoded constants). Tests document this limitation.
  • Per-event vs global limits: This feature doesn't exist yet (only global limits are implemented). Tests document this limitation.

These features would require implementation before they can be tested. The current test suite focuses on comprehensive coverage of existing functionality.

Testing

To run the bet limit tests:

cd contracts/predictify-hybrid
cargo test --lib -- bet

To run a specific test:

cargo test --lib test_place_bet_exactly_minimum
cargo test --lib test_validate_bet_amount_at_minimum

Checklist

  • Tests written for successful bets at min and max limits
  • Tests written for rejection below min and above max
  • Tests written for edge cases (zero, negative, boundary values)
  • Tests written for error code verification
  • Tests written for integration flows
  • Tests written for comprehensive coverage scenarios
  • All tests compile successfully
  • All tests pass
  • Minimum 95% test coverage achieved
  • Clear documentation in test comments

Related Issues

Closes #244

- Introduced a suite of tests for validating bet amounts, including boundary value checks for minimum and maximum limits.
- Added tests to ensure rejection of invalid bet amounts (below minimum, above maximum, zero, and negative values).
- Implemented integration tests to verify bet placement with sufficient balance at minimum and maximum amounts.
- Enhanced overall test coverage for bet validation logic.
@greatest0fallt1me
Copy link
Contributor

@Villarley Resolve the merge conflicts

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: add comprehensive tests for minimum and maximum bet limits

2 participants