refactor(douyu): 统一账号准入与本地查询链路

This commit is contained in:
yml
2026-11-22 19:31:32 +08:00
parent e652d30283
commit d78437c6ac
14 changed files with 592 additions and 548 deletions
@@ -10,6 +10,7 @@ from web.backend.models import (
AccountLease,
AccountWorkItem,
AutomationJob,
PlatformFulfillmentSetting,
PlatformFulfillmentTask,
User,
)
@@ -125,6 +126,68 @@ def test_no_pending_task_means_no_refresh(db):
assert job.build_select_query(db, automation_job, NOW).all() == []
def test_refresh_ignores_tasks_before_match_cutoff(db):
db.add(
PlatformFulfillmentSetting(
id=1,
lease_duration_minutes=None,
order_match_started_at=NOW - timedelta(days=1),
)
)
db.add_all(
[
PlatformFulfillmentTask(
source_system="order_site",
source_order_id="old",
source_order_item_id="old",
internal_sku_key="old-skin",
commodity_id="OLD",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=NOW - timedelta(days=2),
),
PlatformFulfillmentTask(
source_system="order_site",
source_order_id="new",
source_order_item_id="new",
internal_sku_key="new-skin",
commodity_id="NEW",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=NOW,
),
]
)
old_account = _account(db, "cap-cutoff-old")
new_account = _account(db, "cap-cutoff-new")
_state(
db,
old_account,
commodity_id="OLD",
valid_until=NOW - timedelta(minutes=1),
)
_state(
db,
new_account,
commodity_id="NEW",
valid_until=NOW - timedelta(minutes=1),
)
db.commit()
automation_job = AutomationJob(
key=job.JOB_KEY,
title="履约商品能力补充刷新",
interval_seconds=3600,
batch_size=10,
concurrency=3,
)
selected = job.build_select_query(db, automation_job, NOW).all()
assert [row.id for row in selected] == [new_account.id]
def test_run_enqueues_stale_accounts_without_external_query(db):
db.add(
PlatformFulfillmentTask(
+36
View File
@@ -118,6 +118,42 @@ def test_supply_inventory_separates_available_and_allocatable(db):
assert inventory.needs_replenishment is True
def test_supply_allocatable_uses_the_same_pool_owner_filter_as_selection(db):
customer = User(
username="supply-customer",
password_hash="h",
role="customer",
is_active=True,
)
free = _account("supply-pool-free", points=400)
customer_assigned = _account("supply-customer-assigned", points=400)
db.add(customer)
db.flush()
customer_assigned.assigned_to = customer.id
db.add_all([free, customer_assigned])
db.flush()
db.add_all([_state(free), _state(customer_assigned)])
db.add(
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=2,
target_available=2,
target_allocatable=2,
)
)
db.commit()
target = db.query(AccountSupplyTarget).one()
inventory = evaluate_supply_target(db, target, now=NOW)
assert inventory.covered == 2
assert inventory.available == 2
assert inventory.allocatable == 1
def test_global_supply_inventory_batches_value_classes_in_two_queries(db):
user = User(
username="global-supply-owner",
+50 -21
View File
@@ -7,8 +7,10 @@ import pytest
from tests.support.db import get_test_engine, make_test_session
from web.backend.models import (
Account,
AccountCommodityState,
AccountEvent,
AccountLease,
AccountSyncState,
AuditLog,
DouyuTask,
PlatformCommodityMapping,
@@ -116,19 +118,6 @@ class FakeNoBindClient(FakeClient):
raise RuntimeError("未绑定角色")
class FakeUnboundClient(FakeClient):
def bind_info(self, **_):
return {
"is_bound_account": False,
"is_bound_role": False,
"is_bound_act": False,
"role_name": "",
"area_name": "",
"plat_name": "",
"nickname": "",
}
class TestDouyuCollabOrders:
def setup_method(self):
self.engine = get_test_engine()
@@ -378,7 +367,17 @@ class TestDouyuCollabOrders:
assert order.account_id is None
assert order.status == collab.STATUS_INCOMPLETE
def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(self):
def test_recommend_accounts_prefers_unbound_and_can_exclude_rebindable(
self,
monkeypatch,
):
monkeypatch.setattr(
collab,
"probe_account",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
AssertionError("本地推荐不应访问斗鱼官网")
),
)
mapping = PlatformCommodityMapping(
target_platform="douyu",
internal_sku_key="推荐礼包",
@@ -405,9 +404,12 @@ class TestDouyuCollabOrders:
email_password="mail-password",
cookie="acf_uid=200; acf_nickname=bound",
status="active",
ck_check_status="valid",
auto_managed=True,
elite_opened=True,
points=1000,
role_bind_status="bound",
game_name="角色B",
)
empty = Account(
username="douyu-account-empty",
@@ -416,18 +418,37 @@ class TestDouyuCollabOrders:
email_password="mail-password",
cookie="acf_uid=300; acf_nickname=empty",
status="active",
ck_check_status="valid",
auto_managed=True,
elite_opened=True,
points=500,
role_bind_status="unbound",
)
self.session.add_all([bound, empty])
self.session.flush()
self.session.add_all(
[
AccountCommodityState(
account_id=bound.id,
platform="douyu",
scope="elite",
commodity_id="gift-a",
availability="available",
observed_at=datetime.now(UTC),
valid_until=datetime.now(UTC) + timedelta(hours=1),
),
AccountCommodityState(
account_id=empty.id,
platform="douyu",
scope="elite",
commodity_id="gift-a",
availability="available",
observed_at=datetime.now(UTC),
valid_until=datetime.now(UTC) + timedelta(hours=1),
),
]
)
self.session.commit()
clients = {
int(self.account.id): FakeClient(),
int(bound.id): FakeClient(),
int(empty.id): FakeUnboundClient(),
}
result = collab.recommend_accounts(
self.session,
@@ -435,7 +456,6 @@ class TestDouyuCollabOrders:
order,
include_rebindable=True,
limit=5,
client_factory=lambda account: clients[int(account.id)],
)
assert order.commodity_id == "gift-a"
@@ -450,7 +470,6 @@ class TestDouyuCollabOrders:
order,
include_rebindable=False,
limit=5,
client_factory=lambda account: clients[int(account.id)],
)
assert [item["account_id"] for item in unbound_only["candidates"]] == [
empty.id
@@ -1611,6 +1630,16 @@ class TestDouyuCollabOrders:
assert client.offsets == [0, 100]
assert len(loaded) == 101
sync_state = (
self.session.query(AccountSyncState)
.filter_by(
account_id=order.account_id,
platform="douyu",
domain="exchange_records",
)
.one()
)
assert len(sync_state.raw_payload_json["elite"]) == 101
def test_record_query_rejects_old_record_and_wrong_commodity(self):
order = collab.create_order(