File size: 23,935 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
"""Usage records for model calls and GPU steps (#262). No network, no GPU, no database."""

from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path
from types import SimpleNamespace

import pytest
from llm.usage import calls, gpu, load_bronze, prices, report


@pytest.fixture()
def log_dir(tmp_path, monkeypatch):
    monkeypatch.setenv("LLM_USAGE_LOG_DIR", str(tmp_path))
    monkeypatch.delenv("LLM_USAGE_LOG", raising=False)
    return tmp_path


def _rows(log_dir: Path, prefix: str) -> list[dict]:
    return list(load_bronze.read_rows(log_dir, prefix))


# --------------------------------------------------------------------------- #
# Token counts
# --------------------------------------------------------------------------- #
def test_usage_from_genai_sdk_object():
    raw = SimpleNamespace(
        usage_metadata=SimpleNamespace(
            prompt_token_count=1200,
            cached_content_token_count=800,
            candidates_token_count=300,
            thoughts_token_count=450,
            total_token_count=1950,
        )
    )
    usage = calls.usage_from_genai_response(raw)
    assert usage.input_token_count == 1200
    assert usage.cached_input_token_count == 800
    assert usage.output_token_count == 300
    assert usage.thinking_token_count == 450
    assert usage.total_token_count == 1950


def test_usage_from_batch_result_json_is_camel_case():
    raw = {"usageMetadata": {"promptTokenCount": 10, "candidatesTokenCount": 4, "totalTokenCount": 14}}
    usage = calls.usage_from_genai_response(raw)
    assert (usage.input_token_count, usage.output_token_count, usage.total_token_count) == (10, 4, 14)
    assert usage.thinking_token_count is None


def test_missing_usage_is_none_not_zero():
    usage = calls.usage_from_genai_response(SimpleNamespace(usage_metadata=None))
    assert usage.as_dict() == {k: None for k in usage.as_dict()}
    assert calls.usage_from_genai_response(None).input_token_count is None


def test_openai_reasoning_tokens_are_split_from_output():
    raw = SimpleNamespace(
        usage=SimpleNamespace(
            prompt_tokens=100,
            completion_tokens=70,
            total_tokens=170,
            prompt_cache_hit_tokens=40,
            completion_tokens_details=SimpleNamespace(reasoning_tokens=50),
        )
    )
    usage = calls.usage_from_openai_response(raw)
    assert usage.output_token_count == 20
    assert usage.thinking_token_count == 50
    assert usage.cached_input_token_count == 40


# --------------------------------------------------------------------------- #
# The call log
# --------------------------------------------------------------------------- #
def test_record_llm_call_writes_one_row_with_context(log_dir):
    usage = calls.LLMCallUsage(input_token_count=5, output_token_count=2, latency_seconds=1.23456, attempt_count=3)
    with calls.llm_call_context(stage="part1", video_id="abc", jurisdiction_id="m_1", transcript="SECRET"):
        calls.record_llm_call(provider="gemini", model="gemini-2.5-flash", usage=usage, is_ok=True)
    (row,) = _rows(log_dir, "llm_calls")
    assert row["stage"] == "part1" and row["video_id"] == "abc" and row["jurisdiction_id"] == "m_1"
    assert row["api_mode"] == "interactive"
    assert row["input_token_count"] == 5 and row["attempt_count"] == 3
    assert row["latency_seconds"] == 1.235
    assert row["is_ok"] is True and row["error_type"] is None
    assert "SECRET" not in json.dumps(row)  # only the known labels are kept


def test_context_is_restored_after_the_block(log_dir):
    with calls.llm_call_context(stage="part1"):
        with calls.llm_call_context(stage="part2", video_id="v"):
            assert calls.current_context() == {"stage": "part2", "video_id": "v"}
        assert calls.current_context() == {"stage": "part1"}
    assert calls.current_context() == {}


def test_failed_call_is_recorded(log_dir):
    calls.record_llm_call(provider="gemini", model="m", usage=None, is_ok=False, error_type="GenAITransientGiveUp")
    (row,) = _rows(log_dir, "llm_calls")
    assert row["is_ok"] is False and row["error_type"] == "GenAITransientGiveUp"
    assert row["input_token_count"] is None


def test_switch_off(log_dir, monkeypatch):
    monkeypatch.setenv("LLM_USAGE_LOG", "0")
    calls.record_llm_call(provider="gemini", model="m", usage=None, is_ok=True)
    assert _rows(log_dir, "llm_calls") == []


def test_unwritable_log_dir_does_not_raise(tmp_path, monkeypatch):
    blocker = tmp_path / "file"
    blocker.write_text("x")
    monkeypatch.setenv("LLM_USAGE_LOG_DIR", str(blocker / "under_a_file"))
    calls.record_llm_call(provider="gemini", model="m", usage=None, is_ok=True)


# --------------------------------------------------------------------------- #
# call_gemini_text records every call
# --------------------------------------------------------------------------- #
class _FakeModels:
    def __init__(self, chunks=None, error=None):
        self.chunks, self.error, self.calls = chunks or [], error, 0

    def generate_content_stream(self, **_):
        self.calls += 1
        if self.error is not None:
            raise self.error
        return iter(self.chunks)


def _patch_client(monkeypatch, models):
    from llm.gemini import genai_text_client as gtc

    monkeypatch.setattr(gtc, "_key_pool", lambda fallback: ["k"])
    monkeypatch.setattr(gtc, "_next_client", lambda pool: SimpleNamespace(models=models))
    return gtc


def test_call_gemini_text_returns_and_logs_usage(log_dir, monkeypatch):
    pytest.importorskip("google.genai")
    meta = SimpleNamespace(prompt_token_count=9, cached_content_token_count=None, candidates_token_count=3,
                           thoughts_token_count=7, total_token_count=19)  # fmt: skip
    models = _FakeModels(chunks=[SimpleNamespace(text="he", usage_metadata=None),
                                 SimpleNamespace(text="llo", usage_metadata=meta)])  # fmt: skip
    gtc = _patch_client(monkeypatch, models)
    with calls.llm_call_context(stage="part1", video_id="vid"):
        result = gtc.call_gemini_text(api_key="k", model="gemini-2.5-flash", user_text="hi")
    assert result.text == "hello"
    assert result.usage.thinking_token_count == 7 and result.usage.attempt_count == 1
    assert result.usage.latency_seconds is not None
    (row,) = _rows(log_dir, "llm_calls")
    assert row["provider"] == "gemini" and row["model"] == "gemini-2.5-flash"
    assert (row["input_token_count"], row["output_token_count"], row["thinking_token_count"]) == (9, 3, 7)
    assert row["video_id"] == "vid" and row["is_ok"] is True


def test_call_gemini_text_logs_a_failed_call(log_dir, monkeypatch):
    pytest.importorskip("google.genai")
    gtc = _patch_client(monkeypatch, _FakeModels(error=ValueError("400 INVALID_ARGUMENT bad request")))
    with pytest.raises(ValueError):
        gtc.call_gemini_text(api_key="k", model="gemini-2.5-flash", user_text="hi")
    (row,) = _rows(log_dir, "llm_calls")
    assert row["is_ok"] is False and row["error_type"] == "ValueError" and row["attempt_count"] == 1


def test_retry_attempts_are_counted(monkeypatch):
    from llm.gemini import genai_text_client as gtc

    monkeypatch.setattr(gtc.time, "sleep", lambda s: None)
    outcomes = iter([RuntimeError("503 UNAVAILABLE"), RuntimeError("503 UNAVAILABLE"), "done"])

    def fn():
        value = next(outcomes)
        if isinstance(value, Exception):
            raise value
        return value

    stats: dict = {}
    assert gtc.call_with_genai_quota_retry(fn, label="t", stats=stats) == "done"
    assert stats["attempts"] == 3


# --------------------------------------------------------------------------- #
# GPU steps
# --------------------------------------------------------------------------- #
def _stamp(unix: float) -> str:
    return datetime.fromtimestamp(unix).strftime("%Y/%m/%d %H:%M:%S.%f")[:-3]


def test_gpu_stats_from_samples(tmp_path):
    start = datetime(2026, 11, 13, 12, 0, 0).timestamp()
    samples = tmp_path / "s.csv"
    lines = [gpu.SAMPLE_HEADER]
    lines.append(f"{_stamp(start - 60)}, 0, 99, 9999, 300.0")  # before the step: ignored
    for i, (util, mem, watts) in enumerate([(50, 4000, 60.0), (100, 9000, 80.0)]):
        lines.append(f"{_stamp(start + 5 + i * 5)}, 0, {util}, {mem}, {watts}")
        lines.append(f"{_stamp(start + 5 + i * 5)}, 1, 0, 100, [N/A]")  # a GPU with no power reading
    lines.append("2026/11/13 12:00:2")  # a cut-off last line
    samples.write_text("\n".join(lines) + "\n")
    stats = gpu.gpu_stats(samples, start, start + 3600)
    assert stats["gpu_sample_count"] == 4
    assert stats["avg_gpu_utilization_pct"] == 37.5
    assert stats["max_gpu_memory_used_mb"] == 9000
    assert stats["energy_wh"] == 70.0  # GPU 0's mean 70 W over one hour; GPU 1 reported none


def test_gpu_stats_none_without_samples(tmp_path):
    assert gpu.gpu_stats(tmp_path / "missing.csv", 0, 1) is None
    empty = tmp_path / "e.csv"
    empty.write_text(gpu.SAMPLE_HEADER + "\n")
    assert gpu.gpu_stats(empty, 0, 1) is None


def test_gpu_run_is_a_no_op_without_a_gpu(log_dir, monkeypatch):
    monkeypatch.setattr(gpu, "gpu_names", list)
    with gpu.gpu_run("whisperx_diarization", video_id="v"):
        pass
    assert _rows(log_dir, "gpu_runs") == []


def test_gpu_run_skips_cpu_device_without_asking(log_dir, monkeypatch):
    monkeypatch.setattr(gpu, "gpu_names", lambda: pytest.fail("nvidia-smi must not be asked for a CPU step"))
    with gpu.gpu_run("whisperx_diarization", device="cpu"):
        pass


def test_gpu_run_writes_a_row_even_when_the_step_fails(log_dir, monkeypatch):
    monkeypatch.setattr(gpu, "gpu_names", lambda: ["Tesla T4", "Tesla T4"])
    monkeypatch.setattr(gpu, "start_gpu_sampler", lambda path, interval_ms=5000: None)
    monkeypatch.setattr(gpu, "stop_gpu_sampler", lambda sampler: None)
    with pytest.raises(RuntimeError), gpu.gpu_run("whisperx_diarization", video_id="v", jurisdiction_id="j"):
        raise RuntimeError("out of memory")
    (row,) = _rows(log_dir, "gpu_runs")
    assert row["step_name"] == "whisperx_diarization" and row["video_id"] == "v"
    assert row["gpu_count"] == 2 and row["gpu_name"] == "Tesla T4"
    assert row["gpu_hours"] == pytest.approx(row["wall_seconds"] / 3600 * 2, abs=1e-4)
    assert row["avg_gpu_utilization_pct"] is None  # no samples: not guessed


# --------------------------------------------------------------------------- #
# Prices: a cost only from a sourced, dated price
# --------------------------------------------------------------------------- #
PRICED = {
    "models": {
        "gemini-2.5-flash": {
            "source": "https://example.test/pricing",
            "read_date": "2026-11-13",
            "interactive": {"input_usd_per_1m": 1.0, "cached_input_usd_per_1m": 0.1, "output_usd_per_1m": 10.0},
            "batch": {"input_usd_per_1m": 0.5, "cached_input_usd_per_1m": None, "output_usd_per_1m": 5.0},
        },
        "unsourced": {"source": None, "read_date": None, "interactive": {"input_usd_per_1m": 1.0, "output_usd_per_1m": 1.0}},
    },
    "gpus": {"T4": {"usd_per_gpu_hour": 0.5, "source": "https://example.test/t4", "read_date": "2026-11-04"},
             "L4": {"usd_per_gpu_hour": 0.8, "source": None, "read_date": None}},
}  # fmt: skip


def test_call_cost_uses_each_rate():
    row = {"model": "gemini-2.5-flash", "api_mode": "interactive", "input_token_count": 1_000_000,
           "cached_input_token_count": 400_000, "output_token_count": 100_000, "thinking_token_count": 200_000}  # fmt: skip
    # 600k uncached at $1 + 400k cached at $0.10 + 300k output and thinking at $10
    assert prices.call_cost_usd(row, PRICED) == pytest.approx(0.6 + 0.04 + 3.0)
    assert prices.call_cost_usd({**row, "api_mode": "batch", "cached_input_token_count": None}, PRICED) == pytest.approx(0.5 + 1.5)


def test_call_cost_is_none_without_a_usable_price():
    row = {"model": "gemini-2.5-flash", "api_mode": "interactive", "input_token_count": 10, "output_token_count": 5}
    assert prices.call_cost_usd({**row, "model": "unsourced"}, PRICED) is None
    assert prices.call_cost_usd({**row, "model": "never-heard-of-it"}, PRICED) is None
    assert prices.call_cost_usd({**row, "input_token_count": None}, PRICED) is None
    # cached tokens were used but the batch entry has no cached rate
    assert prices.call_cost_usd({**row, "api_mode": "batch", "cached_input_token_count": 3}, PRICED) is None


def test_model_variant_takes_the_longest_matching_entry():
    both = {"models": {**PRICED["models"], "gemini-2.5-flash-lite": {
        "source": "s", "read_date": "d", "interactive": {"input_usd_per_1m": 0.2, "output_usd_per_1m": 0.4}}}}  # fmt: skip
    assert prices.model_price("gemini-2.5-flash-lite-preview-09", "interactive", both)["input_usd_per_1m"] == 0.2
    assert prices.model_price("gemini-2.5-flash-preview-05-20", "interactive", both)["input_usd_per_1m"] == 1.0


def test_gpu_cost():
    assert prices.gpu_cost_usd({"gpu_name": "Tesla T4", "gpu_hours": 2.0}, PRICED) == 1.0
    assert prices.gpu_cost_usd({"gpu_name": "NVIDIA L4", "gpu_hours": 2.0}, PRICED) is None  # unsourced
    assert prices.gpu_cost_usd({"gpu_name": "NVIDIA H100", "gpu_hours": 2.0}, PRICED) is None


def test_shipped_gemini_flash_prices_cover_interactive_and_batch_from_googles_page():
    """#266: both Gemini 2.5 Flash models are priced in both API modes, sourced from Google's own pricing page."""
    shipped = prices.load_prices()
    for model in ("gemini-2.5-flash", "gemini-2.5-flash-lite"):
        for mode in ("interactive", "batch"):
            rates = prices.model_price(model, mode, shipped)
            assert rates is not None, f"{model} {mode} has no usable price"
            assert rates["source"].startswith("https://ai.google.dev/"), rates["source"]
    batch_call = {"model": "gemini-2.5-flash-lite", "api_mode": "batch",
                  "input_token_count": 1_000_000, "output_token_count": 1_000_000}
    assert prices.call_cost_usd(batch_call, shipped) == pytest.approx(0.05 + 0.20)


def test_shipped_prices_file_has_no_unsourced_price():
    shipped = prices.load_prices()
    assert shipped["models"] and shipped["gpus"]
    for name, entry in shipped["models"].items():
        rates = [v for mode in ("interactive", "batch") for v in (entry.get(mode) or {}).values()]
        if any(v is not None for v in rates):
            assert entry.get("source") and entry.get("read_date"), f"{name} has a price with no source or date"
    for name, entry in shipped["gpus"].items():
        if entry.get("usd_per_gpu_hour") is not None:
            assert entry.get("source") and entry.get("read_date"), f"{name} has a price with no source or date"


# --------------------------------------------------------------------------- #
# Report and loader
# --------------------------------------------------------------------------- #
def _call(day, model, **over):
    return {"llm_call_id": f"{day}-{model}-{len(over)}", "called_at": f"{day}T10:00:00+00:00", "model": model,
            "api_mode": "interactive", "is_ok": True, "attempt_count": 1, "input_token_count": 1_000_000,
            "cached_input_token_count": None, "output_token_count": 100_000, "thinking_token_count": None,
            "total_token_count": 1_100_000, "latency_seconds": 10.0, **over}  # fmt: skip


def test_summarize_calls_groups_and_prices():
    rows = [
        _call("2026-11-12", "gemini-2.5-flash"),
        _call("2026-11-12", "gemini-2.5-flash", latency_seconds=30.0, attempt_count=4),
        _call("2026-11-12", "gemini-2.5-flash", is_ok=False, input_token_count=None, output_token_count=None,
              total_token_count=None, error_type="GenAITransientGiveUp"),
        _call("2026-11-13", "unsourced"),
        _call("2026-11-01", "gemini-2.5-flash"),
    ]
    out = report.summarize_calls(rows, PRICED, since="2026-11-10")
    assert [(r["day"], r["model"]) for r in out] == [("2026-11-12", "gemini-2.5-flash"), ("2026-11-13", "unsourced")]
    flash, unsourced = out
    assert flash["call_count"] == 3 and flash["failed_call_count"] == 1 and flash["retried_call_count"] == 1
    assert flash["input_token_count"] == 2_000_000 and flash["thinking_token_count"] is None
    assert flash["avg_latency_seconds"] == pytest.approx(16.67) and flash["max_latency_seconds"] == 30.0
    assert flash["cost_usd"] == pytest.approx(2 * (1.0 + 1.0))
    assert unsourced["cost_usd"] is None


def test_summarize_calls_by_stage():
    rows = [_call("2026-11-12", "gemini-2.5-flash", stage="part1"), _call("2026-11-12", "gemini-2.5-flash", stage="part2")]
    out = report.summarize_calls(rows, PRICED, extra_keys=["stage"])
    assert [r["stage"] for r in out] == ["part1", "part2"]


def test_summarize_gpu_weights_utilization_by_time():
    rows = [
        {"started_at": "2026-11-12T01:00:00+00:00", "step_name": "whisperx_diarization", "gpu_name": "Tesla T4",
         "wall_seconds": 3000.0, "gpu_hours": 0.8333, "avg_gpu_utilization_pct": 90.0,
         "max_gpu_memory_used_mb": 9000.0, "energy_wh": 50.0},
        {"started_at": "2026-11-12T03:00:00+00:00", "step_name": "whisperx_diarization", "gpu_name": "Tesla T4",
         "wall_seconds": 1000.0, "gpu_hours": 0.2778, "avg_gpu_utilization_pct": 10.0,
         "max_gpu_memory_used_mb": 4000.0, "energy_wh": None},
    ]  # fmt: skip
    (out,) = report.summarize_gpu(rows, PRICED)
    assert out["run_count"] == 2 and out["gpu_hours"] == pytest.approx(1.1111)
    assert out["avg_gpu_utilization_pct"] == 70.0
    assert out["max_gpu_memory_used_mb"] == 9000.0 and out["energy_wh"] == 50.0
    assert out["estimated_cost_usd"] == pytest.approx(1.1111 * 0.5, abs=1e-3)


def test_format_table_shows_a_dash_for_missing():
    text = report.format_table([{"day": "2026-11-12", "cost_usd": None, "call_count": 12345}], ["day", "call_count", "cost_usd"])
    assert text.splitlines()[-1].split() == ["2026-11-12", "12,345", "-"]
    assert report.format_table([], ["day"]) == "(no rows)"


def test_loader_reads_rows_and_skips_bad_lines(log_dir):
    calls.record_llm_call(provider="gemini", model="m", usage=None, is_ok=True)
    path = next(log_dir.glob("llm_calls_*.jsonl"))
    with path.open("a") as fh:
        fh.write("not json\n\n" + json.dumps({"model": "row with no id"}) + "\n")
    counts = load_bronze.load(log_dir, dry_run=True)
    assert counts == {"llm_calls_read": 1, "gpu_runs_read": 0}


def test_loader_columns_match_the_migration():
    sql = (Path(__file__).resolve().parents[3] / "packages/hosting/scripts/neon/migrations" / load_bronze.MIGRATION).read_text().lower()
    for table, columns in load_bronze.TABLES.values():
        body = sql.split(f"create table if not exists {table} (", 1)[1].split(");", 1)[0]
        declared = [line.split()[0] for line in body.splitlines() if line.strip()]
        assert declared == list(columns)
    assert load_bronze.insert_sql("bronze.bronze_llm_call", ("llm_call_id", "model")) == (
        "INSERT INTO bronze.bronze_llm_call (llm_call_id, model) VALUES %s ON CONFLICT (llm_call_id) DO NOTHING RETURNING 1"
    )


def test_recorded_row_has_exactly_the_table_columns(log_dir, monkeypatch):
    calls.record_llm_call(provider="gemini", model="m", usage=None, is_ok=True)
    (row,) = _rows(log_dir, "llm_calls")
    assert set(row) == set(load_bronze.LLM_CALL_COLUMNS)
    monkeypatch.setattr(gpu, "gpu_names", lambda: ["Tesla T4"])
    monkeypatch.setattr(gpu, "start_gpu_sampler", lambda path, interval_ms=5000: None)
    monkeypatch.setattr(gpu, "stop_gpu_sampler", lambda sampler: None)
    with gpu.gpu_run("step"):
        pass
    (gpu_row,) = _rows(log_dir, "gpu_runs")
    assert set(gpu_row) == set(load_bronze.GPU_RUN_COLUMNS)


def test_a_price_past_its_valid_until_gives_no_cost():
    dated = {"models": {"m": {"source": "s", "read_date": "2026-11-14", "valid_until": "2026-12-31",
                              "interactive": {"input_usd_per_1m": 1.0, "output_usd_per_1m": 10.0}}}}  # fmt: skip
    row = {"model": "m", "api_mode": "interactive", "input_token_count": 1_000_000, "output_token_count": 0}
    assert prices.call_cost_usd({**row, "called_at": "2026-12-31T23:59:00+00:00"}, dated) == 1.0
    assert prices.call_cost_usd({**row, "called_at": "2027-01-01T00:00:00+00:00"}, dated) is None
    assert prices.call_cost_usd(row, dated) == 1.0  # no date on the row: the price applies


PEAKED = {"models": {"m": {
    "source": "s", "read_date": "2026-11-14",
    "peak": {"days": ["mon", "tue", "wed", "thu", "fri"], "hours_utc": ["01:00-04:00", "06:00-10:00"]},
    "interactive": {"input_usd_per_1m": 2.0, "output_usd_per_1m": 8.0,
                    "off_peak": {"input_usd_per_1m": 1.0, "output_usd_per_1m": 4.0}},
}}}  # fmt: skip


@pytest.mark.parametrize(
    ("called_at", "input_rate"),
    [
        ("2026-11-18T02:30:00+00:00", 2.0),  # Wednesday, inside 01:00-04:00: peak
        ("2026-11-18T03:59:59+00:00", 2.0),
        ("2026-11-18T04:00:00+00:00", 1.0),  # a window's end is off-peak
        ("2026-11-18T05:00:00+00:00", 1.0),  # between the windows
        ("2026-11-18T06:00:00+00:00", 2.0),  # a window's start is peak
        ("2026-11-18T10:00:00+00:00", 1.0),
        ("2026-11-21T02:30:00+00:00", 1.0),  # Saturday: off-peak all day
        ("2026-11-17T21:30:00-05:00", 2.0),  # 02:30 UTC on Wednesday, written in Eastern time
    ],
)
def test_a_peak_priced_model_takes_the_rate_for_the_calls_utc_time(called_at, input_rate):
    """#328: DeepSeek prices by time of day. The call's UTC time picks the peak or the off-peak rate."""
    assert prices.model_price("m", "interactive", PEAKED, on=called_at)["input_usd_per_1m"] == input_rate


def test_a_peak_priced_model_has_no_price_without_the_time_of_the_call():
    assert prices.model_price("m", "interactive", PEAKED, on="2026-11-18") is None  # a date alone can't choose
    assert prices.model_price("m", "interactive", PEAKED) is None
    row = {"model": "m", "api_mode": "interactive", "input_token_count": 1_000_000, "output_token_count": 0}
    assert prices.call_cost_usd(row, PEAKED) is None


def test_shipped_deepseek_flash_prices_one_call_at_each_time():
    """#328: the shipped deepseek-flash rates, from DeepSeek's own page: a peak call costs twice an off-peak one."""
    shipped = prices.load_prices()
    entry = shipped["models"]["deepseek-flash"]
    assert entry["source"] == "https://api-docs.deepseek.com/quick_start/pricing/"
    row = {"model": "deepseek-flash", "api_mode": "interactive", "input_token_count": 1_000_000,
           "cached_input_token_count": 0, "output_token_count": 1_000_000}  # fmt: skip
    peak = prices.call_cost_usd({**row, "called_at": "2026-11-18T02:30:00+00:00"}, shipped)
    off_peak = prices.call_cost_usd({**row, "called_at": "2026-11-18T05:00:00+00:00"}, shipped)
    assert peak == pytest.approx(0.30 + 1.20)
    assert off_peak == pytest.approx(0.15 + 0.60)
    # the legacy name DeepSeek still accepts is billed at the Flash price
    legacy = prices.call_cost_usd({**row, "model": "deepseek-v4-flash-vision-exp",
                                   "called_at": "2026-11-18T05:00:00+00:00"}, shipped)  # fmt: skip
    assert legacy == off_peak
    assert "deepseek-chat" not in shipped["models"]  # DeepSeek's page neither lists nor accepts it


def test_shipped_prices_cite_google_and_have_batch_rates():
    shipped = prices.load_prices()["models"]
    for name in ("gemini-2.5-flash", "gemini-2.5-flash-lite", "gemini-3.5-flash-lite", "gemini-3.8-flash"):
        entry = shipped[name]
        assert entry["source"].startswith(("https://ai.google.dev/gemini-api/docs/pricing",
                                           "https://aistudio.google.com/docs/pricing")), name  # fmt: skip
        assert prices.model_price(name, "batch", {"models": shipped}) is not None, name
    assert shipped["gemini-3.8-flash"]["valid_until"] == "2026-12-31"