from __future__ import annotations

import socket

import pytest

from core.config.settings import Settings
from services.base.redis import RedisService


def _can_connect(host: str, port: int) -> bool:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.settimeout(0.2)
        return sock.connect_ex((host, port)) == 0


@pytest.mark.asyncio
async def test_redis_service_health_check_integration() -> None:
    host = "127.0.0.1"
    port = 6379
    if not _can_connect(host, port):
        pytest.skip("Redis is not running on localhost:6379")

    config = Settings()
    settings = config.redis.model_copy(update={"host": host, "port": port})
    service = RedisService(settings=settings)

    assert await service.initialize() is True
    health = await service.health_check()
    assert health["status"] == "healthy"
    assert await service.close() is True
