Skip to content

[1] Add dedicated tests for repay_installment() #1

@EmeditWeb

Description

@EmeditWeb

Problem

The repay_installment() function in contracts/creditline-contract/src/lib.rs lacks isolated unit tests. The only existing coverage is incidental via end-to-end flow tests, leaving error paths (double-pay, out-of-bounds, unauthorized, zero-amount) untested. Without these, regressions in repayment logic can silently break borrower balances.

Context

Repayment correctness is the most safety-critical operation in StepFi — a bug here can either lock learners out of repaying or allow them to pay twice. Sponsors lose trust if installments are mis-accounted, and the reputation contract derives scoring from these calls. This must be airtight before mainnet.

Before Starting

Read these context files first:

  • context/architecture-context.md
  • context/code-standards.md
  • context/progress-tracker.md
  • contracts/creditline-contract/src/lib.rs
  • contracts/creditline-contract/src/tests.rs

What To Build

  1. Add a helper setup_loan_with_schedule(env, borrower, amount, n_installments) at the top of tests.rs that initializes the contract, creates a loan, and approves it.
  2. Test repay_installment_happy_path: pay installment 0, assert Installment.paid == true, assert outstanding balance decremented by exact amount.
  3. Test repay_installment_double_pay_rejected: pay installment 0, then call again — assert ContractError::InstallmentAlreadyPaid is returned.
  4. Test repay_installment_out_of_bounds: call with index installments.len() — assert ContractError::InvalidInstallmentIndex.
  5. Test repay_installment_non_borrower_rejected: call from a wallet other than the loan borrower — assert auth panic via env.mock_auths() mismatch.
  6. Test repay_installment_zero_amount_rejected: ensure zero-amount payments are explicitly rejected with ContractError::InvalidAmount.

Files To Touch

  • contracts/creditline-contract/src/tests.rs
  • contracts/creditline-contract/src/errors.rs (if InvalidInstallmentIndex / InstallmentAlreadyPaid not yet defined)

Acceptance Criteria

  • 5 new #[test] functions covering each scenario above
  • cargo test -p creditline-contract passes with all new tests green
  • Each test uses Env::default() and mock_all_auths() where appropriate
  • No existing test is modified or weakened
  • Test names follow the repay_installment_<scenario> convention
  • Coverage report shows repay_installment() branch coverage ≥ 90%

Mandatory Checks Before PR

  • cargo build passes with zero errors
  • cargo test — all 93 existing tests still pass
  • require_auth() is FIRST line of every mutating function
  • extend_ttl() called after EVERY persistent storage write
  • New unit tests written for every new function
  • context/progress-tracker.md updated

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions