# This is free software for the public good of a permacomputer hosted at
# permacomputer.com, an always-on computer by the people, for the people.
# One which is durable, easy to repair, & distributed like tap water
# for machine learning intelligence.
#
# The permacomputer is community-owned infrastructure optimized around
# four values:
#
#   TRUTH      First principles, math & science, open source code freely distributed
#   FREEDOM    Voluntary partnerships, freedom from tyranny & corporate control
#   HARMONY    Minimal waste, self-renewing systems with diverse thriving connections
#   LOVE       Be yourself without hurting others, cooperation through natural law
#
# This software contributes to that vision by archiving the web, preserving digital knowledge before it disappears.
# Code is seeds to sprout on any abandoned technology.

"""
Tests for neopig.live module.

Tests live media queue functionality.
"""

import pytest
import asyncio
from unittest.mock import patch

from neopig.live import get_live_queue, emit_live_media, LIVE_MEDIA_QUEUE


class TestGetLiveQueue:
    """Test get_live_queue function."""

    def setup_method(self):
        # Reset global queue before each test
        import neopig.live
        neopig.live.LIVE_MEDIA_QUEUE = None

    def test_creates_queue_on_first_call(self):
        """Test that queue is created on first call."""
        queue = get_live_queue()
        assert queue is not None
        assert isinstance(queue, asyncio.Queue)

    def test_returns_same_queue_on_subsequent_calls(self):
        """Test that same queue is returned on subsequent calls."""
        queue1 = get_live_queue()
        queue2 = get_live_queue()
        assert queue1 is queue2

    def test_queue_has_max_size(self):
        """Test that queue has maxsize of 1000."""
        queue = get_live_queue()
        assert queue.maxsize == 1000


class TestEmitLiveMedia:
    """Test emit_live_media function."""

    def setup_method(self):
        import neopig.live
        neopig.live.LIVE_MEDIA_QUEUE = None

    def test_emits_media_info(self):
        """Test that media info is emitted to queue."""
        media_info = {'md5_hash': 'abc123', 'media_type': 'image'}
        emit_live_media(media_info)

        queue = get_live_queue()
        assert not queue.empty()
        assert queue.get_nowait() == media_info

    def test_does_not_block_on_full_queue(self):
        """Test that emitting to full queue doesn't block."""
        # Fill the queue
        queue = get_live_queue()
        for i in range(1000):
            queue.put_nowait({'id': i})

        # This should not raise or block
        emit_live_media({'id': 'overflow'})

        # Queue should still be at max size
        assert queue.qsize() == 1000


if __name__ == '__main__':
    pytest.main([__file__, '-v'])
