refactor(douyu): 完成阶段5旧链路清理

This commit is contained in:
yml
2026-11-22 21:40:53 +08:00
parent 94de1d167b
commit 5393b668e0
20 changed files with 155 additions and 559 deletions
+8 -60
View File
@@ -5,7 +5,6 @@ from __future__ import annotations
import asyncio
import threading
from datetime import UTC, datetime, timedelta
from unittest.mock import patch
import pytest
from fastapi import HTTPException
@@ -36,7 +35,6 @@ from web.backend.services.account_sync_runner import WORK_STOREHOME_SYNC
from web.backend.services.automation.jobs import (
platform_fulfillment_lease_reaper as reaper,
)
from web.backend.services.douyu_account_state import DouyuLiveAccountState
from web.backend.services.platform_fulfillment import account_selector, service
NOW = datetime.now(UTC).replace(microsecond=0)
@@ -64,31 +62,9 @@ class _Base:
)
)
self.session.commit()
self.refresh_patch = patch.object(
service,
"refresh_douyu_account_state",
side_effect=self._live_state,
)
self.refresh_mock = self.refresh_patch.start()
def teardown_method(self):
self.refresh_patch.stop()
self.session.close()
@staticmethod
def _live_state(_db, account, **_kwargs):
return DouyuLiveAccountState(
account_id=account.id,
account_name=account.username,
checked_at=NOW,
ck_verdict="valid",
ck_message="有效",
elite_opened=True,
points=account.points or 2000,
limited_ids=frozenset(),
role_change_available_at=account.role_change_available_at,
)
def account(
self,
name: str,
@@ -1084,9 +1060,8 @@ class TestAccountSelection(_Base):
assert detail["task"]["account"] is None
assert task.next_allocate_at is not None
assert old_retry_at is None or task.next_allocate_at >= old_retry_at
self.refresh_mock.assert_not_called()
def test_capability_match_uses_local_index_without_live_probe(self, monkeypatch):
def test_capability_match_uses_local_index_without_live_probe(self):
account = self.account("capability-fast", capability=False)
task = self.task()
self.session.add(
@@ -1106,8 +1081,6 @@ class TestAccountSelection(_Base):
)
)
self.session.commit()
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "capability")
detail = service.allocate_task(
self.session,
task_id=task.id,
@@ -1116,20 +1089,14 @@ class TestAccountSelection(_Base):
assert detail["task"]["account"]["id"] == account.id
assert detail["task"]["executionStatus"] == "allocated"
self.refresh_mock.assert_not_called()
def test_allocation_reports_role_cooldown_without_fast_retry_loop(
self,
monkeypatch,
):
def test_allocation_reports_role_cooldown_without_fast_retry_loop(self):
self.account(
"capability-role-cooldown",
points=2000,
role_available_at=NOW + timedelta(days=2),
)
task = self.task()
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "capability")
detail = service.allocate_task(
self.session,
task_id=task.id,
@@ -1151,13 +1118,9 @@ class TestAccountSelection(_Base):
assert event.detail_json["availabilityCounts"]["account:eligible"] == 1
assert event.detail_json["availabilityCounts"]["account:roleCooldown"] == 1
def test_hybrid_without_capability_does_not_fall_back_to_live_scan(
self,
monkeypatch,
):
def test_missing_capability_does_not_fall_back_to_live_scan(self):
self.account("live-fallback", capability=False)
task = self.task()
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "hybrid")
detail = service.allocate_task(
self.session,
@@ -1168,28 +1131,13 @@ class TestAccountSelection(_Base):
assert detail["task"]["account"] is None
assert detail["task"]["executionStatus"] == "ready"
assert detail["task"]["issueCode"] == "capability_cache_miss"
self.refresh_mock.assert_not_called()
work_item = self.session.query(AccountWorkItem).one()
assert work_item.work_type == WORK_STOREHOME_SYNC
assert work_item.priority == 20
def test_legacy_mode_keeps_live_scan_fallback(self, monkeypatch):
def test_legacy_mode_env_cannot_enable_live_scan(self, monkeypatch):
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "legacy")
account = Account(
username="live-refresh-account",
password="p",
email="live@example.com",
email_password="ep",
status="imported",
ck_check_status="",
cookie="PHPSESSID=live",
elite_opened=None,
points=None,
gold_balance=100,
auto_managed=True,
)
self.session.add(account)
self.session.commit()
self.account("forced-local-selection", capability=False)
task = self.task()
detail = service.allocate_task(
@@ -1198,9 +1146,9 @@ class TestAccountSelection(_Base):
current=self.admin,
)
assert detail["task"]["account"]["id"] == account.id
assert detail["task"]["executionStatus"] == "allocated"
self.refresh_mock.assert_called()
assert detail["task"]["account"] is None
assert detail["task"]["executionStatus"] == "ready"
assert detail["task"]["issueCode"] == "capability_cache_miss"
def test_live_candidates_include_support_accounts_and_prefer_valid_cache(self):
support = User(