Coding policy for Viktor Gamov's AI agents: language-agnostic quality rules, autonomous shipping discipline, and stack defaults for JVM, Swift, TypeScript, and Python
93%
Does it follow best practices?
Impact
99%
1.47xAverage score across 10 eval scenarios
Low
Low-risk findings worth noting
"""Tests for teamlead.parsers.
Every fixture is an inline string copied from a real pane read. No clock, no
randomness, no files on disk.
"""
import os as _os
import sys as _sys
# Run as a script (`python3 tests/test_x.py`), Python puts tests/ on sys.path
# rather than the repo root, so neither `teamlead` nor `tests.fakes` would
# resolve. Under `-m unittest` from the root this is already true and the
# insert is a no-op. The consuming repo's runner executes files as scripts.
_ROOT = _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))
if _ROOT not in _sys.path:
_sys.path.insert(0, _ROOT)
import unittest
from teamlead.errors import ParseError
from teamlead.parsers import (
headroom_pct,
parse_claude_usage,
parse_codex_usage,
parse_grok_usage,
parse_usage,
)
from tests.claude_usage_fixtures import LIVE_INLINE as CLAUDE_INLINE_RESETS
CLAUDE_SAMPLE = """\
Settings Status Config Usage Stats
Session
Total cost: $0.0000
Current session
████ 8% used
Resets 12:49am (Europe/Oslo)
Current week (all models)
█ 2% used
Resets Sep 5 at 11:59pm (Europe/Oslo)
+50% weekly limits promo through Sep 13 · clau.de/cc-50-promo
Current week (Fable)
▌ 1% used
Resets Sep 5 at 11:59pm (Europe/Oslo)
"""
# A real `/usage` read from 2026-10-23: the Fable-only weekly window is spent
# while the account-wide windows are not. The bar glyphs are trimmed.
CLAUDE_FABLE_EXHAUSTED = """\
Current session
█ 2% used
Resets 3pm (Europe/Oslo)
Current week (all models)
████████████████████████████████████████ 79% used
Resets Oct 20 at 9am (Europe/Oslo)
Current week (Fable)
██████████████████████████████████████████████ 100% used
Resets Oct 20 at 9am (Europe/Oslo)
"""
# Narrow-pane wrap: the header ends at the separator and `Resets` starts
# the next line. Synthetic; no live capture of this shape exists.
CLAUDE_NARROW_HEADER = """\
Current session ·
Resets 5pm (America/New_York)
██▋
5% used
Current week (all models) ·
Resets Nov 2 at 10am
(America/New_York)
████████████████████▋
39% used
"""
CODEX_SAMPLE = """\
│ Account: jbaruch@sadogursky.com (Pro) │
│ Weekly limit: [█████████████████░░░] 87% left (resets 17:26 on 7 Sep) │
│ GPT-5.3-Codex-Spark limit: │
│ 5h limit: [████████████████████] 100% left (resets 02:19 on 2 Sep) │
│ Weekly limit: [████████████████████] 100% left (resets 21:19 on 8 Sep) │
"""
# Grok's inline shape.
GROK_SAMPLE = """\
Session usage: no model calls yet in this session.
Weekly limit: 0%
Next reset: September 6, 12:55
Credits: $16.42
Auto topup: $20
"""
# Grok's dialog shape, verbatim from `herdr agent read grok --source visible
# --lines 60` after a restart: a boxed modal on the alternate screen, with the
# surrounding page text still visible on the closing border row.
GROK_DIALOG_SAMPLE = """\
┌──────────────────────────────────────────────────────────────────────────────────────── [✗] ─┐
│ Context usage Usage limit Session info │
│──────────────────────────────────────────────────────────────────────────────────────────────│
│ Weekly limit (X Premium+) │
│ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1% │
│ Resets: September 6, 12:55 │
│ Credits: $16.42 │
│ Tab switch | ↑/↓ scroll | Esc close │
Help improve Grok └──────────────────────────────────────────────────────────────────────────────────────────────┘ [Opt out] [Opt in]
"""
# The same modal painted OVER the transcript, verbatim from a live
# `--source visible --lines 80` read. Every row carries transcript text to the
# left of the box's first vertical, and some carry more to the right of its
# last one.
GROK_OVERLAID_SAMPLE = """\
/usage is credit/bill│ Context usage Usage limit Session info │ 10:36 PM
│──────────────────────────────────────────────────────────────────────────────────────────────│
◈ Read 4 files, Searc│ Weekly limit (X Premium+) │
◆ Thought for 4.2s │ ██░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 7% │
│ Resets: September 6, 12:55 │
Turn cancelled by use│ │
│ Credits: $16.42 │
┃ We shipped the tester│ Input tokens: 7,616,129 (7,387,392 cached) │an independent
"""
class ClaudeParserTest(unittest.TestCase):
def test_captures_every_window_in_order(self):
result = parse_claude_usage(CLAUDE_SAMPLE)
self.assertEqual(
list(result["windows"]),
["Current session", "Current week (all models)", "Current week (Fable)"],
)
def test_session_window_numbers(self):
window = parse_claude_usage(CLAUDE_SAMPLE)["windows"]["Current session"]
self.assertEqual(window["used_pct"], 8.0)
self.assertEqual(window["remaining_pct"], 92.0)
self.assertEqual(window["resets"], "12:49am (Europe/Oslo)")
def test_promo_line_is_not_read_as_a_percentage(self):
# "+50% weekly limits promo" sits between two windows and must not
# leak into either of them.
windows = parse_claude_usage(CLAUDE_SAMPLE)["windows"]
self.assertEqual(windows["Current week (all models)"]["used_pct"], 2.0)
self.assertEqual(windows["Current week (Fable)"]["used_pct"], 1.0)
def test_per_model_week_window_is_keyed_by_its_label(self):
windows = parse_claude_usage(CLAUDE_SAMPLE)["windows"]
self.assertEqual(windows["Current week (Fable)"]["remaining_pct"], 99.0)
def test_claude_reports_no_credits(self):
self.assertIsNone(parse_claude_usage(CLAUDE_SAMPLE)["credits"])
def test_window_without_a_reset_line_yields_none(self):
text = "Current session\n █ 8% used\nCurrent week (all models)\n █ 2% used\n"
windows = parse_claude_usage(text)["windows"]
self.assertIsNone(windows["Current session"]["resets"])
self.assertIsNone(windows["Current week (all models)"]["resets"])
def test_header_without_a_percentage_is_rejected(self):
# A header that closes with no percentage is a pane that cut the
# dialog, so the read fails instead of dropping that window.
text = "Current session\nCurrent week (all models)\n █ 2% used\n"
with self.assertRaises(ParseError) as caught:
parse_claude_usage(text)
message = str(caught.exception)
self.assertIn("Current session", message)
self.assertIn("taller pane", message)
def test_zero_and_hundred_percent(self):
text = (
"Current session\n 0% used\n Resets 1:00am (UTC)\n"
"Current week (all models)\n 100% used\n Resets Sep 5 at 11:59pm (UTC)\n"
)
windows = parse_claude_usage(text)["windows"]
self.assertEqual(windows["Current session"]["remaining_pct"], 100.0)
self.assertEqual(windows["Current week (all models)"]["remaining_pct"], 0.0)
def test_fractional_percentage(self):
text = "Current session\n 8.5% used\n"
window = parse_claude_usage(text)["windows"]["Current session"]
self.assertEqual(window["used_pct"], 8.5)
self.assertEqual(window["remaining_pct"], 91.5)
def test_empty_text_raises_parse_error(self):
with self.assertRaises(ParseError):
parse_claude_usage("")
def test_dialog_not_open_raises_parse_error(self):
with self.assertRaises(ParseError):
parse_claude_usage("❯ \n ~/Projects/foo main\n")
def test_inline_reset_layout_keeps_labels_percentages_and_timezones(self):
windows = parse_claude_usage(CLAUDE_INLINE_RESETS)["windows"]
self.assertEqual(
list(windows),
["Current session", "Current week (all models)", "Current week (Fable)"],
)
self.assertEqual(windows["Current session"]["used_pct"], 5.0)
self.assertEqual(windows["Current session"]["remaining_pct"], 95.0)
self.assertEqual(windows["Current session"]["resets"], "5pm (America/New_York)")
self.assertEqual(windows["Current week (all models)"]["used_pct"], 39.0)
self.assertEqual(windows["Current week (all models)"]["remaining_pct"], 61.0)
self.assertEqual(
windows["Current week (all models)"]["resets"],
"Nov 2 at 10am (America/New_York)",
)
self.assertEqual(windows["Current week (Fable)"]["used_pct"], 36.0)
self.assertEqual(windows["Current week (Fable)"]["remaining_pct"], 64.0)
self.assertEqual(
windows["Current week (Fable)"]["resets"],
"Nov 2 at 10am (America/New_York)",
)
# Account-wide windows still bind an Opus seat. The Fable week is
# looser than the account week in this capture, so it does not
# change that minimum.
self.assertEqual(headroom_pct(windows, "Opus 5.5"), 61.0)
self.assertEqual(headroom_pct(windows, "Fable 5.1"), 61.0)
def test_narrow_header_wrap_keeps_the_reset_on_the_next_line(self):
windows = parse_claude_usage(CLAUDE_NARROW_HEADER)["windows"]
self.assertEqual(windows["Current session"]["used_pct"], 5.0)
self.assertEqual(windows["Current session"]["resets"], "5pm (America/New_York)")
self.assertEqual(windows["Current week (all models)"]["used_pct"], 39.0)
self.assertEqual(
windows["Current week (all models)"]["resets"],
"Nov 2 at 10am (America/New_York)",
)
def test_a_parenthetical_without_a_slash_is_not_a_wrapped_timezone(self):
text = (
"Current session · Resets Nov 2 at 10am\n"
"(promo note)\n"
"5% used\n"
)
window = parse_claude_usage(text)["windows"]["Current session"]
self.assertEqual(window["used_pct"], 5.0)
self.assertEqual(window["resets"], "Nov 2 at 10am")
def test_header_ending_at_resets_keeps_the_reset_on_the_next_line(self):
text = (
"Current week (all models) · Resets\n"
"Nov 2 at 10am (America/New_York)\n"
"████████████████████▋\n"
"39% used\n"
)
window = parse_claude_usage(text)["windows"]["Current week (all models)"]
self.assertEqual(window["used_pct"], 39.0)
self.assertEqual(window["resets"], "Nov 2 at 10am (America/New_York)")
def test_a_second_percentage_under_the_same_label_is_rejected(self):
text = (
"Current session\n"
"5% used\n"
"Resets 5pm (America/New_York)\n"
"Current week (all\n"
"models) · Resets Nov 2 at 10am (America/New_York)\n"
"39% used\n"
)
with self.assertRaises(ParseError) as caught:
parse_claude_usage(text)
message = str(caught.exception)
self.assertIn("second", message)
self.assertIn("39% used", message)
self.assertIn("Current session", message)
self.assertIn("Current week (all", message)
def test_credit_sections_in_the_inline_layout_are_not_windows(self):
text = (
"Current session · Resets 5pm (America/New_York)\n"
"██▋\n"
"5% used\n"
"Current week (all models) · Resets Nov 2 at 10am\n"
"(America/New_York)\n"
"████████████████████▋\n"
"39% used\n"
"Current week (Fable) · Resets Nov 2 at 10am\n"
"(America/New_York)\n"
"███████████████████\n"
"36% used\n"
"Claude Code and Cowork credit · Resets Dec 1 (America/New_York)\n"
"████\n"
"12% used\n"
"Credit · Resets Dec 1 (America/New_York)\n"
"██\n"
"4% used\n"
"Usage credits · $5.00 / $50.00 spent · Resets December 1\n"
"██\n"
"10% used\n"
)
windows = parse_claude_usage(text)["windows"]
self.assertEqual(
{label: window["used_pct"] for label, window in windows.items()},
{
"Current session": 5.0,
"Current week (all models)": 39.0,
"Current week (Fable)": 36.0,
},
)
self.assertEqual(headroom_pct(windows, "Opus 5.5"), 61.0)
def test_credit_sections_in_the_stacked_layout_are_not_windows(self):
text = (
"Current session\n"
"████ 5% used\n"
"Resets 5pm (America/New_York)\n"
"Current week (all models)\n"
"████████████████████▋ 39% used\n"
"Resets Nov 2 at 10am (America/New_York)\n"
"Current week (Fable)\n"
"███████████████████ 36% used\n"
"Resets Nov 2 at 10am (America/New_York)\n"
"Claude Code and Cowork credit\n"
"████ 12% used\n"
"Resets Dec 1 (America/New_York)\n"
"Credit\n"
"██ 4% used\n"
"Resets Dec 1 (America/New_York)\n"
"Usage credits\n"
"██ 10% used\n"
"$5.00 / $50.00 spent · Resets December 1\n"
)
windows = parse_claude_usage(text)["windows"]
self.assertEqual(
{label: window["used_pct"] for label, window in windows.items()},
{
"Current session": 5.0,
"Current week (all models)": 39.0,
"Current week (Fable)": 36.0,
},
)
self.assertEqual(headroom_pct(windows, "Opus 5.5"), 61.0)
def test_wrapped_utc_timezone_is_kept(self):
text = "Current session · Resets Nov 2 at 10am\n(UTC)\n5% used\n"
window = parse_claude_usage(text)["windows"]["Current session"]
self.assertEqual(window["used_pct"], 5.0)
self.assertEqual(window["resets"], "Nov 2 at 10am (UTC)")
def test_mid_phrase_reset_wrap_keeps_the_full_reset(self):
text = "Current week (all models) · Resets Nov 2\nat 10am (America/New_York)\n39% used\n"
window = parse_claude_usage(text)["windows"]["Current week (all models)"]
self.assertEqual(window["used_pct"], 39.0)
self.assertEqual(window["resets"], "Nov 2 at 10am (America/New_York)")
def test_percent_and_reset_without_a_window_header_are_rejected(self):
text = (
"Settings Status Config Usage Stats\n"
"██▋\n"
"5% used\n"
"Resets 5pm (America/New_York)\n"
)
with self.assertRaises(ParseError) as caught:
parse_claude_usage(text)
self.assertIn("No usage windows found", str(caught.exception))
class CodexParserTest(unittest.TestCase):
def test_primary_and_secondary_model_windows(self):
windows = parse_codex_usage(CODEX_SAMPLE)["windows"]
self.assertEqual(
sorted(windows),
[
"GPT-5.3-Codex-Spark 5h limit",
"GPT-5.3-Codex-Spark Weekly limit",
"Weekly limit",
],
)
def test_percent_left_is_remaining_not_used(self):
window = parse_codex_usage(CODEX_SAMPLE)["windows"]["Weekly limit"]
self.assertEqual(window["remaining_pct"], 87.0)
self.assertEqual(window["used_pct"], 13.0)
def test_secondary_block_is_not_attributed_to_the_primary_model(self):
windows = parse_codex_usage(CODEX_SAMPLE)["windows"]
# The second "Weekly limit" row is the Spark model's, so the primary
# weekly number must stay 87, not be overwritten by 100.
self.assertEqual(windows["Weekly limit"]["remaining_pct"], 87.0)
self.assertEqual(windows["GPT-5.3-Codex-Spark Weekly limit"]["remaining_pct"], 100.0)
def test_reset_text_is_kept_verbatim(self):
windows = parse_codex_usage(CODEX_SAMPLE)["windows"]
self.assertEqual(windows["Weekly limit"]["resets"], "17:26 on 7 Sep")
self.assertEqual(windows["GPT-5.3-Codex-Spark 5h limit"]["resets"], "02:19 on 2 Sep")
def test_last_status_block_wins(self):
stale = CODEX_SAMPLE.replace("87% left", "40% left")
text = stale + "some interleaved agent output\n" + CODEX_SAMPLE
windows = parse_codex_usage(text)["windows"]
self.assertEqual(windows["Weekly limit"]["remaining_pct"], 87.0)
def test_block_without_an_account_row_still_parses(self):
text = "│ Weekly limit: [███] 55% left (resets 17:26 on 7 Sep) │\n"
windows = parse_codex_usage(text)["windows"]
self.assertEqual(windows["Weekly limit"]["remaining_pct"], 55.0)
def test_row_without_a_bar_still_parses(self):
text = "Account: a@b (Pro)\nWeekly limit: 12% left (resets 17:26 on 7 Sep)\n"
windows = parse_codex_usage(text)["windows"]
self.assertEqual(windows["Weekly limit"]["remaining_pct"], 12.0)
def test_row_without_a_reset_clause_yields_none(self):
text = "Account: a@b (Pro)\nWeekly limit: [██] 12% left\n"
self.assertIsNone(parse_codex_usage(text)["windows"]["Weekly limit"]["resets"])
def test_zero_and_hundred_percent_left(self):
text = (
"Account: a@b (Pro)\n"
"5h limit: [░░░] 0% left (resets 02:19 on 2 Sep)\n"
"Weekly limit: [███] 100% left (resets 21:19 on 8 Sep)\n"
)
windows = parse_codex_usage(text)["windows"]
self.assertEqual(windows["5h limit"]["remaining_pct"], 0.0)
self.assertEqual(windows["5h limit"]["used_pct"], 100.0)
self.assertEqual(windows["Weekly limit"]["used_pct"], 0.0)
def test_missing_rows_raise_parse_error(self):
with self.assertRaises(ParseError):
parse_codex_usage("Account: a@b (Pro)\nnothing useful here\n")
def test_empty_text_raises_parse_error(self):
with self.assertRaises(ParseError):
parse_codex_usage("")
class GrokParserTest(unittest.TestCase):
def test_weekly_limit_is_percent_used(self):
window = parse_grok_usage(GROK_SAMPLE)["windows"]["Weekly limit"]
self.assertEqual(window["used_pct"], 0.0)
self.assertEqual(window["remaining_pct"], 100.0)
def test_next_reset_is_attached_to_the_weekly_window(self):
window = parse_grok_usage(GROK_SAMPLE)["windows"]["Weekly limit"]
self.assertEqual(window["resets"], "September 6, 12:55")
def test_credits_are_captured_as_a_number(self):
self.assertEqual(parse_grok_usage(GROK_SAMPLE)["credits"], 16.42)
def test_last_report_wins(self):
text = GROK_SAMPLE + "\n" + GROK_SAMPLE.replace("Weekly limit: 0%", "Weekly limit: 73%")
result = parse_grok_usage(text)
self.assertEqual(result["windows"]["Weekly limit"]["used_pct"], 73.0)
def test_reset_of_an_earlier_report_does_not_leak_forward(self):
first = "Weekly limit: 10%\nNext reset: September 1, 09:00\n"
second = "Weekly limit: 20%\n"
window = parse_grok_usage(first + second)["windows"]["Weekly limit"]
self.assertEqual(window["used_pct"], 20.0)
self.assertIsNone(window["resets"])
def test_missing_credits_line_yields_none(self):
self.assertIsNone(parse_grok_usage("Weekly limit: 5%\n")["credits"])
def test_hundred_percent_used(self):
window = parse_grok_usage("Weekly limit: 100%\n")["windows"]["Weekly limit"]
self.assertEqual(window["remaining_pct"], 0.0)
def test_fractional_percent_and_credits(self):
result = parse_grok_usage("Weekly limit: 12.5%\nCredits: $0.01\n")
self.assertEqual(result["windows"]["Weekly limit"]["remaining_pct"], 87.5)
self.assertEqual(result["credits"], 0.01)
def test_missing_weekly_line_raises_parse_error(self):
with self.assertRaises(ParseError):
parse_grok_usage("Session usage: no model calls yet in this session.\n")
class GrokDialogParserTest(unittest.TestCase):
"""Grok renders `/usage` as a modal after a restart; both shapes must read."""
def test_dialog_percentage_is_percent_used(self):
window = parse_grok_usage(GROK_DIALOG_SAMPLE)["windows"]["Weekly limit"]
self.assertEqual(window["used_pct"], 1.0)
self.assertEqual(window["remaining_pct"], 99.0)
def test_dialog_resets_row_is_attached_to_the_window(self):
window = parse_grok_usage(GROK_DIALOG_SAMPLE)["windows"]["Weekly limit"]
self.assertEqual(window["resets"], "September 6, 12:55")
def test_dialog_credits_are_captured(self):
self.assertEqual(parse_grok_usage(GROK_DIALOG_SAMPLE)["credits"], 16.42)
def test_dialog_plan_name_is_captured_as_informational(self):
self.assertEqual(parse_grok_usage(GROK_DIALOG_SAMPLE)["plan"], "X Premium+")
def test_dialog_window_is_keyed_the_same_as_the_inline_one(self):
# Same key either way, so headroom and the planner never care which
# shape grok happened to render.
self.assertEqual(
list(parse_grok_usage(GROK_DIALOG_SAMPLE)["windows"]),
list(parse_grok_usage(GROK_SAMPLE)["windows"]),
)
def test_inline_shape_reports_no_plan(self):
self.assertIsNone(parse_grok_usage(GROK_SAMPLE)["plan"])
def test_box_borders_do_not_defeat_matching(self):
# The chrome row carrying "Esc close" and the closing border row with
# page text on it must both be ignored.
result = parse_grok_usage(GROK_DIALOG_SAMPLE)
self.assertEqual(len(result["windows"]), 1)
def test_dialog_without_a_plan_name_still_parses(self):
text = "│ Weekly limit │\n│ ░░░░ 7% │\n│ Resets: September 6, 12:55 │\n"
result = parse_grok_usage(text)
self.assertEqual(result["windows"]["Weekly limit"]["used_pct"], 7.0)
self.assertIsNone(result["plan"])
def test_dialog_zero_and_hundred_percent(self):
zero = "│ Weekly limit (Pro) │\n│ ░░░░░░░░ 0% │\n"
full = "│ Weekly limit (Pro) │\n│ ████████ 100% │\n"
self.assertEqual(parse_grok_usage(zero)["windows"]["Weekly limit"]["remaining_pct"], 100.0)
self.assertEqual(parse_grok_usage(full)["windows"]["Weekly limit"]["remaining_pct"], 0.0)
def test_dialog_fractional_percentage(self):
text = "│ Weekly limit (Pro) │\n│ ▒▒▒ 12.5% │\n"
self.assertEqual(parse_grok_usage(text)["windows"]["Weekly limit"]["remaining_pct"], 87.5)
def test_label_with_no_percentage_row_after_it_is_dropped(self):
text = "│ Weekly limit (Pro) │\n│ Resets: September 6, 12:55 │\n"
with self.assertRaises(ParseError):
parse_grok_usage(text)
def test_a_later_dialog_supersedes_an_earlier_one(self):
stale = GROK_DIALOG_SAMPLE.replace(" 1%", " 44%")
result = parse_grok_usage(stale + GROK_DIALOG_SAMPLE)
self.assertEqual(result["windows"]["Weekly limit"]["used_pct"], 1.0)
def test_a_dialog_after_an_inline_report_supersedes_it(self):
result = parse_grok_usage(GROK_SAMPLE + GROK_DIALOG_SAMPLE)
self.assertEqual(result["windows"]["Weekly limit"]["used_pct"], 1.0)
self.assertEqual(result["plan"], "X Premium+")
def test_an_inline_report_after_a_dialog_supersedes_it_and_clears_the_plan(self):
result = parse_grok_usage(GROK_DIALOG_SAMPLE + GROK_SAMPLE)
self.assertEqual(result["windows"]["Weekly limit"]["used_pct"], 0.0)
self.assertIsNone(result["plan"])
def test_a_screen_with_neither_shape_stays_a_loud_parse_error(self):
text = (
" ╭──────────────────────────────────╮\n"
" │ ❯ │\n"
" ╰──────────────────────────────────╯\n"
" Shift+Tab:mode │ Ctrl+.:shortcuts\n"
)
with self.assertRaises(ParseError) as caught:
parse_grok_usage(text)
self.assertIn("--source visible", str(caught.exception))
def test_a_stray_percentage_elsewhere_on_screen_is_not_a_reading(self):
with self.assertRaises(ParseError):
parse_grok_usage("Coverage rose to 91% this week.\nBuild green.\n")
class GrokOverlaidDialogTest(unittest.TestCase):
"""The modal is painted over the transcript, not into a clear screen.
Stripping frame characters from the ends of a row leaves the transcript
text attached to it. Only what lies between the first and last vertical is
the dialog.
"""
def test_the_percentage_comes_from_the_box_not_the_transcript(self):
window = parse_grok_usage(GROK_OVERLAID_SAMPLE)["windows"]["Weekly limit"]
self.assertEqual(window["used_pct"], 7.0)
self.assertEqual(window["remaining_pct"], 93.0)
def test_the_plan_name_survives_the_transcript_prefix(self):
self.assertEqual(parse_grok_usage(GROK_OVERLAID_SAMPLE)["plan"], "X Premium+")
def test_the_reset_row_is_attached(self):
window = parse_grok_usage(GROK_OVERLAID_SAMPLE)["windows"]["Weekly limit"]
self.assertEqual(window["resets"], "September 6, 12:55")
def test_credits_come_from_the_box(self):
self.assertEqual(parse_grok_usage(GROK_OVERLAID_SAMPLE)["credits"], 16.42)
def test_exactly_one_window_is_found(self):
# The transcript to either side must contribute nothing.
self.assertEqual(len(parse_grok_usage(GROK_OVERLAID_SAMPLE)["windows"]), 1)
def test_text_to_the_right_of_the_box_is_ignored(self):
# The last row ends "│an independent" outside the box.
result = parse_grok_usage(GROK_OVERLAID_SAMPLE)
self.assertEqual(result["windows"]["Weekly limit"]["used_pct"], 7.0)
def test_a_transcript_percentage_left_of_the_box_is_not_read(self):
text = GROK_OVERLAID_SAMPLE.replace(
" ◆ Thought for 4.2s │", " we were at 99% capacity │"
)
self.assertEqual(parse_grok_usage(text)["windows"]["Weekly limit"]["used_pct"], 7.0)
def test_a_row_whose_right_edge_ran_off_the_viewport_still_reads(self):
text = (
" transcript here │ Weekly limit (Pro)\n"
" more transcript │ ███ 4%\n"
)
result = parse_grok_usage(text)
self.assertEqual(result["windows"]["Weekly limit"]["used_pct"], 4.0)
self.assertEqual(result["plan"], "Pro")
def test_the_inline_format_still_parses_with_no_box_at_all(self):
self.assertEqual(parse_grok_usage(GROK_SAMPLE)["windows"]["Weekly limit"]["used_pct"], 0.0)
def test_a_clean_dialog_with_no_overlay_still_parses(self):
self.assertEqual(
parse_grok_usage(GROK_DIALOG_SAMPLE)["windows"]["Weekly limit"]["used_pct"], 1.0
)
class DispatchTest(unittest.TestCase):
def test_dispatch_picks_the_right_parser(self):
self.assertEqual(
parse_usage("codex", CODEX_SAMPLE)["windows"]["Weekly limit"]["remaining_pct"],
87.0,
)
self.assertEqual(parse_usage("grok", GROK_SAMPLE)["credits"], 16.42)
self.assertEqual(parse_usage("grok", GROK_DIALOG_SAMPLE)["plan"], "X Premium+")
self.assertIn("Current session", parse_usage("claude", CLAUDE_SAMPLE)["windows"])
def test_unknown_kind_raises_parse_error_naming_the_supported_kinds(self):
with self.assertRaises(ParseError) as caught:
parse_usage("gemini", "whatever")
self.assertIn("claude", str(caught.exception))
class HeadroomTest(unittest.TestCase):
def test_headroom_is_the_smallest_remaining_window(self):
windows = parse_claude_usage(CLAUDE_SAMPLE)["windows"]
self.assertEqual(headroom_pct(windows), 92.0)
def test_headroom_across_codex_windows(self):
windows = parse_codex_usage(CODEX_SAMPLE)["windows"]
self.assertEqual(headroom_pct(windows), 87.0)
def test_no_windows_yields_none(self):
self.assertIsNone(headroom_pct({}))
self.assertIsNone(headroom_pct(None))
class PerModelWindowHeadroomTest(unittest.TestCase):
"""A `Current week (<Model>)` window binds only a seat running that model.
`Current session` and `Current week (all models)` bind every seat on the
account. A seat whose model is unknown keeps every window, the
conservative minimum.
"""
def setUp(self):
self.windows = parse_claude_usage(CLAUDE_FABLE_EXHAUSTED)["windows"]
def test_the_real_snapshot_parses_three_windows(self):
self.assertEqual(
{label: window["remaining_pct"] for label, window in self.windows.items()},
{
"Current session": 98.0,
"Current week (all models)": 21.0,
"Current week (Fable)": 0.0,
},
)
def test_an_opus_seat_is_bound_by_the_account_wide_windows_only(self):
self.assertEqual(headroom_pct(self.windows, "Opus 5.5"), 21.0)
def test_a_pinned_model_id_matches_the_same_way(self):
self.assertEqual(headroom_pct(self.windows, "claude-opus-5-5"), 21.0)
def test_a_fable_seat_is_bound_by_the_fable_window(self):
self.assertEqual(headroom_pct(self.windows, "Fable 5.1"), 0.0)
def test_the_family_name_matches_case_insensitively(self):
self.assertEqual(headroom_pct(self.windows, "claude-FABLE-5-1"), 0.0)
def test_a_seat_with_no_model_keeps_every_window(self):
self.assertEqual(headroom_pct(self.windows), 0.0)
self.assertEqual(headroom_pct(self.windows, ""), 0.0)
def test_a_word_containing_the_family_is_not_a_match(self):
# Matching is by whole word, so "Fabled" never runs as Fable.
self.assertEqual(headroom_pct(self.windows, "Fabled 1"), 21.0)
def test_codex_windows_ignore_the_model(self):
windows = parse_codex_usage(CODEX_SAMPLE)["windows"]
self.assertEqual(headroom_pct(windows, "gpt-5.5"), 87.0)
if __name__ == "__main__":
unittest.main().tessl-plugin
hooks
rules
skills
herdr-standup
herdr-teamlead
references
teamlead
templates
tests
onboard-repo
release
tests