from __future__ import annotations

import json
from pathlib import Path

from jarvis_gateway.adapters.health_probe import HealthLocalProbeAdapter
from jarvis_gateway.adapters.health_sanitizer import sanitize_health_probe

FIXTURES = Path(__file__).parent / "fixtures" / "health_probe_synthetic"


def test_local_probe_reads_only_metadata(tmp_path: Path) -> None:
    (tmp_path / "safe_metadata.marker").write_text("synthetic content that must not appear", encoding="utf-8")
    adapter = HealthLocalProbeAdapter(str(tmp_path))
    snapshot = adapter.get_snapshot()
    text = json.dumps(snapshot.model_dump(mode="json")).lower()
    assert snapshot.module_id == "health"
    assert snapshot.source_health.source_type == "local_probe"
    assert "synthetic content" not in text
    assert str(tmp_path).lower() not in text


def test_fresh_synthetic_probe_maps_to_safe_snapshot() -> None:
    raw = json.loads((FIXTURES / "fresh_pipeline.json").read_text(encoding="utf-8"))
    snapshot = sanitize_health_probe(raw)
    assert snapshot.status == "ok"
    assert len(snapshot.kpis) <= 3
    assert snapshot.kpis[0].key == "pipeline_status"


def test_stale_synthetic_probe_adds_safe_attention() -> None:
    raw = json.loads((FIXTURES / "stale_pipeline.json").read_text(encoding="utf-8"))
    snapshot = sanitize_health_probe(raw)
    assert snapshot.status == "attention"
    assert snapshot.attention_items
    titles = {item.title for item in snapshot.attention_items}
    assert "Bericht muss geprüft werden" in titles
