feat: Anthropic API budget checker, fails open on error

This commit is contained in:
2026-04-04 14:26:55 +02:00
parent b2f38f071d
commit 02d44d30be
2 changed files with 58 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
import sys
sys.path.insert(0, 'bin')
from unittest.mock import patch
import pytest
from check_budget import is_budget_ok, get_used_usd
def test_budget_ok_when_used_less_than_limit():
with patch('check_budget.get_used_usd', return_value=2.0):
assert is_budget_ok(limit_usd=5.0) is True
def test_budget_not_ok_when_used_exceeds_limit():
with patch('check_budget.get_used_usd', return_value=5.5):
assert is_budget_ok(limit_usd=5.0) is False
def test_budget_ok_on_api_error():
"""Fail open: if we can't check the budget, assume OK to avoid blocking research."""
with patch('check_budget.get_used_usd', side_effect=Exception("network error")):
assert is_budget_ok(limit_usd=5.0) is True
def test_budget_exactly_at_limit_is_not_ok():
with patch('check_budget.get_used_usd', return_value=5.0):
assert is_budget_ok(limit_usd=5.0) is False