llmtap-proxy 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. {llmtap_proxy-0.2.0/llmtap_proxy.egg-info → llmtap_proxy-0.2.1}/PKG-INFO +1 -1
  2. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/__init__.py +2 -2
  3. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/cli.py +17 -0
  4. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1/llmtap_proxy.egg-info}/PKG-INFO +1 -1
  5. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/pyproject.toml +1 -1
  6. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_cli_extra.py +14 -0
  7. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_term.py +201 -201
  8. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/LICENSE +0 -0
  9. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/README.md +0 -0
  10. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/__main__.py +0 -0
  11. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/budget.py +0 -0
  12. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/cache.py +0 -0
  13. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/cassette.py +0 -0
  14. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/config.py +0 -0
  15. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/dashboard.py +0 -0
  16. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/demo.py +0 -0
  17. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/doctor.py +0 -0
  18. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/guard.py +0 -0
  19. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/mock.py +0 -0
  20. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/models.py +0 -0
  21. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/pricing.py +0 -0
  22. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/providers.py +0 -0
  23. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/proxy.py +0 -0
  24. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/pytest_plugin.py +0 -0
  25. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/report.py +0 -0
  26. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/store.py +0 -0
  27. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/term.py +0 -0
  28. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap/ui/index.html +0 -0
  29. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap_proxy.egg-info/SOURCES.txt +0 -0
  30. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap_proxy.egg-info/dependency_links.txt +0 -0
  31. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap_proxy.egg-info/entry_points.txt +0 -0
  32. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap_proxy.egg-info/requires.txt +0 -0
  33. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/llmtap_proxy.egg-info/top_level.txt +0 -0
  34. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/setup.cfg +0 -0
  35. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_budget.py +0 -0
  36. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_cache.py +0 -0
  37. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_cassette.py +0 -0
  38. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_cli.py +0 -0
  39. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_dashboard.py +0 -0
  40. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_e2e.py +0 -0
  41. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_gemini.py +0 -0
  42. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_guard.py +0 -0
  43. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_hardening.py +0 -0
  44. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_mock.py +0 -0
  45. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_pricing.py +0 -0
  46. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_providers.py +0 -0
  47. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_proxy.py +0 -0
  48. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_pytest_plugin.py +0 -0
  49. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_report.py +0 -0
  50. {llmtap_proxy-0.2.0 → llmtap_proxy-0.2.1}/tests/test_store.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: llmtap-proxy
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: The flight recorder & firewall for LLM apps. See, cache, replay, budget and secure every LLM call — zero code changes, zero dependencies.
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://github.com/AshishAgrawal101/llmtap
@@ -1,2 +1,2 @@
1
- """llmtap — the flight recorder & firewall for LLM apps."""
2
- __version__ = "0.2.0"
1
+ """llmtap — the flight recorder & firewall for LLM apps."""
2
+ __version__ = "0.2.1"
@@ -1034,8 +1034,25 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
1034
1034
  return args
1035
1035
 
1036
1036
 
1037
+ def _ensure_unicode_output() -> None:
1038
+ """Switch stdout/stderr to UTF-8 when their encoding can't represent llmtap's output.
1039
+
1040
+ On Windows, output redirected to a file or pipe (CI logs, ``> out.txt``) uses the ANSI code page
1041
+ (e.g. cp1252), which has no box-drawing or check-mark characters, so printing a table crashed.
1042
+ """
1043
+ for stream in (sys.stdout, sys.stderr):
1044
+ try:
1045
+ "┌─✓".encode(getattr(stream, "encoding", None) or "ascii")
1046
+ except (LookupError, UnicodeEncodeError):
1047
+ try:
1048
+ stream.reconfigure(encoding="utf-8", errors="replace") # type: ignore[union-attr]
1049
+ except Exception:
1050
+ pass
1051
+
1052
+
1037
1053
  def main(argv: list[str] | None = None) -> int:
1038
1054
  """Entry point for the ``llmtap`` console script."""
1055
+ _ensure_unicode_output()
1039
1056
  try:
1040
1057
  args = parse_args(argv)
1041
1058
  except SystemExit as e: # argparse --help / errors
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: llmtap-proxy
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: The flight recorder & firewall for LLM apps. See, cache, replay, budget and secure every LLM call — zero code changes, zero dependencies.
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://github.com/AshishAgrawal101/llmtap
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "llmtap-proxy"
7
- version = "0.2.0"
7
+ version = "0.2.1"
8
8
  description = "The flight recorder & firewall for LLM apps. See, cache, replay, budget and secure every LLM call — zero code changes, zero dependencies."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -232,3 +232,17 @@ def test_new_commands_listed_in_help(capsys: pytest.CaptureFixture[str]) -> None
232
232
  assert cli.main(["--help"]) == 0
233
233
  out = capsys.readouterr().out
234
234
  assert "report" in out and "doctor" in out
235
+
236
+
237
+ @pytest.mark.parametrize("argv", [["prices", "claude"], ["doctor"], ["scan", "-"]])
238
+ def test_cli_survives_non_unicode_output_encoding(argv: list[str], tmp_path: Path) -> None:
239
+ """Windows redirects output in the ANSI code page (cp1252); tables and check marks must not crash."""
240
+ import subprocess
241
+ import sys
242
+
243
+ env = dict(os.environ, PYTHONIOENCODING="cp1252", LLMTAP_HOME=str(tmp_path / "home"))
244
+ p = subprocess.run([sys.executable, "-m", "llmtap", *argv], input=b"nothing secret here\n",
245
+ capture_output=True, env=env, timeout=60)
246
+ assert b"codec can't encode" not in p.stderr, p.stderr
247
+ assert p.returncode in (0, 1), p.stderr # doctor may report setup problems (1); 2 is the crash path
248
+ assert p.stdout
@@ -1,201 +1,201 @@
1
- """Tests for llmtap.term: ANSI handling, width math, formatters, banner/summary/table, log lines."""
2
- from __future__ import annotations
3
-
4
- import io
5
-
6
- import pytest
7
-
8
- from llmtap import term
9
- from llmtap.config import Config
10
- from llmtap.models import Exchange, Usage
11
-
12
-
13
- @pytest.fixture(autouse=True)
14
- def _no_force(monkeypatch: pytest.MonkeyPatch) -> None:
15
- monkeypatch.delenv("FORCE_COLOR", raising=False)
16
- monkeypatch.delenv("NO_COLOR", raising=False)
17
- monkeypatch.setenv("COLUMNS", "120")
18
-
19
-
20
- class _TTY(io.StringIO):
21
- def isatty(self) -> bool:
22
- return True
23
-
24
-
25
- def test_strip_ansi_and_vlen() -> None:
26
- s = "\x1b[1;31mhello\x1b[0m world"
27
- assert term.strip_ansi(s) == "hello world"
28
- assert term.vlen(s) == 11
29
- assert term.vlen("⚡ttft") == 6 # wide glyph counts as 2
30
- assert term.vlen("") == 0
31
-
32
-
33
- def test_pad_and_truncate() -> None:
34
- red = term.paint("ab", "red", color=True)
35
- assert term.vlen(term.pad(red, 5)) == 5
36
- assert term.pad("ab", 5, ">") == " ab"
37
- assert term.pad("ab", 6, "^") == " ab "
38
- assert term.pad("abcdef", 3) == "abcdef"
39
- assert term.truncate("abcdefghij", 5) == "abcd…"
40
- assert term.truncate("abc", 5) == "abc"
41
- assert term.vlen(term.truncate(term.paint("x" * 50, "red", color=True), 10)) == 10
42
-
43
-
44
- def test_paint_respects_color_flag() -> None:
45
- assert term.paint("x", "red", color=False) == "x"
46
- assert term.paint("x", "red", color=True) == "\x1b[31mx\x1b[0m"
47
- assert term.paint("x", color=True) == "x"
48
-
49
-
50
- def test_color_enabled_rules(monkeypatch: pytest.MonkeyPatch) -> None:
51
- assert term.color_enabled(io.StringIO()) is False
52
- assert term.color_enabled(_TTY()) is True
53
- monkeypatch.setenv("NO_COLOR", "1")
54
- assert term.color_enabled(_TTY()) is False
55
- monkeypatch.delenv("NO_COLOR")
56
- monkeypatch.setenv("FORCE_COLOR", "1")
57
- assert term.color_enabled(io.StringIO()) is True
58
- monkeypatch.setenv("FORCE_COLOR", "0")
59
- assert term.color_enabled(io.StringIO()) is False
60
-
61
-
62
- @pytest.mark.parametrize("n,expected", [
63
- (0, "0"), (999, "999"), (1000, "1k"), (1234, "1.2k"), (12_345, "12.3k"), (340, "340"),
64
- (2_500_000, "2.5M"), (150_000, "150k"), (None, "0"), (3_000_000_000, "3B"),
65
- ])
66
- def test_fmt_tokens(n: int | None, expected: str) -> None:
67
- assert term.fmt_tokens(n) == expected
68
-
69
-
70
- @pytest.mark.parametrize("ms,expected", [
71
- (210, "210ms"), (1240, "1.24s"), (0, "0ms"), (None, "-"), (12_500, "12.5s"), (75_000, "1m15s"), (0.5, "0.5ms"),
72
- ])
73
- def test_fmt_ms(ms: float | None, expected: str) -> None:
74
- assert term.fmt_ms(ms) == expected
75
-
76
-
77
- def test_fmt_usd() -> None:
78
- assert term.fmt_usd(0) == "$0.00"
79
- assert term.fmt_usd(0.0042) == "$0.0042"
80
- assert term.fmt_usd(1.234) == "$1.23"
81
-
82
-
83
- def test_table_alignment_ignores_ansi() -> None:
84
- out = term.table([[term.paint("red", "red", color=True), "1"], ["longer-cell", "22"]], ["NAME", "N"], color=False)
85
- lines = out.splitlines()
86
- widths = {term.vlen(l) for l in lines}
87
- assert len(widths) == 1, out
88
- assert lines[0].startswith("┌") and lines[-1].startswith("└")
89
- assert "│ 1 │" in term.strip_ansi(out) # numeric column right-aligned
90
-
91
-
92
- def test_table_empty_and_ragged() -> None:
93
- out = term.table([], ["A", "B"], color=False)
94
- assert "A" in out and "B" in out
95
- out = term.table([["x"], ["y", "z", "w"]], ["A"], color=False)
96
- assert len({term.vlen(l) for l in out.splitlines()}) == 1
97
-
98
-
99
- def test_table_shrinks_to_width() -> None:
100
- out = term.table([["x" * 300, "y"]], ["A", "B"], color=False, max_width=60)
101
- assert all(term.vlen(l) <= 60 for l in out.splitlines())
102
-
103
-
104
- def test_box_is_rectangular() -> None:
105
- out = term.box(["hello", term.paint("colored ⚡", "red", color=True), ""], title="T", color=True)
106
- assert len({term.vlen(l) for l in out.splitlines()}) == 1
107
-
108
-
109
- def test_banner_contents() -> None:
110
- cfg = Config(cache=True, cassette="tapes/ci.jsonl", mode="replay", guard="redact",
111
- budget_daily_usd=5, loop_threshold=4)
112
- out = term.strip_ansi(term.banner(cfg, "http://127.0.0.1:4141", color=False))
113
- assert "llmtap v0.2.0" in out and "recording LLM traffic" in out
114
- assert "http://127.0.0.1:4141/_llmtap/" in out
115
- assert "export ANTHROPIC_BASE_URL=http://127.0.0.1:4141" in out
116
- assert "export OPENAI_BASE_URL=http://127.0.0.1:4141/v1" in out
117
- assert "redact" in out and "ci.jsonl" in out and "replay" in out
118
- assert "$5.00/day" in out and "4×" in out
119
- lines = out.splitlines()
120
- assert len({term.vlen(l) for l in lines}) == 1
121
-
122
-
123
- def test_banner_defaults_off() -> None:
124
- out = term.banner(Config(), "http://127.0.0.1:4141/", color=False)
125
- assert "cache off" in out and "unlimited" in out and "loop-breaker off" in out
126
-
127
-
128
- def test_log_exchange_minimal_does_not_crash() -> None:
129
- buf = io.StringIO()
130
- term.log_exchange(Exchange(), stream=buf)
131
- line = buf.getvalue()
132
- assert line.endswith("\n") and "\x1b[" not in line
133
- assert "unknown" in line
134
-
135
-
136
- def test_log_exchange_full_line() -> None:
137
- ex = Exchange(provider="anthropic", model="claude-sonnet-4-5", status=200,
138
- usage=Usage(input_tokens=1234, output_tokens=340), cost_usd=0.0123,
139
- latency_ms=1240, streamed=True, ttft_ms=210,
140
- guard_findings=[{"kind": "aws_access_key"}, {"kind": "openai_key"}],
141
- request_body='{"x": "[REDACTED:aws_access_key]"}')
142
- line = term.format_exchange(ex, color=False, width=120)
143
- assert "anthropic/claude-sonnet-4-5" in line
144
- assert "1.2k→340 tok" in line
145
- assert "$0.0123" in line and "1.24s" in line
146
- assert "⚡ttft 210ms" in line
147
- assert "⚠ 2 secrets redacted" in line
148
- assert "LIVE" in line and "200" in line
149
-
150
-
151
- def test_log_exchange_blocked_and_cache() -> None:
152
- ex = Exchange(source="blocked", status=400, guard_findings=[{"kind": "x"}])
153
- assert "⛔ blocked" in term.format_exchange(ex, color=False)
154
- ex = Exchange(source="cache", status=200, saved_usd=0.5, model="gpt-4o")
155
- line = term.format_exchange(ex, color=False)
156
- assert "CACHE" in line and "saved $0.5" in line
157
- ex = Exchange(source="loop", status=429, error="loop detected: identical request sent 4 times in 60s")
158
- assert "loop detected" in term.format_exchange(ex, color=False)
159
-
160
-
161
- def test_log_exchange_truncates_long_model() -> None:
162
- ex = Exchange(provider="openrouter", model="m" * 200, status=200)
163
- line = term.format_exchange(ex, color=False, width=100)
164
- assert "…" in line and term.vlen(line) < 160
165
-
166
-
167
- def test_log_exchange_with_colors_aligned() -> None:
168
- a = Exchange(provider="openai", model="gpt-4o", status=200, latency_ms=12)
169
- b = Exchange(provider="anthropic", model="claude-haiku-4-5", status=500, latency_ms=3000)
170
- la = term.format_exchange(a, color=True, width=120)
171
- lb = term.format_exchange(b, color=True, width=120)
172
- assert "\x1b[" in la
173
- assert term.vlen(la) == term.vlen(lb)
174
-
175
-
176
- def test_status_color() -> None:
177
- assert term.status_color(200) == "green"
178
- assert term.status_color(429) == "yellow"
179
- assert term.status_color(502) == "red"
180
- assert term.status_color(0) == "red"
181
-
182
-
183
- def test_summary_box() -> None:
184
- stats = {"requests": 12, "input_tokens": 12_000, "output_tokens": 3400, "cost_usd": 0.42, "saved_usd": 0.1,
185
- "avg_latency_ms": 850.0, "errors": 1, "cache_hits": 3, "blocked": 1, "guard_findings": 2,
186
- "groups": [{"key": "claude-sonnet-4-5", "requests": 8, "cost_usd": 0.4, "input_tokens": 10_000,
187
- "output_tokens": 3000},
188
- {"key": "gpt-4o-mini", "requests": 4, "cost_usd": 0.02, "input_tokens": 2000,
189
- "output_tokens": 400}]}
190
- out = term.summary(stats, 0.42, color=False)
191
- for needle in ("12", "12k in", "3.4k out", "$0.42", "$0.10", "850ms", "Errors", "Blocked",
192
- "2 secret findings", "claude-sonnet-4-5", "gpt-4o-mini", "Top models"):
193
- assert needle in out, needle
194
- assert len({term.vlen(l) for l in out.splitlines()}) == 1
195
-
196
-
197
- def test_summary_empty() -> None:
198
- out = term.summary({}, 0.0, color=False)
199
- assert "No LLM requests" in out
200
- out = term.summary({"requests": 0}, 0.0, color=False)
201
- assert "No LLM requests" in out
1
+ """Tests for llmtap.term: ANSI handling, width math, formatters, banner/summary/table, log lines."""
2
+ from __future__ import annotations
3
+
4
+ import io
5
+
6
+ import pytest
7
+
8
+ from llmtap import __version__, term
9
+ from llmtap.config import Config
10
+ from llmtap.models import Exchange, Usage
11
+
12
+
13
+ @pytest.fixture(autouse=True)
14
+ def _no_force(monkeypatch: pytest.MonkeyPatch) -> None:
15
+ monkeypatch.delenv("FORCE_COLOR", raising=False)
16
+ monkeypatch.delenv("NO_COLOR", raising=False)
17
+ monkeypatch.setenv("COLUMNS", "120")
18
+
19
+
20
+ class _TTY(io.StringIO):
21
+ def isatty(self) -> bool:
22
+ return True
23
+
24
+
25
+ def test_strip_ansi_and_vlen() -> None:
26
+ s = "\x1b[1;31mhello\x1b[0m world"
27
+ assert term.strip_ansi(s) == "hello world"
28
+ assert term.vlen(s) == 11
29
+ assert term.vlen("⚡ttft") == 6 # wide glyph counts as 2
30
+ assert term.vlen("") == 0
31
+
32
+
33
+ def test_pad_and_truncate() -> None:
34
+ red = term.paint("ab", "red", color=True)
35
+ assert term.vlen(term.pad(red, 5)) == 5
36
+ assert term.pad("ab", 5, ">") == " ab"
37
+ assert term.pad("ab", 6, "^") == " ab "
38
+ assert term.pad("abcdef", 3) == "abcdef"
39
+ assert term.truncate("abcdefghij", 5) == "abcd…"
40
+ assert term.truncate("abc", 5) == "abc"
41
+ assert term.vlen(term.truncate(term.paint("x" * 50, "red", color=True), 10)) == 10
42
+
43
+
44
+ def test_paint_respects_color_flag() -> None:
45
+ assert term.paint("x", "red", color=False) == "x"
46
+ assert term.paint("x", "red", color=True) == "\x1b[31mx\x1b[0m"
47
+ assert term.paint("x", color=True) == "x"
48
+
49
+
50
+ def test_color_enabled_rules(monkeypatch: pytest.MonkeyPatch) -> None:
51
+ assert term.color_enabled(io.StringIO()) is False
52
+ assert term.color_enabled(_TTY()) is True
53
+ monkeypatch.setenv("NO_COLOR", "1")
54
+ assert term.color_enabled(_TTY()) is False
55
+ monkeypatch.delenv("NO_COLOR")
56
+ monkeypatch.setenv("FORCE_COLOR", "1")
57
+ assert term.color_enabled(io.StringIO()) is True
58
+ monkeypatch.setenv("FORCE_COLOR", "0")
59
+ assert term.color_enabled(io.StringIO()) is False
60
+
61
+
62
+ @pytest.mark.parametrize("n,expected", [
63
+ (0, "0"), (999, "999"), (1000, "1k"), (1234, "1.2k"), (12_345, "12.3k"), (340, "340"),
64
+ (2_500_000, "2.5M"), (150_000, "150k"), (None, "0"), (3_000_000_000, "3B"),
65
+ ])
66
+ def test_fmt_tokens(n: int | None, expected: str) -> None:
67
+ assert term.fmt_tokens(n) == expected
68
+
69
+
70
+ @pytest.mark.parametrize("ms,expected", [
71
+ (210, "210ms"), (1240, "1.24s"), (0, "0ms"), (None, "-"), (12_500, "12.5s"), (75_000, "1m15s"), (0.5, "0.5ms"),
72
+ ])
73
+ def test_fmt_ms(ms: float | None, expected: str) -> None:
74
+ assert term.fmt_ms(ms) == expected
75
+
76
+
77
+ def test_fmt_usd() -> None:
78
+ assert term.fmt_usd(0) == "$0.00"
79
+ assert term.fmt_usd(0.0042) == "$0.0042"
80
+ assert term.fmt_usd(1.234) == "$1.23"
81
+
82
+
83
+ def test_table_alignment_ignores_ansi() -> None:
84
+ out = term.table([[term.paint("red", "red", color=True), "1"], ["longer-cell", "22"]], ["NAME", "N"], color=False)
85
+ lines = out.splitlines()
86
+ widths = {term.vlen(l) for l in lines}
87
+ assert len(widths) == 1, out
88
+ assert lines[0].startswith("┌") and lines[-1].startswith("└")
89
+ assert "│ 1 │" in term.strip_ansi(out) # numeric column right-aligned
90
+
91
+
92
+ def test_table_empty_and_ragged() -> None:
93
+ out = term.table([], ["A", "B"], color=False)
94
+ assert "A" in out and "B" in out
95
+ out = term.table([["x"], ["y", "z", "w"]], ["A"], color=False)
96
+ assert len({term.vlen(l) for l in out.splitlines()}) == 1
97
+
98
+
99
+ def test_table_shrinks_to_width() -> None:
100
+ out = term.table([["x" * 300, "y"]], ["A", "B"], color=False, max_width=60)
101
+ assert all(term.vlen(l) <= 60 for l in out.splitlines())
102
+
103
+
104
+ def test_box_is_rectangular() -> None:
105
+ out = term.box(["hello", term.paint("colored ⚡", "red", color=True), ""], title="T", color=True)
106
+ assert len({term.vlen(l) for l in out.splitlines()}) == 1
107
+
108
+
109
+ def test_banner_contents() -> None:
110
+ cfg = Config(cache=True, cassette="tapes/ci.jsonl", mode="replay", guard="redact",
111
+ budget_daily_usd=5, loop_threshold=4)
112
+ out = term.strip_ansi(term.banner(cfg, "http://127.0.0.1:4141", color=False))
113
+ assert f"llmtap v{__version__}" in out and "recording LLM traffic" in out
114
+ assert "http://127.0.0.1:4141/_llmtap/" in out
115
+ assert "export ANTHROPIC_BASE_URL=http://127.0.0.1:4141" in out
116
+ assert "export OPENAI_BASE_URL=http://127.0.0.1:4141/v1" in out
117
+ assert "redact" in out and "ci.jsonl" in out and "replay" in out
118
+ assert "$5.00/day" in out and "4×" in out
119
+ lines = out.splitlines()
120
+ assert len({term.vlen(l) for l in lines}) == 1
121
+
122
+
123
+ def test_banner_defaults_off() -> None:
124
+ out = term.banner(Config(), "http://127.0.0.1:4141/", color=False)
125
+ assert "cache off" in out and "unlimited" in out and "loop-breaker off" in out
126
+
127
+
128
+ def test_log_exchange_minimal_does_not_crash() -> None:
129
+ buf = io.StringIO()
130
+ term.log_exchange(Exchange(), stream=buf)
131
+ line = buf.getvalue()
132
+ assert line.endswith("\n") and "\x1b[" not in line
133
+ assert "unknown" in line
134
+
135
+
136
+ def test_log_exchange_full_line() -> None:
137
+ ex = Exchange(provider="anthropic", model="claude-sonnet-4-5", status=200,
138
+ usage=Usage(input_tokens=1234, output_tokens=340), cost_usd=0.0123,
139
+ latency_ms=1240, streamed=True, ttft_ms=210,
140
+ guard_findings=[{"kind": "aws_access_key"}, {"kind": "openai_key"}],
141
+ request_body='{"x": "[REDACTED:aws_access_key]"}')
142
+ line = term.format_exchange(ex, color=False, width=120)
143
+ assert "anthropic/claude-sonnet-4-5" in line
144
+ assert "1.2k→340 tok" in line
145
+ assert "$0.0123" in line and "1.24s" in line
146
+ assert "⚡ttft 210ms" in line
147
+ assert "⚠ 2 secrets redacted" in line
148
+ assert "LIVE" in line and "200" in line
149
+
150
+
151
+ def test_log_exchange_blocked_and_cache() -> None:
152
+ ex = Exchange(source="blocked", status=400, guard_findings=[{"kind": "x"}])
153
+ assert "⛔ blocked" in term.format_exchange(ex, color=False)
154
+ ex = Exchange(source="cache", status=200, saved_usd=0.5, model="gpt-4o")
155
+ line = term.format_exchange(ex, color=False)
156
+ assert "CACHE" in line and "saved $0.5" in line
157
+ ex = Exchange(source="loop", status=429, error="loop detected: identical request sent 4 times in 60s")
158
+ assert "loop detected" in term.format_exchange(ex, color=False)
159
+
160
+
161
+ def test_log_exchange_truncates_long_model() -> None:
162
+ ex = Exchange(provider="openrouter", model="m" * 200, status=200)
163
+ line = term.format_exchange(ex, color=False, width=100)
164
+ assert "…" in line and term.vlen(line) < 160
165
+
166
+
167
+ def test_log_exchange_with_colors_aligned() -> None:
168
+ a = Exchange(provider="openai", model="gpt-4o", status=200, latency_ms=12)
169
+ b = Exchange(provider="anthropic", model="claude-haiku-4-5", status=500, latency_ms=3000)
170
+ la = term.format_exchange(a, color=True, width=120)
171
+ lb = term.format_exchange(b, color=True, width=120)
172
+ assert "\x1b[" in la
173
+ assert term.vlen(la) == term.vlen(lb)
174
+
175
+
176
+ def test_status_color() -> None:
177
+ assert term.status_color(200) == "green"
178
+ assert term.status_color(429) == "yellow"
179
+ assert term.status_color(502) == "red"
180
+ assert term.status_color(0) == "red"
181
+
182
+
183
+ def test_summary_box() -> None:
184
+ stats = {"requests": 12, "input_tokens": 12_000, "output_tokens": 3400, "cost_usd": 0.42, "saved_usd": 0.1,
185
+ "avg_latency_ms": 850.0, "errors": 1, "cache_hits": 3, "blocked": 1, "guard_findings": 2,
186
+ "groups": [{"key": "claude-sonnet-4-5", "requests": 8, "cost_usd": 0.4, "input_tokens": 10_000,
187
+ "output_tokens": 3000},
188
+ {"key": "gpt-4o-mini", "requests": 4, "cost_usd": 0.02, "input_tokens": 2000,
189
+ "output_tokens": 400}]}
190
+ out = term.summary(stats, 0.42, color=False)
191
+ for needle in ("12", "12k in", "3.4k out", "$0.42", "$0.10", "850ms", "Errors", "Blocked",
192
+ "2 secret findings", "claude-sonnet-4-5", "gpt-4o-mini", "Top models"):
193
+ assert needle in out, needle
194
+ assert len({term.vlen(l) for l in out.splitlines()}) == 1
195
+
196
+
197
+ def test_summary_empty() -> None:
198
+ out = term.summary({}, 0.0, color=False)
199
+ assert "No LLM requests" in out
200
+ out = term.summary({"requests": 0}, 0.0, color=False)
201
+ assert "No LLM requests" in out
File without changes
File without changes
File without changes