#!/usr/bin/env python3
import json
import sys
from pathlib import Path
import numpy as np

# Add scripts/ to path
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT / "scripts"))

from qubo_solve import solve_qubo_neal

def test_qubo_roundtrip_minik():
    # toy: n=8, target k=2, lambda=1.0
    w = np.linspace(-1, 1, 8)
    qubo = {"n": 8, "k": 2, "lambda_budget": 1.0,
            "linear": {str(i): float(-(w[i]**2) + 1.0*(1-2*2)) for i in range(8)},
            "quadratic": {f"{i},{j}": 2.0 for i in range(8) for j in range(i+1,8)}}
    res = solve_qubo_neal(qubo, seed=0, sweeps=50, num_reads=1)
    assert "metrics" in res and "proxies" in res
    assert res["certificate"]["energy_match"] is True
    print(f"✅ QUBO roundtrip test passed")
    print(f"   Energy: {res['energy']}")
    print(f"   k_selected: {res['k_selected']}")
    print(f"   Metrics: {res['metrics']}")
    print(f"   Proxies: {res['proxies']}")
    print(f"   Certificate: energy_match={res['certificate']['energy_match']}")


if __name__ == "__main__":
    test_qubo_roundtrip_minik()
