import pytest_mock
from temporalio.testing import ActivityEnvironment

from csirtmu.beta_temporal.config import Config as WorkerConfig
from csirtmu.beta_temporal.external_services.rt import structs
from csirtmu.beta_temporal.external_services.rt.client import RTClient
from csirtmu.beta_temporal.workflows.set_spam_category_on_spam_header import SpamCategoryOnHeaderActivities


class TestIsSpam:
    async def test_is_spam_transaction_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mocker: pytest_mock.MockerFixture,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        activities = SpamCategoryOnHeaderActivities(RTClient(worker_config.rt))
        get_transaction_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_spam_category_on_spam_header.RTClient.get_transaction",
        )
        get_transaction_mock.return_value = None
        assert not await test_activity_env.run(activities.is_spam, mock_transaction_obj)

    async def test_is_spam_no_header(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_api_transaction: structs.TransactionObjectFromAPI,
        mock_attachments: list[structs.AttachmentObjectFromAPI],
        mocker: pytest_mock.MockerFixture,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        activities = SpamCategoryOnHeaderActivities(RTClient(worker_config.rt))
        get_transaction_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_spam_category_on_spam_header.RTClient.get_transaction",
        )
        get_transaction_mock.return_value = mock_api_transaction
        get_attachments_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_spam_category_on_spam_header.RTClient.get_attachments",
        )
        get_attachments_mock.return_value = mock_attachments
        assert not await test_activity_env.run(activities.is_spam, mock_transaction_obj)

    async def test_is_spam_true(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_api_transaction: structs.TransactionObjectFromAPI,
        mock_attachments: list[structs.AttachmentObjectFromAPI],
        mocker: pytest_mock.MockerFixture,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        activities = SpamCategoryOnHeaderActivities(RTClient(worker_config.rt))
        get_transaction_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_spam_category_on_spam_header.RTClient.get_transaction",
        )
        get_transaction_mock.return_value = mock_api_transaction
        get_attachments_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_spam_category_on_spam_header.RTClient.get_attachments",
        )
        mock_attachments[0].headers = "X-Spam: yes"
        get_attachments_mock.return_value = mock_attachments
        assert await test_activity_env.run(activities.is_spam, mock_transaction_obj)
