from __future__ import annotations import json import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[2] def write_hard_negative_summary( path: Path, *, category: str, detection_counts: list[int], ) -> None: items = [ { "sample_slug": f"{category}_{index}", "background_category": category, "model_asset_id": "candidate-context-sensitive", "tile_size": 512, "tile_overlap": 64, "threshold": 0.35, "tile_count": 4, "detection_count": detection_count, "false_positive_pressure": detection_count / 4, } for index, detection_count in enumerate(detection_counts, start=1) ] path.write_text( json.dumps( { "generated_at": "2026-07-10T00:00:00+00:00", "background_category_counts": {category: len(items)}, "items": items, } ), encoding="utf-8", ) def test_promotion_report_uses_split_pure_empty_as_gate_and_sparse_context_as_review( tmp_path: Path, ) -> None: script_path = ROOT / "scripts" / "build_detection_model_promotion_report.py" positive_path = tmp_path / "positive_portfolio.json" pure_summary_path = tmp_path / "pure_empty_summary.json" sparse_summary_path = tmp_path / "sparse_context_summary.json" split_summary_path = tmp_path / "background_corpus_split_summary.json" output_dir = tmp_path / "promotion-report" positive_path.write_text( json.dumps( { "items": [ { "sample_slug": "geel", "model_asset_id": "candidate-context-sensitive", "tile_size": 512, "tile_overlap": 64, "threshold": 0.35, "precision": 0.72, "recall": 0.5, "f1_score": 0.59, }, { "sample_slug": "mol", "model_asset_id": "candidate-context-sensitive", "tile_size": 512, "tile_overlap": 64, "threshold": 0.35, "precision": 0.68, "recall": 0.48, "f1_score": 0.56, }, ] } ), encoding="utf-8", ) write_hard_negative_summary( pure_summary_path, category="pure_empty_negative", detection_counts=[0, 0], ) write_hard_negative_summary( sparse_summary_path, category="sparse_building_context", detection_counts=[4, 7], ) split_summary_path.write_text( json.dumps( { "schema_version": 1, "source_summaries": { "pure_empty_negative": str(pure_summary_path), "sparse_building_context": str(sparse_summary_path), }, "strict_default_gate": { "category": "pure_empty_negative", "review_only": False, "sample_count": 2, "run_count": 2, "total_detection_count": 0, "max_detection_count": 0, "passes_zero_detection_gate": True, }, "context_review": { "category": "sparse_building_context", "review_only": True, "sample_count": 2, "run_count": 2, "total_detection_count": 11, "max_detection_count": 7, }, } ), encoding="utf-8", ) result = subprocess.run( [ "python", str(script_path), "--positive-portfolio", str(positive_path), "--background-split-summary", str(split_summary_path), "--output-dir", str(output_dir), "--min-positive-samples", "2", "--min-background-samples", "2", "--min-mean-f1", "0.5", "--max-background-detections-per-sample", "0", ], cwd=ROOT, check=True, text=True, capture_output=True, ) assert "Detection model promotion report passed" in result.stdout report = json.loads((output_dir / "detection_model_promotion_report.json").read_text(encoding="utf-8")) decision = report["candidate_decisions"][0] assert report["hard_negative_summary_paths"] == [str(pure_summary_path)] assert report["background_split_summary_paths"] == [str(split_summary_path)] assert report["background_context_reviews"] == [ { "source_split_summary_path": str(split_summary_path), "source_summary_path": str(sparse_summary_path), "category": "sparse_building_context", "review_only": True, "sample_count": 2, "run_count": 2, "total_detection_count": 11, "max_detection_count": 7, } ] assert decision["candidate_key"] == "candidate-context-sensitive|512|64|0.35" assert decision["background_sample_count"] == 2 assert decision["max_background_detections"] == 0 assert decision["promotion_status"] == "promote_candidate" assert report["recommended_candidate"]["candidate_key"] == decision["candidate_key"] markdown = (output_dir / "detection_model_promotion_report.md").read_text(encoding="utf-8") assert "Background split summaries: 1" in markdown assert "Sparse-context review evidence" in markdown assert "not used as a default-promotion gate" in markdown