feat(douyu): 完成账号平台阶段一到四并接入最新协议
This commit is contained in:
@@ -13,7 +13,15 @@ from unittest.mock import Mock
|
||||
import pytest
|
||||
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import Account, AccountEvent, DouyuTask, User
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountSyncState,
|
||||
AccountWorkItem,
|
||||
DouyuTask,
|
||||
User,
|
||||
)
|
||||
from web.backend.services.douyu_runner_overview import (
|
||||
DEFAULT_HIGH_VALUE_GOODS,
|
||||
OverviewMixin,
|
||||
@@ -423,6 +431,176 @@ class TestOverviewAggregation:
|
||||
self.session.refresh(self.account)
|
||||
assert self.account.status == status_before
|
||||
|
||||
def test_reuses_fresh_overview_without_calling_official_helpers(
|
||||
self,
|
||||
monkeypatch,
|
||||
):
|
||||
monkeypatch.setenv("DOUYU_OVERVIEW_CACHE_SECONDS", "600")
|
||||
self.account.overview_snapshot = {
|
||||
"queried_at": datetime.now(UTC).isoformat(),
|
||||
"assets": {"gold_balance": 7},
|
||||
"high_value_goods": [
|
||||
{"name": "套装-兔兔白日梦", "owned": True, "missing": False}
|
||||
],
|
||||
}
|
||||
self.account.overview_queried_at = datetime.now(UTC)
|
||||
self.session.commit()
|
||||
probe = _Harness(_FakeClient())
|
||||
assert probe._cached_overview(
|
||||
self.account,
|
||||
self.task,
|
||||
now=datetime.now(UTC),
|
||||
) is not None
|
||||
|
||||
h = self._run(client=object())
|
||||
result = h.marked["result"]
|
||||
|
||||
assert h.marked["status"] == "success"
|
||||
assert result["cache_hit"] is True
|
||||
assert result["assets"]["gold_balance"] == 7
|
||||
assert "兔兔白日梦" in h.marked["message"]
|
||||
|
||||
def test_stale_storehome_projection_is_queued_without_official_query(self):
|
||||
now = datetime.now(UTC)
|
||||
self.account.overview_queried_at = now - timedelta(hours=1)
|
||||
self.account.overview_snapshot = {
|
||||
"queried_at": self.account.overview_queried_at.isoformat(),
|
||||
"high_value_goods": [],
|
||||
}
|
||||
self.session.add_all(
|
||||
[
|
||||
AccountSyncState(
|
||||
account_id=self.account.id,
|
||||
platform="douyu",
|
||||
domain="storehome",
|
||||
status="success",
|
||||
last_success_at=now - timedelta(hours=1),
|
||||
valid_until=now - timedelta(minutes=1),
|
||||
),
|
||||
AccountCommodityState(
|
||||
account_id=self.account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=RABBIT_ID,
|
||||
commodity_name="套装-兔兔白日梦",
|
||||
availability="available",
|
||||
observed_at=now - timedelta(hours=1),
|
||||
valid_until=now - timedelta(minutes=1),
|
||||
raw_json=GOODS_AVAILABLE,
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
seen: list[str] = []
|
||||
|
||||
class _Client(_FakeClient):
|
||||
def list_goods(self, *, manual_id, rid):
|
||||
seen.append(manual_id)
|
||||
return super().list_goods(manual_id=manual_id, rid=rid)
|
||||
|
||||
result = self._run(client=_Client()).marked["result"]
|
||||
work = self.session.query(AccountWorkItem).one()
|
||||
|
||||
assert seen == []
|
||||
assert result["pending_domains"] == ["storehome"]
|
||||
assert result["goods_source"] == "local_stale_projection"
|
||||
assert result["limited_goods_total"] == 1
|
||||
assert work.work_type == "storehome_sync"
|
||||
assert work.account_id == self.account.id
|
||||
|
||||
def test_fresh_storehome_projection_skips_official_query(self):
|
||||
now = datetime.now(UTC)
|
||||
self.session.add_all(
|
||||
[
|
||||
AccountSyncState(
|
||||
account_id=self.account.id,
|
||||
platform="douyu",
|
||||
domain="storehome",
|
||||
status="success",
|
||||
last_success_at=now,
|
||||
valid_until=now + timedelta(minutes=10),
|
||||
),
|
||||
AccountCommodityState(
|
||||
account_id=self.account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=RABBIT_ID,
|
||||
commodity_name="套装-兔兔白日梦",
|
||||
availability="available",
|
||||
observed_at=now,
|
||||
valid_until=now + timedelta(minutes=10),
|
||||
raw_json=GOODS_AVAILABLE,
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
seen: list[str] = []
|
||||
|
||||
class _Client(_FakeClient):
|
||||
def list_goods(self, *, manual_id, rid):
|
||||
seen.append(manual_id)
|
||||
return super().list_goods(manual_id=manual_id, rid=rid)
|
||||
|
||||
result = self._run(client=_Client()).marked["result"]
|
||||
|
||||
assert seen == []
|
||||
assert result["goods_source"] == "local_fresh_projection"
|
||||
assert result["pending_domains"] == []
|
||||
assert result["limited_goods_total"] == 1
|
||||
assert self.session.query(AccountWorkItem).count() == 0
|
||||
|
||||
def test_stale_esports_and_exchange_records_use_local_projection_and_queue(self):
|
||||
now = datetime.now(UTC)
|
||||
self.account.esports_opened = True
|
||||
self.account.esports_points = 88
|
||||
self.account.esports_expire_time = 1795967999
|
||||
self.session.add_all(
|
||||
[
|
||||
AccountSyncState(
|
||||
account_id=self.account.id,
|
||||
platform="douyu",
|
||||
domain="esports",
|
||||
status="success",
|
||||
last_success_at=now - timedelta(hours=1),
|
||||
valid_until=now - timedelta(minutes=1),
|
||||
raw_payload_json={"manual_type": 2, "manual_score": 88},
|
||||
),
|
||||
AccountSyncState(
|
||||
account_id=self.account.id,
|
||||
platform="douyu",
|
||||
domain="exchange_records",
|
||||
status="success",
|
||||
last_success_at=now - timedelta(hours=1),
|
||||
valid_until=now - timedelta(minutes=1),
|
||||
raw_payload_json={
|
||||
"elite": [
|
||||
{
|
||||
"commodityId": "cached-item",
|
||||
"commodityName": "缓存套装",
|
||||
}
|
||||
]
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
result = self._run().marked["result"]
|
||||
work_types = {
|
||||
row.work_type
|
||||
for row in self.session.query(AccountWorkItem)
|
||||
.filter_by(account_id=self.account.id)
|
||||
.all()
|
||||
}
|
||||
|
||||
assert result["domain_sources"]["esports"] == "queued_stale"
|
||||
assert result["domain_sources"]["exchange_records"] == "queued_stale"
|
||||
assert result["esports"]["opened"] is True
|
||||
assert result["esports"]["points"] == 88
|
||||
assert result["exchange_records"]["elite"][0]["commodityId"] == "cached-item"
|
||||
assert {"esports", "exchange_records"}.issubset(result["pending_domains"])
|
||||
assert work_types == {"esports_sync", "exchange_records_sync"}
|
||||
|
||||
|
||||
class TestOverviewTaskTypeWiring:
|
||||
def test_declared_for_all_workbenches(self):
|
||||
|
||||
Reference in New Issue
Block a user