import json
import logging

from backend.bootstrap.logging import JsonFormatter, redact


def test_nested_sensitive_values_are_redacted() -> None:
    payload = {
        "account": "leefer",
        "api_key": "private-key",
        "nested": {"refresh-token": "private-token", "count": 3},
    }

    assert redact(payload) == {
        "account": "leefer",
        "api_key": "[REDACTED]",
        "nested": {"refresh-token": "[REDACTED]", "count": 3},
    }


def test_json_formatter_keeps_context_without_secret_values() -> None:
    record = logging.LogRecord(
        "test", logging.INFO, __file__, 1, "saved token=plain-secret", (), None
    )
    record.request_id = "request-1"
    record.context = {"token": "hidden", "feature": "health"}

    payload = json.loads(JsonFormatter().format(record))

    assert payload["timestamp"].endswith("+08:00")
    assert payload["request_id"] == "request-1"
    assert payload["message"] == "saved token=[REDACTED]"
    assert payload["context"] == {"token": "[REDACTED]", "feature": "health"}
