from __future__ import annotations

from mgf.livepush import Broker, MockBroker, Subscription


async def test_mock_broker_delivers_to_prior_subscriber() -> None:
    b = MockBroker()
    sub = await b.subscribe("c1")
    n = await b.publish("c1", "hello")
    assert n == 1
    assert await sub.get_message(timeout=0.1) == b"hello"


async def test_mock_broker_isolates_channels() -> None:
    b = MockBroker()
    s1 = await b.subscribe("c1")
    s2 = await b.subscribe("c2")
    await b.publish("c1", b"x")
    assert await s1.get_message(timeout=0.1) == b"x"
    assert await s2.get_message(timeout=0.05) is None


async def test_mock_broker_no_subscriber_returns_zero() -> None:
    b = MockBroker()
    assert await b.publish("empty", "x") == 0


async def test_mock_broker_only_delivers_to_subscribers_present_at_publish() -> None:
    b = MockBroker()
    await b.publish("c", "missed")  # no subscriber yet
    sub = await b.subscribe("c")
    assert await sub.get_message(timeout=0.05) is None


def test_mock_broker_satisfies_protocol() -> None:
    assert isinstance(MockBroker(), Broker)


async def test_subscription_satisfies_protocol() -> None:
    sub = await MockBroker().subscribe("c")
    assert isinstance(sub, Subscription)
