2026-05-29 12:26:34 +02:00
|
|
|
"""Shared fixtures for ha-diag-agent tests."""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import AsyncGenerator
|
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
import pytest_asyncio
|
|
|
|
|
|
|
|
|
|
from ha_diag.event_emitter import EventEmitter
|
|
|
|
|
from ha_diag.storage import Storage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
2026-05-29 13:41:55 +02:00
|
|
|
# Filesystem fixtures
|
2026-05-29 12:26:34 +02:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def tmp_events_dir(tmp_path: Path) -> Path:
|
|
|
|
|
events = tmp_path / "events"
|
|
|
|
|
events.mkdir()
|
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
2026-05-29 13:41:55 +02:00
|
|
|
# Storage fixture (tmp SQLite — fast, no mocking)
|
2026-05-29 12:26:34 +02:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
|
|
|
async def storage(tmp_path: Path) -> AsyncGenerator[Storage, None]:
|
|
|
|
|
s = Storage(tmp_path / "test.db")
|
|
|
|
|
await s.open()
|
|
|
|
|
yield s
|
|
|
|
|
await s.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# EventEmitter fixture
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def emitter(tmp_events_dir: Path) -> EventEmitter:
|
|
|
|
|
return EventEmitter(tmp_events_dir, node_name="test-node")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# Mock HA client fixture
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_ha_client():
|
2026-05-29 13:41:55 +02:00
|
|
|
"""Plain HAClient mock — no context manager, just async methods."""
|
2026-05-29 12:26:34 +02:00
|
|
|
client = MagicMock()
|
|
|
|
|
client.get_api_status = AsyncMock(return_value={"message": "API running."})
|
2026-05-29 13:41:55 +02:00
|
|
|
client.get_states = AsyncMock(return_value=[])
|
|
|
|
|
client.get_entity_registry = AsyncMock(return_value=[])
|
2026-05-29 12:26:34 +02:00
|
|
|
return client
|