import pytest
import tempfile
import os
import json
from privacy_tuner.output.reporter import HTMLReporter

class DummyMechanism:
    __name__ = "DummyMechanism"

def test_html_reporter_basic():
    template_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'privacy_tuner', 'output', 'templates'))
    template_dir = os.path.abspath(template_dir)
    reporter = HTMLReporter(template_dir=template_dir)

    original_info = {"shape": (100, 5)}
    mechanism = DummyMechanism()
    param_space = {"noise": [0.1, 0.5]}
    risk_constraint = (0.0, 1.0)
    all_results = [
        {"risk": 0.1, "utility": 0.9, "params": {"noise": 0.1}},
        {"risk": 0.5, "utility": 0.7, "params": {"noise": 0.5}}
    ]
    best_params = {"noise": 0.1}
    best_utility = 0.9
    best_risk = 0.1

    with tempfile.TemporaryDirectory() as tmpdir:
        output_path = os.path.join(tmpdir, "report.html")
        result_path = reporter.generate(
            original_info=original_info,
            mechanism=mechanism,
            param_space=param_space,
            risk_constraint=risk_constraint,
            all_results=all_results,
            best_params=best_params,
            best_utility=best_utility,
            best_risk=best_risk,
            output_path=output_path
        )
        assert os.path.exists(result_path)
        with open(result_path, 'r', encoding='utf-8') as f:
            content = f.read()
        assert "Privacy Tuner 报告" in content
        assert "DummyMechanism" in content
        assert "noise" in content
        assert "0.9000" in content