'azurerm_monitor_metric_alert' attribute 'scopes' behavior is incorrect

I am using following terraform code to create monitor metric alert for my availability web tests

resource "azurerm_monitor_metric_alert" "availabilty_test_failure" {

  for_each            = azurerm_application_insights_web_test.app_insights_test

  name                = "${each.value.name}-alert"

  resource_group_name = data.terraform_remote_state.product_bootstrap.outputs.product_diag_rg.name

  scopes              = [each.value.id, data.azurerm_application_insights.instance.id]

  application_insights_web_test_location_availability_criteria {

    web_test_id           = each.value.id

    component_id          = data.azurerm_application_insights.instance.id

    failed_location_count = 2

  }

  action {

    action_group_id = data.terraform_remote_state.product_bootstrap.outputs.monitor_action_group.id

  }

  tags = map("hidden-link:${data.azurerm_application_insights.instance.id}", "Resource", "hidden-link:${each.value.id}", "Resource")

}

The following resources are created in Azure portal

Please notice that only one is created with availability test as a target, the rest got Appinsights as a target.

Running fiddler on my machine reveals the following payloads to be sent to Azure api
First alert metric is created properly with webtest as a target, please notice that in ‘scopes’ test appears to be the first resource

{
  "id": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/Microsoft.Insights/metricAlerts/app508-dfpg-dev3-diag-eastus2-backoffice-ai-test-alert",
  "name": "app508-dfpg-dev3-diag-eastus2-backoffice-ai-test-alert",
  "type": "Microsoft.Insights/metricAlerts",
  "location": "global",
  "tags": {
    "hidden-link:/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai": "Resource",
    "hidden-link:/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-backoffice-ai-test": "Resource"
  },
  "properties": {
    "description": "",
    "severity": 3,
    "enabled": true,
    "scopes": [
      "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-backoffice-ai-test",
      "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai"
    ],
    "evaluationFrequency": "PT1M",
    "windowSize": "PT5M",
    "criteria": {
      "webTestId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-backoffice-ai-test",
      "componentId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai",
      "failedLocationCount": 2,
      "odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria"
    },
    "autoMitigate": true,
    "targetResourceType": "",
    "targetResourceRegion": "",
    "actions": [
      {
        "actionGroupId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-cfg-eastus2/providers/microsoft.insights/actionGroups/app508-dfpg-ne-ag",
        "webHookProperties": {}
      }
    ]
  }
}

Second is created wrong with Appinsights is the target, please notice that in ‘scopes’ Appinsights appears to be the first resource

{
  "id": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/Microsoft.Insights/metricAlerts/app508-dfpg-dev3-diag-eastus2-gateway-ai-test-alert",
  "name": "app508-dfpg-dev3-diag-eastus2-gateway-ai-test-alert",
  "type": "Microsoft.Insights/metricAlerts",
  "location": "global",
  "tags": {
    "hidden-link:/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai": "Resource",
    "hidden-link:/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-gateway-ai-test": "Resource"
  },
  "properties": {
    "description": "",
    "severity": 3,
    "enabled": true,
    "scopes": [
      "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai",
      "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-gateway-ai-test"
    ],
    "evaluationFrequency": "PT1M",
    "windowSize": "PT5M",
    "criteria": {
      "webTestId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-gateway-ai-test",
      "componentId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai",
      "failedLocationCount": 2,
      "odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria"
    },
    "autoMitigate": true,
    "targetResourceType": "",
    "targetResourceRegion": "",
    "actions": [
      {
        "actionGroupId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-cfg-eastus2/providers/microsoft.insights/actionGroups/app508-dfpg-ne-ag",
        "webHookProperties": {}
      }
    ]
  }
}

Third is created wrong with Appinsights is the target, please notice that in ‘scopes’ Appinsights appears to be the first resource

{
  "id": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/Microsoft.Insights/metricAlerts/app508-dfpg-dev3-diag-eastus2-portal-ai-test-alert",
  "name": "app508-dfpg-dev3-diag-eastus2-portal-ai-test-alert",
  "type": "Microsoft.Insights/metricAlerts",
  "location": "global",
  "tags": {
    "hidden-link:/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai": "Resource",
    "hidden-link:/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-portal-ai-test": "Resource"
  },
  "properties": {
    "description": "",
    "severity": 3,
    "enabled": true,
    "scopes": [
      "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai",
      "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-portal-ai-test"
    ],
    "evaluationFrequency": "PT1M",
    "windowSize": "PT5M",
    "criteria": {
      "webTestId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/webtests/app508-dfpg-dev3-diag-eastus2-portal-ai-test",
      "componentId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-diag-eastus2/providers/microsoft.insights/components/app508-dfpg-dev3-diag-eastus2-ai",
      "failedLocationCount": 2,
      "odata.type": "Microsoft.Azure.Monitor.WebtestLocationAvailabilityCriteria"
    },
    "autoMitigate": true,
    "targetResourceType": "",
    "targetResourceRegion": "",
    "actions": [
      {
        "actionGroupId": "/subscriptions/s/resourceGroups/app508-dfpg-ne-cfg-eastus2/providers/microsoft.insights/actionGroups/app508-dfpg-ne-ag",
        "webHookProperties": {}
      }
    ]
  }
}

I think the problem is with ‘scopes’ argument implementation, which uses hash set data structure instead of using array. Unit test that I am referencing here, works accidently, exactly as a first metric alert created by my code. When the api call make by terraform happens, the first resource in a scope is test and the second is Appinsights. If the hash set order would be different, the result would be the same as mine (for two resources that were created botched) and Appinsights would be a target resource in created metric alert.
https://github.com/terraform-providers/terraform-provider-azurerm/blob/1f316b3c34ce16bc423bb87691fb8c5f3c4a7955/azurerm/internal/services/monitor/tests/monitor_metric_alert_resource_test.go#L474