"""Compara baseline Python vs respuesta del microservicio Go.""" from __future__ import annotations import json import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SC_ROOT = Path(os.environ.get("SERVICES_COUNT_ROOT", ROOT.parent / "services-count")) BASELINE = ROOT / "data" / "parity_baseline.json" API_URL = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080/v1/count" def call_api(pdf_path: Path) -> dict: result = subprocess.run( ["curl", "-s", "-F", f"files=@{pdf_path}", API_URL], capture_output=True, text=True, check=True, ) payload = json.loads(result.stdout) for item in payload.get("results", []): if item.get("filename") == pdf_path.name: return item raise RuntimeError(f"sin resultado para {pdf_path.name}") def main() -> None: if not BASELINE.exists(): raise SystemExit("Ejecuta primero: python scripts/parity_baseline.py") import yaml config_path = SC_ROOT / "config" / "default.yaml" pdfs_dir = SC_ROOT / yaml.safe_load(config_path.read_text())["paths"]["pdfs_dir"] baseline = json.loads(BASELINE.read_text(encoding="utf-8")) mismatches = 0 for expected in baseline: pdf = pdfs_dir / expected["filename"] if not pdf.exists(): print(f"SKIP {expected['filename']} (no existe)") continue got = call_api(pdf) if got.get("service_count") != expected.get("service_count"): mismatches += 1 print(f"MISMATCH {expected['filename']}: py={expected['service_count']} go={got.get('service_count')}") else: print(f"OK {expected['filename']}: count={got.get('service_count')}") if mismatches: raise SystemExit(f"{mismatches} diferencias encontradas") print("Paridad OK") if __name__ == "__main__": main()