The platform for continuously improving AI agents.
from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
import pytest
from factories import make_project, make_user
from overbae.models import BillingTelemetry
from overbae.services import provider_check
pytestmark = [pytest.mark.django_db, pytest.mark.covers("ledger")]
def charge(project, usd, resource, when):
return BillingTelemetry.objects.create(
user=project.owner,
project=project,
amount=-usd,
service="evaluation",
resource=resource,
ended_at=when,
)
def test_the_daily_check_alerts_only_when_ledger_and_bills_differ_by_more_than_two_percent(
fake_modal, fake_llm, settings, caplog
):
settings.MODAL_ENVIRONMENT = "main"
project = make_project(member=make_user())
yesterday = datetime.now(UTC) - timedelta(days=1)
charge(project, 10, "modal H100 x1, 8 CPU, 64 GiB", yesterday)
charge(project, 5, "openrouter m", yesterday)
fake_modal.billing_items = [
SimpleNamespace(cost=10.1, environment_name="main"),
SimpleNamespace(cost=900, environment_name="other-environment"),
]
fake_llm.key_usage_monthly = 5
before = list(BillingTelemetry.objects.values())
results = provider_check.check()
assert [result["within_tolerance"] for result in results] == [True, True]
assert "differs from the provider bill" not in caplog.text
fake_llm.key_usage_monthly = 6
(_, openrouter) = provider_check.check()
assert openrouter["within_tolerance"] is False
assert "differs from the provider bill by more than 2%" in caplog.text
assert list(BillingTelemetry.objects.values()) == before