84 lines
2.7 KiB
Python
84 lines
2.7 KiB
Python
import pytest
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
from src.config import KodiRpcConfig
|
|
from src.player.models import MediaMetadata, PlaybackState, PlayerStatus
|
|
from src.player.mpv_controller import MpvController
|
|
from src.remotes.kodi_rpc import KodiRpcServer
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_player():
|
|
player = MagicMock(spec=MpvController)
|
|
player.status = PlayerStatus(
|
|
state=PlaybackState.PLAYING,
|
|
current_time=65.5,
|
|
duration=300.0,
|
|
volume=85,
|
|
muted=False,
|
|
metadata=MediaMetadata(
|
|
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
title="Never Gonna Give You Up",
|
|
artist="Rick Astley",
|
|
duration=300.0,
|
|
)
|
|
)
|
|
player.play_url = AsyncMock()
|
|
player.pause = AsyncMock()
|
|
player.resume = AsyncMock()
|
|
player.play_pause = AsyncMock()
|
|
player.stop = AsyncMock()
|
|
player.seek = AsyncMock()
|
|
player.set_volume = AsyncMock()
|
|
player.set_mute = AsyncMock()
|
|
player.toggle_mute = AsyncMock()
|
|
player.add_listener = MagicMock()
|
|
return player
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_kodi_rpc_ping(mock_player):
|
|
rpc = KodiRpcServer(KodiRpcConfig(), mock_player)
|
|
resp = await rpc._dispatch({"jsonrpc": "2.0", "method": "JSONRPC.Ping", "id": 1})
|
|
assert resp["result"] == "pong"
|
|
assert resp["id"] == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_kodi_rpc_active_players(mock_player):
|
|
rpc = KodiRpcServer(KodiRpcConfig(), mock_player)
|
|
resp = await rpc._dispatch({"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 2})
|
|
assert len(resp["result"]) == 1
|
|
assert resp["result"][0]["playerid"] == 1
|
|
assert resp["result"][0]["type"] == "video"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_kodi_rpc_player_properties(mock_player):
|
|
rpc = KodiRpcServer(KodiRpcConfig(), mock_player)
|
|
resp = await rpc._dispatch({
|
|
"jsonrpc": "2.0",
|
|
"method": "Player.GetProperties",
|
|
"params": {"playerid": 1, "properties": ["percentage", "time", "totaltime", "speed"]},
|
|
"id": 3
|
|
})
|
|
result = resp["result"]
|
|
assert result["speed"] == 1
|
|
assert result["time"]["minutes"] == 1
|
|
assert result["time"]["seconds"] == 5
|
|
assert result["totaltime"]["minutes"] == 5
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_kodi_rpc_open_youtube(mock_player):
|
|
rpc = KodiRpcServer(KodiRpcConfig(), mock_player)
|
|
resp = await rpc._dispatch({
|
|
"jsonrpc": "2.0",
|
|
"method": "Player.Open",
|
|
"params": {"item": {"file": "plugin://plugin.video.youtube/play/?video_id=dQw4w9WgXcQ"}},
|
|
"id": 4
|
|
})
|
|
assert resp["result"] == "OK"
|
|
assert mock_player.play_url.called
|
|
args, kwargs = mock_player.play_url.call_args
|
|
assert "dQw4w9WgXcQ" in args[0]
|