Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MatrixBot not resolving room aliases per-command #106347

Merged
merged 12 commits into from Jan 16, 2024

Conversation

PaarthShah
Copy link
Contributor

@PaarthShah PaarthShah commented Dec 24, 2023

Proposed change

Restore the intended behavior of the rooms field in commands as documented in https://www.home-assistant.io/integrations/matrix/#rooms

As the result of a previous refactor, this behavior regressed as described in the original linked issue.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant home-assistant bot marked this pull request as draft December 28, 2023 08:46
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@PaarthShah PaarthShah marked this pull request as ready for review December 28, 2023 23:36
tests/components/matrix/test_commands.py Outdated Show resolved Hide resolved
tests/components/matrix/test_commands.py Outdated Show resolved Hide resolved
tests/components/matrix/test_commands.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft January 2, 2024 09:13
@PaarthShah PaarthShah marked this pull request as ready for review January 6, 2024 10:53
@home-assistant home-assistant bot requested a review from edenhaus January 6, 2024 10:53
tests/components/matrix/test_commands.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft January 8, 2024 08:25
@PaarthShah PaarthShah marked this pull request as ready for review January 13, 2024 04:05
Copy link
Contributor

@edenhaus edenhaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @PaarthShah 👍

@edenhaus edenhaus added this to the 2024.1.4 milestone Jan 16, 2024
@edenhaus edenhaus merged commit 5afe155 into home-assistant:dev Jan 16, 2024
23 checks passed
@PaarthShah PaarthShah deleted the matrix-103611 branch January 16, 2024 09:09
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the comment in a new PR. Thanks!


await hass.async_start()
assert len(command_events) == 0
await matrix_bot._handle_room_message(room, command_params.room_message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer patching the library and setting up the integration, then calling the (notify) service and assert mock call args. We shouldn't interact with integration details in the tests.

https://developers.home-assistant.io/docs/development_testing#writing-tests-for-integrations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In test_notify, that's exactly what's being done for the purpose of actually testing that the underlying implementation detail is working as expected in the context of an automation using the notify service to send a message.

However, using the notify service like that is actively incorrect for the purpose of testing commands: the notify service will cause a message to be sent by the user that the bot itself is logged in as.

The bot is not supposed to respond to otherwise-valid commands sent by its own user (whether in this current session as used by home assistant, or by another login on another device).

One of the new test cases even explicitly checks for this.

In summary:
_handle_send_message is therefore being invoked directly because:

  • we test its proper behavior with the notify service in a different set of unit tests
  • using it is a convenience over having to completely re-implement sending a message within the test code just to be able to send a message as someone else (when that implementation would look nearly the same as the implementation detail, for good reason)
  • also prevent us from having to unnecessarily mock away the other implementation detail of AsyncClient any further than it already has been
    • MockAsyncClient in conftest already mocks away the lower-level IO logic performed by the 3rd party dependency matrix-nio, but further mocking the actual result of sending a message would make it unsafe (in my view as the actual maintainer of matrix-nio) since it'd obscure any changes that occur in its API

I'm not against trying to improve the test, just the opposite, and I appreciate you taking a look! But I'd need to find a way to preserve the total effectiveness/correctness of the tests while doing so, and the notify service simply would cause it to become incorrect: any ideas anyone may have on this are very welcome!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the integration a bit more. _handle_room_message is a callback passed to client.add_event_callback. So instead of knowing about an integration detail we should access the callback via the mock_client.return_value.add_event_callback.call_args attribute.

Please refactor away all direct usage of matrix_bot and use mock_client as needed instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding correctly, this would involve actually unittest.patching add_event_callback?

Because right now that method on MockAsyncClient isn't patched or even overridden so grabbing its call_args isn't possible; is it actually desirable/acceptable to do so for this purpose? (I vaguely remember getting some mixed feedback on this approach during my first implementation of this, but I could be mistaken).

But if that's all fine, then perfect I can take that on in a different PR!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend autospeccing the mock instead and setting return values on the mock methods that need that, eg the methods that should return responses. An autospecced mock will have all real methods mocked and using other mock methods will raise.

Currently we return a custom class that inherit the real class as mock.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep sounds good 👌🏾

@github-actions github-actions bot locked and limited conversation to collaborators Jan 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No event is fired for matrix commands
4 participants