import pytest
import numpy as np
from sklearn.datasets import make_blobs
from privacy_tuner.core.utility.clustering_silhouette import ClusteringSilhouette

def test_clustering_silhouette_similar():
    # 生成相同的数据，效用应为1
    X, _ = make_blobs(n_samples=100, centers=3, random_state=42)
    evaluator = ClusteringSilhouette(n_clusters=3, random_state=42)
    utility = evaluator.evaluate(X, X)
    assert utility == 1.0

def test_clustering_silhouette_different():
    X, _ = make_blobs(n_samples=100, centers=3, random_state=42)
    # 对数据加大量噪声，破坏聚类结构
    X_noisy = X + np.random.normal(0, 5, size=X.shape)
    evaluator = ClusteringSilhouette(n_clusters=3, random_state=42)
    utility = evaluator.evaluate(X, X_noisy)
    assert 0.0 <= utility <= 1.0
    assert utility < 0.9  # 应该较低

def test_clustering_silhouette_few_samples():
    X = np.random.randn(2, 5)  # 只有2个样本，无法聚类
    evaluator = ClusteringSilhouette(n_clusters=3)
    utility = evaluator.evaluate(X, X)
    assert utility == 0.0