credits: an adjustment below the drawn amount must not eat the next pack
ci/woodpecker/push/woodpecker Pipeline was canceled

credits_drawn could exceed credit_balance after an admin set/correction (or a
negative adjustment), so a later pack was only partly usable: available =
max(0, purchased - drawn) swallowed the difference. Cap credits_drawn at the
row's balance on every add_credits movement; ledger history is unchanged.
Tests cover reduction-then-repurchase.
This commit is contained in:
inference-bot committed 2026-11-27 22:05:19 +00:00
1 parent 066a6fc2cf
commit d10b36ed34
2 files changed
+20 -2

No files matched your search

+11 -2
View File
@@ -32,8 +32,8 @@ for _cand in (os.environ.get("PORTAL_CODE_DIR"), "/app/code",
break
from app.main import ( # noqa: E402
_prev_month, apply_cycle_draw, compute_max_budget, cycle_start_for_anchor_day,
get_credit_balance, get_credits_drawn, get_db,
_prev_month, add_credits, apply_cycle_draw, compute_max_budget,
cycle_start_for_anchor_day, get_credit_balance, get_credits_drawn, get_db,
)
failures = 0
@@ -189,6 +189,15 @@ r2 = asyncio.run(m.close_credit_cycles())
check("cap applied once spend resets", applied, [15.0])
check("no second draw on the same cycle", r2["credits_drawn"], 0.0)
# --- lowering a pack below what was drawn must not swallow NEW credits ----
add_credits("small@example.com", -10.0, reason="admin correction (test)")
check_near("drawn follows the pack down when it is reduced", get_credits_drawn("small@example.com"), 0.0)
check_near("balance reduced to zero", get_credit_balance("small@example.com"), 0.0)
add_credits("small@example.com", 5.0, reason="new pack (test)")
check_near("a new pack is fully available after the correction",
compute_max_budget(15, get_credit_balance("small@example.com"), 0.0,
get_credits_drawn("small@example.com"))["credits_remaining"], 5.0)
print()
if failures:
print(f"{failures} FAILURE(S)")