from __future__ import annotations import unittest from unittest.mock import patch from app.automation.context import WorkflowContext from app.automation.nodes.vision import locate_element_node class VisionLocateNodeTest(unittest.TestCase): def test_locate_element_converts_percent_to_screen_coordinates(self) -> None: context = WorkflowContext(workflow_id=1, provider_id=1, model_id=1, temperature=0.1) node = { "params": { "target_description": "点击第一个推荐视频", "screen_context": "视频首页", "save_screenshot": False, } } with ( patch( "app.automation.nodes.vision.windows_automation.take_screenshot", return_value={ "image_base64": "abc", "mime_type": "image/png", "width": 800, "height": 600, "path": None, }, ), patch( "app.automation.nodes.vision.ai_service.chat_with_images", return_value={ "content": ( '{"found": true, "x_percent": 25, "y_percent": 50, ' '"confidence": 0.9, "target_label": "推荐视频", "reason": "清晰可见"}' ) }, ), ): result = locate_element_node(node, {}, context) self.assertTrue(result["located"]) self.assertEqual(result["x"], 200) self.assertEqual(result["y"], 300) self.assertEqual(result["x_percent"], 25) self.assertEqual(result["y_percent"], 50) if __name__ == "__main__": unittest.main()