from __future__ import annotations

import json
from pathlib import Path

import pytest

from jarvis_gateway.adapters.health_sanitizer import assert_no_sensitive_health_output, sanitize_health_probe
from jarvis_gateway.redaction import redact_error_message

FIXTURES = Path(__file__).parent / "fixtures" / "health_probe_synthetic"
FORBIDDEN = ["diagnosis", "diagnose", "befund", "labor", "medication", "medikament", "ocr", "patient", "symptom", "yazio", "apple_health", "blood", "crp", "cholesterol", "faktor", "eliquis", "colchicin", "adalimumab", "hyrimoz", "pdf_text", "drive_web_url", "drive_file_id", "local_original_path", "health_data.db", "/home/agent"]


def test_dirty_health_fixture_is_not_passed_through() -> None:
    raw = json.loads((FIXTURES / "dirty_sensitive_health_response.json").read_text(encoding="utf-8"))
    snapshot = sanitize_health_probe(raw)
    text = json.dumps(snapshot.model_dump(mode="json")).lower()
    for forbidden in FORBIDDEN:
        assert forbidden.lower() not in text


def test_error_redaction_removes_health_paths_and_terms() -> None:
    message = "Traceback File /home/agent/synthetic/health_data.db diagnosis labor medication ocr"
    redacted = redact_error_message(message).lower()
    assert "traceback" not in redacted
    assert "/home/agent" not in redacted
    assert "health_data.db" not in redacted
    assert "diagnosis" not in redacted
    assert "labor" not in redacted


def test_sensitive_output_guard_rejects_forbidden_strings() -> None:
    with pytest.raises(ValueError):
        assert_no_sensitive_health_output({"value": "synthetic diagnosis"})
