import asyncio

import pytest

from app.audio.router import AudioRouter
from app.audio.endpoints.input.local_default import LocalDefaultInput
from app.audio.endpoints.input.bluetooth import BluetoothInput
from app.audio.endpoints.output.local_default import LocalDefaultOutput
from app.audio.endpoints.output.loopback import LoopbackOutput
from app.errors import UnknownEndpointError


def make_router():
    return AudioRouter(
        inputs=[LocalDefaultInput(), BluetoothInput()],
        outputs=[LocalDefaultOutput(), LoopbackOutput()],
    )


def test_select_default_output():
    router = make_router()
    endpoint = asyncio.run(router.select_output())
    caps = asyncio.run(endpoint.capabilities())
    assert caps.default is True
    assert caps.kind == "local-default"


def test_select_output_by_kind():
    router = make_router()
    endpoint = asyncio.run(router.select_output("loopback"))
    assert asyncio.run(endpoint.capabilities()).kind == "loopback"


def test_select_input_by_id():
    router = make_router()
    endpoint = asyncio.run(router.select_input("local-default-mic"))
    assert asyncio.run(endpoint.capabilities()).id == "local-default-mic"


def test_unknown_endpoint_raises():
    router = make_router()
    with pytest.raises(UnknownEndpointError):
        asyncio.run(router.select_output("does-not-exist"))
