"""履约需求驱动的账号商品能力刷新作业。""" from __future__ import annotations from datetime import UTC, datetime, timedelta from web.backend.models import ( Account, AccountCommodityState, AccountLease, AccountWorkItem, AutomationJob, PlatformFulfillmentTask, User, ) from web.backend.services.automation.jobs import account_capability_refresh as job from web.backend.services.automation.scheduler import JobSpec, ensure_jobs, register_job NOW = datetime.now(UTC).replace(microsecond=0) def _account(db, name: str) -> Account: row = 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, overview_queried_at=NOW - timedelta(days=1), ) db.add(row) db.flush() return row def _state( db, account: Account, *, commodity_id: str = "C", valid_until: datetime | None = None, ) -> AccountCommodityState: row = AccountCommodityState( account_id=account.id, platform="douyu", scope="elite", commodity_id=commodity_id, availability="available", observed_at=NOW - timedelta(hours=2), valid_until=valid_until, next_check_at=valid_until, ) db.add(row) return row def test_selects_only_stale_demand_accounts(db): user = User( username="cap-refresh-admin", password_hash="h", role="super_admin", is_active=True, ) db.add(user) db.flush() db.add( PlatformFulfillmentTask( source_system="order_site", source_order_id="1", source_order_item_id="1", internal_sku_key="skin", commodity_id="C", execution_status="ready", binding_status="not_started", exchange_status="not_started", paid_at=NOW, ) ) stale = _account(db, "cap-stale") fresh = _account(db, "cap-fresh") other = _account(db, "cap-other") leased = _account(db, "cap-leased") _state(db, stale, valid_until=NOW - timedelta(minutes=1)) _state(db, fresh, valid_until=NOW + timedelta(hours=1)) _state(db, other, commodity_id="OTHER", valid_until=NOW - timedelta(minutes=1)) _state(db, leased, valid_until=NOW - timedelta(minutes=1)) db.add( AccountLease( account_id=leased.id, holder_user_id=user.id, scope="elite", purpose="platform_fulfillment", status="active", ) ) 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] == [stale.id] def test_no_pending_task_means_no_refresh(db): stale = _account(db, "cap-no-demand") _state(db, stale, valid_until=NOW - timedelta(minutes=1)) db.commit() automation_job = AutomationJob( key=job.JOB_KEY, title="履约商品能力补充刷新", interval_seconds=3600, batch_size=10, concurrency=3, ) assert job.build_select_query(db, automation_job, NOW).all() == [] def test_run_enqueues_stale_accounts_without_external_query(db): db.add( PlatformFulfillmentTask( source_system="order_site", source_order_id="2", source_order_item_id="2", internal_sku_key="skin", commodity_id="C", execution_status="ready", binding_status="not_started", exchange_status="not_started", paid_at=NOW, ) ) stale = _account(db, "cap-run-stale") _state(db, stale, valid_until=NOW - timedelta(minutes=1)) db.commit() automation_job = AutomationJob( key=job.JOB_KEY, title="履约商品能力补充刷新", interval_seconds=3600, batch_size=10, concurrency=3, ) result = job.run_account_capability_refresh(db, automation_job) work = ( db.query(AccountWorkItem) .filter_by(account_id=stale.id, work_type="storehome_sync") .one() ) assert result["scanned"] == 1 assert result["success"] == 1 assert work.status == "pending" assert work.priority == 40 def test_default_enabled_job_is_created_enabled(db, monkeypatch): from web.backend.services.automation import scheduler monkeypatch.setattr(scheduler, "_REGISTRY", {}) register_job( JobSpec( key="retention-default-on", title="默认启用测试", interval_seconds=3600, batch_size=10, concurrency=1, run=lambda _db, _job: {"scanned": 0}, default_enabled=True, ) ) ensure_jobs(db) row = ( db.query(AutomationJob) .filter(AutomationJob.key == "retention-default-on") .one() ) assert row.enabled is True