Fix the balance checks for a funds transfer test

Review the AI's codeEasy10 minStripe style

Your task

An AI agent wrote a test for transferFunds(from, to, amount) in your payments service. It mocked the database and asserted only that the call returned { success: true }, so a transfer that debits twice or credits nothing still passed. Your team now checks both balances after every transfer instead.

An assistant wrote transfer_violations(from_before_cents, to_before_cents, from_after_cents, to_after_cents, amount_cents). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • from_before_cents and to_before_cents: the sender's and the receiver's balances before the transfer, as integer cents.
  • from_after_cents and to_after_cents: the same two balances after the transfer, as integer cents. Any balance can be negative.
  • amount_cents: the amount the transfer was asked to move, as integer cents. It can be 0 or negative.
  • Return the list of violations, empty when the transfer is correct.

Rules the tests check:

  • Run every check below, in this order, and add each one that applies:
    1. invalid_amount when amount_cents is 0 or less.
    2. source_debit_wrong when from_after_cents is not exactly from_before_cents minus amount_cents.
    3. destination_credit_wrong when to_after_cents is not exactly to_before_cents plus amount_cents.
    4. total_not_conserved when the two balances after do not add up to the same sum as the two balances before.
    5. source_overdrawn when from_after_cents is below 0.
  • invalid_amount does not stop the other checks. They still use amount_cents as given.

Examples

transfer_violations(from_before_cents: int, to_before_cents: int, from_after_cents: int, to_after_cents: int, amount_cents: int) -> list[str]
Example 1
Inputtransfer_violations(10000, 5000, 7500, 7500, 2500)
Output[]
Example 2
Inputtransfer_violations(10000, 5000, 7500, 5000, 2500)
Output["destination_credit_wrong", "total_not_conserved"]
Example 3
Inputtransfer_violations(10000, 5000, 10000, 5000, 2500)
Output["source_debit_wrong", "destination_credit_wrong"]