Files
ai-agent/backend/test_usage_billing.py
gabrielvidalandClaude Opus 5.5 49e027eafd feat(usage): when the Claude plan renews, and whether a smaller plan would hold
Anthropic's API gives no next-charge date, only subscriptionCreatedAt
(already read by the sidecar into /accounts → plan). usage/billing.py takes
the monthly renewal on that day and time — an estimate — and peaks each
seven_day window of this cycle and the previous one.

- /api/usage/limits → billing on Claude accounts in the totals (not the
  work seat, Orus's bill)
- Plan usage: a Billing cycle panel — next charge, the weeks' peaks against
  where the Pro / Max 5× caps sit on this plan's scale
- Home strip: a "Renews" tile (hold to bench it), French strings included
- Plan usage is now lazy (the root chunk had crept past the PWA 2 MiB cap)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 03:02:36 +01:00

69 lines
3.0 KiB
Python

"""Billing cycle estimate (usage/billing.py) — ``python3 -m unittest test_usage_billing``."""
import datetime as dt
import pathlib
import tempfile
import unittest
from usage import billing
from usage.store import UsageStore
D = 86400
def ts(s: str) -> float:
return dt.datetime.fromisoformat(s.replace("Z", "+00:00")).timestamp()
class Period(unittest.TestCase):
def test_monthly_on_the_start_day(self):
start = ts("2026-03-09T13:43:40Z")
last, nxt = billing.period(start, ts("2026-11-14T12:00:00Z"))
self.assertEqual(last, ts("2026-11-09T13:43:40Z"))
self.assertEqual(nxt, ts("2026-12-09T13:43:40Z"))
def test_before_this_months_day(self):
start = ts("2026-03-09T13:43:40Z")
last, nxt = billing.period(start, ts("2026-11-09T10:00:00Z"))
self.assertEqual(last, ts("2026-10-09T13:43:40Z"))
self.assertEqual(nxt, ts("2026-11-09T13:43:40Z"))
def test_short_months_clamp_and_year_wraps(self):
start = ts("2026-01-31T00:00:00Z")
self.assertEqual(billing.period(start, ts("2026-02-15T00:00:00Z")),
(ts("2026-01-31T00:00:00Z"), ts("2026-02-28T00:00:00Z")))
self.assertEqual(billing.period(start, ts("2026-12-31T12:00:00Z"))[1],
ts("2027-01-31T00:00:00Z"))
class Weeks(unittest.TestCase):
def test_peaks_per_window_with_jitter(self):
now = ts("2026-11-14T12:00:00Z")
r1, r2 = ts("2026-11-08T19:00:00Z"), ts("2026-11-15T19:00:00Z")
pts = [(r1 - 3 * D, 40, r1), (r1 - D, 90, r1 + 1800), (r1 - 10, 88, r1),
(r2 - 3 * D, 10, r2), (r2 - 2 * D, 51, r2 - 600),
(r1 - 9 * D, 70, r1 - 7 * D)] # ended before the cycle
weeks = billing.week_peaks(pts, ts("2026-11-09T13:43:40Z") - 7 * D, now)
self.assertEqual([(w["peak"], w["current"]) for w in weeks], [(90, False), (51, True)])
def test_billing_from_the_store(self):
with tempfile.TemporaryDirectory() as d:
store = UsageStore(str(pathlib.Path(d) / "x.db"))
now = ts("2026-11-14T12:00:00Z")
r = ts("2026-11-15T19:00:00Z")
store.add_points([("personal", "seven_day", now - D, 42.0, r, "test")])
row = {"plan": {"subscriptionCreatedAt": "2026-03-09T13:43:40.376400Z"}}
b = billing.billing(store, "personal", row, None, now)
self.assertAlmostEqual(b["nextBillingAt"], ts("2026-12-09T13:43:40.376400Z"))
self.assertEqual(b["weeks"], [{"resetsAt": r, "peak": 42.0, "current": True,
"inCycle": True}])
self.assertEqual(b["previousPeriodStart"], ts("2026-10-09T13:43:40.376400Z"))
# no live row: the start the plan tracker stored with the segment
seg = {"data": {"subscriptionCreatedAt": "2026-03-09T13:43:40Z"}}
self.assertIsNotNone(billing.billing(store, "personal", None, seg, now))
self.assertIsNone(billing.billing(store, "personal", {}, None, now))
if __name__ == "__main__":
unittest.main()