from __future__ import annotations from types import SimpleNamespace from unittest.mock import patch def test_capture_screenshot_direct_invalid_fallback_png_returns_false(tmp_path): from douyin.core.underlying_operations_pkg.screenshot_direct import ( capture_screenshot_direct, ) fake_self = SimpleNamespace( _adb_path=lambda: "adb", get_screenshot=lambda *_a, **_k: False, ) pic_dir = tmp_path / "pic" pic_dir.mkdir() with patch( "douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir", return_value=str(pic_dir), ), patch( "douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run", return_value=SimpleNamespace(returncode=0), ), patch( "douyin.core.underlying_operations_pkg.screenshot_direct._valid_png", return_value=False, ): result = capture_screenshot_direct(fake_self, "MOCK", "pic") assert result is False def test_capture_screenshot_direct_uses_shell_stdout_fallback_when_execout_and_pull_fail(tmp_path): from douyin.core.underlying_operations_pkg.screenshot_direct import ( capture_screenshot_direct, ) fake_self = SimpleNamespace( _adb_path=lambda: "adb", get_screenshot=lambda *_a, **_k: False, ) pic_dir = tmp_path / "pic" pic_dir.mkdir() png_bytes = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND" calls: list[list[str]] = [] class FakePopen: returncode = 1 def __init__(self, cmd, **_kwargs): calls.append(list(cmd)) def communicate(self, timeout=None): # noqa: ARG002 return b"", None def fake_run(cmd, **_kwargs): calls.append(list(cmd)) if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and "-p" in cmd and len(cmd) == 6: return SimpleNamespace(returncode=0, stdout=png_bytes, stderr=b"") raise RuntimeError("file fallback unavailable") with patch( "douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir", return_value=str(pic_dir), ), patch( "douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.Popen", FakePopen, ), patch( "douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run", side_effect=fake_run, ): result = capture_screenshot_direct(fake_self, "MOCK", "pic") assert result is True assert (pic_dir / "screenshot.png").exists() assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls)