perf(fulfillment): 优化本地能力选号查询与索引

This commit is contained in:
yml
2026-11-23 18:53:06 +08:00
parent d5422b0ecd
commit 7e0d27927c
23 changed files with 1624 additions and 95 deletions
+202
View File
@@ -1153,6 +1153,35 @@ class TestSnapshotSync(_Base):
class TestAccountSelection(_Base):
def test_capability_candidate_query_selects_only_selection_columns(self):
query = account_selector._capability_candidate_query(
self.session,
commodity_id=COMMODITY,
required_points=1280,
now=NOW,
)
assert query is not None
selected = {
str(description["name"])
for description in query.column_descriptions
}
assert {
"account_id",
"account_username",
"account_points",
"account_elite_opened",
"state_offer_state",
"state_observed_at",
} <= selected
assert {
"account_cookie",
"account_overview_snapshot",
"account_ck_check_result",
"state_raw_json",
"state_schedule_json",
}.isdisjoint(selected)
def test_manual_allocation_request_only_marks_task_due(self):
task = self.task()
old_retry_at = task.next_allocate_at
@@ -1774,6 +1803,88 @@ class TestBindingAndCompletion(_Base):
assert task.issue_code == ""
assert task.last_error == ""
def test_standalone_account_confirmation_syncs_active_fulfillment_task(self):
task = self._allocated()
old_bind_task = DouyuTask(
batch_id="bind-old-role",
account_id=task.account_id,
account_assigned_to=self.admin_id,
task_type="get_bind_qr",
handbook_scope="elite",
status="success",
result={
"role_name": "旧角色",
"current_role_name": "旧角色",
"baseline_role_name": "旧角色",
"baseline_is_bound_act": True,
"before_bind_info": {
"act_alias": "20260120QYOOB",
"role_name": "旧角色",
"is_bound_act": True,
"need_bind_act": True,
},
"need_bind_act": True,
"is_bound_act": False,
"bind_ready_for_confirm": True,
"bind_confirmed": False,
"bind_phase": "role_ready",
},
created_by=self.admin_id,
)
self.session.add(old_bind_task)
self.session.flush()
task.bind_task_id = old_bind_task.id
task.binding_status = "role_ready"
task.execution_status = "role_ready"
account = self.session.get(Account, task.account_id)
account.role_bind_status = "bound"
account.game_name = "新角色"
account.game_channel = "微信 · 苹果(iOS)"
confirm_task = DouyuTask(
batch_id="confirm-from-elite-workbench",
account_id=task.account_id,
account_assigned_to=self.admin_id,
task_type="confirm_bind",
handbook_scope="elite",
status="success",
result={
"role_name": "新角色",
"is_bound_act": True,
"bind_confirmed": True,
"bind_phase": "confirmed",
},
created_by=self.admin_id,
)
self.session.add(confirm_task)
self.session.commit()
pre_sync_detail = service.get_task_detail(self.session, task.id)
assert pre_sync_detail["task"]["bindTask"]["roleName"] == "新角色"
assert pre_sync_detail["task"]["bindTask"]["bindReadyForConfirm"] is False
synced = service.sync_task_from_binding_runner(
self.session,
douyu_task_id=confirm_task.id,
)
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_synced_from_account",
)
.one()
)
assert synced == 1
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
assert event.detail_json["roleName"] == "新角色"
def test_customer_start_mapping_missing_is_idempotent(self, monkeypatch):
task = self.task(mapped=False)
task.issue_code = "mapping_missing"
@@ -3009,6 +3120,97 @@ class TestBindingAndCompletion(_Base):
assert detail["task"]["exchangeStatus"] == "not_opened"
assert detail["task"]["issueCode"] == "elite_not_opened"
def test_account_change_auto_refreshes_active_exchange_projection(
self,
monkeypatch,
):
task = self._allocated()
account = self.session.get(Account, task.account_id)
task.binding_status = "bound"
task.execution_status = "bound"
task.exchange_status = "points_short"
task.issue_code = "points_short"
account.points = 2000
account.elite_opened = True
self.session.commit()
monkeypatch.setattr(
service.lifecycle,
"probe_account",
lambda *_args, **_kwargs: {
"probe_id": "auto-eligible",
"session": {"status": "valid", "message": "有效"},
"elite": {
"status": "ok",
"raw": {"manual_type": 1},
"manual_type": 1,
"manual_score": 2000,
"opened": True,
},
"eligibility": {
"points": 2000,
"points_response": {"points": 2000},
"limited_ids": [],
"limited_goods": [],
"storehome_goods": [],
},
"roles": {},
"errors": {},
},
)
detail = service.refresh_active_exchange_eligibility_for_account(
self.session,
account_id=int(account.id),
scope="elite",
actor_id=int(self.admin.id),
)
assert detail is not None
assert detail["task"]["executionStatus"] == "bound"
assert detail["task"]["exchangeStatus"] == "eligible"
assert detail["task"]["issueCode"] == ""
def test_auto_exchange_refresh_failure_keeps_bound_task_unchanged(
self,
monkeypatch,
):
task = self._allocated()
account = self.session.get(Account, task.account_id)
task.binding_status = "bound"
task.execution_status = "bound"
task.exchange_status = "points_short"
task.issue_code = "points_short"
self.session.commit()
monkeypatch.setattr(
service.lifecycle,
"probe_account",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
RuntimeError("暂时查询失败")
),
)
detail = service.refresh_active_exchange_eligibility_for_account(
self.session,
account_id=int(account.id),
scope="elite",
actor_id=int(self.admin.id),
)
self.session.refresh(task)
event = (
self.session.query(PlatformFulfillmentEvent)
.filter_by(
task_id=task.id,
event_type="eligibility_auto_refresh_failed",
)
.one()
)
assert detail is not None
assert task.execution_status == "bound"
assert task.exchange_status == "points_short"
assert task.issue_code == "points_short"
assert "暂时查询失败" in event.detail_json["error"]
def test_expired_lease_moves_unfinished_task_to_manual(self):
task = self._allocated()
lease = self.session.query(AccountLease).one()