diff --git a/src/ctxlens/cli.py b/src/ctxlens/cli.py index 8b1dcc7..ef13178 100644 --- a/src/ctxlens/cli.py +++ b/src/ctxlens/cli.py @@ -155,7 +155,7 @@ def _load(path, fmt, tokenizer, top, tool_result_cap, tool_def_budget): def _maybe_fail(ratio: float, threshold: float | None): - if threshold is not None and ratio > threshold: + if threshold is not None and ratio >= threshold: err_console.print( f"[red]waste ratio {ratio * 100:.1f}% exceeds threshold {threshold * 100:.1f}%[/red]" ) diff --git a/tests/test_cli.py b/tests/test_cli.py index 7abbd17..b4b77f4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,9 +2,11 @@ import json +import pytest +import typer from typer.testing import CliRunner -from ctxlens.cli import app +from ctxlens.cli import EXIT_THRESHOLD, _maybe_fail, app runner = CliRunner() @@ -49,6 +51,17 @@ def test_analyze_fail_over_threshold_ok(claude_jsonl): assert result.exit_code == 0 +def test_maybe_fail_exact_threshold(): + with pytest.raises(typer.Exit) as exc_info: + _maybe_fail(0.5, 0.5) + assert exc_info.value.exit_code == EXIT_THRESHOLD + + +def test_maybe_fail_below_threshold(): + _maybe_fail(0.4, 0.5) + _maybe_fail(0.5, None) + + def test_report_html_to_file(tmp_path, codex_session): out = tmp_path / "r.html" result = runner.invoke(app, ["report", str(codex_session), "--html", "-o", str(out)])