124 lines
3.7 KiB
Python
124 lines
3.7 KiB
Python
"""Login budget recommendation tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from web.backend.models import Account, LoginAttempt, LoginQuotaSetting
|
|
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
|