from collections.abc import Sequence from fastapi.testclient import TestClient from app.ha.models import HaEntitySummary from app.ha.reader import HaReader from app.main import app class FakeHaReader(HaReader): def __init__(self) -> None: pass def read_entities(self) -> Sequence[HaEntitySummary]: return [HaEntitySummary(entity_id="sensor.temperature", domain="sensor")] def test_openapi_docs_are_available() -> None: with TestClient(app) as client: response = client.get("/docs") assert response.status_code == 200 assert "SillyHome Next API" in response.text def test_entities_returns_reader_data() -> None: with TestClient(app) as client: app.state.ha_reader = FakeHaReader() response = client.get("/v1/entities") assert response.status_code == 200 assert response.json() == [ { "entity_id": "sensor.temperature", "domain": "sensor", "state_class": None, "device_class": None, "unit_of_measurement": None, } ] def test_entities_returns_503_without_home_assistant_config() -> None: with TestClient(app) as client: if hasattr(app.state, "ha_reader"): delattr(app.state, "ha_reader") response = client.get("/v1/entities") assert response.status_code == 503