Files
seismo-relay/tests/test_check_s3_decode.py
T
serversdownandClaude Opus 5.5 c0d837c14f fix(checker): final-review fixes -- regressions, baselines, the PVS carve-out, the headline
- A matching event that now fails outright is a regression: compare_runs
  walks the previous run's matching rows, so a check that collapses to
  one error row no longer hides every field it used to match.  Counts
  moved are taken over the events both runs checked.
- The default baseline is the newest completed run (run.json written) of
  the same scope; a 2,000-event sample is no baseline for a full run.
  changes.md names the previous run's scope.
- The PVS saturation carve-out now needs Blastware's OORANGE, a channel
  Blastware clamped at SFM's ceiling, and SFM's sum within three channels
  at the ceiling -- an unclipped Sensitive-range channel read 8x can no
  longer let an 8x sum through.
- 'Fields fully matching' needs a match: an always-empty element proves
  nothing.  Events without a report are a line of their own.
- CLAUDE.md: histogram PVS described correctly; 35 sub-minute histograms
  plus 2 one-minute no-mic ones, not '~37 sub-minute'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9
2026-10-29 20:11:33 -04:00

174 lines
8.0 KiB
Python

"""
The decode checker's runner and reports (decode checker, Tasks 5-7).
Runs on the fixture folder, which holds the binaries and their XML reports
side by side.
"""
from __future__ import annotations
import gzip
import importlib
import json
from pathlib import Path
run = importlib.import_module("scratch.check_s3_decode")
FIX = Path(__file__).parent / "fixtures/decode-checker-10-29-26"
def test_find_pairs_dedupes_and_pairs(tmp_path):
(tmp_path / "Sent").mkdir()
for n in ("R352HIUI.F40W", "Sent/R352HIUI.F40W", "UM11719_20240404193109.IDFH"):
(tmp_path / n).write_bytes(b"x")
pairs = run.find_pairs(tmp_path, FIX)
assert [p[0] for p in pairs] == ["R352HIUI.F40W"]
assert pairs[0][2].name == "R352HIUI_F40W_XML.XML"
def test_check_event_rows_and_attrs():
out = run.check_event("M007HVWF.K30W", str(FIX / "M007HVWF.K30W"), str(FIX / "M007HVWF_K30W_XML.XML"))
assert out["attrs"]["range"] == "1.250" and out["attrs"]["model"] == "MiniMate Plus"
assert any(r[0] == "TranPPV" and r[1] == "mismatch" for r in out["results"])
def test_bad_input_is_one_error_row(tmp_path):
bad = tmp_path / "R352HIUI.F40W"
bad.write_bytes(b"not a blastware file")
out = run.check_event(bad.name, str(bad), str(FIX / "R352HIUI_F40W_XML.XML"))
assert out["results"] == [["event", "error", None, None, out["results"][0][4], None]]
def test_missing_reference_is_one_no_reference_row():
out = run.check_event("R352HIUI.F40W", str(FIX / "R352HIUI.F40W"), None)
assert [r[:2] for r in out["results"]] == [["event", "no_reference"]]
def test_cli_writes_results(tmp_path):
assert run.main([str(FIX), str(FIX), "--out", str(tmp_path / "run"), "--jobs", "1"]) == 0
lines = gzip.open(tmp_path / "run/results.jsonl.gz", "rt").read().splitlines()
assert len(lines) == 7
assert json.loads((tmp_path / "run/run.json").read_text())["events"] == 7
def test_field_filter_keeps_only_matching_rows(tmp_path):
assert run.main([str(FIX), str(FIX), "--out", str(tmp_path / "run"), "--jobs", "1",
"--field", "samples."]) == 0
for line in gzip.open(tmp_path / "run/results.jsonl.gz", "rt"):
assert all(r[0].startswith("samples.") for r in json.loads(line)["results"])
# ── Task 6: summary and detailed report ─────────────────────────────────────
report = importlib.import_module("scratch.decode_check_report")
def _write_results(path, events):
with gzip.open(path, "wt") as fh:
for ev in events:
fh.write(json.dumps(ev) + "\n")
def _ev(name, sr, verdict):
return {"file": name, "attrs": {"sample_rate": sr, "year": "2024"},
"results": [["SampleRate", verdict, sr + " sps", "1024", "", None]]}
def test_details_group_failures_by_what_they_share(tmp_path):
res = tmp_path / "results.jsonl.gz"
_write_results(res, [_ev("A.W", "2048", "mismatch"), _ev("B.W", "2048", "mismatch"),
_ev("C.W", "1024", "match")])
agg = report.aggregate(res)
report.write_details(agg, tmp_path / "details.json")
d = json.loads((tmp_path / "details.json").read_text())["SampleRate|mismatch"]
assert d["total"] == 2
assert d["groups"]["sample_rate"] == [
{"value": "2048", "failing": 2, "compared": 2, "rate": 1.0, "concentrated": True}]
assert d["examples"] and all("--file" in e["rerun"] for e in d["examples"])
def test_summary_counts_fields_fully_matching(tmp_path):
res = tmp_path / "results.jsonl.gz"
_write_results(res, [_ev("A.W", "2048", "mismatch"), _ev("C.W", "1024", "match")])
report.write_summary(report.aggregate(res), tmp_path / "summary.md")
text = (tmp_path / "summary.md").read_text()
assert "0 of 1 fields fully matching" in text and "| SampleRate |" in text
def test_a_run_writes_both_reports(tmp_path):
assert run.main([str(FIX), str(FIX), "--out", str(tmp_path / "run"), "--jobs", "1"]) == 0
assert (tmp_path / "run/summary.md").exists() and (tmp_path / "run/details.json").exists()
# ── Task 7: run-to-run changes and the one-event view ───────────────────────
def _one(name, field, verdict):
return {"file": name, "attrs": {}, "results": [[field, verdict, "x", "y", "", None]]}
def test_compare_runs_finds_regressions_and_fixes(tmp_path):
prev, cur = tmp_path / "prev.jsonl.gz", tmp_path / "cur.jsonl.gz"
_write_results(prev, [_one("A.W", "SampleRate", "match"), _one("B.W", "notes", "sfm_blank")])
_write_results(cur, [_one("A.W", "SampleRate", "mismatch"), _one("B.W", "notes", "match")])
chg = report.compare_runs(prev, cur)
assert chg["regressions"] == [{"file": "A.W", "field": "SampleRate",
"before": "match", "after": "mismatch"}]
assert chg["fixed"] == 1
report.write_changes(chg, tmp_path / "changes.md")
assert "A.W" in (tmp_path / "changes.md").read_text()
def test_compare_to_marks_the_summary(tmp_path):
assert run.main([str(FIX), str(FIX), "--out", str(tmp_path / "a"), "--jobs", "1"]) == 0
assert run.main([str(FIX), str(FIX), "--out", str(tmp_path / "b"), "--jobs", "1",
"--compare-to", str(tmp_path / "a")]) == 0
assert "**0 regressions**" in (tmp_path / "b/summary.md").read_text()
assert (tmp_path / "b/changes.md").exists()
def test_file_prints_one_event(tmp_path, capsys):
assert run.main([str(FIX), str(FIX), "--file", "M007HVWF.K30W"]) == 0
out = capsys.readouterr().out
assert any("TranPPV" in line and "mismatch" in line for line in out.splitlines())
# ── Final review fixes ──────────────────────────────────────────────────────
def test_a_matching_event_that_now_errors_is_a_regression(tmp_path):
prev, cur = tmp_path / "prev.jsonl.gz", tmp_path / "cur.jsonl.gz"
_write_results(prev, [_one("A.W", "SampleRate", "match"), _one("B.W", "notes", "match")])
_write_results(cur, [_one("A.W", "event", "error"), _one("B.W", "notes", "match")])
chg = report.compare_runs(prev, cur)
assert chg["regressions"] == [{"file": "A.W", "field": "SampleRate",
"before": "match", "after": "error"}]
def test_counts_moved_cover_only_shared_events(tmp_path):
prev, cur = tmp_path / "prev.jsonl.gz", tmp_path / "cur.jsonl.gz"
_write_results(prev, [_one("A.W", "SampleRate", "match")])
_write_results(cur, [_one("A.W", "SampleRate", "match"), _one("B.W", "SampleRate", "match")])
assert report.compare_runs(prev, cur)["fields"]["SampleRate"]["match"] == [1, 1]
def test_default_comparison_is_the_newest_completed_run_of_the_same_scope(tmp_path, monkeypatch):
runs = tmp_path / "runs"
monkeypatch.setattr(run, "RUNS_DIR", runs)
base = [str(FIX), str(FIX), "--jobs", "1"]
assert run.main(base + ["--out", str(runs / "20990101-000001")]) == 0 # same scope
assert run.main(base + ["--out", str(runs / "20990101-000002"), "--type", "W"]) == 0 # narrower
(runs / "20990101-000003").mkdir() # never finished
assert run.main(base + ["--out", str(runs / "20990101-000004")]) == 0
assert "20990101-000001" in (runs / "20990101-000004/changes.md").read_text().splitlines()[0]
def test_fields_fully_matching_need_a_match_and_skip_the_event_row(tmp_path):
res = tmp_path / "results.jsonl.gz"
_write_results(res, [
{"file": "A.W", "attrs": {}, "results": [["SampleRate", "match", "1", "1", "", None],
["UsbSummaryChannels", "no_reference", "", None, "", None]]},
{"file": "B.W", "attrs": {}, "results": [["event", "no_reference", None, None, "no XML report", None]]},
])
report.write_summary(report.aggregate(res), tmp_path / "summary.md")
text = (tmp_path / "summary.md").read_text()
assert "1 of 2 fields fully matching" in text
assert "1 event without a report" in text and "| event |" not in text