|
| 1 | +"""Regression tests for the HA-MCP component's actionable restart repair. |
| 2 | +
|
| 3 | +Issue #2210: the legacy OAuth restart warning must offer a fix flow that |
| 4 | +restarts Home Assistant instead of only allowing the issue to be ignored. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import json |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | +from types import ModuleType |
| 13 | +from unittest.mock import AsyncMock, MagicMock |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from ._embedded_stubs import install |
| 18 | + |
| 19 | +install() |
| 20 | + |
| 21 | + |
| 22 | +class _RepairsFlow: |
| 23 | + """Small HA RepairsFlow stand-in with real flow-result behavior.""" |
| 24 | + |
| 25 | + def async_show_form(self, *, step_id, data_schema): |
| 26 | + return {"type": "form", "step_id": step_id, "data_schema": data_schema} |
| 27 | + |
| 28 | + def async_create_entry(self, *, data): |
| 29 | + return {"type": "create_entry", "data": data} |
| 30 | + |
| 31 | + |
| 32 | +data_entry_flow = ModuleType("homeassistant.data_entry_flow") |
| 33 | +data_entry_flow.FlowResult = dict |
| 34 | +sys.modules["homeassistant.data_entry_flow"] = data_entry_flow |
| 35 | +sys.modules["homeassistant"].data_entry_flow = data_entry_flow |
| 36 | + |
| 37 | +repairs_platform = ModuleType("homeassistant.components.repairs") |
| 38 | +repairs_platform.RepairsFlow = _RepairsFlow |
| 39 | +sys.modules["homeassistant.components.repairs"] = repairs_platform |
| 40 | + |
| 41 | + |
| 42 | +def _load_repairs_module(): |
| 43 | + from custom_components.ha_mcp_tools import repairs |
| 44 | + |
| 45 | + return repairs |
| 46 | + |
| 47 | + |
| 48 | +async def test_legacy_oauth_fix_flow_restarts_home_assistant_blocking(): |
| 49 | + """A missing/wrong restart service call would leave the repair unresolved.""" |
| 50 | + repairs = _load_repairs_module() |
| 51 | + hass = MagicMock() |
| 52 | + hass.services.async_call = AsyncMock() |
| 53 | + flow = await repairs.async_create_fix_flow( |
| 54 | + hass, |
| 55 | + "legacy_oauth_restart", |
| 56 | + None, |
| 57 | + ) |
| 58 | + flow.hass = hass |
| 59 | + |
| 60 | + result = await flow.async_step_confirm({}) |
| 61 | + |
| 62 | + hass.services.async_call.assert_awaited_once_with( |
| 63 | + "homeassistant", |
| 64 | + "restart", |
| 65 | + {}, |
| 66 | + blocking=True, |
| 67 | + ) |
| 68 | + assert result == {"type": "create_entry", "data": {}} |
| 69 | + |
| 70 | + |
| 71 | +async def test_legacy_oauth_fix_flow_prompts_before_restart(): |
| 72 | + """Opening the repair must show confirmation without restarting HA.""" |
| 73 | + repairs = _load_repairs_module() |
| 74 | + hass = MagicMock() |
| 75 | + hass.services.async_call = AsyncMock() |
| 76 | + flow = await repairs.async_create_fix_flow( |
| 77 | + hass, |
| 78 | + "legacy_oauth_restart", |
| 79 | + None, |
| 80 | + ) |
| 81 | + flow.hass = hass |
| 82 | + |
| 83 | + result = await flow.async_step_init() |
| 84 | + |
| 85 | + assert result["type"] == "form" |
| 86 | + assert result["step_id"] == "confirm" |
| 87 | + hass.services.async_call.assert_not_awaited() |
| 88 | + |
| 89 | + |
| 90 | +async def test_legacy_oauth_fix_flow_does_not_complete_rejected_restart(): |
| 91 | + """A rejected restart must leave the repair flow—and issue—unfinished.""" |
| 92 | + repairs = _load_repairs_module() |
| 93 | + hass = MagicMock() |
| 94 | + hass.services.async_call = AsyncMock(side_effect=RuntimeError("restart rejected")) |
| 95 | + flow = await repairs.async_create_fix_flow( |
| 96 | + hass, |
| 97 | + "legacy_oauth_restart", |
| 98 | + None, |
| 99 | + ) |
| 100 | + flow.hass = hass |
| 101 | + flow.async_create_entry = MagicMock() |
| 102 | + |
| 103 | + with pytest.raises(RuntimeError, match="restart rejected"): |
| 104 | + await flow.async_step_confirm({}) |
| 105 | + |
| 106 | + flow.async_create_entry.assert_not_called() |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize( |
| 110 | + "catalog_path", |
| 111 | + [ |
| 112 | + "custom_components/ha_mcp_tools/strings.json", |
| 113 | + "custom_components/ha_mcp_tools/translations/en.json", |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_legacy_oauth_repair_catalog_has_fix_flow(catalog_path): |
| 117 | + """Both HA English catalogs must render the actionable confirmation flow.""" |
| 118 | + root = Path(__file__).parents[3] |
| 119 | + catalog = json.loads((root / catalog_path).read_text()) |
| 120 | + |
| 121 | + issue = catalog["issues"]["legacy_oauth_restart"] |
| 122 | + assert "description" not in issue |
| 123 | + confirm = issue["fix_flow"]["step"]["confirm"] |
| 124 | + assert confirm["title"] |
| 125 | + assert confirm["description"] |
0 commit comments