"""Tests for buffer interface compliance."""
import asyncio

import pytest


def test_event_buffer_is_subclass_of_base():
    """Test that EventBuffer is a subclass of BufferBase."""
    from collector.buffer import EventBuffer
    from collector.buffer_base import BufferBase

    assert issubclass(EventBuffer, BufferBase)


@pytest.mark.asyncio
async def test_publish_and_subscribe(make_event):
    """Test basic publish and subscribe functionality."""
    from collector.buffer import EventBuffer

    buf = EventBuffer()
    queue = await buf.subscribe("s1")
    event = make_event()
    await buf.publish("s1", event)
    received = await asyncio.wait_for(queue.get(), timeout=1.0)
    assert received.id == event.id
    await buf.unsubscribe("s1", queue)


@pytest.mark.asyncio
async def test_get_events(make_event):
    """Test retrieving stored events."""
    from collector.buffer import EventBuffer

    buf = EventBuffer()
    event1 = make_event(session_id="s1", name="event1")
    event2 = make_event(session_id="s1", name="event2")

    await buf.publish("s1", event1)
    await buf.publish("s1", event2)

    events = await buf.get_events("s1")
    assert len(events) == 2
    assert events[0].name == "event1"
    assert events[1].name == "event2"


@pytest.mark.asyncio
async def test_get_session_ids(make_event):
    """Test retrieving session IDs."""
    from collector.buffer import EventBuffer

    buf = EventBuffer()
    await buf.publish("s1", make_event(session_id="s1"))
    await buf.publish("s2", make_event(session_id="s2"))

    session_ids = await buf.get_session_ids()
    assert set(session_ids) == {"s1", "s2"}
