Files
ai-agent/backend/test_usage_burn.py
gabrielvidalandClaude Opus 5.5 cc7e83ec85 feat(usage): burn-rate speedometer on the home strip
How fast the week is being spent right now — list $ per hour over the last
60 min from the hourly plan-meter bins — against the pace that reaches the
week target at the reset. GET /api/usage/burn (usage/burn.py) for every
account with a weekTarget; BurnGauge tile beside its week gauge: needle vs
the needed pace at the top of the dial, amber + snail behind, green + flame
on pace, "need $X/h" and "→ N% at reset". Polled every minute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 12:55:42 +01:00

84 lines
3.3 KiB
Python

"""The burn rate: the last hour's spend against the pace the week target
needs (``python3 -m unittest test_usage_burn`` from ``backend/``)."""
import types
import unittest
from unittest import mock
from usage import burn, calibrate, target
H = 3600
class Rolling(unittest.TestCase):
def test_current_hour_whole_previous_prorated(self):
now = 100 * H + 15 * 60 # 15 min into hour 100
bins = {98: [50.0, 5.0], 99: [8.0, 4.0], 100: [3.0, 1.0]}
usd, units = burn.rolling(bins, now)
# hour 100 whole + 45 min of hour 99; hour 98 is outside.
self.assertAlmostEqual(usd, 3.0 + 8.0 * 0.75)
self.assertAlmostEqual(units, 1.0 + 4.0 * 0.75)
def test_on_the_hour(self):
now = 100 * H
usd, _ = burn.rolling({99: [6.0, 1.0], 100: [2.0, 1.0]}, now)
self.assertAlmostEqual(usd, 6.0) # exactly the previous hour, whole
def test_nothing_ran(self):
self.assertEqual(burn.rolling({}, 100 * H), (0.0, 0.0))
class Burn(unittest.TestCase):
def setUp(self):
self.now = 1000 * H + 30 * 60
self.bins = {999: [10.0, 4.0], 1000: [5.0, 2.0]}
cal = {"week": {"unitsPerPct": 8.0, "usdPerPct": 20.0}}
self._p = [mock.patch.object(calibrate, "meters", lambda deps, a: self.bins),
mock.patch.object(calibrate, "current", lambda deps, a: cal),
mock.patch.object(target, "of", lambda a: {"goal": 100} if a == "personal" else None)]
for p in self._p:
p.start()
def tearDown(self):
for p in self._p:
p.stop()
def _deps(self, pct=60.0, resets_in=40 * H, stale=False):
return types.SimpleNamespace(usage_poller=types.SimpleNamespace(limits=lambda: {"personal": {
"windows": {"seven_day": {"pct": pct, "resetsAt": self.now + resets_in}},
"stale": stale}}))
def test_rate_against_needed(self):
b = burn.burn(self._deps(), "personal", now=self.now)
self.assertAlmostEqual(b["usdPerHour"], 5.0 + 10.0 * 0.5)
self.assertAlmostEqual(b["unitsPerHour"], 2.0 + 4.0 * 0.5)
self.assertAlmostEqual(b["pctPerHour"], 4.0 / 8.0)
self.assertAlmostEqual(b["neededPctPerHour"], 40.0 / 40)
self.assertAlmostEqual(b["neededUsdPerHour"], 20.0)
self.assertAlmostEqual(b["projected"], 60.0 + 0.5 * 40)
self.assertFalse(b["stale"])
def test_no_target_no_rate(self):
self.assertIsNone(burn.burn(self._deps(), "work", now=self.now))
def test_target_reached_needs_nothing(self):
b = burn.burn(self._deps(pct=100.0), "personal", now=self.now)
self.assertEqual(b["neededPctPerHour"], 0.0)
self.assertEqual(b["projected"], 100.0)
def test_reset_passed(self):
b = burn.burn(self._deps(resets_in=-H), "personal", now=self.now)
self.assertEqual(b["pct"], 0.0)
self.assertIsNone(b["neededPctPerHour"])
self.assertIsNone(b["projected"])
def test_usd_per_pct_from_the_meter_when_the_fit_has_none(self):
with mock.patch.object(calibrate, "current", lambda deps, a: {"week": {"unitsPerPct": 8.0}}):
b = burn.burn(self._deps(), "personal", now=self.now)
# $ per unit over the week's bins = 15 / 6 → 20 $ per %.
self.assertAlmostEqual(b["neededUsdPerHour"], 1.0 * 8.0 * 15.0 / 6.0)
if __name__ == "__main__":
unittest.main()