fix(fulfillment): reconcile stale pending roles from newer observations

This commit is contained in:
yml
2026-11-23 22:57:49 +08:00
parent e6629a9c91
commit 23c4b93e9c
9 changed files with 586 additions and 14 deletions
+152
View File
@@ -2076,6 +2076,158 @@ class TestBindingAndCompletion(_Base):
assert event.detail_json["previousBindTaskId"] == old_bind_task.id
assert event.detail_json["bindTaskId"] == latest_bind_task.id
def test_newer_observation_invalidates_different_pending_role(self):
task = self._allocated()
account = self.session.get(Account, task.account_id)
account.game_name = "续梦乖"
account.game_channel = "手Q · 安卓(android)"
account.role_bind_status = "bound"
pending_task = DouyuTask(
batch_id="pending-old-role",
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={
"role_name": "布丁贩卖机",
"area_name": "微信",
"plat_name": "苹果(iOS)",
"baseline_role_name": "续梦乖",
"before_bind_info": {
"act_alias": "20260120QYOOB",
"role_name": "续梦乖",
"is_bound_act": True,
"need_bind_act": True,
},
"act_alias": "20260120QYOOB",
"need_bind_act": True,
"bind_ready_for_confirm": True,
"bind_confirmed": False,
"bind_phase": "role_ready",
},
created_by=self.admin_id,
)
self.session.add(pending_task)
self.session.flush()
task.bind_task_id = pending_task.id
task.binding_status = "role_ready"
task.execution_status = "role_ready"
observation = DouyuTask(
batch_id="observe-new-role",
account_id=task.account_id,
account_assigned_to=self.admin_id,
task_type="query_game_name",
handbook_scope="elite",
status="success",
platform_task_id=task.id,
result={
"role_name": "续梦乖",
"area_name": "手Q",
"plat_name": "安卓(android)",
"is_bound_act": True,
"bind_confirmed": True,
"bind_phase": "confirmed",
},
created_by=self.admin_id,
)
self.session.add(observation)
self.session.commit()
synced = service.reconcile_binding_observations(self.session)
self.session.refresh(task)
detail = service.get_task_detail(self.session, task.id)
event = (
self.session.query(PlatformFulfillmentEvent)
.filter_by(
task_id=task.id,
event_type="binding_pending_observation_superseded",
)
.one()
)
assert synced == 1
assert task.bind_task_id == observation.id
assert task.binding_status == "waiting_bind"
assert task.execution_status == "waiting_bind"
assert detail["task"]["bindTask"]["roleName"] == "续梦乖"
assert detail["task"]["bindTask"]["bindReadyForConfirm"] is False
assert detail["task"]["bindTask"]["bindConfirmed"] is False
assert event.detail_json["pendingRoleName"] == "布丁贩卖机"
assert event.detail_json["observedRoleName"] == "续梦乖"
def test_newer_observation_confirms_matching_pending_role(self):
task = self._allocated()
account = self.session.get(Account, task.account_id)
account.game_name = "续梦乖"
account.role_bind_status = "bound"
pending_task = DouyuTask(
batch_id="pending-matching-role",
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={
"role_name": "续梦乖",
"baseline_role_name": "旧角色",
"before_bind_info": {
"act_alias": "20260120QYOOB",
"role_name": "旧角色",
"is_bound_act": True,
"need_bind_act": True,
},
"act_alias": "20260120QYOOB",
"need_bind_act": True,
"bind_ready_for_confirm": True,
"bind_confirmed": False,
"bind_phase": "role_ready",
},
created_by=self.admin_id,
)
self.session.add(pending_task)
self.session.flush()
task.bind_task_id = pending_task.id
task.binding_status = "role_ready"
task.execution_status = "role_ready"
observation = DouyuTask(
batch_id="observe-matching-role",
account_id=task.account_id,
account_assigned_to=self.admin_id,
task_type="query_game_name",
handbook_scope="elite",
status="success",
platform_task_id=task.id,
result={
"role_name": "续梦乖",
"area_name": "手Q",
"plat_name": "安卓(android)",
"is_bound_act": True,
"bind_confirmed": True,
"bind_phase": "confirmed",
},
created_by=self.admin_id,
)
self.session.add(observation)
self.session.commit()
synced = service.sync_task_from_binding_runner(
self.session,
douyu_task_id=observation.id,
)
self.session.refresh(task)
detail = service.get_task_detail(self.session, task.id)
assert synced == 1
assert task.bind_task_id == observation.id
assert task.binding_status == "bound"
assert task.execution_status == "bound"
assert detail["task"]["bindTask"]["roleName"] == "续梦乖"
assert detail["task"]["bindTask"]["bindReadyForConfirm"] is False
assert detail["task"]["bindTask"]["bindConfirmed"] is True
def test_refresh_binding_attaches_latest_pending_account_workbench_role(self):
task = self._allocated()
old_bind_task = DouyuTask(