301 lines
8.8 KiB
Python
301 lines
8.8 KiB
Python
"""账号商品能力索引与兑换历史投影。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
from web.backend.models import Account, AccountCommodityState, AccountEvent, User
|
|
from web.backend.routers.accounts import account_commodities
|
|
from web.backend.services import account_capability_service as service
|
|
|
|
NOW = datetime.now(UTC).replace(microsecond=0)
|
|
|
|
|
|
def _account(db, name: str = "cap-account") -> Account:
|
|
account = Account(
|
|
username=name,
|
|
password="p",
|
|
email=f"{name}@example.com",
|
|
email_password="ep",
|
|
cookie="acf_uid=1; acf_ccn=c",
|
|
ck_check_status="valid",
|
|
status="active",
|
|
auto_managed=True,
|
|
)
|
|
db.add(account)
|
|
db.commit()
|
|
return account
|
|
|
|
|
|
def test_classify_goods_item_keeps_unknown_when_status_missing():
|
|
available, reason, raw = service.classify_goods_item({"status": 0})
|
|
assert (available, reason, raw) == ("available", "", "0")
|
|
|
|
limited, reason, raw = service.classify_goods_item({"status": "1"})
|
|
assert (limited, reason, raw) == ("limited", "该账号已达此商品兑换上限", "1")
|
|
|
|
no_stock, reason, _ = service.classify_goods_item(
|
|
{"status": 0, "storeStatus": 0}
|
|
)
|
|
assert (no_stock, reason) == ("no_stock", "官方商城当前无库存")
|
|
|
|
unknown, reason, raw = service.classify_goods_item({})
|
|
assert unknown == "unknown"
|
|
assert "未返回" in reason
|
|
assert raw == ""
|
|
|
|
|
|
def test_time_limited_goods_use_schedule_before_account_limit():
|
|
limited = {
|
|
"status": "1",
|
|
"timeLimited": 1,
|
|
"startTime": str(int((NOW - timedelta(hours=1)).timestamp())),
|
|
"endTime": str(int((NOW + timedelta(hours=1)).timestamp())),
|
|
"specTimes": [
|
|
{
|
|
"startTime": str(int((NOW - timedelta(hours=1)).timestamp())),
|
|
"endTime": str(int((NOW + timedelta(hours=1)).timestamp())),
|
|
}
|
|
],
|
|
}
|
|
availability, reason, _ = service.classify_goods_item(limited, now=NOW)
|
|
assert availability == "limited"
|
|
assert "上限" in reason
|
|
|
|
future = {
|
|
"status": "1",
|
|
"timeLimited": 1,
|
|
"startTime": str(int((NOW + timedelta(hours=2)).timestamp())),
|
|
"endTime": str(int((NOW + timedelta(hours=3)).timestamp())),
|
|
}
|
|
availability, reason, _ = service.classify_goods_item(future, now=NOW)
|
|
assert availability == "not_open"
|
|
assert "未开始" in reason
|
|
|
|
expired = {
|
|
"status": "0",
|
|
"timeLimited": 1,
|
|
"startTime": str(int((NOW - timedelta(hours=3)).timestamp())),
|
|
"endTime": str(int((NOW - timedelta(hours=2)).timestamp())),
|
|
}
|
|
availability, reason, _ = service.classify_goods_item(expired, now=NOW)
|
|
assert availability == "expired"
|
|
assert "已结束" in reason
|
|
|
|
|
|
def test_sync_stores_time_window_and_next_boundary(db):
|
|
account = _account(db, "cap-window")
|
|
end_at = NOW + timedelta(minutes=5)
|
|
service.sync_account_goods_states(
|
|
db,
|
|
account,
|
|
[
|
|
{
|
|
"commodityId": "LIMITED",
|
|
"commodityName": "套装-兔兔白日梦",
|
|
"status": "1",
|
|
"timeLimited": 1,
|
|
"startTime": str(int((NOW - timedelta(days=1)).timestamp())),
|
|
"endTime": str(int(end_at.timestamp())),
|
|
"specTimes": [
|
|
{
|
|
"startTime": str(int((NOW - timedelta(days=1)).timestamp())),
|
|
"endTime": str(int(end_at.timestamp())),
|
|
}
|
|
],
|
|
}
|
|
],
|
|
observed_at=NOW,
|
|
)
|
|
db.commit()
|
|
|
|
state = (
|
|
db.query(AccountCommodityState)
|
|
.filter_by(account_id=account.id, commodity_id="LIMITED")
|
|
.one()
|
|
)
|
|
assert state.availability == "limited"
|
|
assert state.time_limited is True
|
|
assert state.available_until == end_at
|
|
assert state.next_check_at == end_at
|
|
assert state.schedule_json
|
|
|
|
|
|
def test_sync_account_goods_states_writes_capabilities_and_exchange_stats(db):
|
|
account = _account(db)
|
|
db.add(
|
|
AccountEvent(
|
|
account_id=account.id,
|
|
event_type="exchange",
|
|
category="business",
|
|
occurred_at=NOW - timedelta(days=10),
|
|
title="兑换 A",
|
|
source="official_sync",
|
|
subject_type="commodity",
|
|
subject_key="A",
|
|
)
|
|
)
|
|
db.commit()
|
|
|
|
changed = service.sync_account_goods_states(
|
|
db,
|
|
account,
|
|
[
|
|
{"commodityId": "A", "name": "商品 A", "status": "0"},
|
|
{"commodityId": "B", "name": "商品 B", "status": "1"},
|
|
{"commodityId": "C", "name": "商品 C", "status": "0", "storeStatus": 0},
|
|
{"commodityId": "D", "name": "商品 D"},
|
|
],
|
|
observed_at=NOW,
|
|
)
|
|
db.commit()
|
|
|
|
assert changed == 4
|
|
states = {
|
|
row.commodity_id: row
|
|
for row in db.query(AccountCommodityState).filter_by(account_id=account.id)
|
|
}
|
|
assert states["A"].availability == "available"
|
|
assert states["A"].exchange_count_365d == 1
|
|
assert states["A"].last_exchange_at == NOW - timedelta(days=10)
|
|
assert states["B"].availability == "limited"
|
|
assert states["C"].availability == "no_stock"
|
|
assert states["D"].availability == "unknown"
|
|
assert states["A"].valid_until == NOW + timedelta(
|
|
seconds=service.capability_ttl_seconds()
|
|
)
|
|
|
|
|
|
def test_next_sync_marks_missing_goods_not_offered(db):
|
|
account = _account(db, "cap-remove")
|
|
service.sync_account_goods_states(
|
|
db,
|
|
account,
|
|
[
|
|
{"commodityId": "A", "status": "0"},
|
|
{"commodityId": "B", "status": "0"},
|
|
],
|
|
observed_at=NOW,
|
|
)
|
|
db.commit()
|
|
|
|
service.sync_account_goods_states(
|
|
db,
|
|
account,
|
|
[{"commodityId": "A", "status": "0"}],
|
|
observed_at=NOW + timedelta(minutes=1),
|
|
)
|
|
db.commit()
|
|
|
|
removed = (
|
|
db.query(AccountCommodityState)
|
|
.filter_by(account_id=account.id, commodity_id="B")
|
|
.one()
|
|
)
|
|
assert removed.availability == "not_offered"
|
|
assert removed.valid_until is None
|
|
|
|
|
|
def test_mark_exchange_keeps_projection_in_sync(db):
|
|
account = _account(db, "cap-exchange")
|
|
service.sync_account_goods_states(
|
|
db,
|
|
account,
|
|
[{"commodityId": "A", "name": "商品 A", "status": "0"}],
|
|
observed_at=NOW,
|
|
)
|
|
db.commit()
|
|
|
|
occurred_at = NOW + timedelta(minutes=2)
|
|
db.add(
|
|
AccountEvent(
|
|
account_id=account.id,
|
|
event_type="exchange",
|
|
category="business",
|
|
occurred_at=occurred_at,
|
|
title="兑换 商品 A",
|
|
source="douyu_task",
|
|
subject_type="commodity",
|
|
subject_key="A",
|
|
)
|
|
)
|
|
db.flush()
|
|
service.mark_commodity_exchanged(
|
|
db,
|
|
account_id=account.id,
|
|
commodity_id="A",
|
|
occurred_at=occurred_at,
|
|
commodity_name="商品 A",
|
|
)
|
|
db.commit()
|
|
|
|
state = (
|
|
db.query(AccountCommodityState)
|
|
.filter_by(account_id=account.id, commodity_id="A")
|
|
.one()
|
|
)
|
|
assert state.availability == "limited"
|
|
assert state.exchange_count_365d == 1
|
|
assert state.last_exchange_at == occurred_at
|
|
assert state.valid_until is None
|
|
|
|
|
|
def test_invalidating_ck_removes_available_from_matching_pool(db):
|
|
account = _account(db, "cap-invalid")
|
|
service.sync_account_goods_states(
|
|
db,
|
|
account,
|
|
[{"commodityId": "A", "status": "0"}],
|
|
observed_at=NOW,
|
|
)
|
|
db.commit()
|
|
|
|
changed = service.invalidate_account_commodity_states(
|
|
db,
|
|
account_id=account.id,
|
|
reason="CK 已失效",
|
|
observed_at=NOW + timedelta(minutes=1),
|
|
)
|
|
db.commit()
|
|
|
|
assert changed == 1
|
|
state = db.query(AccountCommodityState).filter_by(account_id=account.id).one()
|
|
assert state.availability == "unknown"
|
|
assert state.valid_until is None
|
|
assert "CK" in state.blocking_reason
|
|
|
|
|
|
def test_account_commodities_api_returns_summary_and_freshness(db):
|
|
account = _account(db, "cap-api")
|
|
user = User(
|
|
username="cap-api-admin",
|
|
password_hash="h",
|
|
role="super_admin",
|
|
is_active=True,
|
|
)
|
|
db.add(user)
|
|
service.sync_account_goods_states(
|
|
db,
|
|
account,
|
|
[
|
|
{"commodityId": "A", "name": "商品 A", "status": "0"},
|
|
{"commodityId": "B", "name": "商品 B", "status": "1"},
|
|
],
|
|
observed_at=NOW,
|
|
)
|
|
db.commit()
|
|
|
|
result = account_commodities(
|
|
account.id,
|
|
scope="",
|
|
availability="",
|
|
limit=500,
|
|
db=db,
|
|
current=user,
|
|
)
|
|
|
|
assert result["summary"]["total"] == 2
|
|
assert result["summary"]["fresh_available"] == 1
|
|
assert {item["commodity_id"] for item in result["items"]} == {"A", "B"}
|
|
assert all(item["fresh"] for item in result["items"])
|