feat(douyu): 落地账号平台同步与登录配额
This commit is contained in:
@@ -264,26 +264,9 @@ class TestCkInvalidOnlyOnTransition(_Base):
|
||||
self._run()
|
||||
assert len(self.events(account.id, svc.EVENT_CK_INVALID)) == 1
|
||||
|
||||
# 恢复(巡检判 valid)
|
||||
from web.backend.services.automation.jobs import ck_health
|
||||
|
||||
original = ck_health.check_douyu_cookie
|
||||
ck_health.check_douyu_cookie = lambda _c: {
|
||||
"valid": True,
|
||||
"message": "ok",
|
||||
"fish_ball": 1,
|
||||
"nickname": "n",
|
||||
"level": 1,
|
||||
"checked_at": NOW,
|
||||
}
|
||||
self.session.query(Account).update(
|
||||
{Account.ck_checked_at: None}, synchronize_session=False
|
||||
)
|
||||
# invalid 不再进入普通 CK 巡检;恢复由重登成功后的 CK 验证写回。
|
||||
on_ck_verdict(self.session, account, "valid", detail={}, occurred_at=NOW)
|
||||
self.session.commit()
|
||||
try:
|
||||
ck_health.run_ck_health(self.session, self._job())
|
||||
finally:
|
||||
ck_health.check_douyu_cookie = original
|
||||
|
||||
# 再失效
|
||||
self._run()
|
||||
|
||||
@@ -117,8 +117,11 @@ def test_sync_stores_time_window_and_next_boundary(db):
|
||||
assert state.availability == "limited"
|
||||
assert state.time_limited is True
|
||||
assert state.available_until == end_at
|
||||
assert state.valid_until == end_at
|
||||
assert state.next_check_at == end_at
|
||||
assert state.schedule_json
|
||||
assert state.raw_json["timeLimited"] == 1
|
||||
assert state.raw_json["specTimes"][0]["endTime"] == str(int(end_at.timestamp()))
|
||||
|
||||
|
||||
def test_sync_account_goods_states_writes_capabilities_and_exchange_stats(db):
|
||||
|
||||
@@ -0,0 +1,380 @@
|
||||
"""账号平台基础闭环:全量快照、工作队列、CK 周期和登录预算。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountSyncState,
|
||||
AccountWorkItem,
|
||||
LoginQuotaSetting,
|
||||
)
|
||||
from web.backend.services import account_sync_service
|
||||
from web.backend.services.account_lifecycle import on_ck_verdict
|
||||
from web.backend.services.account_sync_service import (
|
||||
DOMAIN_STOREHOME,
|
||||
sync_storehome_snapshot,
|
||||
)
|
||||
from web.backend.services.account_work_queue import (
|
||||
complete_account_work,
|
||||
enqueue_account_work,
|
||||
)
|
||||
from web.backend.services.automation.jobs import ck_health
|
||||
from web.backend.services.douyu_account_state import DouyuLiveAccountState
|
||||
from web.backend.services.douyu_read_gateway import StorehomeSnapshot
|
||||
from web.backend.services.login_quota_service import (
|
||||
CATEGORY_MANUAL,
|
||||
finish_login_attempt,
|
||||
reserve_login_attempt,
|
||||
)
|
||||
from web.backend.services.platform_fulfillment.service import _exchange_gate
|
||||
|
||||
|
||||
def _account(name: str, **overrides) -> Account:
|
||||
values = {
|
||||
"username": name,
|
||||
"password": "p",
|
||||
"email": f"{name}@example.com",
|
||||
"email_password": "ep",
|
||||
"cookie": "acf_uid=100",
|
||||
"cookie_updated_at": datetime.now(UTC),
|
||||
"status": "active",
|
||||
"auto_managed": True,
|
||||
}
|
||||
values.update(overrides)
|
||||
return Account(**values)
|
||||
|
||||
|
||||
def _snapshot(count: int = 60) -> StorehomeSnapshot:
|
||||
items = tuple(
|
||||
{
|
||||
"commodityId": f"goods-{index}",
|
||||
"commodityName": f"商品 {index}",
|
||||
"score": index * 10,
|
||||
"status": 1 if index == 0 else 0,
|
||||
}
|
||||
for index in range(count)
|
||||
)
|
||||
return StorehomeSnapshot(
|
||||
manual_id="manual",
|
||||
rid="room",
|
||||
observed_at=datetime.now(UTC),
|
||||
items=items,
|
||||
limited_ids=frozenset({"goods-0"}),
|
||||
raw={"data": {"list": list(items)}},
|
||||
)
|
||||
|
||||
|
||||
def test_storehome_snapshot_writes_all_goods_and_sync_state(db):
|
||||
account = _account("snapshot-account")
|
||||
db.add(account)
|
||||
db.commit()
|
||||
|
||||
result = sync_storehome_snapshot(
|
||||
db,
|
||||
account,
|
||||
_snapshot(),
|
||||
source="test.storehome",
|
||||
)
|
||||
db.commit()
|
||||
|
||||
states = (
|
||||
db.query(AccountCommodityState)
|
||||
.filter(
|
||||
AccountCommodityState.account_id == account.id,
|
||||
AccountCommodityState.scope == "elite",
|
||||
)
|
||||
.all()
|
||||
)
|
||||
sync_state = (
|
||||
db.query(AccountSyncState)
|
||||
.filter(
|
||||
AccountSyncState.account_id == account.id,
|
||||
AccountSyncState.domain == DOMAIN_STOREHOME,
|
||||
)
|
||||
.one()
|
||||
)
|
||||
assert result.item_count == 60
|
||||
assert len(states) == 60
|
||||
assert {row.commodity_id for row in states} == {
|
||||
f"goods-{index}" for index in range(60)
|
||||
}
|
||||
assert sync_state.status == "success"
|
||||
assert sync_state.valid_until is not None
|
||||
assert sync_state.raw_payload_json["data"]["list"][0]["commodityId"] == "goods-0"
|
||||
assert len(sync_state.payload_hash) == 64
|
||||
limited = next(row for row in states if row.commodity_id == "goods-0")
|
||||
assert limited.raw_json["status"] == 1
|
||||
|
||||
|
||||
def test_storehome_sync_failure_is_recorded_without_success_state(db, monkeypatch):
|
||||
account = _account("snapshot-failure")
|
||||
db.add(account)
|
||||
db.commit()
|
||||
|
||||
def fail(*_args, **_kwargs):
|
||||
raise RuntimeError("projection failed")
|
||||
|
||||
monkeypatch.setattr(account_sync_service, "sync_account_goods_states", fail)
|
||||
|
||||
with pytest.raises(RuntimeError, match="projection failed"):
|
||||
sync_storehome_snapshot(
|
||||
db,
|
||||
account,
|
||||
_snapshot(),
|
||||
source="test.storehome",
|
||||
)
|
||||
|
||||
sync_state = (
|
||||
db.query(AccountSyncState)
|
||||
.filter(
|
||||
AccountSyncState.account_id == account.id,
|
||||
AccountSyncState.domain == DOMAIN_STOREHOME,
|
||||
)
|
||||
.one()
|
||||
)
|
||||
assert sync_state.status == "failed"
|
||||
assert "projection failed" in sync_state.error_message
|
||||
assert sync_state.next_retry_at is not None
|
||||
assert sync_state.raw_payload_json["data"]["list"][0]["commodityId"] == "goods-0"
|
||||
|
||||
|
||||
def test_account_work_queue_deduplicates_only_active_items(db):
|
||||
account = _account("queue-account")
|
||||
db.add(account)
|
||||
db.commit()
|
||||
|
||||
first = enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type="storehome_sync",
|
||||
priority=80,
|
||||
)
|
||||
second = enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type="storehome_sync",
|
||||
priority=20,
|
||||
)
|
||||
assert second.id == first.id
|
||||
assert second.priority == 20
|
||||
|
||||
complete_account_work(db, first)
|
||||
third = enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type="storehome_sync",
|
||||
priority=80,
|
||||
)
|
||||
assert third.id != first.id
|
||||
assert db.query(AccountWorkItem).count() == 2
|
||||
|
||||
|
||||
def test_ck_verdict_updates_lifecycle_and_moves_relogin_work(db):
|
||||
issued_at = datetime.now(UTC) - timedelta(days=7, hours=9)
|
||||
account = _account("ck-lifecycle", cookie_updated_at=issued_at)
|
||||
db.add(account)
|
||||
db.commit()
|
||||
|
||||
invalid_at = datetime.now(UTC)
|
||||
on_ck_verdict(
|
||||
db,
|
||||
account,
|
||||
"invalid",
|
||||
detail={"message": "未登录"},
|
||||
occurred_at=invalid_at,
|
||||
)
|
||||
db.flush()
|
||||
|
||||
work = (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(
|
||||
AccountWorkItem.account_id == account.id,
|
||||
AccountWorkItem.work_type == "login_relogin",
|
||||
)
|
||||
.one()
|
||||
)
|
||||
assert work.status == "pending"
|
||||
assert account.ck_invalid_detected_at == invalid_at
|
||||
assert account.ck_observed_lifetime_seconds is not None
|
||||
assert account.ck_expected_expires_at is None
|
||||
|
||||
account.cookie_updated_at = datetime.now(UTC)
|
||||
on_ck_verdict(
|
||||
db,
|
||||
account,
|
||||
"valid",
|
||||
detail={"message": "有效"},
|
||||
occurred_at=datetime.now(UTC),
|
||||
)
|
||||
db.flush()
|
||||
|
||||
db.refresh(work)
|
||||
assert work.status == "success"
|
||||
assert account.ck_last_valid_at is not None
|
||||
assert account.ck_expected_expires_at is not None
|
||||
assert account.ck_expiry_source == "predicted"
|
||||
|
||||
|
||||
def test_ck_health_skips_invalid_and_manual_accounts(db):
|
||||
accounts = [
|
||||
_account("ck-empty", ck_check_status=""),
|
||||
_account("ck-valid", ck_check_status="valid"),
|
||||
_account("ck-invalid", ck_check_status="invalid"),
|
||||
_account("ck-manual", ck_check_status="manual"),
|
||||
]
|
||||
db.add_all(accounts)
|
||||
db.commit()
|
||||
job = SimpleNamespace(interval_seconds=3600, batch_size=200)
|
||||
|
||||
selected = ck_health._select_accounts(db, job, datetime.now(UTC))
|
||||
|
||||
assert {account.username for account in selected} == {"ck-empty", "ck-valid"}
|
||||
|
||||
|
||||
def test_time_limited_goods_block_fulfillment_outside_window():
|
||||
state = DouyuLiveAccountState(
|
||||
account_id=1,
|
||||
account_name="time-limited",
|
||||
checked_at=datetime.now(UTC),
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=1000,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=None,
|
||||
commodity_availability={"time-goods": "expired"},
|
||||
commodity_blocking_reasons={"time-goods": "限时兑换已结束"},
|
||||
)
|
||||
|
||||
reasons = state.blocking_reasons(commodity_id="time-goods")
|
||||
gate = _exchange_gate(state, "time-goods")
|
||||
|
||||
assert "限时兑换已结束" in reasons
|
||||
assert gate == ("not_available", "commodity_expired", "限时兑换已结束")
|
||||
|
||||
|
||||
def test_missing_storehome_goods_blocks_fulfillment_as_off_shelf():
|
||||
state = DouyuLiveAccountState(
|
||||
account_id=2,
|
||||
account_name="off-shelf",
|
||||
checked_at=datetime.now(UTC),
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=1000,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=None,
|
||||
commodity_availability={"another-goods": "available"},
|
||||
)
|
||||
|
||||
reasons = state.blocking_reasons(commodity_id="removed-goods")
|
||||
gate = _exchange_gate(state, "removed-goods")
|
||||
|
||||
assert any("可能已下架" in reason for reason in reasons)
|
||||
assert gate[:2] == ("not_available", "commodity_not_offered")
|
||||
|
||||
|
||||
def test_login_quota_enforces_per_account_and_hourly_budget(db):
|
||||
first = _account("quota-first")
|
||||
second = _account("quota-second")
|
||||
third = _account("quota-third")
|
||||
fourth = _account("quota-fourth")
|
||||
db.add_all([first, second, third, fourth])
|
||||
db.add(
|
||||
LoginQuotaSetting(
|
||||
id=1,
|
||||
daily_attempt_limit=20,
|
||||
hourly_attempt_limit=2,
|
||||
per_account_daily_limit=2,
|
||||
manual_daily_limit=10,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
now = datetime.now(UTC).replace(microsecond=0)
|
||||
|
||||
first_attempt = reserve_login_attempt(
|
||||
db,
|
||||
account=first,
|
||||
mode="login",
|
||||
trigger_source="manual",
|
||||
batch_id="batch-1",
|
||||
created_by=None,
|
||||
now=now.replace(hour=1),
|
||||
)
|
||||
assert first_attempt.allowed
|
||||
assert first_attempt.attempt is not None
|
||||
assert first_attempt.attempt.category == CATEGORY_MANUAL
|
||||
finish_login_attempt(
|
||||
db,
|
||||
first_attempt.attempt,
|
||||
status="success",
|
||||
now=now.replace(hour=1),
|
||||
)
|
||||
|
||||
second_attempt = reserve_login_attempt(
|
||||
db,
|
||||
account=first,
|
||||
mode="login",
|
||||
trigger_source="manual",
|
||||
batch_id="batch-2",
|
||||
created_by=None,
|
||||
now=now.replace(hour=2),
|
||||
)
|
||||
assert second_attempt.allowed
|
||||
assert second_attempt.attempt is not None
|
||||
finish_login_attempt(
|
||||
db,
|
||||
second_attempt.attempt,
|
||||
status="failed",
|
||||
now=now.replace(hour=2),
|
||||
)
|
||||
|
||||
denied_same_account = reserve_login_attempt(
|
||||
db,
|
||||
account=first,
|
||||
mode="login",
|
||||
trigger_source="manual",
|
||||
batch_id="batch-3",
|
||||
created_by=None,
|
||||
now=now.replace(hour=3),
|
||||
)
|
||||
assert not denied_same_account.allowed
|
||||
assert "上限" in denied_same_account.reason
|
||||
|
||||
first_hour = reserve_login_attempt(
|
||||
db,
|
||||
account=second,
|
||||
mode="login",
|
||||
trigger_source="manual",
|
||||
batch_id="batch-4",
|
||||
created_by=None,
|
||||
now=now.replace(hour=4),
|
||||
)
|
||||
second_hour = reserve_login_attempt(
|
||||
db,
|
||||
account=third,
|
||||
mode="login",
|
||||
trigger_source="manual",
|
||||
batch_id="batch-4",
|
||||
created_by=None,
|
||||
now=now.replace(hour=4),
|
||||
)
|
||||
third_hour = reserve_login_attempt(
|
||||
db,
|
||||
account=fourth,
|
||||
mode="login",
|
||||
trigger_source="manual",
|
||||
batch_id="batch-4",
|
||||
created_by=None,
|
||||
now=now.replace(hour=4),
|
||||
)
|
||||
assert first_hour.allowed
|
||||
assert second_hour.allowed
|
||||
assert not third_hour.allowed
|
||||
assert "每小时" in third_hour.reason
|
||||
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261121_0109"
|
||||
HEAD_REVISION = "20261121_0110"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
@@ -58,6 +58,54 @@ EXTRA_INDEXES = (
|
||||
)
|
||||
|
||||
|
||||
_ACCOUNT_PLATFORM_COLUMNS = {
|
||||
"ck_expected_expires_at": "DATETIME NULL",
|
||||
"ck_last_valid_at": "DATETIME NULL",
|
||||
"ck_invalid_detected_at": "DATETIME NULL",
|
||||
"ck_observed_lifetime_seconds": "INTEGER NULL",
|
||||
"ck_expiry_source": "VARCHAR(24) NOT NULL DEFAULT ''",
|
||||
}
|
||||
|
||||
_LATER_ORM_COLUMNS = {
|
||||
"platform_fulfillment_tasks": {
|
||||
"sale_mode": "VARCHAR(16) NOT NULL DEFAULT 'unknown'",
|
||||
"campaign_key": "VARCHAR(128) NOT NULL DEFAULT ''",
|
||||
"campaign_name": "VARCHAR(255) NOT NULL DEFAULT ''",
|
||||
"offer_available_from": "DATETIME NULL",
|
||||
"offer_available_until": "DATETIME NULL",
|
||||
"classification_source": "VARCHAR(24) NOT NULL DEFAULT 'unknown'",
|
||||
"exchange_result_image_key": "VARCHAR(512) NOT NULL DEFAULT ''",
|
||||
"exchange_result_image_saved_at": "DATETIME NULL",
|
||||
},
|
||||
"douyu_external_orders": {
|
||||
"succeeded_at": "DATETIME NULL",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _add_account_platform_columns(engine) -> None:
|
||||
"""让“旧 revision + 当前 ORM”测试夹具兼容已新增的账号列。"""
|
||||
inspector = inspect(engine)
|
||||
existing = {item["name"] for item in inspector.get_columns("accounts")}
|
||||
with engine.begin() as conn:
|
||||
for name, ddl in _ACCOUNT_PLATFORM_COLUMNS.items():
|
||||
if name not in existing:
|
||||
conn.execute(text(f"ALTER TABLE accounts ADD COLUMN `{name}` {ddl}"))
|
||||
|
||||
|
||||
def _add_later_orm_columns(engine, tables: tuple[str, ...]) -> None:
|
||||
"""旧 revision 上用当前 ORM 写夹具时,补齐后续纯列迁移。"""
|
||||
inspector = inspect(engine)
|
||||
with engine.begin() as conn:
|
||||
for table in tables:
|
||||
existing = {item["name"] for item in inspector.get_columns(table)}
|
||||
for name, ddl in _LATER_ORM_COLUMNS[table].items():
|
||||
if name not in existing:
|
||||
conn.execute(
|
||||
text(f"ALTER TABLE `{table}` ADD COLUMN `{name}` {ddl}")
|
||||
)
|
||||
|
||||
|
||||
def _migration_config() -> Config:
|
||||
config = Config(str(PROJECT_ROOT / "alembic.ini"))
|
||||
config.set_main_option(
|
||||
@@ -458,6 +506,7 @@ class TestMigrationSmoke:
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261121_0106")
|
||||
_add_account_platform_columns(mig_engine)
|
||||
with Session(mig_engine) as session:
|
||||
user = User(
|
||||
username="repair-0107-user",
|
||||
@@ -919,6 +968,7 @@ class TestMigrationSmoke:
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261118_0089")
|
||||
_add_account_platform_columns(mig_engine)
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
user = User(
|
||||
@@ -1059,6 +1109,7 @@ class TestMigrationSmoke:
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261118_0091")
|
||||
_add_account_platform_columns(mig_engine)
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
account = Account(
|
||||
@@ -1121,6 +1172,10 @@ class TestMigrationSmoke:
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261118_0094")
|
||||
_add_later_orm_columns(
|
||||
mig_engine,
|
||||
("platform_fulfillment_tasks", "douyu_external_orders"),
|
||||
)
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
repaired_task = PlatformFulfillmentTask(
|
||||
@@ -1194,6 +1249,8 @@ class TestMigrationSmoke:
|
||||
cfg = _migration_config()
|
||||
cfg.set_main_option("sqlalchemy.url", mig_url)
|
||||
command.upgrade(cfg, "20261119_0097")
|
||||
_add_account_platform_columns(mig_engine)
|
||||
_add_later_orm_columns(mig_engine, ("douyu_external_orders",))
|
||||
|
||||
with Session(mig_engine) as session:
|
||||
owner = User(
|
||||
|
||||
Reference in New Issue
Block a user