code: def test_collect_action_metadata(
      self,
      trained_policy: UnexpecTEDIntentPolicy,
      model_storage: ModelStorage,
      resource: Resource,
      execution_context: ExecutionContext,
      default_domain: Domain,
      tmp_path: Path,
      query_intent_index: int,
      ranking_length: int,
  ):
      loaded_policy = self.persist_and_load_policy(
          trained_policy, model_storage, resource, execution_context
      )
  
      def test_individual_label_metadata(
          label_metadata: RankingCandidateMetadata,
          all_thresholds: Dict[int, float],
          all_similarities: np.array,
          label_index: int,
      ):
          expected_score = all_similarities[0][label_index]
          expected_threshold = (
              all_thresholds[label_index]
              if label_index in all_thresholds
              else None
          )
          expected_severity = (
              expected_threshold - expected_score
              if expected_threshold
              else None
          )
  
          assert label_metadata.score == expected_score
          assert label_metadata.threshold == expected_threshold
          assert label_metadata.severity == expected_severity
  
      # Monkey-patch certain attributes of the policy to make the testing easier.
      label_thresholds = {0: 1.2, 1: -0.3, 4: -2.3, 5: 0.2}
      loaded_policy.label_thresholds = label_thresholds
      loaded_policy.config[RANKING_LENGTH] = ranking_length
  
      # Some dummy similarities
      similarities = np.array([[3.2, 0.2, -1.2, -4.3, -5.1, 2.3]])
  
      query_intent = default_domain.intents[query_intent_index]
  
      metadata = loaded_policy._collect_action_metadata(
          default_domain, similarities, query_intent=query_intent
      )
  
      # Test all elements of metadata for query intent
      assert metadata.query_intent.name == query_intent
      test_individual_label_metadata(
          metadata.query_intent,
          label_thresholds,
          similarities,
          query_intent_index,
      )
  
      # Check if ranking is sorted correctly and truncated to `ranking_length`
      sorted_label_similarities = sorted(
          [
              (index, score)
              for index, score in enumerate(similarities[0])
          ],
          key=lambda x: -x[1],
      )
      sorted_label_similarities = (
          sorted_label_similarities[:ranking_length]
          if ranking_length
          else sorted_label_similarities
      )
      expected_label_rankings = [
          default_domain.intents[index]
          for index, _ in sorted_label_similarities
      ]
      collected_label_rankings = [
          label_metadata.name for label_metadata in metadata.ranking
      ]
      assert collected_label_rankings == expected_label_rankings
  
      # Test all elements of metadata for all labels in ranking
      for label_metadata in metadata.ranking:
          label_index = default_domain.intents.index(
              label_metadata.name
          )
          test_individual_label_metadata(
              label_metadata,
              label_thresholds,
              similarities,
              label_index,
          )
  
imports: import json
  from pathlib import Path
  from typing import Optional, List, Dict, Type
  import tensorflow
  import numpy
  import pytest
  from _pytest.monkeypatch import MonkeyPatch
  from _pytest.logging import LogCaptureFixture
  import logging
  from rasa.core.featurizers.single_state_featurizer import IntentTokenizerSingleStateFeaturizer
  from rasa.core.featurizers.tracker_featurizers import TrackerFeaturizer
  from rasa.core.featurizers.tracker_featurizers import IntentMaxHistoryTrackerFeaturizer
  from rasa.nlu.classifiers import LABEL_RANKING_LENGTH
  from rasa.shared.constants import LATEST_TRAINING_DATA_FORMAT_VERSION
  from rasa.shared.core.generator import TrackerWithCachedStates
  from rasa.core.policies.ted_policy import PREDICTION_FEATURES
  from rasa.core.policies.unexpected_intent_policy import UnexpecTEDIntentPolicy, RankingCandidateMetadata
  from rasa.engine.graph import ExecutionContext
  from rasa.engine.storage.resource import Resource
  from rasa.engine.storage.storage import ModelStorage
  from rasa.shared.core.constants import ACTION_UNLIKELY_INTENT_NAME, ACTION_LISTEN_NAME
  from rasa.shared.core.domain import Domain
  from rasa.shared.core.events import ActionExecuted, UserUttered, EntitiesAdded, SlotSet, ActionExecutionRejected, ActiveLoop
  from rasa.shared.core.trackers import DialogueStateTracker
  from rasa.utils.tensorflow.constants import IGNORE_INTENTS_LIST, LABEL, MASK, SENTENCE, IDS, POSITIVE_SCORES_KEY, NEGATIVE_SCORES_KEY, RANKING_KEY, RANKING_LENGTH
  from rasa.shared.nlu.constants import INTENT
  from rasa.shared.core.events import Event
  from rasa.utils.tensorflow import model_data_utils
  from tests.core.test_policies import train_trackers
  from tests.core.policies.test_ted_policy import TestTEDPolicy
label_desc: Use image node_id 21 for calling the TestUnexpecTEDIntentPolicy obj's underlying member method code with example usage: obj.test_collect_action_metadata(trained_policy, model_storage, resource, execution_context, default_domain, tmp_path, query_intent_index, ranking_length) without return types