import context

import unittest
import uuid
from typing import Any

from a2a.server.agent_execution import RequestContext
from a2a.types import Message, Role, Part, TextPart, MessageSendParams

from a2a_acl.protocol_extension import MIME_TAG, message_codec
from a2a_acl.protocol_extension.message_codec import check_illocution_regexpr


URI = "https://gitlab.eclipse.org/eclipse-research-labs/mosaico-project/a2a-acl/"


class TestACLtoA2A(unittest.TestCase):
    def test_illocution_in_message(self):
        md: dict[str, Any] = {
            "illocution": "tell",
            "codec": "nl",
            "mediaType": MIME_TAG,
        }
        m = Message(
            role=Role.agent,
            message_id=uuid.uuid4().hex,
            parts=[Part(root=TextPart(text="I am ready."))],
            extensions=[URI],
            metadata={URI: md},
        )
        p = MessageSendParams(message=m)
        c = RequestContext(request=p)
        self.assertEqual(message_codec.acl_of_a2a(c).illocution, "tell")

    def test_illocution_regexpr(self):
        self.assertTrue(check_illocution_regexpr("tell"))
        self.assertTrue(check_illocution_regexpr("not-understood"))
        self.assertFalse(check_illocution_regexpr("not ready"))
        self.assertFalse(check_illocution_regexpr("not_ready"))
        self.assertFalse(check_illocution_regexpr("-not-ready"))
        self.assertFalse(check_illocution_regexpr("Ready"))
        self.assertFalse(check_illocution_regexpr(" ready"))
        self.assertFalse(check_illocution_regexpr(""))
        self.assertFalse(check_illocution_regexpr("ready1"))


if __name__ == "__main__":
    unittest.main()
