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
+16
View File
@@ -411,6 +411,8 @@ class TestOverviewAggregation:
assert seen == ["ELITE-M"], "商城商品必须用精英宝典的 manualID 拉"
assert "esports" in r["errors"] # 电竞那一项照常报未配置
assert r["limited_goods_total"] == 2
assert r["limited_count"] == 1
assert len(r["limited_goods"]) == 1
# 高价值三态照样落库(官方 status 说了算)
self.session.commit()
self.session.refresh(self.account)
@@ -547,6 +549,8 @@ class TestOverviewAggregation:
assert result["goods_source"] == "local_fresh_projection"
assert result["pending_domains"] == []
assert result["limited_goods_total"] == 1
assert result["limited_count"] == 0
assert result["limited_goods"] == []
assert self.session.query(AccountWorkItem).count() == 0
def test_stale_esports_and_exchange_records_use_local_projection_and_queue(self):
@@ -613,6 +617,18 @@ class TestOverviewTaskTypeWiring:
for scope in ("elite", "esports", "peace"):
assert "query_account_overview" in DOUYU_HANDBOOK_TASK_TYPES[scope]
def test_obsolete_goods_tasks_are_not_supported(self):
from web.backend.services.douyu_service import (
DOUYU_HANDBOOK_TASK_TYPES,
SUPPORTED_DOUYU_TASK_TYPES,
)
assert "refresh_goods" not in SUPPORTED_DOUYU_TASK_TYPES
assert "query_limited_goods" not in SUPPORTED_DOUYU_TASK_TYPES
for scope in ("elite", "esports", "peace"):
assert "refresh_goods" not in DOUYU_HANDBOOK_TASK_TYPES[scope]
assert "query_limited_goods" not in DOUYU_HANDBOOK_TASK_TYPES[scope]
def test_handler_is_dispatched(self):
from web.backend.services.douyu_runner import DouyuBatchRunner
+1 -1
View File
@@ -133,7 +133,7 @@ def _seed_tasks(db):
batch_id="batch-main",
account_id=support_account.id,
account_assigned_to=support.id,
task_type="query_limited_goods",
task_type="query_gold_balance",
handbook_scope="legacy",
status="success",
created_by=support.id,
+4 -1
View File
@@ -244,5 +244,8 @@ class TestClassification(_Base):
"""噪声名单里的类型必须都是真实存在的任务类型(防拼写错误导致永不生效)。"""
from web.backend.services.douyu_service import SUPPORTED_DOUYU_TASK_TYPES
unknown = set(retention.NOISE_TASK_TYPES) - set(SUPPORTED_DOUYU_TASK_TYPES)
known = set(SUPPORTED_DOUYU_TASK_TYPES) | set(
retention.RETIRED_NOISE_TASK_TYPES
)
unknown = set(retention.NOISE_TASK_TYPES) - known
assert not unknown, f"噪声名单里有不存在的任务类型:{unknown}"
+25
View File
@@ -0,0 +1,25 @@
"""阶段 5:所有登录入口必须继续经过统一预算执行器。"""
from __future__ import annotations
import inspect
from pathlib import Path
from web.backend.services import login_service
ROOT = Path(__file__).resolve().parents[1]
def test_login_runner_reserves_quota_before_attempt() -> None:
source = inspect.getsource(login_service.LoginBatchRunner.run)
assert "reserve_login_attempt(" in source
def test_known_login_entrypoints_construct_login_runner() -> None:
for relative in (
"web/backend/routers/login.py",
"web/backend/routers/cookies.py",
"web/backend/services/automation/jobs/ck_relogin.py",
):
source = (ROOT / relative).read_text(encoding="utf-8")
assert "LoginBatchRunner(" in source, relative
+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(