修复(履约绑定): 修复候选快照丢失与全链路绑定校验一致性
This commit is contained in:
@@ -1494,6 +1494,98 @@ def test_workbench_rebind_detects_new_cjm_scan_without_expected_role(
|
||||
assert pending_binding_role_from_result(task.result) == "巫师基尔斯84"
|
||||
|
||||
|
||||
def test_binding_poll_empty_baseline_preserves_need_bind_act_on_candidates(
|
||||
db, monkeypatch
|
||||
):
|
||||
"""扫码前双方都为空、扫码后动作通道出现 pending 时,bind_candidates 不能丢弃 need_bind_act。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = datetime.now(UTC)
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
empty_legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": "",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
empty_action = {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": False,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
pending_legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": "欧美真心",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
pending_action = {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "欧美真心",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
stage = {"candidates": [empty_legacy, empty_action]}
|
||||
client = type(
|
||||
"Client",
|
||||
(),
|
||||
{"get_bind_qr": lambda self, _alias: {"url": "https://example.test/qr"}},
|
||||
)()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_fetch_bind_info_candidates",
|
||||
lambda _client, aliases: [
|
||||
item
|
||||
for alias in aliases
|
||||
for item in stage["candidates"]
|
||||
if item["act_alias"] == alias
|
||||
],
|
||||
)
|
||||
config = {
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
}
|
||||
|
||||
runner._execute_get_bind_qr(db, task, account, "PHPSESSID=x", config)
|
||||
stage["candidates"] = [pending_legacy, pending_action]
|
||||
runner.poll_bind_role_once(db, task, account, config)
|
||||
db.refresh(task)
|
||||
|
||||
assert task.status == "success"
|
||||
assert task.result["bind_phase"] == "role_ready"
|
||||
action_cand = next(
|
||||
item
|
||||
for item in task.result["bind_candidates"]
|
||||
if item["act_alias"] == "20260120QYOOB"
|
||||
)
|
||||
assert action_cand["need_bind_act"] is True
|
||||
assert action_cand["is_bound_role"] is True
|
||||
assert action_cand["area_name"] == "手Q"
|
||||
assert action_cand["plat_name"] == "苹果(iOS)"
|
||||
assert pending_binding_role_from_result(task.result) == "欧美真心"
|
||||
|
||||
|
||||
def test_binding_poll_preserves_concurrently_written_expected_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
@@ -1801,3 +1893,140 @@ def test_mysql_lock_wait_timeout_is_bounded():
|
||||
).scalar()
|
||||
assert value is not None
|
||||
assert int(value) <= 10
|
||||
|
||||
|
||||
def test_confirm_bind_rejects_when_after_bound_role_mismatches_expected(
|
||||
db, monkeypatch
|
||||
):
|
||||
"""actBind 后动作通道仍绑定旧角色而非目标角色时,必须判失败且保留 action_act_aliases。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
old_role = "历史旧角色"
|
||||
target_role = "目标新角色"
|
||||
task.task_type = "confirm_bind"
|
||||
task.result = {
|
||||
"expected_role_name": target_role,
|
||||
"expected_role_required": True,
|
||||
"baseline_role_name": old_role,
|
||||
}
|
||||
account.game_name = old_role
|
||||
account.role_bind_status = "bound"
|
||||
db.commit()
|
||||
|
||||
old_bound = {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": old_role,
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_account": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
cjm_target = {
|
||||
**old_bound,
|
||||
"act_alias": "cjm",
|
||||
"role_name": target_role,
|
||||
"is_bound_act": False,
|
||||
"need_bind_act": False,
|
||||
}
|
||||
|
||||
class Client:
|
||||
@staticmethod
|
||||
def confirm_bind(_alias):
|
||||
return {"ctn": "ctn", "raw": {"error": 0, "msg": "ok"}}
|
||||
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"confirm_bind",
|
||||
close_db=False,
|
||||
)
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: Client())
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_fetch_bind_info_candidates",
|
||||
lambda *_args: [cjm_target, old_bound],
|
||||
)
|
||||
|
||||
runner._execute_confirm_bind(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
db.refresh(account)
|
||||
assert task.status == "failed"
|
||||
assert task.result["issue_code"] == "role_changed"
|
||||
assert task.result["bind_confirmed"] is False
|
||||
assert task.result["action_act_aliases"] == ["20260120QYOOB"]
|
||||
assert len(task.result["bind_candidates"]) == 2
|
||||
assert account.game_name == old_role
|
||||
|
||||
|
||||
def test_query_esports_game_name_uses_valid_bind_status_enums(db, monkeypatch):
|
||||
"""电竞手册未确认或未绑定时,查询角色不能向 set_bind_status 传入旧自由文本。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
task.task_type = "query_esports_game_name"
|
||||
task.handbook_scope = "esports"
|
||||
db.commit()
|
||||
|
||||
states = iter(
|
||||
[
|
||||
{
|
||||
"role_name": "电竞待确认角色",
|
||||
"area_name": "微信",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"esports_bound": False,
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"game_id": "pel",
|
||||
},
|
||||
{
|
||||
"role_name": "",
|
||||
"area_name": "",
|
||||
"plat_name": "",
|
||||
"esports_bound": False,
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": False,
|
||||
"game_id": "pel",
|
||||
},
|
||||
]
|
||||
)
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"query_esports_game_name",
|
||||
close_db=False,
|
||||
)
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: object())
|
||||
monkeypatch.setattr(runner, "_esports_bind_state", lambda *_args: next(states))
|
||||
|
||||
runner._execute_query_esports_game_name(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{"esports_act_alias": "ESPORTS_ACT"},
|
||||
)
|
||||
db.refresh(account)
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert account.esports_bind_status == "pending"
|
||||
|
||||
runner._execute_query_esports_game_name(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{"esports_act_alias": "ESPORTS_ACT"},
|
||||
)
|
||||
db.refresh(account)
|
||||
assert account.esports_bind_status == "unbound"
|
||||
|
||||
Reference in New Issue
Block a user