from scripts.quant.aggregate import SUMMARY_FIELDS, build_row, rows_to_csv, rows_to_markdown


def test_build_row_defaults_unset_fields_to_none():
    row = build_row(method="awq", status="ok", t4_deployable=True, mean_cer=0.01)
    assert row["method"] == "awq"
    assert row["status"] == "ok"
    assert row["t4_deployable"] is True
    assert row["mean_cer"] == 0.01
    assert row["throughput_rps"] is None
    assert set(row.keys()) == set(SUMMARY_FIELDS)


def test_build_row_rejects_unknown_field():
    try:
        build_row(method="awq", bogus=1)
        assert False, "expected KeyError"
    except KeyError as exc:
        assert "bogus" in str(exc)


def test_rows_to_csv_has_header_and_order():
    rows = [build_row(method="bf16", status="ok")]
    csv_text = rows_to_csv(rows)
    assert csv_text.splitlines()[0] == ",".join(SUMMARY_FIELDS)
    assert "bf16" in csv_text.splitlines()[1]


def test_rows_to_markdown_renders_failed_row():
    rows = [build_row(method="gptq", status="failed", error="OOM at load")]
    md = rows_to_markdown(rows)
    assert "| gptq |" in md
    assert "failed" in md
    assert "OOM at load" in md
