修复(履约绑定): 修复候选快照丢失与全链路绑定校验一致性
This commit is contained in:
@@ -1278,3 +1278,67 @@ def test_livelink_mini_qrcode_uses_har_query_and_headers(monkeypatch):
|
||||
assert calls[0]["headers"]["Gap-Act-Id"] == "16046"
|
||||
assert calls[0]["headers"]["Gap-Plat-Id"] == "douyu"
|
||||
assert calls[0]["headers"]["Origin"] == "https://livelink.qq.com"
|
||||
|
||||
|
||||
def test_pending_binding_role_hydrates_stripped_action_candidate_without_baseline():
|
||||
"""历史任务 bind_candidates 缺少 need_bind_act 时,应从 bind_info / decisions 补全。"""
|
||||
result = {
|
||||
"act_alias": ACT_ALIAS,
|
||||
"action_act_aliases": [ACT_ALIAS],
|
||||
"baseline_role_name": "",
|
||||
"baseline_is_bound_act": False,
|
||||
"before_bind_info": {},
|
||||
"expected_role_name": "欧美真心",
|
||||
"expected_role_required": True,
|
||||
"role_name": "欧美真心",
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_info": {
|
||||
"act_alias": ACT_ALIAS,
|
||||
"role_name": "欧美真心",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
},
|
||||
"bind_candidates": [
|
||||
{
|
||||
"act_alias": LEGACY_ALIAS,
|
||||
"role_name": "欧美真心",
|
||||
"is_bound_act": False,
|
||||
},
|
||||
{
|
||||
"act_alias": ACT_ALIAS,
|
||||
"role_name": "欧美真心",
|
||||
"is_bound_act": False,
|
||||
},
|
||||
],
|
||||
"bind_candidate_decisions": [
|
||||
{
|
||||
"alias": ACT_ALIAS,
|
||||
"role_name": "欧美真心",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
"actionable_alias": True,
|
||||
"confirmable": True,
|
||||
"reason": "existing_actionable_pending",
|
||||
},
|
||||
{
|
||||
"alias": LEGACY_ALIAS,
|
||||
"role_name": "欧美真心",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
"actionable_alias": False,
|
||||
"confirmable": False,
|
||||
"reason": "pending_without_baseline",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
assert pending_binding_role_from_result(result) == "欧美真心"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -5491,3 +5491,254 @@ class TestBindingAndCompletion(_Base):
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
def test_empty_baseline_stripped_bind_candidates_reconciles_to_role_ready(self):
|
||||
"""历史无 baseline 且 bind_candidates 被裁剪的扫码任务,刷新/对账后应收敛为待确认。"""
|
||||
task = self._allocated()
|
||||
task.customer_role_name = "欧美真心"
|
||||
account = self.session.get(Account, task.account_id)
|
||||
account.game_name = "曲奇小诺゛"
|
||||
account.role_bind_status = "bound"
|
||||
qr_task = DouyuTask(
|
||||
batch_id="stripped-pending-qr",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
message="已识别角色: 欧美真心,待确认绑定",
|
||||
platform_task_id=task.id,
|
||||
result={
|
||||
"act_alias": "20260120QYOOB",
|
||||
"action_act_aliases": ["20260120QYOOB"],
|
||||
"baseline_role_name": "",
|
||||
"baseline_is_bound_act": False,
|
||||
"before_bind_info": {},
|
||||
"expected_role_name": "欧美真心",
|
||||
"expected_role_required": True,
|
||||
"role_name": "欧美真心",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
"url": "https://livelink.example/qr",
|
||||
"bind_info": {
|
||||
"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,
|
||||
},
|
||||
"bind_decision": {
|
||||
"alias": "20260120QYOOB",
|
||||
"role_name": "欧美真心",
|
||||
"actionable_alias": True,
|
||||
"confirmable": True,
|
||||
"reason": "existing_actionable_pending",
|
||||
},
|
||||
"bind_candidates": [
|
||||
{
|
||||
"act_alias": "cjm",
|
||||
"role_name": "欧美真心",
|
||||
"is_bound_act": False,
|
||||
},
|
||||
{
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "欧美真心",
|
||||
"is_bound_act": False,
|
||||
},
|
||||
],
|
||||
"bind_candidate_decisions": [
|
||||
{
|
||||
"alias": "20260120QYOOB",
|
||||
"role_name": "欧美真心",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
"actionable_alias": True,
|
||||
"confirmable": True,
|
||||
"reason": "existing_actionable_pending",
|
||||
}
|
||||
],
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(qr_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = qr_task.id
|
||||
task.binding_status = "qr_pending"
|
||||
task.execution_status = "waiting_bind"
|
||||
self.session.commit()
|
||||
|
||||
reconciled = service.reconcile_binding_observations(self.session)
|
||||
self.session.commit()
|
||||
detail = service.get_task_detail(self.session, task.id)
|
||||
item = detail["task"]
|
||||
|
||||
assert reconciled >= 1
|
||||
assert item["bindingStatus"] == "role_ready"
|
||||
assert item["executionStatus"] == "role_ready"
|
||||
assert item["bindTask"]["roleName"] == "欧美真心"
|
||||
assert item["bindTask"]["bindReadyForConfirm"] is True
|
||||
assert item["bindingEvidence"]["scannedRole"] == "欧美真心"
|
||||
assert item["bindingEvidence"]["baselineRole"] == ""
|
||||
assert item["bindingEvidence"]["confirmable"] is True
|
||||
assert item["bindingEvidence"]["actionPending"]["needBindAct"] is True
|
||||
assert item["bindingEvidence"]["actionPending"]["channel"] == "手Q / 苹果(iOS)"
|
||||
|
||||
def test_current_confirm_bind_with_mismatched_customer_role_is_rejected(self):
|
||||
"""即使 bind_task_id 已指向 confirm_bind,确认出非客户目标角色也必须拒绝。"""
|
||||
task = self._allocated()
|
||||
task.customer_role_name = "客户指定角色"
|
||||
confirm_task = DouyuTask(
|
||||
batch_id="current-mismatched-confirm",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="confirm_bind",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
platform_task_id=task.id,
|
||||
result={
|
||||
"act_alias": "20260120QYOOB",
|
||||
"action_act_aliases": ["20260120QYOOB"],
|
||||
"role_name": "账号旧角色",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"bind_confirmed": True,
|
||||
"bind_phase": "confirmed",
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(confirm_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = confirm_task.id
|
||||
task.binding_status = "confirming"
|
||||
task.execution_status = "binding_confirming"
|
||||
self.session.commit()
|
||||
|
||||
synced = service.sync_task_from_binding_runner(
|
||||
self.session,
|
||||
douyu_task_id=confirm_task.id,
|
||||
)
|
||||
|
||||
self.session.refresh(task)
|
||||
rejection = (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(task_id=task.id, event_type="binding_sync_rejected")
|
||||
.one()
|
||||
)
|
||||
assert synced >= 0
|
||||
assert task.binding_status != "bound"
|
||||
assert rejection.detail_json["reason"] == "target_mismatch"
|
||||
|
||||
def test_save_customer_profile_immediately_reconciles_existing_qr_candidates(self):
|
||||
"""先出码扫码、后提交客户资料时,保存资料应立即把订单收敛到 role_ready。"""
|
||||
task = self._allocated()
|
||||
task.customer_role_name = ""
|
||||
qr_task = DouyuTask(
|
||||
batch_id="profile-late-qr",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
platform_task_id=task.id,
|
||||
result={
|
||||
"act_alias": "20260120QYOOB",
|
||||
"action_act_aliases": ["20260120QYOOB"],
|
||||
"expected_role_name": "",
|
||||
"expected_role_required": True,
|
||||
"role_name": "后补角色",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
"url": "https://livelink.example/qr",
|
||||
"bind_candidates": [
|
||||
{
|
||||
"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,
|
||||
}
|
||||
],
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(qr_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = qr_task.id
|
||||
task.binding_status = "qr_pending"
|
||||
task.execution_status = "waiting_bind"
|
||||
self.session.commit()
|
||||
|
||||
out = service.save_customer_profile(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
customer_role_name="后补角色",
|
||||
platform="qq",
|
||||
system="ios",
|
||||
home_images=[{"url": "https://img.example/1.png"}],
|
||||
)
|
||||
|
||||
self.session.refresh(task)
|
||||
assert task.binding_status == "role_ready"
|
||||
assert task.execution_status == "role_ready"
|
||||
assert out["bindingStatus"] == "role_ready"
|
||||
assert out["roleName"] == "后补角色"
|
||||
assert out["bindReadyForConfirm"] is True
|
||||
|
||||
def test_unlinked_workbench_qr_with_wrong_role_is_not_attached(self):
|
||||
"""工作台无归属扫码任务若角色与客户目标不一致,绝不能挂到订单上。"""
|
||||
task = self._allocated()
|
||||
task.customer_role_name = "客户目标角色"
|
||||
task.binding_status = "qr_pending"
|
||||
task.execution_status = "waiting_bind"
|
||||
workbench_qr = DouyuTask(
|
||||
batch_id="workbench-wrong-role",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
result={
|
||||
"act_alias": "20260120QYOOB",
|
||||
"action_act_aliases": ["20260120QYOOB"],
|
||||
"role_name": "工作台别的号角色",
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
"bind_candidates": [
|
||||
{
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "工作台别的号角色",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
],
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(workbench_qr)
|
||||
self.session.commit()
|
||||
|
||||
reconciled = service.reconcile_binding_observations(self.session)
|
||||
|
||||
self.session.refresh(task)
|
||||
assert reconciled == 0
|
||||
assert task.binding_status == "qr_pending"
|
||||
assert task.bind_task_id is None
|
||||
|
||||
Reference in New Issue
Block a user