|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 AIwork4me |
| 3 | +"""CPU tests for the hunyuan-ocr benchmark + report subcommands (no network/GPU).""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import hashlib |
| 8 | +import json |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from hunyuan_ocr import cli |
| 12 | + |
| 13 | + |
| 14 | +# --- benchmark --------------------------------------------------------------- |
| 15 | + |
| 16 | + |
| 17 | +def test_benchmark_prints_lock_results(tmp_path, capsys): |
| 18 | + lock = tmp_path / "reproducibility.lock.yaml" |
| 19 | + lock.write_text( |
| 20 | + "benchmark:\n canary_148:\n vllm_overall: 94.81\n llamacpp_overall: 93.33\n", |
| 21 | + encoding="utf-8", |
| 22 | + ) |
| 23 | + rc = cli.main(["benchmark", "--lock", str(lock)]) |
| 24 | + assert rc == 0 |
| 25 | + out = capsys.readouterr().out |
| 26 | + assert "94.81" in out and "93.33" in out and "BEGIN GENERATED RESULTS" in out |
| 27 | + |
| 28 | + |
| 29 | +def test_benchmark_missing_lock(tmp_path): |
| 30 | + rc = cli.main(["benchmark", "--lock", str(tmp_path / "nope.yaml")]) |
| 31 | + assert rc == 2 |
| 32 | + |
| 33 | + |
| 34 | +# --- report ------------------------------------------------------------------ |
| 35 | + |
| 36 | + |
| 37 | +def _write_manifest(pred_dir: Path): |
| 38 | + manifest = { |
| 39 | + "schema_version": 2, |
| 40 | + "repo_commit": "abc123", |
| 41 | + "backend": "llamacpp", |
| 42 | + "model": "HYVL", |
| 43 | + "timestamp_iso": "2026-07-18T03:00:00Z", |
| 44 | + "status": "ok", |
| 45 | + "run_counts": {"attempted": 1, "succeeded": 1, "failed": 0, "skipped": 0, "interrupted": 0}, |
| 46 | + "final_state": {"expected": 1, "complete": 1, "failed": 0, "pending": 0}, |
| 47 | + "command": ["run_inference.py", "--backend-name", "llamacpp"], |
| 48 | + "env": {"torch": "2.9.1"}, |
| 49 | + "platform": {"python": "3.12.3"}, |
| 50 | + } |
| 51 | + pred_dir.mkdir(parents=True, exist_ok=True) |
| 52 | + (pred_dir / "run_manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8") |
| 53 | + return manifest |
| 54 | + |
| 55 | + |
| 56 | +def test_report_assembles_bundle_with_checksums(tmp_path): |
| 57 | + pred = tmp_path / "pred" |
| 58 | + _write_manifest(pred) |
| 59 | + repo_root = tmp_path / "repo" |
| 60 | + repo_root.mkdir() |
| 61 | + (repo_root / "reproducibility.lock.yaml").write_text("hunyuanocr_rocm:\n commit: x\n", encoding="utf-8") |
| 62 | + out = tmp_path / "artifact" |
| 63 | + |
| 64 | + rc = cli.main(["report", "--pred-dir", str(pred), "--out", str(out), "--repo-root", str(repo_root)]) |
| 65 | + assert rc == 0 |
| 66 | + |
| 67 | + assert (out / "run_manifest.json").is_file() |
| 68 | + assert (out / "environment.json").is_file() |
| 69 | + assert (out / "commands.txt").is_file() |
| 70 | + assert (out / "reproducibility.lock.yaml").is_file() |
| 71 | + assert (out / "README.md").is_file() |
| 72 | + # checksums cover every other file and verify |
| 73 | + sums = (out / "checksums.sha256").read_text(encoding="utf-8").strip().splitlines() |
| 74 | + assert len(sums) == 5 |
| 75 | + for line in sums: |
| 76 | + digest, name = line.split(" ", 1) |
| 77 | + assert hashlib.sha256((out / name).read_bytes()).hexdigest() == digest |
| 78 | + |
| 79 | + |
| 80 | +def test_report_missing_manifest(tmp_path): |
| 81 | + pred = tmp_path / "empty" |
| 82 | + pred.mkdir() |
| 83 | + rc = cli.main(["report", "--pred-dir", str(pred), "--out", str(tmp_path / "o"), "--repo-root", str(tmp_path)]) |
| 84 | + assert rc == 2 |
0 commit comments