Files
live-hub-py/tests/test_login_budget_recommendation.py
T

174 lines
5.3 KiB
Python

"""Login budget recommendation tests."""
from __future__ import annotations
from datetime import UTC, datetime
from web.backend.models import (
Account,
AutomationJob,
LoginAttempt,
LoginQuotaSetting,
PlatformFulfillmentTask,
)
from web.backend.services.automation.jobs import ck_health
from web.backend.services.login_budget_recommendation import recommend_login_budget
def _account(
name: str,
*,
points: int = 0,
cookie: str = "acf_uid=1",
ck_check_status: str = "valid",
status: str = "active",
) -> Account:
return Account(
username=name,
password="p",
email=f"{name}@example.com",
email_password="ep",
cookie=cookie,
ck_check_status=ck_check_status,
status=status,
auto_managed=True,
points=points,
)
def test_budget_recommendation_uses_inventory_and_recent_success(db):
now = datetime.now(UTC)
high_valid = _account("budget-high-valid", points=400)
ordinary_valid = _account("budget-ordinary-valid")
high_invalid = _account(
"budget-high-invalid",
points=400,
ck_check_status="invalid",
status="invalid",
)
ordinary_invalid = _account(
"budget-ordinary-invalid",
ck_check_status="invalid",
status="invalid",
)
imported = _account(
"budget-imported",
cookie="",
ck_check_status="",
status="imported",
)
db.add_all([high_valid, ordinary_valid, high_invalid, ordinary_invalid, imported])
db.flush()
db.add_all(
[
LoginAttempt(
account_id=high_invalid.id,
category="relogin_high_value",
trigger_source="automation",
started_at=now,
finished_at=now,
status="success" if index < 18 else "failed",
failure_kind="" if index < 18 else "network",
quota_day=now.date(),
)
for index in range(20)
]
)
db.commit()
recommendation = recommend_login_budget(db, now=now)
assert recommendation["basis"]["valid_accounts"] == 2
assert recommendation["basis"]["valid_high_value"] == 1
assert recommendation["basis"]["invalid_backlog"] == 2
assert recommendation["basis"]["invalid_high_value"] == 1
assert recommendation["basis"]["imported_backlog"] == 1
assert recommendation["basis"]["recent_sample"] == 20
assert recommendation["basis"]["recent_success_rate"] == 90.0
assert recommendation["recommended"]["daily_attempt_limit"] >= 50
assert recommendation["recommended"]["login_concurrency"] >= 3
assert db.query(LoginQuotaSetting).count() == 0
def test_email_quota_failures_do_not_lower_success_rate(db):
now = datetime.now(UTC)
account = _account("budget-email-quota", ck_check_status="invalid")
db.add(account)
db.flush()
db.add_all(
[
LoginAttempt(
account_id=account.id,
category="relogin_ordinary",
trigger_source="automation",
started_at=now,
finished_at=now,
status="success",
quota_day=now.date(),
)
for _ in range(10)
]
+ [
LoginAttempt(
account_id=account.id,
category="relogin_ordinary",
trigger_source="automation",
started_at=now,
finished_at=now,
status="failed",
failure_kind="email_quota",
quota_day=now.date(),
)
for _ in range(10)
]
)
db.commit()
recommendation = recommend_login_budget(db, now=now)
assert recommendation["basis"]["recent_sample"] == 10
assert recommendation["basis"]["recent_success_rate"] == 100.0
def test_protected_inventory_is_allocated_once_within_daily_budget(db):
now = datetime.now(UTC)
protected = _account("budget-protected-valid")
db.add(protected)
db.flush()
db.add_all(
[
PlatformFulfillmentTask(
source_order_id="budget-protected-order",
source_order_item_id="budget-protected-item",
sale_mode="presale",
account_id=protected.id,
binding_status="bound",
execution_status="bound",
),
AutomationJob(
key=ck_health.JOB_KEY,
interval_seconds=6 * 3600,
batch_size=200,
concurrency=3,
),
]
)
db.commit()
recommendation = recommend_login_budget(db, now=now)
recommended = recommendation["recommended"]
allocated = (
recommended["relogin_protected_daily_limit"]
+ recommended["lease_refresh_daily_limit"]
+ recommended["relogin_high_value_daily_limit"]
+ recommended["relogin_ordinary_daily_limit"]
+ recommended["onboarding_daily_limit"]
+ recommended["manual_daily_limit"]
)
assert recommendation["basis"]["protected_valid_accounts"] == 1
assert recommendation["basis"]["protected_invalid_backlog"] == 0
assert recommended["relogin_protected_daily_limit"] >= 50
assert recommended["lease_refresh_daily_limit"] >= 50
assert allocated <= recommended["daily_attempt_limit"]