CtrlK
BlogDocsLog inGet started
Tessl Logo

jbaruch/coding-policy

General-purpose coding policy for Baruch's AI agents

74

Quality

93%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide
SecuritybySnyk

Medium

Suggest reviewing before use

Overview
Quality
Evals
Security
Files

test_parsers.pyskills/herdr-foreman/tests/

"""Tests for foreman.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 `foreman` 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 foreman.errors import ParseError
from foreman.parsers import (
    headroom_pct,
    parse_claude_usage,
    parse_codex_usage,
    parse_grok_usage,
    parse_usage,
)

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)
"""

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_inline_reset_headings_keep_labels_percent_used_and_reset_text(self):
        text = (
            "Current session · Resets 6:30pm (America/Chicago)\n▍ 1% used\n"
            "Current week (all models) · Resets Sep 5 at 5pm (America/Chicago)\n██ 96% used\n"
        )
        result = parse_claude_usage(text)
        self.assertEqual(result, {"windows": {
            "Current session": {"used_pct": 1.0, "remaining_pct": 99.0,
                "resets": "6:30pm (America/Chicago)"},
            "Current week (all models)": {"used_pct": 96.0, "remaining_pct": 4.0,
                "resets": "Sep 5 at 5pm (America/Chicago)"}}, "credits": None, "plan": None})
        self.assertEqual(headroom_pct(result["windows"]), 4.0)

    def test_narrow_reset_and_bar_wrapping_preserves_available_metadata(self):
        text = (
            "Current session · Resets 6:30pm\n(America/Chicago)\n▍\n\n1% used\n\n"
            "Current week (all models) · Resets Sep 5 at\n5pm (America/Chicago)\n████\n██\n96% used\n"
            "What's contributing to your limits usage?\nOther sessions\n72% used\n"
        )
        windows = parse_claude_usage(text)["windows"]
        self.assertEqual(windows["Current session"]["resets"], "6:30pm (America/Chicago)")
        self.assertEqual(windows["Current week (all models)"]["resets"], "Sep 5 at 5pm (America/Chicago)")
        self.assertEqual(windows["Current week (all models)"]["remaining_pct"], 4.0)

    def test_mixed_old_new_and_per_model_windows_keep_decimal_associations(self):
        text = (
            "Unrelated 99% used\nCurrent session\n▍ 1.5% used\nResets 6:30pm (UTC)\n"
            "Current week (all models) · Resets Sep 5 at 5pm (UTC)\n96.25% used\n"
            "Current week (Fable) · Resets Sep 5 at 5pm (UTC)\n20% used\n"
        )
        result = parse_claude_usage(text)
        self.assertEqual(result["windows"]["Current session"]["used_pct"], 1.5)
        self.assertEqual(result["windows"]["Current week (Fable)"]["remaining_pct"], 80.0)
        self.assertEqual(headroom_pct(result["windows"]), 3.75)

    def test_false_header_prefixes_and_suffixes_do_not_establish_windows(self):
        for header in ("Current session forecast", "Current session · unrelated text",
                "Current week (all models)foo", "This is Current session",
                "Current week (all models) · unsupported suffix", "Current session Resets 5pm"):
            with self.subTest(header=header):
                with self.assertRaises(ParseError):
                    parse_claude_usage(header + "\n1% used\n")

    def test_invalid_next_header_cannot_assign_its_percentage_to_prior_window(self):
        with self.assertRaises(ParseError):
            parse_claude_usage("Current session\nCurrent week (all models)foo\n96% used\n")

    def test_unrelated_prose_percentages_are_not_window_usage(self):
        for row in ("Other clients show 50% used", "What's contributing to your limits usage?\n50% used"):
            with self.subTest(row=row):
                with self.assertRaises(ParseError):
                    parse_claude_usage("Current session · Resets 6:30pm (UTC)\n" + row + "\n")

    def test_wrapped_unknown_prose_ends_the_window_before_unrelated_percentages(self):
        for prose in ("Other clients show", "What's contributing to your", "Unrecognized section"):
            with self.subTest(prose=prose), self.assertRaises(ParseError):
                parse_claude_usage("Current session · Resets 6:30pm (UTC)\n" + prose + "\n50% used\n")

    def test_unknown_boundary_preserves_complete_windows_and_accepts_next_header(self):
        text = ("Current session\n8% used\nResets 6:30pm (UTC)\n"
                "Unknown wrapped section\n50% used\n"
                "Current week (all models)\n████\n\n2% used\n")
        windows = parse_claude_usage(text)["windows"]
        self.assertEqual(windows["Current session"]["used_pct"], 8.0)
        self.assertEqual(windows["Current session"]["resets"], "6:30pm (UTC)")
        self.assertEqual(windows["Current week (all models)"]["used_pct"], 2.0)

    def test_reset_continuation_stops_at_blank_bar_percentage_or_section(self):
        for boundary in ("", "████", "1% used", "What's contributing to your limits usage?", "Unrelated UI section"):
            with self.subTest(boundary=boundary):
                text = "Current session · Resets 6:30pm\n" + boundary + "\n(UTC)\n1% used\n"
                text += "Current week (all models) · Resets Sep 5 at 5pm (UTC)\n96% used\n"
                windows = parse_claude_usage(text)["windows"]
                if "Current session" in windows:
                    self.assertEqual(windows["Current session"]["resets"], "6:30pm")
                self.assertEqual(windows["Current week (all models)"]["resets"], "Sep 5 at 5pm (UTC)")

    def test_wrapped_reset_cannot_consume_another_window(self):
        text = "Current session · Resets 6:30pm\n1% used\nCurrent week (all models) · Resets 5pm (UTC)\n96% used\n"
        windows = parse_claude_usage(text)["windows"]
        self.assertEqual(windows["Current session"]["resets"], "6:30pm")
        self.assertEqual(windows["Current week (all models)"]["used_pct"], 96.0)

    def test_truncated_or_missing_inline_metadata_is_not_invented(self):
        for metadata, expected in (("", None), (" Sep", "Sep"), (" Sep 5 at", "Sep 5 at")):
            with self.subTest(metadata=metadata):
                result = parse_claude_usage("Current session · Resets" + metadata + "\n1% used\n")
                self.assertEqual(result["windows"]["Current session"]["resets"], expected)

    def test_claude_out_of_range_percentages_are_refused_in_both_layouts(self):
        for header in ("Current session", "Current session · Resets 5pm (UTC)"):
            for value in ("101", "100.000001", "-1"):
                with self.subTest(header=header, value=value):
                    with self.assertRaisesRegex(ParseError, "outside 0..100"):
                        parse_claude_usage(header + "\n" + value + "% used\n")

    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_dropped(self):
        text = "Current session\nCurrent week (all models)\n  █ 2% used\n"
        windows = parse_claude_usage(text)["windows"]
        self.assertEqual(list(windows), ["Current week (all models)"])

    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")


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))


if __name__ == "__main__":
    unittest.main()

skills

herdr-foreman

tests

__init__.py

fakes.py

test_assign.py

test_attention.py

test_billing.py

test_bounded_run.sh

test_capabilities.py

test_capability_routing.py

test_chronology.py

test_churn.py

test_classify.sh

test_claude_native.py

test_cli.py

test_compose_briefs.sh

test_composer.py

test_composition.py

test_config.py

test_continuity_cli.py

test_cost_report.py

test_diagnostics.py

test_engagement.py

test_entrypoints.py

test_foreman_launcher.sh

test_foreman_queue.py

test_foreman_reset.py

test_foreman_seat.py

test_foreman_tier_check.py

test_freeze.py

test_herdr.py

test_historical.py

test_home.py

test_label_workspaces.sh

test_launch.py

test_legacy_recovery.py

test_lifecycle.py

test_load_set.py

test_measure.py

test_members.py

test_memory.py

test_minimum_adequate.py

test_oracle.py

test_parsers.py

test_partition.py

test_planner.py

test_probe.py

test_provision_worktree.sh

test_prune_remote_branches.sh

test_prune_report_caches.py

test_prune_result.py

test_prune_worktrees.sh

test_recovery_cli.py

test_recovery.py

test_renderable.py

test_report_contract.py

test_report_delivery.py

test_report_gates.py

test_report_verdict.py

test_reset_input_hook.py

test_resolve_gates.sh

test_resolve_policy_paths.py

test_restoration.py

test_retrospective_runtime.py

test_retrospective.py

test_review_package.py

test_role_clear.py

test_roster.sh

test_round_preflight.sh

test_runnable.py

test_scoring.py

test_script_dir_newline.sh

test_seat_holds.py

test_selection.py

test_skill_invocations.sh

test_slice_scope_parity.py

test_specialist_cli.py

test_specialist_delivery.py

test_specialist_recovery.py

test_specialist_retention.py

test_stale_grok_delivery.py

test_start_judge_worker.py

test_state.py

test_successors.py

test_supervision_cli.py

test_supervision_diagnostics.py

test_supervision_gate.py

test_supervision_replay.py

test_supervision.py

test_sweep_worktrees.sh

test_tier_integration.py

test_tiers.py

test_triggers.py

test_typesafe_client.py

test_verdict_gates.py

test_verify_authority.sh

test_wait_report.sh

tier_fixture.py

bounded-run.sh

compose-briefs.sh

config.example.json

foreman-tier-check.py

foreman.sh

label-workspaces.sh

provision-worktree.sh

prune-remote-branches.sh

prune-report-caches.py

prune-worktrees.sh

resolve-gates.sh

resolve-policy-paths.sh

review-package.sh

roster.sh

round-preflight.sh

SKILL.md

start-judge-worker.sh

state-schema.md

sweep-worktrees.sh

verify-authority.sh

wait-report.sh

README.md

tile.json