| import unittest |
| import json |
| import os |
| import sys |
|
|
| |
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src'))) |
|
|
| from magentic_ui.utils.midscene_adapter import MidsceneAdapter |
|
|
| class TestMidsceneAdapter(unittest.TestCase): |
| |
| def setUp(self): |
| self.adapter = MidsceneAdapter() |
|
|
| def test_format_prompt(self): |
| user_instruction = "Click the login button" |
| screenshot_base64 = "base64encodedimage" |
| |
| prompt = self.adapter.format_prompt(user_instruction, screenshot_base64) |
| |
| self.assertIn("messages", prompt) |
| messages = prompt["messages"] |
| self.assertEqual(len(messages), 1) |
| content = messages[0]["content"] |
| |
| has_image = False |
| has_text = False |
| |
| for item in content: |
| if item.get("type") == "image_url": |
| self.assertIn(screenshot_base64, item["image_url"]["url"]) |
| has_image = True |
| if item.get("type") == "text": |
| self.assertIn(user_instruction, item["text"]) |
| has_text = True |
| |
| self.assertTrue(has_image) |
| self.assertTrue(has_text) |
|
|
| def test_parse_response_click(self): |
| mock_response = { |
| "choices": [{ |
| "message": { |
| "content": "I see the button. click(150, 300)" |
| } |
| }] |
| } |
| |
| actions = self.adapter.parse_response(mock_response) |
| self.assertEqual(len(actions), 1) |
| self.assertEqual(actions[0]["type"], "click") |
| self.assertEqual(actions[0]["x"], 150) |
| self.assertEqual(actions[0]["y"], 300) |
|
|
| def test_parse_response_type(self): |
| mock_response = { |
| "choices": [{ |
| "message": { |
| "content": "type('hello world')" |
| } |
| }] |
| } |
| |
| actions = self.adapter.parse_response(mock_response) |
| self.assertEqual(len(actions), 1) |
| self.assertEqual(actions[0]["type"], "type") |
| self.assertEqual(actions[0]["text"], "hello world") |
|
|
| if __name__ == '__main__': |
| unittest.main() |
|
|