feat(fulfillment): 增加账号商品能力索引与生命周期事件
This commit is contained in:
@@ -12,6 +12,8 @@ from fastapi import HTTPException
|
||||
from tests.support.db import get_test_engine, make_test_session
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
AutomationJob,
|
||||
DouyuGoodsSnapshot,
|
||||
@@ -453,6 +455,45 @@ class TestSnapshotSync(_Base):
|
||||
outbox = self.session.query(IntegrationOutbox).one()
|
||||
assert outbox.payload_json["items"][0]["stateVersion"] == 1
|
||||
|
||||
def test_duplicate_snapshot_keeps_allocated_task_state(self):
|
||||
self.mapping()
|
||||
account = self.account("snapshot-allocated")
|
||||
service.sync_order_snapshot(self.session, {"data": self.snapshot()})
|
||||
task = self.session.query(PlatformFulfillmentTask).one()
|
||||
service.allocate_task(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
service.sync_order_snapshot(self.session, {"data": self.snapshot()})
|
||||
|
||||
self.session.refresh(task)
|
||||
assert task.execution_status == "allocated"
|
||||
assert task.account_id == account.id
|
||||
assert task.lease_id is not None
|
||||
assert task.next_allocate_at is None
|
||||
assert task.exchange_status == "eligible"
|
||||
assert task.issue_code == ""
|
||||
|
||||
def test_duplicate_snapshot_preserves_allocation_retry(self):
|
||||
self.mapping()
|
||||
service.sync_order_snapshot(self.session, {"data": self.snapshot()})
|
||||
task = self.session.query(PlatformFulfillmentTask).one()
|
||||
retry_at = NOW + timedelta(seconds=45)
|
||||
task.issue_code = "no_eligible_account"
|
||||
task.last_error = "当前没有可用账号"
|
||||
task.next_allocate_at = retry_at
|
||||
self.session.commit()
|
||||
|
||||
service.sync_order_snapshot(self.session, {"data": self.snapshot()})
|
||||
|
||||
self.session.refresh(task)
|
||||
assert task.execution_status == "ready"
|
||||
assert task.issue_code == "no_eligible_account"
|
||||
assert task.last_error == "当前没有可用账号"
|
||||
assert task.next_allocate_at == retry_at
|
||||
|
||||
def test_legacy_event_only_reads_the_specified_order_item(self, monkeypatch):
|
||||
self.mapping()
|
||||
requested: list[str] = []
|
||||
@@ -496,6 +537,85 @@ class TestSnapshotSync(_Base):
|
||||
|
||||
|
||||
class TestAccountSelection(_Base):
|
||||
def test_capability_match_checks_only_selected_candidate(self, monkeypatch):
|
||||
account = self.account("capability-fast")
|
||||
task = self.task()
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=COMMODITY,
|
||||
commodity_name=INTERNAL_SKU,
|
||||
availability="available",
|
||||
observed_at=NOW,
|
||||
valid_until=NOW + timedelta(minutes=15),
|
||||
source="overview",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "capability")
|
||||
|
||||
detail = service.allocate_task(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert detail["task"]["account"]["id"] == account.id
|
||||
assert detail["task"]["executionStatus"] == "allocated"
|
||||
self.refresh_mock.assert_called_once()
|
||||
|
||||
def test_hybrid_falls_back_to_live_scan_when_capability_candidate_fails(
|
||||
self,
|
||||
monkeypatch,
|
||||
):
|
||||
capability_account = self.account("capability-stale")
|
||||
live_account = self.account("live-fallback")
|
||||
task = self.task()
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=capability_account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=COMMODITY,
|
||||
availability="available",
|
||||
observed_at=NOW,
|
||||
valid_until=NOW + timedelta(minutes=15),
|
||||
source="overview",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
def live_state(_db, account, **_kwargs):
|
||||
return DouyuLiveAccountState(
|
||||
account_id=account.id,
|
||||
account_name=account.username,
|
||||
checked_at=NOW,
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=2000,
|
||||
limited_ids=(
|
||||
frozenset({COMMODITY})
|
||||
if account.id == capability_account.id
|
||||
else frozenset()
|
||||
),
|
||||
role_change_available_at=None,
|
||||
)
|
||||
|
||||
self.refresh_mock.side_effect = live_state
|
||||
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "hybrid")
|
||||
|
||||
detail = service.allocate_task(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert detail["task"]["account"]["id"] == live_account.id
|
||||
assert self.refresh_mock.call_count == 2
|
||||
|
||||
def test_allocation_refreshes_live_state_instead_of_cached_filters(self):
|
||||
account = Account(
|
||||
username="live-refresh-account",
|
||||
@@ -678,6 +798,15 @@ class TestAccountSelection(_Base):
|
||||
assert lease.purpose == "platform_fulfillment"
|
||||
assert lease.status == "active"
|
||||
assert lease.expires_at is None
|
||||
acquired = (
|
||||
self.session.query(AccountEvent)
|
||||
.filter(
|
||||
AccountEvent.account_id == account.id,
|
||||
AccountEvent.event_type == "lease_acquired",
|
||||
)
|
||||
.one()
|
||||
)
|
||||
assert acquired.detail["task_id"] == task.id
|
||||
|
||||
released = service.release_task_account(
|
||||
self.session,
|
||||
@@ -687,6 +816,15 @@ class TestAccountSelection(_Base):
|
||||
assert released["task"]["executionStatus"] == "ready"
|
||||
self.session.refresh(lease)
|
||||
assert lease.status == "released"
|
||||
released_event = (
|
||||
self.session.query(AccountEvent)
|
||||
.filter(
|
||||
AccountEvent.account_id == account.id,
|
||||
AccountEvent.event_type == "lease_released",
|
||||
)
|
||||
.one()
|
||||
)
|
||||
assert released_event.detail["lease_id"] == lease.id
|
||||
|
||||
def test_lease_settings_support_unlimited_and_finite_duration(self):
|
||||
self.account("settings-account")
|
||||
|
||||
Reference in New Issue
Block a user