test_vision_node.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from __future__ import annotations
  2. import unittest
  3. from unittest.mock import patch
  4. from app.automation.context import WorkflowContext
  5. from app.automation.nodes.vision import locate_element_node
  6. class VisionLocateNodeTest(unittest.TestCase):
  7. def test_locate_element_converts_percent_to_screen_coordinates(self) -> None:
  8. context = WorkflowContext(workflow_id=1, provider_id=1, model_id=1, temperature=0.1)
  9. node = {
  10. "params": {
  11. "target_description": "点击第一个推荐视频",
  12. "screen_context": "视频首页",
  13. "save_screenshot": False,
  14. }
  15. }
  16. with (
  17. patch(
  18. "app.automation.nodes.vision.windows_automation.take_screenshot",
  19. return_value={
  20. "image_base64": "abc",
  21. "mime_type": "image/png",
  22. "width": 800,
  23. "height": 600,
  24. "path": None,
  25. },
  26. ),
  27. patch(
  28. "app.automation.nodes.vision.ai_service.chat_with_images",
  29. return_value={
  30. "content": (
  31. '{"found": true, "x_percent": 25, "y_percent": 50, '
  32. '"confidence": 0.9, "target_label": "推荐视频", "reason": "清晰可见"}'
  33. )
  34. },
  35. ),
  36. ):
  37. result = locate_element_node(node, {}, context)
  38. self.assertTrue(result["located"])
  39. self.assertEqual(result["x"], 200)
  40. self.assertEqual(result["y"], 300)
  41. self.assertEqual(result["x_percent"], 25)
  42. self.assertEqual(result["y_percent"], 50)
  43. if __name__ == "__main__":
  44. unittest.main()