text: # -*- encoding: utf-8 -*-
  #
  # Licensed under the Apache License, Version 2.0 (the "License"); you may
  # not use this file except in compliance with the License. You may obtain
  # a copy of the License at
  #
  #      http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  # License for the specific language governing permissions and limitations
  # under the License.
  
  from unittest import mock
  
  import pytest
  
  from mergify_engine import context
  from mergify_engine import github_types
  from mergify_engine import signals
  from mergify_engine import subscription
  
  
  @pytest.mark.asyncio
  async def test_signals(redis_cache):
      gh_owner = github_types.GitHubAccount(
          {
              "login": github_types.GitHubLogin("user"),
              "id": github_types.GitHubAccountIdType(0),
              "type": "User",
              "avatar_url": "",
          }
      )
  
      gh_repo = github_types.GitHubRepository(
          {
              "archived": False,
              "url": "",
              "default_branch": github_types.GitHubRefType(""),
              "id": github_types.GitHubRepositoryIdType(456),
              "full_name": "user/ref",
              "name": github_types.GitHubRepositoryName("name"),
              "private": False,
              "owner": gh_owner,
          }
      )
  
      client = mock.AsyncMock()
      client.auth.get_access_token.return_value = "<token>"
  
      sub = subscription.Subscription(redis_cache, 0, False, "", frozenset())
      installation = context.Installation(
          gh_owner["id"],
          gh_owner["login"],
          sub,
          client,
          redis_cache,
      )
      repository = context.Repository(installation, gh_repo["name"], gh_repo["id"])
      ctxt = await context.Context.create(
          repository,
          {
              "title": "",
              "id": github_types.GitHubPullRequestId(0),
              "maintainer_can_modify": False,
              "rebaseable": False,
              "draft": False,
              "merge_commit_sha": None,
              "labels": [],
              "number": github_types.GitHubPullRequestNumber(6),
              "commits": 1,
              "merged": True,
              "state": "closed",
              "changed_files": 1,
              "html_url": "<html_url>",
              "base": {
                  "label": "",
                  "sha": github_types.SHAType("sha"),
                  "user": {
                      "login": github_types.GitHubLogin("user"),
                      "id": github_types.GitHubAccountIdType(0),
                      "type": "User",
                      "avatar_url": "",
                  },
                  "ref": github_types.GitHubRefType("ref"),
                  "label": "",
                  "repo": gh_repo,
              },
              "head": {
                  "label": "",
                  "sha": github_types.SHAType("old-sha-one"),
                  "ref": github_types.GitHubRefType("fork"),
                  "user": {
                      "login": github_types.GitHubLogin("user"),
                      "id": github_types.GitHubAccountIdType(0),
                      "type": "User",
                      "avatar_url": "",
                  },
                  "repo": {
                      "archived": False,
                      "url": "",
                      "default_branch": github_types.GitHubRefType(""),
                      "id": github_types.GitHubRepositoryIdType(123),
                      "full_name": "fork/other",
                      "name": github_types.GitHubRepositoryName("other"),
                      "private": False,
                      "owner": {
                          "login": github_types.GitHubLogin("user"),
                          "id": github_types.GitHubAccountIdType(0),
                          "type": "User",
                          "avatar_url": "",
                      },
                  },
              },
              "user": {
                  "login": github_types.GitHubLogin("user"),
                  "id": github_types.GitHubAccountIdType(0),
                  "type": "User",
                  "avatar_url": "",
              },
              "merged_by": None,
              "merged_at": None,
              "mergeable_state": "clean",
          },
      )
  
      assert len(signals.SIGNALS) == 0
      signals.setup()
      assert len(signals.SIGNALS) == 1
  
      with mock.patch("datadog.statsd.increment") as increment:
          await signals.send(ctxt, "action.update")
          increment.assert_called_once_with(
              "engine.signals.action.count", tags=["event:update"]
          )
  