Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -e .
pip install pytest pytest-asyncio pytest-cov aioresponses
pip install pytest pytest-asyncio pytest-cov aiohttp aiointercept

- name: Run tests with coverage
run: |
Expand Down
120 changes: 60 additions & 60 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from aiohttp import ClientError
from aioresponses import aioresponses
import pytest_asyncio
from aiointercept import aiointercept

from hdfury.api import HDFuryAPI
from hdfury.exceptions import HDFuryConnectionError, HDFuryParseError
Expand All @@ -15,10 +15,10 @@
# Fixtures
# ---------------------------------------------------------------------------

@pytest.fixture
@pytest_asyncio.fixture
async def client():
"""HDFury client fixture."""
api = HDFuryAPI("192.168.1.123")
api = HDFuryAPI("hdfury.test")
# Disable debounce delay to keep tests fast and deterministic
api._debounce_delay = 0
yield api
Expand Down Expand Up @@ -64,26 +64,26 @@ def test_normalize_state_invalid_raises(bad):
@pytest.mark.asyncio
async def test_request_json_success(client: HDFuryAPI):
"""Verify _request_json returns parsed JSON on success."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/test", body='{"a": 1}')
async with aiointercept(mock_external_urls=True) as mock:
mock.get("http://hdfury.test/test", body='{"a": 1}')

result = await client._request_json("/test")
assert result == {"a": 1}

@pytest.mark.asyncio
async def test_request_json_invalid_json_raises(client: HDFuryAPI):
"""Verify _request_json raises HDFuryParseError for invalid JSON."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/test", body="not-json")
async with aiointercept(mock_external_urls=True) as mock:
mock.get("http://hdfury.test/test", body="not-json")

with pytest.raises(HDFuryParseError):
await client._request_json("/test")

@pytest.mark.asyncio
async def test_request_non_200_raises_connection_error(client: HDFuryAPI):
"""Verify _request raises HDFuryConnectionError on non-200 responses."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/test", status=500)
async with aiointercept(mock_external_urls=True) as mock:
mock.get("http://hdfury.test/test", status=500)

with pytest.raises(HDFuryConnectionError) as exc:
await client._request("/test")
Expand All @@ -93,9 +93,9 @@ async def test_request_non_200_raises_connection_error(client: HDFuryAPI):
@pytest.mark.asyncio
async def test_request_timeout(client):
"""Verify that _request raises HDFuryConnectionError on a timeout."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/test", exception=TimeoutError())

# aiointercept's exception= option always surfaces ClientConnectionError,
# so patch the aiohttp call directly to exercise the TimeoutError branch.
with patch.object(client._session, "get", side_effect=TimeoutError()):
with pytest.raises(HDFuryConnectionError) as exc:
await client._request("/test")

Expand All @@ -104,8 +104,8 @@ async def test_request_timeout(client):
@pytest.mark.asyncio
async def test_request_client_error(client):
"""Verify that _request raises HDFuryConnectionError on aiohttp ClientError."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/test", exception=ClientError("Error"))
async with aiointercept(mock_external_urls=True) as mock:
mock.get("http://hdfury.test/test", exception=True)

with pytest.raises(HDFuryConnectionError) as exc:
await client._request("/test")
Expand All @@ -119,9 +119,9 @@ async def test_request_client_error(client):
@pytest.mark.asyncio
async def test_get_info_success(client: HDFuryAPI):
"""Verify get_info returns the correct info dictionary."""
with aioresponses() as mock:
async with aiointercept(mock_external_urls=True) as mock:
mock.get(
"http://192.168.1.123/ssi/infopage.ssi",
"http://hdfury.test/ssi/infopage.ssi",
body='{"opmode": "0"}',
)

Expand All @@ -131,9 +131,9 @@ async def test_get_info_success(client: HDFuryAPI):
@pytest.mark.asyncio
async def test_get_board_success(client: HDFuryAPI):
"""Verify get_board returns the correct board info dictionary."""
with aioresponses() as mock:
async with aiointercept(mock_external_urls=True) as mock:
mock.get(
"http://192.168.1.123/ssi/brdinfo.ssi",
"http://hdfury.test/ssi/brdinfo.ssi",
body='{"hostname": "VRROOM-02"}',
)

Expand All @@ -147,13 +147,13 @@ async def test_get_board_success(client: HDFuryAPI):
@pytest.mark.asyncio
async def test_get_config_merges_cec_and_config(client: HDFuryAPI):
"""Verify get_config merges standard config and CEC config correctly."""
with aioresponses() as mock:
async with aiointercept(mock_external_urls=True) as mock:
mock.get(
"http://192.168.1.123/ssi/confpage.ssi",
"http://hdfury.test/ssi/confpage.ssi",
body='{"autosw": "1", "oled": "1"}',
)
mock.get(
"http://192.168.1.123/ssi/cecpage.ssi",
"http://hdfury.test/ssi/cecpage.ssi",
body='{"cec0en": "1"}',
)

Expand All @@ -167,14 +167,14 @@ async def test_get_config_merges_cec_and_config(client: HDFuryAPI):
@pytest.mark.asyncio
async def test_get_config_cec_failure_is_ignored(client: HDFuryAPI):
"""Verify get_config ignores CEC page errors and returns available config."""
with aioresponses() as mock:
async with aiointercept(mock_external_urls=True) as mock:
mock.get(
"http://192.168.1.123/ssi/confpage.ssi",
"http://hdfury.test/ssi/confpage.ssi",
body='{"autosw": "1"}',
)
# cecpage returns 500 -> ignored
mock.get(
"http://192.168.1.123/ssi/cecpage.ssi",
"http://hdfury.test/ssi/cecpage.ssi",
status=500,
)

Expand All @@ -184,13 +184,13 @@ async def test_get_config_cec_failure_is_ignored(client: HDFuryAPI):
@pytest.mark.asyncio
async def test_get_config_cec_invalid_json_is_ignored(client: HDFuryAPI):
"""Verify get_config ignores invalid JSON from CEC page."""
with aioresponses() as mock:
async with aiointercept(mock_external_urls=True) as mock:
mock.get(
"http://192.168.1.123/ssi/confpage.ssi",
"http://hdfury.test/ssi/confpage.ssi",
body='{"autosw": "1"}',
)
mock.get(
"http://192.168.1.123/ssi/cecpage.ssi",
"http://hdfury.test/ssi/cecpage.ssi",
body="not-json",
)

Expand All @@ -204,33 +204,33 @@ async def test_get_config_cec_invalid_json_is_ignored(client: HDFuryAPI):
@pytest.mark.asyncio
async def test_issue_reboot(client: HDFuryAPI):
"""Verify issue_reboot sends the correct command to the device."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/cmd?reboot=", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get("http://hdfury.test/cmd?reboot=", status=200)

await client.issue_reboot()

@pytest.mark.asyncio
async def test_issue_hotplug(client: HDFuryAPI):
"""Verify issue_hotplug sends the correct command to the device."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/cmd?hotplug=", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get("http://hdfury.test/cmd?hotplug=", status=200)

await client.issue_hotplug()

@pytest.mark.asyncio
async def test_set_operation_mode(client: HDFuryAPI):
"""Verify set_operation_mode sends the correct operation mode command."""
with aioresponses() as mock:
mock.get("http://192.168.1.123/cmd?opmode=3", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get("http://hdfury.test/cmd?opmode=3", status=200)

await client.set_operation_mode("3")

@pytest.mark.asyncio
async def test_set_port_selection(client: HDFuryAPI):
"""Verify set_port_selection sends the correct input selection command."""
with aioresponses() as mock:
async with aiointercept(mock_external_urls=True) as mock:
# Note: space encoded as %20 in implementation
mock.get("http://192.168.1.123/cmd?insel=0%204", status=200)
mock.get("http://hdfury.test/cmd?insel=0%204", status=200)

await client.set_port_selection("0", "4")

Expand All @@ -244,8 +244,8 @@ async def test_set_port_selection(client: HDFuryAPI):
)
async def test_set_auto_switch_inputs(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_auto_switch_inputs sends the correct command for each state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -259,8 +259,8 @@ async def test_set_auto_switch_inputs(client: HDFuryAPI, endpoint: str, method:
)
async def test_set_audio_unmute(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_audio_unmute sends the correct command for each Audio Unmute state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -274,8 +274,8 @@ async def test_set_audio_unmute(client: HDFuryAPI, endpoint: str, method: str, v
)
async def test_set_earc_unmute(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_earc_unmute sends the correct command for each eARC Unmute state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -295,8 +295,8 @@ async def test_set_earc_unmute(client: HDFuryAPI, endpoint: str, method: str, va
)
async def test_set_htpc_mode(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_htpc_mode commands are sent correctly for each input and state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -312,8 +312,8 @@ async def test_set_htpc_mode(client: HDFuryAPI, endpoint: str, method: str, valu
)
async def test_set_mute_tx_audio(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_mute_tx_audio commands are sent correctly for each transmitter and state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -329,8 +329,8 @@ async def test_set_mute_tx_audio(client: HDFuryAPI, endpoint: str, method: str,
)
async def test_set_tx_force_5v(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_tx_force_5v commands are sent correctly for each transmitter and state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -344,8 +344,8 @@ async def test_set_tx_force_5v(client: HDFuryAPI, endpoint: str, method: str, va
)
async def test_set_oled(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_oled sends the correct command for each OLED state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -359,8 +359,8 @@ async def test_set_oled(client: HDFuryAPI, endpoint: str, method: str, value: st
)
async def test_set_oled_fade(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_oled_fade sends the correct command for each OLED fade state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -374,8 +374,8 @@ async def test_set_oled_fade(client: HDFuryAPI, endpoint: str, method: str, valu
)
async def test_set_ir_active(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_ir_active sends the correct command for IR activation states."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -389,8 +389,8 @@ async def test_set_ir_active(client: HDFuryAPI, endpoint: str, method: str, valu
)
async def test_set_relay(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_relay sends the correct command for relay states."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -407,8 +407,8 @@ async def test_set_relay(client: HDFuryAPI, endpoint: str, method: str, value: s
)
async def test_set_cec_rx(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_cec_rx commands are sent correctly for each CEC input."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand All @@ -422,8 +422,8 @@ async def test_set_cec_rx(client: HDFuryAPI, endpoint: str, method: str, value:
)
async def test_set_reboot_timer(client: HDFuryAPI, endpoint: str, method: str, value: str):
"""Verify set_reboot_timer sends the correct command for each Reboot Timer state."""
with aioresponses() as mock:
mock.get(f"http://192.168.1.123/cmd?{endpoint}={value}", status=200)
async with aiointercept(mock_external_urls=True) as mock:
mock.get(f"http://hdfury.test/cmd?{endpoint}={value}", status=200)

await getattr(client, method)(value)

Expand Down
Loading