import scripts.quant.run_all as ra
from scripts.quant.aggregate import SUMMARY_FIELDS


def test_run_method_failure_becomes_failed_row(monkeypatch):
    def boom(*a, **k):
        raise RuntimeError("OOM at load")

    monkeypatch.setattr(ra, "_measure_method", boom)
    row = ra.run_method("gptq", base_model="b", work_dir=ra.Path("/tmp/x"),
                        eval_images=[], reference_dir=ra.Path("/tmp/ref"))
    assert row["method"] == "gptq"
    assert row["status"] == "failed"
    assert "OOM at load" in row["error"]
    assert set(row.keys()) == set(SUMMARY_FIELDS)


def test_run_method_success_passes_through_metrics(monkeypatch):
    def fake_measure(method, base_model, work_dir, eval_images, reference_dir):
        return {"mean_cer": 0.01, "mean_bbox_iou": 0.98, "mean_latency_s": 4.2,
                "model_size_mb": 512.0}

    monkeypatch.setattr(ra, "_measure_method", fake_measure)
    row = ra.run_method("awq", base_model="b", work_dir=ra.Path("/tmp/x"),
                        eval_images=[], reference_dir=ra.Path("/tmp/ref"))
    assert row["status"] == "ok"
    assert row["mean_cer"] == 0.01
    assert row["t4_deployable"] is True
    assert row["mean_latency_s"] == 4.2
