340 lines
9.5 KiB
Python
340 lines
9.5 KiB
Python
"""账号同步队列执行器测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
from tests.support.db import make_test_session
|
|
from web.backend.models import (
|
|
Account,
|
|
AccountCommodityState,
|
|
AccountEvent,
|
|
AccountSyncState,
|
|
AccountWorkItem,
|
|
DouyuConfig,
|
|
)
|
|
from web.backend.services.account_sync_runner import (
|
|
WORK_ESPORTS_SYNC,
|
|
WORK_EXCHANGE_RECORDS_SYNC,
|
|
WORK_STOREHOME_SYNC,
|
|
execute_account_work_item,
|
|
run_account_sync_queue,
|
|
)
|
|
from web.backend.services.account_work_queue import (
|
|
claim_account_work,
|
|
enqueue_account_work,
|
|
recover_running_account_work,
|
|
)
|
|
from web.backend.services.douyu_read_gateway import StorehomeSnapshot
|
|
|
|
|
|
def _config(db, **overrides) -> None:
|
|
db.add(
|
|
DouyuConfig(
|
|
manual_id="manual",
|
|
rid="room",
|
|
**overrides,
|
|
)
|
|
)
|
|
db.commit()
|
|
|
|
|
|
def _account(name: str = "sync-account") -> Account:
|
|
return Account(
|
|
username=name,
|
|
password="p",
|
|
email=f"{name}@example.com",
|
|
email_password="ep",
|
|
cookie="acf_uid=100",
|
|
cookie_updated_at=datetime.now(UTC),
|
|
ck_check_status="valid",
|
|
status="active",
|
|
auto_managed=True,
|
|
)
|
|
|
|
|
|
def _snapshot() -> StorehomeSnapshot:
|
|
items = (
|
|
{
|
|
"commodityId": "limited",
|
|
"commodityName": "限兑商品",
|
|
"status": 1,
|
|
},
|
|
{
|
|
"commodityId": "available",
|
|
"commodityName": "可兑商品",
|
|
"status": 0,
|
|
"startTime": str(int((datetime.now(UTC) - timedelta(hours=1)).timestamp())),
|
|
"endTime": str(int((datetime.now(UTC) + timedelta(hours=1)).timestamp())),
|
|
"timeLimited": 1,
|
|
},
|
|
)
|
|
return StorehomeSnapshot(
|
|
manual_id="manual",
|
|
rid="room",
|
|
observed_at=datetime.now(UTC),
|
|
items=items,
|
|
limited_ids=frozenset({"limited"}),
|
|
raw={"data": {"list": list(items)}},
|
|
)
|
|
|
|
|
|
def test_execute_work_item_syncs_all_goods_and_completes(db):
|
|
_config(db)
|
|
account = _account()
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
claimed = claim_account_work(
|
|
db,
|
|
work_types=(WORK_STOREHOME_SYNC,),
|
|
limit=1,
|
|
)
|
|
db.commit()
|
|
assert [item.id for item in claimed] == [work.id]
|
|
assert claimed[0].attempt_count == 1
|
|
db.commit()
|
|
|
|
result = execute_account_work_item(
|
|
work.id,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: object(),
|
|
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
work_row = verify.get(AccountWorkItem, work.id)
|
|
states = (
|
|
verify.query(AccountCommodityState)
|
|
.filter(AccountCommodityState.account_id == account.id)
|
|
.all()
|
|
)
|
|
sync_state = (
|
|
verify.query(AccountSyncState)
|
|
.filter_by(account_id=account.id, domain="storehome")
|
|
.one()
|
|
)
|
|
assert result["ok"] is True
|
|
assert result["item_count"] == 2
|
|
assert work_row.status == "success"
|
|
assert len(states) == 2
|
|
assert sync_state.status == "success"
|
|
|
|
|
|
def test_work_item_failure_returns_to_pending_for_retry(db):
|
|
_config(db)
|
|
account = _account("sync-retry")
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
claimed = claim_account_work(
|
|
db,
|
|
work_types=(WORK_STOREHOME_SYNC,),
|
|
limit=1,
|
|
)
|
|
db.commit()
|
|
assert claimed[0].attempt_count == 1
|
|
db.commit()
|
|
|
|
def fail(*_args, **_kwargs):
|
|
raise RuntimeError("network unavailable")
|
|
|
|
result = execute_account_work_item(
|
|
work.id,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: object(),
|
|
snapshot_fetcher=fail,
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
work_row = verify.get(AccountWorkItem, work.id)
|
|
assert result["ok"] is False
|
|
assert work_row.status == "pending"
|
|
assert work_row.attempt_count == 1
|
|
assert "network unavailable" in work_row.last_error
|
|
assert work_row.due_at > datetime.now(UTC)
|
|
|
|
|
|
def test_esports_profile_sync_persists_data_without_rewriting_ck(db):
|
|
_config(db, esports_manual_id="esports-manual")
|
|
account = _account("sync-esports")
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_ESPORTS_SYNC,
|
|
)
|
|
db.commit()
|
|
claim_account_work(db, work_types=(WORK_ESPORTS_SYNC,), limit=1)
|
|
db.commit()
|
|
seen: dict = {}
|
|
|
|
def probe(_account, _config, **kwargs):
|
|
seen.update(kwargs)
|
|
return {
|
|
"probe_id": "work:1",
|
|
"identity": {"uid": "100", "username": "sync-esports"},
|
|
# 单项资料同步没有独立执行 CK 探针;即使内部推导出 invalid,
|
|
# 也不能覆盖账号主表已有的有效 CK。
|
|
"session": {"status": "invalid", "message": "推导值"},
|
|
"elite": {},
|
|
"eligibility": {},
|
|
"roles": {},
|
|
"gold": {},
|
|
"goods": {},
|
|
"esports": {
|
|
"status": "ok",
|
|
"opened": True,
|
|
"manual_type": 2,
|
|
"manual_score": 321,
|
|
"expire_time": 1795967999,
|
|
},
|
|
"errors": {},
|
|
}
|
|
|
|
result = execute_account_work_item(
|
|
work.id,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: object(),
|
|
probe_fetcher=probe,
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
row = verify.get(Account, account.id)
|
|
state = (
|
|
verify.query(AccountSyncState)
|
|
.filter_by(account_id=account.id, domain="esports")
|
|
.one()
|
|
)
|
|
assert result["ok"] is True
|
|
assert seen["with_esports"] is True
|
|
assert seen["with_elite"] is False
|
|
assert row.ck_check_status == "valid"
|
|
assert row.esports_opened is True
|
|
assert row.esports_points == 321
|
|
assert row.esports_expire_time == 1795967999
|
|
assert state.status == "success"
|
|
|
|
|
|
def test_exchange_record_work_persists_events_and_raw_projection(db):
|
|
_config(db, esports_manual_id="esports-manual")
|
|
account = _account("sync-exchange")
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_EXCHANGE_RECORDS_SYNC,
|
|
)
|
|
db.commit()
|
|
claim_account_work(db, work_types=(WORK_EXCHANGE_RECORDS_SYNC,), limit=1)
|
|
db.commit()
|
|
|
|
class _Client:
|
|
def exchange_records(self, *, manual_id, limit, offset=0):
|
|
return {
|
|
"records": [
|
|
{
|
|
"commodityId": f"{manual_id}-item",
|
|
"commodityName": "测试套装",
|
|
"score": "18",
|
|
"time": "1795967997",
|
|
}
|
|
]
|
|
}
|
|
|
|
result = execute_account_work_item(
|
|
work.id,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: _Client(),
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
state = (
|
|
verify.query(AccountSyncState)
|
|
.filter_by(account_id=account.id, domain="exchange_records")
|
|
.one()
|
|
)
|
|
events = (
|
|
verify.query(AccountEvent)
|
|
.filter_by(account_id=account.id, event_type="exchange")
|
|
.all()
|
|
)
|
|
assert result["ok"] is True
|
|
assert result["item_count"] == 2
|
|
assert set(state.raw_payload_json) == {"elite", "esports"}
|
|
assert state.raw_payload_json["elite"][0]["commodityId"] == "manual-item"
|
|
assert {event.subject_key for event in events} == {
|
|
"manual-item",
|
|
"esports-manual-item",
|
|
}
|
|
|
|
|
|
def test_run_account_sync_queue_claims_and_executes(db):
|
|
_config(db)
|
|
account = _account("sync-runner")
|
|
db.add(account)
|
|
db.commit()
|
|
enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
|
|
result = run_account_sync_queue(
|
|
db,
|
|
batch_size=5,
|
|
session_factory=make_test_session,
|
|
client_factory=lambda _cookie: object(),
|
|
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
|
|
)
|
|
|
|
with make_test_session() as verify:
|
|
work = (
|
|
verify.query(AccountWorkItem)
|
|
.filter_by(account_id=account.id, work_type=WORK_STOREHOME_SYNC)
|
|
.one()
|
|
)
|
|
assert result["scanned"] == 1
|
|
assert result["success"] == 1
|
|
assert work.status == "success"
|
|
|
|
|
|
def test_restart_recovers_running_work(db):
|
|
account = _account("sync-recovery")
|
|
db.add(account)
|
|
db.commit()
|
|
work = enqueue_account_work(
|
|
db,
|
|
account_id=account.id,
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
)
|
|
db.commit()
|
|
claim_account_work(
|
|
db,
|
|
work_types=(WORK_STOREHOME_SYNC,),
|
|
limit=1,
|
|
)
|
|
db.commit()
|
|
|
|
recovered = recover_running_account_work(db)
|
|
|
|
with make_test_session() as verify:
|
|
row = verify.get(AccountWorkItem, work.id)
|
|
assert recovered == 1
|
|
assert row.status == "pending"
|
|
assert row.due_at <= datetime.now(UTC)
|
|
assert "服务重启" in row.last_error
|