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_ip_custom_field_on_create import SetIPCustomFieldOnCreateActivities


class TestExtractIPAddressesFromTicket:
    async def test_extract_ip_addresses_from_ticket_ticket_data_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        activities = SetIPCustomFieldOnCreateActivities(RTClient(worker_config.rt))
        ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_ip_custom_field_on_create.RTClient.get_ticket_data"
        )
        ticket_data_mock.return_value = None
        assert (await test_activity_env.run(activities.extract_ip_addresses_from_ticket, mock_transaction_obj)) == []
        ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_extract_ip_addresses_from_ticket_test_extraction(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data_with_ip_body: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        activities = SetIPCustomFieldOnCreateActivities(RTClient(worker_config.rt))
        ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_ip_custom_field_on_create.RTClient.get_ticket_data"
        )
        ticket_data_mock.return_value = mock_ticket_data_with_ip_body
        data = await test_activity_env.run(activities.extract_ip_addresses_from_ticket, mock_transaction_obj)
        ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        assert set(data) == {
            "147.251.23.112",
            "147.251.22.83",
            "147.251.60.46",
            "147.251.11.159",
            "147.251.69.145",
            "147.251.31.47",
            "147.251.27.189",
            "147.251.48.169",
            "147.251.4.232",
            "111.68.25.98",
            "147.251.64.182",
            "2001:0DB8:AC10:FE01::",
            "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
            "FE80::0202:B3FF:FE1E:8329",
        }
