fix(douyu): 拆分账号资格活动窗口与库存语义

This commit is contained in:
yml
2026-11-22 20:04:49 +08:00
parent d78437c6ac
commit 1906f924bc
26 changed files with 896 additions and 174 deletions
@@ -49,6 +49,9 @@ def _state(
platform="douyu",
scope="elite",
commodity_id=commodity_id,
account_eligibility="eligible",
offer_state="active",
stock_state="in_stock",
availability="available",
observed_at=NOW - timedelta(hours=2),
valid_until=valid_until,
+51 -5
View File
@@ -34,10 +34,12 @@ def test_classify_goods_item_keeps_unknown_when_status_missing():
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}
sold_out = service.classify_goods_item_assessment(
{"status": 0, "storeStatus": 0, "count": 0}
)
assert (no_stock, reason) == ("no_stock", "官方商城当前无库存")
assert sold_out.account_eligibility == "eligible"
assert sold_out.stock_state == "out_of_stock"
assert sold_out.availability == "no_stock"
unknown, reason, raw = service.classify_goods_item({})
assert unknown == "unknown"
@@ -62,13 +64,22 @@ def test_time_limited_goods_use_schedule_before_account_limit():
assert availability == "limited"
assert "上限" in reason
future = {
future_limited = {
"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)
availability, reason, _ = service.classify_goods_item(future_limited, now=NOW)
assert availability == "limited"
assert "上限" in reason
future_eligible = {**future_limited, "status": "0"}
assessment = service.classify_goods_item_assessment(future_eligible, now=NOW)
availability = assessment.availability
reason = assessment.blocking_reason
assert assessment.account_eligibility == "eligible"
assert assessment.offer_state == "before"
assert availability == "not_open"
assert "未开始" in reason
@@ -83,6 +94,38 @@ def test_time_limited_goods_use_schedule_before_account_limit():
assert "已结束" in reason
def test_har_account_limit_is_independent_of_positive_global_stock():
rabbit = {
"commodityId": "20260120QYOOB_bag10",
"status": 1,
"storeStatus": 1,
"openStatus": 1,
"count": 76968,
}
assessment = service.classify_goods_item_assessment(rabbit, now=NOW)
assert assessment.account_eligibility == "limited"
assert assessment.stock_state == "in_stock"
assert assessment.availability == "limited"
def test_har_zero_global_stock_does_not_change_account_eligibility():
red_bear = {
"commodityId": "20260120QYOOB_bag395",
"status": 0,
"storeStatus": 0,
"openStatus": 1,
"count": 0,
}
assessment = service.classify_goods_item_assessment(red_bear, now=NOW)
assert assessment.account_eligibility == "eligible"
assert assessment.stock_state == "out_of_stock"
assert assessment.availability == "no_stock"
def test_sync_stores_time_window_and_next_boundary(db):
account = _account(db, "cap-window")
end_at = NOW + timedelta(minutes=5)
@@ -163,6 +206,8 @@ def test_sync_account_goods_states_writes_capabilities_and_exchange_stats(db):
assert states["A"].last_exchange_at == NOW - timedelta(days=10)
assert states["B"].availability == "limited"
assert states["C"].availability == "no_stock"
assert states["C"].account_eligibility == "eligible"
assert states["C"].stock_state == "out_of_stock"
assert states["D"].availability == "unknown"
assert states["A"].valid_until == NOW + timedelta(
seconds=service.capability_ttl_seconds("available")
@@ -295,6 +340,7 @@ def test_account_commodities_api_returns_summary_and_freshness(db):
account.id,
scope="",
availability="",
eligibility="",
limit=500,
db=db,
current=user,
+4
View File
@@ -65,6 +65,9 @@ def _state(account: Account, commodity_id: str = "C") -> AccountCommodityState:
platform="douyu",
scope="elite",
commodity_id=commodity_id,
account_eligibility="eligible",
offer_state="active",
stock_state="in_stock",
availability="available",
observed_at=NOW,
valid_until=NOW + timedelta(hours=1),
@@ -172,6 +175,7 @@ def test_global_supply_inventory_batches_value_classes_in_two_queries(db):
db.flush()
limited_state = _state(high_limited)
limited_state.availability = "limited"
limited_state.account_eligibility = "limited"
db.add_all(
[
_state(high_free),
+7 -1
View File
@@ -433,6 +433,9 @@ class TestDouyuCollabOrders:
platform="douyu",
scope="elite",
commodity_id="gift-a",
account_eligibility="eligible",
offer_state="active",
stock_state="in_stock",
availability="available",
observed_at=datetime.now(UTC),
valid_until=datetime.now(UTC) + timedelta(hours=1),
@@ -442,6 +445,9 @@ class TestDouyuCollabOrders:
platform="douyu",
scope="elite",
commodity_id="gift-a",
account_eligibility="eligible",
offer_state="active",
stock_state="in_stock",
availability="available",
observed_at=datetime.now(UTC),
valid_until=datetime.now(UTC) + timedelta(hours=1),
@@ -1927,7 +1933,7 @@ class TestDouyuCollabOrders:
assert goods[0]["status"] == "0"
assert goods[0]["open_status"] == 0
assert goods[0]["start_time"] == 1792749600
assert goods[0]["is_limited"] is True
assert goods[0]["is_limited"] is False
def test_shop_rename_cascades_and_delete_rejects_in_use(self):
shop = collab.create_shop(self.session, self.admin, name="店铺B", remark="测试")
+9 -1
View File
@@ -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 = "20261122_0114"
HEAD_REVISION = "20261122_0115"
# 迁移新增、但不声明在模型里的复合查询索引。
EXTRA_INDEXES = (
@@ -80,6 +80,14 @@ _LATER_ORM_COLUMNS = {
"douyu_external_orders": {
"succeeded_at": "DATETIME NULL",
},
"account_commodity_states": {
"account_eligibility": "VARCHAR(16) NOT NULL DEFAULT 'unknown'",
"offer_state": "VARCHAR(16) NOT NULL DEFAULT 'unknown'",
"stock_state": "VARCHAR(16) NOT NULL DEFAULT 'unknown'",
"stock_count": "BIGINT NULL",
"store_status": "INTEGER NULL",
"open_status": "INTEGER NULL",
},
}
+53 -1
View File
@@ -123,6 +123,13 @@ class _Base:
scope="elite",
commodity_id=COMMODITY,
commodity_name=INTERNAL_SKU,
account_eligibility=(
"limited" if availability == "limited" else "eligible"
),
offer_state="before" if availability == "not_open" else "active",
stock_state=(
"out_of_stock" if availability == "no_stock" else "in_stock"
),
availability=availability,
observed_at=NOW,
valid_until=NOW + timedelta(hours=6),
@@ -976,6 +983,8 @@ class TestSnapshotSync(_Base):
assert task.execution_status == "ready"
assert task.next_allocate_at is not None
assert task.state_version == 1
detail = service.get_task_detail(self.session, task.id)
assert detail["task"]["goodsName"] == INTERNAL_SKU
outbox = self.session.query(IntegrationOutbox).one()
assert outbox.payload_json["items"][0]["stateVersion"] == 1
@@ -1087,6 +1096,9 @@ class TestAccountSelection(_Base):
scope="elite",
commodity_id=COMMODITY,
commodity_name=INTERNAL_SKU,
account_eligibility="eligible",
offer_state="active",
stock_state="in_stock",
availability="available",
observed_at=NOW,
valid_until=NOW + timedelta(minutes=15),
@@ -1248,6 +1260,40 @@ class TestAccountSelection(_Base):
assert detail["task"]["exchangeStatus"] == "waiting_release"
assert detail["task"]["issueCode"] == "commodity_not_open"
def test_presale_allocates_eligible_account_even_when_official_stock_is_zero(self):
account = self.account("presale-no-stock", capability=False)
self.session.add(
AccountCommodityState(
account_id=account.id,
platform="douyu",
scope="elite",
commodity_id=COMMODITY,
commodity_name=INTERNAL_SKU,
account_eligibility="eligible",
offer_state="before",
stock_state="out_of_stock",
availability="no_stock",
observed_at=NOW,
valid_until=NOW + timedelta(hours=1),
available_from=NOW + timedelta(days=4),
available_until=NOW + timedelta(days=10),
source="storehome_sync",
)
)
task = self.task()
task.sale_mode = "presale"
self.session.commit()
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"
assert detail["task"]["exchangeStatus"] == "waiting_release"
def test_direct_sale_still_blocks_when_commodity_not_open(self):
self.account("direct-future", availability="not_open")
task = self.task()
@@ -1273,6 +1319,9 @@ class TestAccountSelection(_Base):
scope="elite",
commodity_id=COMMODITY,
commodity_name=INTERNAL_SKU,
account_eligibility="eligible",
offer_state="before",
stock_state="in_stock",
availability="not_open",
blocking_reason="限时兑换未开始",
observed_at=NOW,
@@ -1300,7 +1349,7 @@ class TestAccountSelection(_Base):
commodity_id=COMMODITY,
required_points=1280,
now=NOW,
allow_not_open=True,
allow_before_open=True,
)
] == [account.id]
@@ -1335,6 +1384,9 @@ class TestAccountSelection(_Base):
scope="elite",
commodity_id=COMMODITY,
commodity_name=INTERNAL_SKU,
account_eligibility="eligible",
offer_state="active",
stock_state="in_stock",
availability="available",
observed_at=NOW,
valid_until=NOW + timedelta(hours=1),