from __future__ import annotations from io import BytesIO from types import SimpleNamespace from unittest.mock import patch PNG_BYTES = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND" 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 = PNG_BYTES 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) def test_capture_screenshot_direct_tries_stdout_after_invalid_pull_file(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() calls: list[list[str]] = [] class FakePopen: returncode = 4294967295 def __init__(self, cmd, **_kwargs): calls.append(list(cmd)) def communicate(self, timeout=None): # noqa: ARG002 return b"broken", None def fake_run(cmd, **kwargs): calls.append(list(cmd)) if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 7: return SimpleNamespace(returncode=0, stdout=b"", stderr=b"") if cmd[:4] == ["adb", "-s", "MOCK", "pull"]: with open(cmd[4], "wb") as f: f.write(b"not a png") return SimpleNamespace(returncode=0, stdout=b"", stderr=b"") if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6: return SimpleNamespace(returncode=0, stdout=PNG_BYTES.replace(b"\r\n", b"\r\r\n"), stderr=b"") raise RuntimeError(f"unexpected command: {cmd}") 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").read_bytes().startswith(b"\x89PNG") assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls) def test_screenshot_operations_recovers_after_zero_byte_execout(tmp_path, monkeypatch): from douyin.core import path_config from PIL import Image from operations import screenshot_operations pic_dir = tmp_path / "pic" pic_dir.mkdir() buf = BytesIO() image = Image.new("RGB", (256, 256)) image.putdata( [ ((x * 17 + y * 3) % 255, (y * 19 + x * 5) % 255, (x * y) % 255) for y in range(256) for x in range(256) ] ) image.save(buf, format="PNG") png_bytes = buf.getvalue() calls: list[list[str]] = [] execout_count = 0 def fake_run(cmd, **_kwargs): nonlocal execout_count calls.append(list(cmd)) if cmd[:4] == ["adb", "-s", "MOCK", "exec-out"]: execout_count += 1 if execout_count == 1: return SimpleNamespace(returncode=0, stdout=b"", stderr=b"") return SimpleNamespace(returncode=0, stdout=png_bytes, stderr=b"") if cmd == ["adb", "kill-server"] or cmd == ["adb", "start-server"]: return SimpleNamespace(returncode=0, stdout=b"", stderr=b"") raise AssertionError(f"unexpected command: {cmd}") monkeypatch.setenv("ADB_EXECUTABLE", "adb") monkeypatch.setattr( path_config, "get_pic_dir", lambda: str(pic_dir), raising=False, ) monkeypatch.setattr(screenshot_operations.subprocess, "run", fake_run) monkeypatch.setattr(screenshot_operations, "_mirror_screenshot_to_external", lambda *_: None) result = screenshot_operations.ScreenshotOperations().get_screenshot("MOCK", use_service=False) assert result is True assert (pic_dir / "screenshot.png").read_bytes().startswith(b"\x89PNG") assert ["adb", "kill-server"] in calls assert ["adb", "start-server"] in calls