fix(weibo): 隔离换绑角色的微豆核账

This commit is contained in:
yml
2026-11-13 17:20:44 +08:00
parent d2550d4ab7
commit b89ee4fd18
3 changed files with 267 additions and 99 deletions
+121
View File
@@ -172,6 +172,127 @@ def test_exchange_record_unique_match_and_archive(monkeypatch, db):
assert any(item["action"] == "weibo:collab:archive" for item in timeline)
def test_exchange_success_promotes_weidou_error_to_done(monkeypatch, db):
user = make_user(db)
account = make_account(db)
order = collab.create_order(
db,
user,
order_no="WB-ERROR-DONE",
shop="店铺A",
account_id=account.id,
goods_name="商品A",
required_weidou=100,
require_verified=False,
)
collab.set_status(
db,
user,
order,
collab.STATUS_WEIDOU_ERROR,
allow_check=True,
)
class FakeClient:
def __init__(self, *_args, **_kwargs):
pass
def list_exchange_records(self, *_args, **_kwargs):
return {
"list": [
{
"goods_name": "商品A",
"game_account": "角色A",
"cost_weidou": 100,
"exchange_time": "2099-01-01 00:00:00",
"reward_status": 2,
}
]
}
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
result = collab.check_exchange_records(db, user, [order])
db.refresh(order)
assert result["success"] == 1
assert order.exchange_verdict == collab.VERDICT_SUCCESS
assert order.status == collab.STATUS_DONE
assert order.done_at is not None
assert collab.summary(db)["by_status"] == {collab.STATUS_DONE: 1}
def test_balance_check_does_not_mix_roles_after_rebind(monkeypatch, db):
user = make_user(db)
account = make_account(db)
first = collab.create_order(
db,
user,
order_no="WB-ROLE-A",
shop="店铺A",
account_id=account.id,
goods_name="商品A",
required_weidou=100,
require_verified=False,
)
collab.set_status(
db,
user,
first,
collab.STATUS_WEIDOU_ERROR,
allow_check=True,
)
account.game_role_name = "角色B"
db.commit()
second = collab.create_order(
db,
user,
order_no="WB-ROLE-B",
shop="店铺A",
account_id=account.id,
goods_name="商品B",
required_weidou=200,
require_verified=False,
)
class FakeClient:
calls = 0
def __init__(self, *_args, **_kwargs):
pass
def list_mall(self, *_args, **_kwargs):
FakeClient.calls += 1
return {"weidou_balance": 200, "list": []}
monkeypatch.setattr(collab, "WeiboClient", FakeClient)
result = collab.check_balance_groups(db, user, [second], threshold=100)
db.refresh(first)
db.refresh(second)
assert result["groups"] == [
{
"account_id": account.id,
"uid": account.uid,
"game_role_name": "角色B",
"status": collab.STATUS_RECHARGED,
"status_label": collab.ORDER_STATUS_LABELS[collab.STATUS_RECHARGED],
"balance": 200,
"required_total": 200,
"surplus": 0,
"threshold": 100,
"reason": "ok",
"message": "核账通过:角色「角色B」,余额 200,订单合计 200,结余 0",
"order_ids": [second.id],
}
]
assert FakeClient.calls == 1
assert first.status == collab.STATUS_WEIDOU_ERROR
assert second.status == collab.STATUS_RECHARGED
def test_export_cks_returns_plaintext_and_audits_without_ck(db):
user = make_user(db)
account = make_account(db)