Issue changed messages include suggestions (#4225)

* Issue changed messages include suggestions

* Fix test so ordering of suggestions doesn't matter
This commit is contained in:
Mike Degatano 2023-04-03 14:00:01 -04:00 committed by GitHub
parent 1123101c87
commit 8cf71ffa81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 10 deletions

View File

@ -87,7 +87,7 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes):
# Event on issue creation # Event on issue creation
self.sys_homeassistant.websocket.supervisor_event( self.sys_homeassistant.websocket.supervisor_event(
WSEvent.ISSUE_CHANGED, attr.asdict(issue) WSEvent.ISSUE_CHANGED, self._make_issue_message(issue)
) )
@property @property
@ -112,7 +112,7 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes):
# Event on suggestion added to issue # Event on suggestion added to issue
for issue in self.issues_for_suggestion(suggestion): for issue in self.issues_for_suggestion(suggestion):
self.sys_homeassistant.websocket.supervisor_event( self.sys_homeassistant.websocket.supervisor_event(
WSEvent.ISSUE_CHANGED, attr.asdict(issue) WSEvent.ISSUE_CHANGED, self._make_issue_message(issue)
) )
@property @property
@ -145,6 +145,15 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes):
attr.asdict(HealthChanged(False, self.unhealthy)), attr.asdict(HealthChanged(False, self.unhealthy)),
) )
def _make_issue_message(self, issue: Issue) -> dict[str, Any]:
"""Make issue into message for core."""
return attr.asdict(issue) | {
"suggestions": [
attr.asdict(suggestion)
for suggestion in self.suggestions_for_issue(issue)
]
}
def get_suggestion(self, uuid: str) -> Suggestion: def get_suggestion(self, uuid: str) -> Suggestion:
"""Return suggestion with uuid.""" """Return suggestion with uuid."""
for suggestion in self._suggestions: for suggestion in self._suggestions:
@ -215,7 +224,7 @@ class ResolutionManager(FileConfiguration, CoreSysAttributes):
# Event on suggestion removed from issues # Event on suggestion removed from issues
for issue in self.issues_for_suggestion(suggestion): for issue in self.issues_for_suggestion(suggestion):
self.sys_homeassistant.websocket.supervisor_event( self.sys_homeassistant.websocket.supervisor_event(
WSEvent.ISSUE_CHANGED, attr.asdict(issue) WSEvent.ISSUE_CHANGED, self._make_issue_message(issue)
) )
def dismiss_issue(self, issue: Issue) -> None: def dismiss_issue(self, issue: Issue) -> None:

View File

@ -3,7 +3,6 @@ import asyncio
from typing import Any from typing import Any
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
import attr
import pytest import pytest
from supervisor.coresys import CoreSys from supervisor.coresys import CoreSys
@ -211,8 +210,22 @@ async def test_events_on_issue_changes(coresys: CoreSys):
assert len(coresys.resolution.suggestions) == 1 assert len(coresys.resolution.suggestions) == 1
issue = coresys.resolution.issues[0] issue = coresys.resolution.issues[0]
suggestion = coresys.resolution.suggestions[0] suggestion = coresys.resolution.suggestions[0]
issue_expected = {
"type": "corrupt_repository",
"context": "store",
"reference": "test_repo",
"uuid": issue.uuid,
}
suggestion_expected = {
"type": "execute_reset",
"context": "store",
"reference": "test_repo",
"uuid": suggestion.uuid,
}
send_message.assert_called_once_with( send_message.assert_called_once_with(
_supervisor_event_message("issue_changed", attr.asdict(issue)) _supervisor_event_message(
"issue_changed", issue_expected | {"suggestions": [suggestion_expected]}
)
) )
# Adding a suggestion that fixes the issue changes it # Adding a suggestion that fixes the issue changes it
@ -221,16 +234,28 @@ async def test_events_on_issue_changes(coresys: CoreSys):
SuggestionType.EXECUTE_REMOVE, ContextType.STORE, "test_repo" SuggestionType.EXECUTE_REMOVE, ContextType.STORE, "test_repo"
) )
await asyncio.sleep(0) await asyncio.sleep(0)
send_message.assert_called_once_with( send_message.assert_called_once()
_supervisor_event_message("issue_changed", attr.asdict(issue)) sent_data = send_message.call_args.args[0]
) assert sent_data["type"] == "supervisor/event"
assert sent_data["data"]["event"] == "issue_changed"
assert sent_data["data"]["data"].items() >= issue_expected.items()
assert len(sent_data["data"]["data"]["suggestions"]) == 2
assert suggestion_expected in sent_data["data"]["data"]["suggestions"]
assert {
"type": "execute_remove",
"context": "store",
"reference": "test_repo",
"uuid": execute_remove.uuid,
} in sent_data["data"]["data"]["suggestions"]
# Removing a suggestion that fixes the issue changes it again # Removing a suggestion that fixes the issue changes it again
send_message.reset_mock() send_message.reset_mock()
coresys.resolution.dismiss_suggestion(execute_remove) coresys.resolution.dismiss_suggestion(execute_remove)
await asyncio.sleep(0) await asyncio.sleep(0)
send_message.assert_called_once_with( send_message.assert_called_once_with(
_supervisor_event_message("issue_changed", attr.asdict(issue)) _supervisor_event_message(
"issue_changed", issue_expected | {"suggestions": [suggestion_expected]}
)
) )
# Applying a suggestion should only fire an issue removed event # Applying a suggestion should only fire an issue removed event
@ -240,7 +265,7 @@ async def test_events_on_issue_changes(coresys: CoreSys):
await asyncio.sleep(0) await asyncio.sleep(0)
send_message.assert_called_once_with( send_message.assert_called_once_with(
_supervisor_event_message("issue_removed", attr.asdict(issue)) _supervisor_event_message("issue_removed", issue_expected)
) )