Skip to content

(Medium) SC-19 real five-contract E2E happy-path loan lifecycle #155

Description

@EmeditWeb

Summary

The creditline suite has two harnesses: TestCtx (mock reputation + mock pool) driving the bulk of the 148 tests, and RealIntegrationCtx (contracts/creditline-contract/src/tests.rs:2535) which wires the real reputation, vendor-registry, pool and parameters contracts. The real harness today only exercises the create_loan (auto-funded) happy path (test_end_to_end_happy_path_across_all_contracts, :2754) — the full request → approve → fund → repay → score-up lifecycle through request_loan (contracts/creditline-contract/src/lib.rs:133-177) and approve_loan (:679-752) across all-real contracts is not covered end-to-end. The Pending → Active transition, the second validation pass at approval (:705-710), and the pool funding that happens only at approval (:726-733) are asserted only against mocks (TestCtx, used by 109 tests). This workstream adds the missing all-real lifecycle so the request/approve path is proven against the actual contracts.

Why this matters: the property is the request/approve/fund/repay lifecycle is proven end-to-end against the real contracts, not mocks. Mock assertions (was_fund_loan_called) cannot catch a divergence between the real pool's accounting and what creditline expects; the request→approve path has no real-contract coverage at all today.

Labels

area: contracts type: testing priority: medium


Workstream 1 — All-real request → approve → fund → repay → score-up lifecycle

Objective

Add a RealIntegrationCtx-based test that drives a loan through request_loan → approve_loan → repay_loan against the real reputation, registry, pool and parameters contracts, asserting balances, statuses and the reputation increase.

Problem

RealIntegrationCtx (contracts/creditline-contract/src/tests.rs:2535-2666) registers ReputationContract (:2565), VendorRegistryContract (:2570), LiquidityPoolContract (:2572) and ParametersContract (:2575), and its existing tests use create_loan (:2745,2789) — the auto-funded path. The request_loan/approve_loan split exists only in the mock TestCtx (create_default_request at :301, and approve tests), where the pool and reputation are mocks and was_fund_loan_called (:353-355) stands in for real accounting. The real approval-time re-validation (validate_vendor/validate_reputation/validate_liquidity, lib.rs:705-710) and pool funding (fund_loan_from_pool, :726-733) therefore have no all-real assertion. vouching's suite likewise has no all-real three-contract flow (contracts/vouching-contract/src/tests.rs uses MockReputationContract throughout).

Scope

  • contracts/creditline-contract/src/tests.rs (edit — add the all-real request/approve/repay test)
  • contracts/vouching-contract/src/tests.rs (edit — see con-test-03; a vouch→creditline eligibility flow lives there)

Implementation

  1. In RealIntegrationCtx, add a helper request_and_approve that calls creditline.request_loan and then creditline.approve_loan(admin, loan_id), funding the pool and minting the borrower's guarantee as needed.
  2. Add test_real_lifecycle_request_approve_fund_repay_across_all_contracts: fund the real pool, register an active vendor in the real registry, set_score on the real reputation, request a loan, assert Pending and the guarantee transfer (lib.rs:159-161), approve it, assert Active + funded_at and the real pool's locked_liquidity increased, repay in full, assert Paid, the guarantee refund, and the real reputation score increase (early vs on-time, :1176).
  3. Assert the real pool received the correct principal/interest split via pool.get_pool_stats() / receive_repayment effects, replacing the mock's was_receive_repayment_called (:360-362).
  4. Keep the existing mock tests where they assert pure creditline-internal logic; this workstream adds the real-contract counterpart, it does not delete the mocks (that is con-test-04/06).

Acceptance Criteria

  • Given a real pool with liquidity, a registered vendor and a scored borrower, when request_loan then approve_loan are called, then the loan is Active, the real pool locked_liquidity equals the pool contribution, and the guarantee sits with creditline.
  • Given full repayment, when repay_loan is called, then the loan is Paid, the guarantee is refunded, and the real reputation score has increased by the configured reward.
  • The test fails if the pool is not registered or the vendor is inactive at approval time (proving the real approval-time validation at :705-710 runs).

Testing

  • Integration (the deliverable): the all-real lifecycle above.
  • Regression: assert the Pending→Active transition leaves funded_at set (:717) — the mock path never checks the real pool side of this.

Shared acceptance criteria (whole epic)

  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo build --locked --target wasm32-unknown-unknown --release, cargo test --locked all exit 0; test count does not drop.
  • New tests live beside the existing real-integration tests and reuse RealIntegrationCtx; no production code changed.
  • No secrets; no changes outside contracts/*-contract/src/tests.rs.

Security & compatibility considerations

  • These are test-only changes; they must not weaken any assertion or introduce a mock where a real contract is required.
  • The real registry must be initialized and the vendor approved for the flow to pass — mirrors production wiring; do not bypass with mock_all_auths shortcuts that hide auth failures beyond what the suite already uses.

References

  • contracts/creditline-contract/src/tests.rs:2535-2666 — RealIntegrationCtx::setup + helpers.
  • contracts/creditline-contract/src/tests.rs:2754-2793 — existing real happy path (create_loan).
  • contracts/creditline-contract/src/tests.rs:159-205,301-330,350-376 — mock harness and was_* helpers to supersede.
  • contracts/creditline-contract/src/lib.rs:133-177,679-752 — request_loan / approve_loan.
  • contracts/vouching-contract/src/tests.rs — vouching suite (real-reputation flow is con-test-03).
  • Extends the real-integration harness introduced for the existing test_end_to_end_* tests; adds the request/approve path they omit.

If you're solving this with AI

In scope: contracts/creditline-contract/src/tests.rs (and contracts/vouching-contract/src/tests.rs only as con-test-03 directs) — only.
Out of scope: any production lib.rs/storage.rs change; the other contracts' non-test code; new runtime deps; deleting existing mock tests (done in con-test-04/06); reformatting untouched tests.
Must: follow the repo PR template exactly; keep CI green for real (fmt + clippy + build + test); the new test must exercise real contracts (no mock externals); reference this issue (Closes #<n>); no secrets.

Contribution requirements: Follow the repo PR template exactly — https://github.com/StepFi-app/StepFi-Contracts/blob/main/.github/pull_request_template.md. Reference this issue in your PR. CI must pass green (fmt + clippy + build + tests). No secrets; no out-of-scope changes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions