from __future__ import annotations

import pytest

from mgf.livepush import MockBroker, SlotCap


class FakeRedis:
    """Minimal async Redis double for SlotCap tests (INCR/DECR/EXPIRE/SET)."""

    def __init__(self) -> None:
        self.store: dict[str, int] = {}
        self.fail = False

    async def incr(self, key: str) -> int:
        if self.fail:
            raise RuntimeError("redis down")
        self.store[key] = self.store.get(key, 0) + 1
        return self.store[key]

    async def decr(self, key: str) -> int:
        if self.fail:
            raise RuntimeError("redis down")
        self.store[key] = self.store.get(key, 0) - 1
        return self.store[key]

    async def expire(self, key: str, ttl: int) -> bool:
        return True

    async def set(self, key: str, value: int, ex: int | None = None) -> None:
        self.store[key] = value


@pytest.fixture
def fake_redis() -> FakeRedis:
    return FakeRedis()


@pytest.fixture
def broker() -> MockBroker:
    return MockBroker()


@pytest.fixture
def slot_cap(fake_redis: FakeRedis) -> SlotCap:
    return SlotCap(fake_redis, key_prefix="t:")
