coding-alt/AutoGPT
0
1# Generated by CodiumAI2import time3import unittest4from unittest.mock import patch5 6from autogpt.chat import create_chat_message, generate_context7 8 9class TestChat(unittest.TestCase):10 # Tests that the function returns a dictionary with the correct keys and values when valid strings are provided for role and content.11 def test_happy_path_role_content(self):12 result = create_chat_message("system", "Hello, world!")13 self.assertEqual(result, {"role": "system", "content": "Hello, world!"})14 15 # Tests that the function returns a dictionary with the correct keys and values when empty strings are provided for role and content.16 def test_empty_role_content(self):17 result = create_chat_message("", "")18 self.assertEqual(result, {"role": "", "content": ""})19 20 # Tests the behavior of the generate_context function when all input parameters are empty.21 @patch("time.strftime")22 def test_generate_context_empty_inputs(self, mock_strftime):23 # Mock the time.strftime function to return a fixed value24 mock_strftime.return_value = "Sat Apr 15 00:00:00 2023"25 # Arrange26 prompt = ""27 relevant_memory = ""28 full_message_history = []29 model = "gpt-3.5-turbo-0301"30 31 # Act32 result = generate_context(prompt, relevant_memory, full_message_history, model)33 34 # Assert35 expected_result = (36 -1,37 47,38 3,39 [40 {"role": "system", "content": ""},41 {42 "role": "system",43 "content": f"The current time and date is {time.strftime('%c')}",44 },45 {46 "role": "system",47 "content": f"This reminds you of these events from your past:\n\n\n",48 },49 ],50 )51 self.assertEqual(result, expected_result)52 53 # Tests that the function successfully generates a current_context given valid inputs.54 def test_generate_context_valid_inputs(self):55 # Given56 prompt = "What is your favorite color?"57 relevant_memory = "You once painted your room blue."58 full_message_history = [59 create_chat_message("user", "Hi there!"),60 create_chat_message("assistant", "Hello! How can I assist you today?"),61 create_chat_message("user", "Can you tell me a joke?"),62 create_chat_message(63 "assistant",64 "Why did the tomato turn red? Because it saw the salad dressing!",65 ),66 create_chat_message("user", "Haha, that's funny."),67 ]68 model = "gpt-3.5-turbo-0301"69 70 # When71 result = generate_context(prompt, relevant_memory, full_message_history, model)72 73 # Then74 self.assertIsInstance(result[0], int)75 self.assertIsInstance(result[1], int)76 self.assertIsInstance(result[2], int)77 self.assertIsInstance(result[3], list)78 self.assertGreaterEqual(result[0], 0)79 self.assertGreaterEqual(result[1], 0)80 self.assertGreaterEqual(result[2], 0)81 self.assertGreaterEqual(82 len(result[3]), 383 ) # current_context should have at least 3 messages84 self.assertLessEqual(85 result[1], 204886 ) # token limit for GPT-3.5-turbo-0301 is 2048 tokens87 