merge: 同步 main 同名角色绑定修复
This commit is contained in:
@@ -238,7 +238,34 @@ def test_same_role_after_activity_bind_transition_is_pending():
|
||||
)
|
||||
|
||||
|
||||
def test_pending_candidate_picker_ignores_legacy_same_role_residual():
|
||||
def test_same_bound_role_with_need_bind_act_is_confirmable():
|
||||
role = "少和我较劲"
|
||||
baseline = _role_candidate(
|
||||
ACT_ALIAS,
|
||||
role,
|
||||
bound_act=True,
|
||||
need_bind_act=True,
|
||||
)
|
||||
candidate = _role_candidate(
|
||||
ACT_ALIAS,
|
||||
role,
|
||||
bound_act=True,
|
||||
need_bind_act=True,
|
||||
)
|
||||
|
||||
decision = binding_pending_decision(
|
||||
candidate,
|
||||
baseline_role_name=role,
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=baseline,
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
)
|
||||
|
||||
assert decision["confirmable"] is True
|
||||
assert decision["reason"] == "same_alias_after_bound_transition"
|
||||
|
||||
|
||||
def test_pending_candidate_picker_uses_action_alias_same_role():
|
||||
old_role = "少和我较劲"
|
||||
baseline = _role_candidate(ACT_ALIAS, old_role, bound_act=True)
|
||||
legacy = _role_candidate(LEGACY_ALIAS, old_role, bound_act=False)
|
||||
@@ -252,7 +279,8 @@ def test_pending_candidate_picker_ignores_legacy_same_role_residual():
|
||||
baseline_info=baseline,
|
||||
)
|
||||
|
||||
assert selected is None
|
||||
assert selected is not None
|
||||
assert selected["act_alias"] == ACT_ALIAS
|
||||
|
||||
|
||||
def test_task_result_helpers_use_shared_same_role_transition_rule():
|
||||
|
||||
@@ -9,7 +9,7 @@ from sqlalchemy import text
|
||||
from tests.support.db import make_test_session
|
||||
from web.backend.database import engine as app_engine
|
||||
from web.backend.main import health
|
||||
from web.backend.models import Account, DouyuTask, User
|
||||
from web.backend.models import Account, DouyuRoleApiLog, DouyuTask, User
|
||||
from web.backend.routers import douyu as douyu_router
|
||||
from web.backend.schemas import DouyuTaskBatchRequest
|
||||
from web.backend.services import (
|
||||
@@ -20,7 +20,9 @@ from web.backend.services import (
|
||||
douyu_service,
|
||||
payment_poll_scheduler,
|
||||
)
|
||||
from web.backend.services.douyu_role_audit import record_role_api_candidates
|
||||
from web.backend.services.douyu_runner import DouyuBatchRunner
|
||||
from web.backend.services.douyu_runner_bind import bind_poll_interval_seconds
|
||||
from web.backend.services.douyu_runner_core import (
|
||||
DouyuBatchRunnerCore,
|
||||
slim_douyu_task_result_for_storage,
|
||||
@@ -187,6 +189,77 @@ def test_cleanup_preserves_recoverable_binding_poll(db):
|
||||
assert task.queue_lane == "binding_poll"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("elapsed", "expected"),
|
||||
[
|
||||
(0, 5),
|
||||
(119, 5),
|
||||
(120, 10),
|
||||
(599, 10),
|
||||
(600, 20),
|
||||
(1200, 20),
|
||||
],
|
||||
)
|
||||
def test_binding_poll_interval_backs_off_by_elapsed_time(elapsed, expected):
|
||||
assert bind_poll_interval_seconds(elapsed) == expected
|
||||
|
||||
|
||||
def test_role_api_audit_redacts_secrets_and_keeps_raw_role_payload(db):
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
|
||||
written = record_role_api_candidates(
|
||||
db,
|
||||
account_id=account.id,
|
||||
task_id=task.id,
|
||||
candidates=[
|
||||
{
|
||||
"act_alias": "bind-act",
|
||||
"api_version": "v1",
|
||||
"role_name": "new-role",
|
||||
"is_bound_act": True,
|
||||
"raw": {
|
||||
"cookie": "should-not-be-stored",
|
||||
"data": {
|
||||
"bindAct": 1,
|
||||
"gameRole": {"roleName": "new-role"},
|
||||
},
|
||||
},
|
||||
}
|
||||
],
|
||||
scope="elite",
|
||||
stage="test",
|
||||
source="pytest",
|
||||
)
|
||||
db.commit()
|
||||
|
||||
row = db.query(DouyuRoleApiLog).one()
|
||||
assert written == 1
|
||||
assert row.account_id == account.id
|
||||
assert row.task_id == task.id
|
||||
assert row.role_name == "new-role"
|
||||
assert row.is_bound_act is True
|
||||
assert row.response_json["cookie"] == "<redacted>"
|
||||
assert row.response_json["data"]["bindAct"] == 1
|
||||
|
||||
|
||||
def test_cleanup_gives_binding_poll_a_final_poll_grace(db):
|
||||
_, _, 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(seconds=30)
|
||||
db.commit()
|
||||
|
||||
cleaned = douyu_service.cleanup_orphan_douyu_tasks(
|
||||
db,
|
||||
active_batch_ids=set(),
|
||||
statuses=("running",),
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert cleaned == 0
|
||||
assert task.status == "running"
|
||||
|
||||
|
||||
def test_cleanup_preserves_recoverable_payment_poll(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
task.task_type = "create_gold_qr"
|
||||
@@ -454,6 +527,230 @@ def test_binding_poll_once_finishes_when_role_is_found(db, monkeypatch):
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
|
||||
|
||||
def test_binding_poll_checks_action_alias_only_on_change_or_cadence(db, monkeypatch):
|
||||
_, 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)
|
||||
legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": "old-role",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"raw": {"data": {"bindAct": 1}},
|
||||
}
|
||||
activity = {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "old-role",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"raw": {"data": {"bindAct": 1}},
|
||||
}
|
||||
task.result = {
|
||||
"url": "https://example.test/qr",
|
||||
"query_act_aliases": ["cjm", "20260120QYOOB"],
|
||||
"baseline_role_name": "old-role",
|
||||
"baseline_is_bound_act": True,
|
||||
"before_bind_info": activity,
|
||||
"bind_poll_legacy_snapshot": {
|
||||
"role_name": "old-role",
|
||||
"area_name": "",
|
||||
"plat_name": "",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": False,
|
||||
},
|
||||
"bind_polling": True,
|
||||
"qr_generated_at": datetime.now(UTC).isoformat(),
|
||||
"poll_count": 0,
|
||||
}
|
||||
db.commit()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
calls: list[list[str]] = []
|
||||
|
||||
def fake_fetch(_client, aliases):
|
||||
calls.append(list(aliases))
|
||||
return [legacy if alias == "cjm" else activity for alias in aliases]
|
||||
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: object())
|
||||
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", fake_fetch)
|
||||
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
|
||||
assert calls == [
|
||||
["cjm"],
|
||||
["cjm"],
|
||||
["cjm"],
|
||||
["20260120QYOOB"],
|
||||
]
|
||||
assert db.query(DouyuRoleApiLog).count() == 1
|
||||
assert db.query(DouyuRoleApiLog).one().stage == "binding_poll.action_alias"
|
||||
|
||||
|
||||
def test_binding_poll_finishes_when_scan_directly_binds_new_role(db, monkeypatch):
|
||||
"""扫码后官方直接返回 bindAct=1 时,也必须结束等待而不是继续轮询。"""
|
||||
_, 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)
|
||||
task.result = {
|
||||
"url": "https://example.test/qr",
|
||||
"query_act_aliases": ["bind-act"],
|
||||
"baseline_role_name": "old-role",
|
||||
"baseline_is_bound_act": True,
|
||||
"before_bind_info": {
|
||||
"act_alias": "bind-act",
|
||||
"role_name": "old-role",
|
||||
"is_bound_act": True,
|
||||
},
|
||||
"bind_polling": True,
|
||||
}
|
||||
db.commit()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
info = {
|
||||
"act_alias": "bind-act",
|
||||
"role_name": "new-role",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
}
|
||||
snapshot = {
|
||||
"role_name": "new-role",
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓",
|
||||
"game_id": "",
|
||||
"game_name": "",
|
||||
"game_channel": "",
|
||||
"change_role_wait_time": None,
|
||||
"can_change_time": None,
|
||||
"can_change_role": True,
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_account": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: type("Client", (), {})())
|
||||
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", lambda *_args: [info])
|
||||
monkeypatch.setattr(runner, "_pick_bind_info", lambda *_args, **_kwargs: info)
|
||||
monkeypatch.setattr(runner, "_bind_snapshot", lambda _info: snapshot)
|
||||
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{"bind_act_alias": "bind-act", "legacy_act_alias": "legacy-act"},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.queue_lane == ""
|
||||
assert task.result["bind_confirmed"] is True
|
||||
assert task.result["bind_phase"] == "confirmed"
|
||||
assert task.result["role_name"] == "new-role"
|
||||
assert task.result["bind_decision"]["reason"] == "directly_confirmed"
|
||||
|
||||
|
||||
def test_deadline_poll_still_checks_role_once_before_timeout(db, monkeypatch):
|
||||
"""到达截止时刻的最后一轮仍要先查角色,避免刚好扫上却被判超时。"""
|
||||
_, 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(seconds=1)
|
||||
task.result = {
|
||||
"url": "https://example.test/qr",
|
||||
"query_act_aliases": ["bind-act"],
|
||||
"baseline_role_name": "",
|
||||
"baseline_is_bound_act": False,
|
||||
"bind_polling": True,
|
||||
}
|
||||
db.commit()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
info = {"act_alias": "bind-act", "role_name": "new-role"}
|
||||
snapshot = {
|
||||
"role_name": "new-role",
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓",
|
||||
"game_id": "",
|
||||
"game_name": "",
|
||||
"game_channel": "",
|
||||
"change_role_wait_time": None,
|
||||
"can_change_time": None,
|
||||
"can_change_role": True,
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"is_bound_account": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: type("Client", (), {})())
|
||||
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", lambda *_args: [info])
|
||||
monkeypatch.setattr(runner, "_pick_bind_info", lambda *_args, **_kwargs: info)
|
||||
monkeypatch.setattr(runner, "_bind_snapshot", lambda _info: snapshot)
|
||||
monkeypatch.setattr(runner, "_is_pending_role", lambda *_args, **_kwargs: True)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_format_bind_summary",
|
||||
lambda *_args, **_kwargs: "role=new-role",
|
||||
)
|
||||
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{"bind_act_alias": "bind-act", "legacy_act_alias": "legacy-act"},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
|
||||
|
||||
def test_qr_preflight_accepts_existing_action_alias_pending_same_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
@@ -518,6 +815,75 @@ def test_qr_preflight_accepts_existing_action_alias_pending_same_role(
|
||||
assert task.result["bind_decision"]["reason"] == "existing_actionable_pending"
|
||||
|
||||
|
||||
def test_qr_preflight_accepts_same_bound_role_that_still_needs_binding(
|
||||
db, monkeypatch
|
||||
):
|
||||
"""用户重新绑定同名角色时,bindAct=1 不应阻断可确认状态。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
role = "你哥奢皇"
|
||||
legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": role,
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_account": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_act": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
"change_role_wait_time": 0,
|
||||
"can_change_time": 0,
|
||||
"can_change_role": True,
|
||||
}
|
||||
activity = {
|
||||
**legacy,
|
||||
"act_alias": "20260120QYOOB",
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
"can_change_time": 1_790_870_400,
|
||||
}
|
||||
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 *_args: [legacy, activity],
|
||||
)
|
||||
|
||||
runner._execute_get_bind_qr(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.result["url"] == "https://example.test/qr"
|
||||
assert task.result["pending_role_name"] == role
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
assert task.result["bind_decision"]["alias"] == "20260120QYOOB"
|
||||
assert (
|
||||
task.result["bind_decision"]["reason"]
|
||||
== "same_alias_after_bound_transition"
|
||||
)
|
||||
|
||||
|
||||
def test_binding_poll_accepts_existing_action_alias_pending_same_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
@@ -688,6 +1054,7 @@ def test_douyu_lanes_separate_interactive_query_payment_and_writes():
|
||||
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 10
|
||||
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 3
|
||||
assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == 5
|
||||
assert _DEFAULT_WORKERS[TaskLane.BINDING_POLL] == 6
|
||||
|
||||
|
||||
def test_slim_task_result_removes_debug_payloads():
|
||||
|
||||
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261121_0110"
|
||||
HEAD_REVISION = "20261121_0111"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
Reference in New Issue
Block a user