6da42ec5ff
1. 新增tools.py模块实现@px.tool装饰器与工具注册表 2. 将所有configs下的YAML工具迁移为ops/xxx.py模块形式 3. 重构cli路由逻辑,优先加载Python工具实现回退YAML 4. 删除所有YAML配置文件与旧的yaml_loader相关代码 5. 调整__init__.py导出API,移除YAML相关依赖
116 lines
5.4 KiB
Python
116 lines
5.4 KiB
Python
"""Tests for cli.screenshot module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from pyflowx.conditions import Constants
|
|
from pyflowx.ops import screenshot as media
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# get_screenshot_path
|
|
# ---------------------------------------------------------------------- #
|
|
class TestGetScreenshotPath:
|
|
"""Test get_screenshot_path function."""
|
|
|
|
def test_get_screenshot_path_with_filename(self, tmp_path: Path) -> None:
|
|
"""Should get screenshot path with filename."""
|
|
with patch.object(Path, "home", return_value=tmp_path):
|
|
result = media.get_screenshot_path("test.png")
|
|
assert result.name == "test.png"
|
|
|
|
def test_get_screenshot_path_without_filename(self, tmp_path: Path) -> None:
|
|
"""Should get screenshot path without filename."""
|
|
with patch.object(Path, "home", return_value=tmp_path):
|
|
result = media.get_screenshot_path()
|
|
assert "screenshot_" in result.name
|
|
assert result.suffix == ".png"
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# take_screenshot_full
|
|
# ---------------------------------------------------------------------- #
|
|
class TestTakeScreenshotFull:
|
|
"""Test take_screenshot_full function."""
|
|
|
|
def test_take_screenshot_full_windows(self, tmp_path: Path) -> None:
|
|
"""Should take full screenshot on Windows."""
|
|
with patch.object(Constants, "IS_WINDOWS", True), patch.object(Constants, "IS_MACOS", False), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_full()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_full_macos(self, tmp_path: Path) -> None:
|
|
"""Should take full screenshot on macOS."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", True), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_full()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_full_linux(self, tmp_path: Path) -> None:
|
|
"""Should take full screenshot on Linux."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", False), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_full()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_full_linux_scrot_fallback(self, tmp_path: Path) -> None:
|
|
"""Should fallback to scrot when gnome-screenshot not found."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", False), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.side_effect = [FileNotFoundError(), MagicMock(returncode=0)]
|
|
media.take_screenshot_full()
|
|
assert mock_run.call_count == 2
|
|
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
# take_screenshot_area
|
|
# ---------------------------------------------------------------------- #
|
|
class TestTakeScreenshotArea:
|
|
"""Test take_screenshot_area function."""
|
|
|
|
def test_take_screenshot_area_windows(self, tmp_path: Path) -> None:
|
|
"""Should take area screenshot on Windows."""
|
|
with patch.object(Constants, "IS_WINDOWS", True), patch.object(Constants, "IS_MACOS", False), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_area()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_area_macos(self, tmp_path: Path) -> None:
|
|
"""Should take area screenshot on macOS."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", True), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_area()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_area_linux(self, tmp_path: Path) -> None:
|
|
"""Should take area screenshot on Linux."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", False), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.return_value = MagicMock(returncode=0)
|
|
media.take_screenshot_area()
|
|
assert mock_run.called
|
|
|
|
def test_take_screenshot_area_linux_scrot_fallback(self, tmp_path: Path) -> None:
|
|
"""Should fallback to scrot -s when gnome-screenshot not found."""
|
|
with patch.object(Constants, "IS_WINDOWS", False), patch.object(Constants, "IS_MACOS", False), patch.object(
|
|
Path, "home", return_value=tmp_path
|
|
), patch("subprocess.run") as mock_run:
|
|
mock_run.side_effect = [FileNotFoundError(), MagicMock(returncode=0)]
|
|
media.take_screenshot_area()
|
|
assert mock_run.call_count == 2
|