Skip to content

cargo mutant test cases. - #36

Merged
MathisWellmann merged 8 commits into
mainfrom
mutant-tests
Nov 27, 2026
Merged

MathisWellmann merged 8 commits into
mainfrom
mutant-tests

Conversation

@MathisWellmann

Copy link
Copy Markdown
Owner

No description provided.

Exchange calls the inherent `check_*_with_market` methods in Cross mode, so the
trait impl was never reached. Its `check_maintenance_margin` duplicated the
isolated engine's static entry-price check, and 10 mutants in it survived
`cargo mutants` (including an inverted liquidation condition). Removing the dead
code is better than testing behaviour no caller can reach.
Cross-mode limit orders had no test, so even inverting the excess check in
`check_limit_order_with_market` survived `cargo mutants`. The flip path of
`check_market_order_with_market` was also untested. The new tests pin the
admission boundaries (zero excess, exactly enough post-flip equity) and make
the uPnL, fees and the realized pnl of the closed leg decide the outcome.

The repeated contract specs, configs and `Bba` literals are replaced by
helpers, and the `bba` helper duplicated in two test files moves to
`tests/mod.rs`.
The "both quotes are positive" check was written out three times (twice in
`Exchange::reconcile_margin`, once in the portfolio's `MarketSlot`). It now
lives on `MarketState`, so one set of tests covers every caller.

The cross branch of `reconcile_margin` had no test where the uPnL or a
missing quote decided the outcome; `cargo mutants` could ignore the uPnL, flip
its sign or invert the quote check unnoticed. New tests cover a margin call
caused by an unrealized loss, orders kept because an unrealized profit covers
them exactly, and the entry-notional maintenance fallback for a one-sided book.
Only the sell side of `clamp_reduce_only_quantity` was tested, so making the
buy guard always true (clamping a reduce-only buy to a long and increasing
it) survived `cargo mutants`. Also assert that zero is neither positive nor
negative for both currencies, because the guard relies on it for a flat
position.
`remove_by_id`, `remove_by_user_id` and both bulk cancels each repeated the
`notional_sum` and `num_reduce_only` updates. They now share `untrack`, which
also asserts that the reduce-only counter cannot underflow.

`cargo mutants` showed that removing by user id or cancelling oversized plain
orders could add the notional instead of subtracting it (overstating order
margin) without a failing test, and that the reduce-only counter could drift.
New tests pin the bookkeeping of every removal path, a reduce-only cap equal
to the resting total, and the maker fee reserve of a new limit order, which
only had positive-fee coverage away from the admission boundary.
`Ledger` had no unit tests, and integration tests only asserted that
`reconciles_to` holds, so replacing it with `true` survived `cargo mutants`.
The new unit tests check that it rejects off-by-one-ulp equities, that
`record_flows` skips zero flows and tracks the running equity, and that a
bad-debt debit panics.

The bad-debt delta in `Exchange::record_ledger_flows` was only exercised with
zero prior bad debt. A funding payment that bankrupts a profitable short,
followed by its liquidation, now journals a settlement that starts with
existing bad debt.

`total_by_kind` and `net_cashflow` share `sum_deltas`.
`MaintenanceMarginSchedule::from_brackets` re-checked each rate and cap, which
`MaintenanceMarginTier::new` validates anyway. `cargo mutants` could disable
these copies without a failing test because they were dead. The existing
`from_brackets_rejects_*` tests still cover both bounds through `new`. The
first-tier guard on the rate-decrease check goes too: a first rate below zero
is rejected either way.

New tests pin the trail-distance bounds of `StopOrder::trailing_stop` for both
sides. Before, a non-positive callback rate, or a rate of 1 on a buy stop, was
accepted when the rate check was disabled. A liquidation fee of zero is now
tested as valid.
@MathisWellmann MathisWellmann self-assigned this Nov 27, 2026
@github-actions

Copy link
Copy Markdown

Summary

Version bump 0.143.2 → 0.143.3 that extracts MarketState::has_positive_quotes, a SortedOrders::untrack/remove_at helper, and a sum_deltas ledger helper, drops the now-unused RiskEngine impl for CrossMarginRiskEngine, and adds broad test coverage (cross-margin admission/funding/liquidation, ledger bad debt, reduce-only rejection, trail-distance validation). The diff is clean: I verified the RiskEngine trait is now only used on IsolatedMarginRiskEngine (exchange.rs lines 494, 881, 1113, 1155, 1196, 1806) with the cross engine driven through its inherent *_with_market methods, so the removed impl breaks no call site; and the simplified from_brackets validation remains equivalent because MaintenanceMarginTier::new (margin_tiers.rs) rejects rate ∉ (0,1], cap ≤ 0, and negative offsets, with new tests covering the rate-decrease edge case.

Issues

None

@MathisWellmann
MathisWellmann merged commit e0ebcd4 into main Nov 27, 2026
2 checks passed
@MathisWellmann
MathisWellmann deleted the mutant-tests branch November 27, 2026 16:17
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.

1 participant