48e9242f8f
display.format_token_count now rounds half-up to whole numbers with tier rollover (999999 -> "1M", never "1000k"; 275860 -> "276k"). display.format_token_line renders '-- I:<s>+<t> O:<s>+<t> C:<s>+<t>': the +N suffixes come from new last_turn token fields in the report (CLI --json raw counts + token_display last_turn_* keys; Hermes plugin passes its in-memory turn bucket), render +0 on zero increments, and fail soft when absent - every display path (CLI, --display-lines, GUI, Vibe hooks, Hermes plugin) changes together per the display-parity rule. Docs and tests updated; 125 pass. MINOR bump to 0.8.0. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
227 lines
8.6 KiB
Python
227 lines
8.6 KiB
Python
"""Tests for the Vibe hook scripts (post_turn.py).
|
|
|
|
The hooks are standalone stdlib-only scripts (they must not import
|
|
the package) and, since the display refactor, hold NO formatting of
|
|
their own: they run ``agent-cost-estimator --display-lines`` and
|
|
relay its stdout verbatim. These tests pin the relay contract.
|
|
"""
|
|
|
|
import importlib.util
|
|
import json
|
|
import pathlib
|
|
import subprocess
|
|
import pytest
|
|
|
|
from agent_cost_estimator.display import format_display_lines
|
|
|
|
HOOKS_DIR = pathlib.Path(__file__).parent.parent / "agent_cost_estimator" / "hooks"
|
|
HOOK_FILES = {
|
|
"mistral-vibe-cli": HOOKS_DIR / "mistral-vibe-cli" / "post_turn.py",
|
|
"mistral-vibe-code": HOOKS_DIR / "mistral-vibe-code" / "post_turn.py",
|
|
}
|
|
|
|
|
|
def _load_hook(name):
|
|
"""Import a hook script by file path (module-name-safe)."""
|
|
path = HOOK_FILES[name]
|
|
spec = importlib.util.spec_from_file_location(f"hook_{name.replace('-', '_')}", path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
@pytest.mark.parametrize("hook_name", sorted(HOOK_FILES))
|
|
class TestHookRelay:
|
|
"""main() runs the CLI with --display-lines and relays it."""
|
|
|
|
def _run_hook(self, hook, monkeypatch, capsys, cli_stdout,
|
|
cli_rc=0, session_exists=True):
|
|
calls = {}
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
calls["cmd"] = cmd
|
|
return subprocess.CompletedProcess(
|
|
cmd, cli_rc, stdout=cli_stdout, stderr="boom"
|
|
if cli_rc else "")
|
|
|
|
class FakeDir:
|
|
def __init__(self, path):
|
|
self.path = path
|
|
def is_dir(self):
|
|
return session_exists
|
|
def __str__(self):
|
|
return self.path
|
|
|
|
monkeypatch.setattr(hook.subprocess, "run", fake_run)
|
|
monkeypatch.setattr(hook, "get_session_dir", lambda: FakeDir("/x/sess"))
|
|
hook.main()
|
|
return calls, json.loads(capsys.readouterr().out)
|
|
|
|
def test_relays_display_lines_with_prefix(self, hook_name,
|
|
monkeypatch, capsys):
|
|
"""The CLI's --display-lines output is relayed verbatim,
|
|
with the system_message '-- ' prefix on the first line."""
|
|
hook = _load_hook(hook_name)
|
|
cli_out = ("zai:zai-glm-5-3 | session:$0.01 | turn:$0.00\n"
|
|
"-- I:42M+1k O:95k+900 C:41M+25k")
|
|
calls, message = self._run_hook(
|
|
hook, monkeypatch, capsys, cli_out)
|
|
assert "--display-lines" in calls["cmd"]
|
|
assert message == {
|
|
"system_message": f"-- {cli_out}",
|
|
}
|
|
|
|
def test_relays_note_line_too(self, hook_name, monkeypatch, capsys):
|
|
"""The model-price note line is relayed like any other
|
|
display line (the canonical format includes it)."""
|
|
hook = _load_hook(hook_name)
|
|
cli_out = ("default:m | session:$0.01 | turn:$0.00\n"
|
|
" **model price not available**\n"
|
|
"-- I:2M+1M O:100k+100k C:500k+500k")
|
|
calls, message = self._run_hook(
|
|
hook, monkeypatch, capsys, cli_out)
|
|
assert message == {"system_message": f"-- {cli_out}"}
|
|
|
|
def test_error_when_cli_fails(self, hook_name, monkeypatch, capsys):
|
|
hook = _load_hook(hook_name)
|
|
calls, message = self._run_hook(
|
|
hook, monkeypatch, capsys, "", cli_rc=1)
|
|
assert message["system_message"] == "-- [cost: error - boom]"
|
|
|
|
def test_missing_session_dir(self, hook_name, monkeypatch, capsys):
|
|
hook = _load_hook(hook_name)
|
|
calls, message = self._run_hook(
|
|
hook, monkeypatch, capsys, "", session_exists=False)
|
|
assert message["system_message"] == "-- [cost: session dir not found]"
|
|
|
|
def test_json_is_not_used_for_display(self, hook_name,
|
|
monkeypatch, capsys):
|
|
"""The hook must not parse --json to format the display:
|
|
formatting lives only in the package's display module."""
|
|
hook = _load_hook(hook_name)
|
|
calls, message = self._run_hook(
|
|
hook, monkeypatch, capsys,
|
|
"zai:m | session:$0.01 | turn:$0.00\n-- I:1 O:1 C:0")
|
|
assert "--json" not in calls["cmd"]
|
|
assert "--display-lines" in calls["cmd"]
|
|
|
|
|
|
@pytest.mark.parametrize("hook_name", sorted(HOOK_FILES))
|
|
class TestNoDuplicatedFormatting:
|
|
"""The hooks must hold no display-formatting code: the format
|
|
lives in agent_cost_estimator.display only."""
|
|
|
|
def test_hook_has_no_local_formatters(self, hook_name):
|
|
hook = _load_hook(hook_name)
|
|
assert not hasattr(hook, "format_cost_output")
|
|
assert not hasattr(hook, "_format_token_count")
|
|
|
|
|
|
class TestDisplayParity:
|
|
"""The canonical display module and the CLI agree."""
|
|
|
|
def test_display_lines_shape(self):
|
|
report = {
|
|
"pricing": {"model_key": "zai:zai-glm-5-3"},
|
|
"cost_usd": {"total_usd": 0.0136},
|
|
"last_turn": {"cost_usd": {"total_usd": 0.0031}},
|
|
"input_tokens": 42116859,
|
|
"cached_input_tokens": 41355264,
|
|
"output_tokens": 95073,
|
|
"model_price_unavailable": False,
|
|
}
|
|
assert format_display_lines(report) == [
|
|
"zai:zai-glm-5-3 | session:$0.01 | turn:$0.00",
|
|
"-- I:42M O:95k C:41M",
|
|
]
|
|
|
|
def test_token_line_turn_suffixes(self):
|
|
"""last_turn token fields render "+N" per field: the token
|
|
line shows session total plus last turn for each of I/O/C."""
|
|
report = {
|
|
"pricing": {"model_key": "zai:m"},
|
|
"cost_usd": {"total_usd": 0.42},
|
|
"last_turn": {
|
|
"cost_usd": {"total_usd": 0.01},
|
|
"input_tokens": 1000,
|
|
"cached_input_tokens": 25_000,
|
|
"output_tokens": 900,
|
|
},
|
|
"input_tokens": 275_860,
|
|
"cached_input_tokens": 250_180,
|
|
"output_tokens": 1700,
|
|
"model_price_unavailable": False,
|
|
}
|
|
assert format_display_lines(report)[-1] == (
|
|
"-- I:276k+1k O:2k+900 C:250k+25k")
|
|
|
|
def test_token_line_zero_turn_increment_renders_plus_zero(self):
|
|
"""A turn that added nothing still gets its suffix (+0);
|
|
only MISSING data omits the parts."""
|
|
report = {
|
|
"pricing": {"model_key": "zai:m"},
|
|
"cost_usd": {"total_usd": 0.42},
|
|
"last_turn": {
|
|
"cost_usd": {"total_usd": 0.00},
|
|
"input_tokens": 0,
|
|
"cached_input_tokens": 0,
|
|
"output_tokens": 0,
|
|
},
|
|
"input_tokens": 906_000,
|
|
"cached_input_tokens": 900_000,
|
|
"output_tokens": 20_000,
|
|
"model_price_unavailable": False,
|
|
}
|
|
assert format_display_lines(report)[-1] == (
|
|
"-- I:906k+0 O:20k+0 C:900k+0")
|
|
|
|
def test_token_line_omitted_without_token_fields(self):
|
|
report = {
|
|
"pricing": {"model_key": "zai:m"},
|
|
"cost_usd": {"total_usd": 1.0},
|
|
"last_turn": None,
|
|
}
|
|
assert format_display_lines(report) == [
|
|
"zai:m | session:$1.00 | turn:$0.00",
|
|
]
|
|
|
|
def test_unknown_provider_falls_back(self):
|
|
report = {
|
|
"pricing": {"model_key": "noprovider"},
|
|
"cost_usd": {"total_usd": 1.0},
|
|
"input_tokens": 0,
|
|
"cached_input_tokens": 0,
|
|
"output_tokens": 0,
|
|
}
|
|
lines = format_display_lines(report)
|
|
assert lines[0] == "unknown:noprovider | session:$1.00 | turn:$0.00"
|
|
assert lines[-1] == "-- I:0 O:0 C:0"
|
|
|
|
def test_model_label_override(self):
|
|
"""An explicit label wins over the report's model_key (the
|
|
Hermes plugin passes its billing-route label)."""
|
|
report = {
|
|
"pricing": {"model_key": "zai:zai-glm-5-3"},
|
|
"cost_usd": {"total_usd": 0.42},
|
|
"last_turn": None,
|
|
"input_tokens": 1000, "cached_input_tokens": 0,
|
|
"output_tokens": 100,
|
|
}
|
|
lines = format_display_lines(report, model_label="mistral:glm-5-3")
|
|
assert lines[0] == "mistral:glm-5-3 | session:$0.42 | turn:$0.00"
|
|
|
|
def test_note_line_between_cost_and_tokens(self):
|
|
report = {
|
|
"pricing": {"model_key": "d:m"},
|
|
"cost_usd": {"total_usd": 0.42},
|
|
"last_turn": None,
|
|
"input_tokens": 2_000_000, "cached_input_tokens": 500_000,
|
|
"output_tokens": 100_000,
|
|
"model_price_unavailable": True,
|
|
}
|
|
assert format_display_lines(report) == [
|
|
"d:m | session:$0.42 | turn:$0.00",
|
|
" **model price not available**",
|
|
"-- I:2M O:100k C:500k",
|
|
]
|