Skip to content

Commit

Permalink
Extend WebSocket client fixture and use it consistently
Browse files Browse the repository at this point in the history
Extend the ha_ws_client WebSocket client fixture to set Supervisor Core
into run state and clear all pending messages.

Currently only some tests use the ha_ws_client WebSocket client fixture.
Use it consistently for all tests.
  • Loading branch information
agners committed Feb 28, 2025
1 parent d9b02ed commit 0389e71
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
10 changes: 8 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ATTR_TYPE,
ATTR_VERSION,
REQUEST_FROM,
CoreState,
)
from supervisor.coresys import CoreSys
from supervisor.dbus.network import NetworkManager
Expand Down Expand Up @@ -388,9 +389,14 @@ async def coresys(


@pytest.fixture
def ha_ws_client(coresys: CoreSys) -> AsyncMock:
async def ha_ws_client(coresys: CoreSys) -> AsyncMock:
"""Return HA WS client mock for assertions."""
return coresys.homeassistant.websocket._client
# Set Supervisor Core state to RUNNING, otherwise WS events won't be delivered
coresys.core.state = CoreState.RUNNING
await asyncio.sleep(0)
client = coresys.homeassistant.websocket._client
client.async_send_command.reset_mock()
return client


@pytest.fixture
Expand Down
30 changes: 15 additions & 15 deletions tests/homeassistant/test_websocket.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Test websocket."""

# pylint: disable=protected-access, import-error
# pylint: disable=import-error
import asyncio
import logging
from unittest.mock import AsyncMock

from awesomeversion import AwesomeVersion

Expand All @@ -11,16 +12,15 @@
from supervisor.homeassistant.const import WSEvent, WSType


async def test_send_command(coresys: CoreSys):
async def test_send_command(coresys: CoreSys, ha_ws_client: AsyncMock):
"""Test websocket error on listen."""
client = coresys.homeassistant.websocket._client
await coresys.homeassistant.websocket.async_send_command({"type": "test"})
client.async_send_command.assert_called_with({"type": "test"})
ha_ws_client.async_send_command.assert_called_with({"type": "test"})

await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
client.async_send_command.assert_called_with(
ha_ws_client.async_send_command.assert_called_with(
{
"type": WSType.SUPERVISOR_EVENT,
"data": {
Expand All @@ -32,11 +32,12 @@ async def test_send_command(coresys: CoreSys):
)


async def test_send_command_old_core_version(coresys: CoreSys, caplog):
async def test_send_command_old_core_version(
coresys: CoreSys, ha_ws_client: AsyncMock, caplog
):
"""Test websocket error on listen."""
caplog.set_level(logging.INFO)
client = coresys.homeassistant.websocket._client
client.ha_version = AwesomeVersion("1970.1.1")
ha_ws_client.ha_version = AwesomeVersion("1970.1.1")

await coresys.homeassistant.websocket.async_send_command(
{"type": "supervisor/event"}
Expand All @@ -50,33 +51,32 @@ async def test_send_command_old_core_version(coresys: CoreSys, caplog):
await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
client.async_send_command.assert_not_called()
ha_ws_client.async_send_command.assert_not_called()


async def test_send_message_during_startup(coresys: CoreSys):
async def test_send_message_during_startup(coresys: CoreSys, ha_ws_client: AsyncMock):
"""Test websocket messages queue during startup."""
client = coresys.homeassistant.websocket._client
await coresys.homeassistant.websocket.load()
coresys.core.state = CoreState.SETUP

await coresys.homeassistant.websocket.async_supervisor_update_event(
"test", {"lorem": "ipsum"}
)
client.async_send_command.assert_not_called()
ha_ws_client.async_send_command.assert_not_called()

coresys.core.state = CoreState.RUNNING
await asyncio.sleep(0)

assert client.async_send_command.call_count == 2
assert client.async_send_command.call_args_list[0][0][0] == {
assert ha_ws_client.async_send_command.call_count == 2
assert ha_ws_client.async_send_command.call_args_list[0][0][0] == {
"type": WSType.SUPERVISOR_EVENT,
"data": {
"event": WSEvent.SUPERVISOR_UPDATE,
"update_key": "test",
"data": {"lorem": "ipsum"},
},
}
assert client.async_send_command.call_args_list[1][0][0] == {
assert ha_ws_client.async_send_command.call_args_list[1][0][0] == {
"type": WSType.SUPERVISOR_EVENT,
"data": {
"event": WSEvent.SUPERVISOR_UPDATE,
Expand Down
11 changes: 4 additions & 7 deletions tests/jobs/test_job_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ async def execute(self):
assert test2.call == 3


async def test_internal_jobs_no_notify(coresys: CoreSys):
async def test_internal_jobs_no_notify(coresys: CoreSys, ha_ws_client: AsyncMock):
"""Test internal jobs do not send any notifications."""

class TestClass:
Expand All @@ -972,18 +972,15 @@ async def execute_default(self) -> bool:
return True

test1 = TestClass(coresys)
# pylint: disable-next=protected-access
client = coresys.homeassistant.websocket._client
client.async_send_command.reset_mock()

await test1.execute_internal()
await asyncio.sleep(0)
client.async_send_command.assert_not_called()
ha_ws_client.async_send_command.assert_not_called()

await test1.execute_default()
await asyncio.sleep(0)
assert client.async_send_command.call_count == 2
client.async_send_command.assert_called_with(
assert ha_ws_client.async_send_command.call_count == 2
ha_ws_client.async_send_command.assert_called_with(
{
"type": "supervisor/event",
"data": {
Expand Down
16 changes: 8 additions & 8 deletions tests/jobs/test_job_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test the condition decorators."""

import asyncio
from unittest.mock import ANY
from unittest.mock import ANY, AsyncMock

import pytest

Expand Down Expand Up @@ -83,14 +83,14 @@ async def test_update_job(coresys: CoreSys):
job.progress = -10


async def test_notify_on_change(coresys: CoreSys):
async def test_notify_on_change(coresys: CoreSys, ha_ws_client: AsyncMock):
"""Test jobs notify Home Assistant on changes."""
job = coresys.jobs.new_job(TEST_JOB)

job.progress = 50
await asyncio.sleep(0)
# pylint: disable=protected-access
coresys.homeassistant.websocket._client.async_send_command.assert_called_with(
ha_ws_client.async_send_command.assert_called_with(
{
"type": "supervisor/event",
"data": {
Expand All @@ -112,7 +112,7 @@ async def test_notify_on_change(coresys: CoreSys):

job.stage = "test"
await asyncio.sleep(0)
coresys.homeassistant.websocket._client.async_send_command.assert_called_with(
ha_ws_client.async_send_command.assert_called_with(
{
"type": "supervisor/event",
"data": {
Expand All @@ -134,7 +134,7 @@ async def test_notify_on_change(coresys: CoreSys):

job.reference = "test"
await asyncio.sleep(0)
coresys.homeassistant.websocket._client.async_send_command.assert_called_with(
ha_ws_client.async_send_command.assert_called_with(
{
"type": "supervisor/event",
"data": {
Expand All @@ -156,7 +156,7 @@ async def test_notify_on_change(coresys: CoreSys):

with job.start():
await asyncio.sleep(0)
coresys.homeassistant.websocket._client.async_send_command.assert_called_with(
ha_ws_client.async_send_command.assert_called_with(
{
"type": "supervisor/event",
"data": {
Expand All @@ -178,7 +178,7 @@ async def test_notify_on_change(coresys: CoreSys):

job.capture_error()
await asyncio.sleep(0)
coresys.homeassistant.websocket._client.async_send_command.assert_called_with(
ha_ws_client.async_send_command.assert_called_with(
{
"type": "supervisor/event",
"data": {
Expand All @@ -204,7 +204,7 @@ async def test_notify_on_change(coresys: CoreSys):
)

await asyncio.sleep(0)
coresys.homeassistant.websocket._client.async_send_command.assert_called_with(
ha_ws_client.async_send_command.assert_called_with(
{
"type": "supervisor/event",
"data": {
Expand Down

0 comments on commit 0389e71

Please sign in to comment.