import pytest
import numpy as np
from typing import Dict, Any, Tuple, Optional
from privacy_tuner.core.optimizer import Optimizer
from privacy_tuner.core.privacy_mechanism import PrivacyMechanism
from privacy_tuner.core.risk_meter import RiskMeter
from privacy_tuner.core.utility_evaluator import UtilityEvaluator

# 虚拟机制、风险、效用
class DummyMechanism(PrivacyMechanism):
    def apply(self, data: np.ndarray, params: Dict[str, Any]) -> np.ndarray:
        return data

class DummyRiskMeter(RiskMeter):
    def measure(self, original: np.ndarray, perturbed: np.ndarray) -> float:
        return 0.1

class DummyUtility(UtilityEvaluator):
    def evaluate(self, original: np.ndarray, perturbed: np.ndarray,
                 task: Optional[Dict[str, Any]] = None) -> float:
        return 0.9

# 虚拟优化器
class DummyOptimizer(Optimizer):
    def search(self,
               original_data: np.ndarray,
               mechanism: PrivacyMechanism,
               risk_meter: RiskMeter,
               utility_evaluator: UtilityEvaluator,
               param_space: Dict[str, Any],
               risk_constraint: Tuple[float, float],
               max_iter: int = 100) -> Tuple[Dict[str, Any], float, float]:
        return {"dummy": 1}, 0.9, 0.1

def test_optimizer_interface():
    opt = DummyOptimizer()
    mechanism = DummyMechanism()
    risk = DummyRiskMeter()
    utility = DummyUtility()
    data = np.random.randn(10, 5)
    param_space = {"param1": [1, 2, 3]}
    constraint = (0.0, 1.0)
    best_params, best_utility, best_risk = opt.search(
        data, mechanism, risk, utility, param_space, constraint, max_iter=10
    )
    assert isinstance(best_params, dict)
    assert isinstance(best_utility, float)
    assert isinstance(best_risk, float)
    assert best_params == {"dummy": 1}
    assert best_utility == 0.9
    assert best_risk == 0.1