# recoil/pipeline/tests/test_calibrate_models_smoke.py
"""Smoke test for the Phase 0.5 calibration script. Mocks all external API
calls and asserts the script writes a report and merges values into the
model_profiles.json fixture."""

import json
from pathlib import Path
from unittest.mock import patch

import pytest


def test_calibrate_models_runs_end_to_end_with_mocks(tmp_path, monkeypatch):
    from recoil.pipeline.tools import calibrate_models

    # Stage a fake model_profiles.json with null empirical fields.
    # schema_version is required by the Pydantic schema.
    fake_config = tmp_path / "model_profiles.json"
    fake_config.write_text(json.dumps({
        "schema_version": 1,
        "gemini-3-pro-image-preview": {
            "provider": "google",
            "max_character_refs": 5,
            "effective_max_character_refs": None,
            "position_bias_severity": None,
            "grid_reference_failure_rate": None,
            "content_filter_block_rate": None,
        },
        "veo-3.1-generate-preview": {
            "provider": "google",
            "max_subjects_per_generation": None,
        },
    }))
    fake_report = tmp_path / "phase_0.5_calibration_report.md"

    # Mock the test runners — calibration just calls them and merges results
    with patch.object(calibrate_models, "run_attention_dilution_sweep", return_value={"effective_max_character_refs": 5}), \
         patch.object(calibrate_models, "run_position_bias_check", return_value={"position_bias_severity": "low"}), \
         patch.object(calibrate_models, "run_safety_filter_baseline", return_value={"content_filter_block_rate": 0.05}), \
         patch.object(calibrate_models, "run_grid_ingestion_check", return_value={"grid_reference_failure_rate": 0.0}), \
         patch.object(calibrate_models, "run_veo_subject_limit_check", return_value={"max_subjects_per_generation": 1}):
        calibrate_models.main(profiles_path=fake_config, report_path=fake_report)

    merged = json.loads(fake_config.read_text())
    assert merged["gemini-3-pro-image-preview"]["effective_max_character_refs"] == 5
    assert merged["gemini-3-pro-image-preview"]["position_bias_severity"] == "low"
    assert merged["gemini-3-pro-image-preview"]["content_filter_block_rate"] == 0.05
    assert merged["gemini-3-pro-image-preview"]["grid_reference_failure_rate"] == 0.0
    assert merged["veo-3.1-generate-preview"]["max_subjects_per_generation"] == 1
    assert fake_report.exists()
    assert "Calibration Report" in fake_report.read_text()
