testsAndMisc/python_pkg/phone_guard/tests/conftest.py
Krzysztof kuhy Rudnicki aa37852296 phone_guard: lease or block raw adb; android_ui drives a session's own virtual display
A raw `adb shell input tap` took no lease, so one session's tap landed in
another session's app (2026-09-25). python_pkg.phone_guard classifies every
adb call in a Bash command and, as the Claude Code PreToolUse hook, takes
the matching phone_lease (one app / real screen / whole phone) or exits 2:
key/text input without -d, input on another session's virtual display, or
a lease still held elsewhere after 40 s.

android_ui gains --display self|<id>: it drives only this session's
phone_vd display under that app's lease, and every input carries -d.
uiautomator sees display 0 only, so virtual-display trees come from a tiny
instrumentation APK (A11yDump.java) built from source without Gradle.

Prunes the oldest evidence artifact to stay under the 60-file cap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WpA6wW2DKiovQADTdjWJoH
2026-09-25 16:09:45 +02:00

34 lines
1.0 KiB
Python

"""Keep every phone_guard test off the live lease and virtual-display state."""
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import patch
import pytest
from python_pkg.phone_lease import lease as lease_mod
if TYPE_CHECKING:
from collections.abc import Iterator
@pytest.fixture(autouse=True)
def isolated_state(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
"""Redirect ~/.cache/phone-lease and /tmp/phone_vd into tmp_path."""
monkeypatch.setenv("PHONE_VD_STATE_DIR", str(tmp_path / "phone_vd"))
with patch.object(lease_mod, "LEASE_DIR", tmp_path / "leases"):
yield tmp_path
def make_display(
root: Path, serial: str, owner_dir: str, display: str, package: str
) -> None:
"""Write the state phone_vd.sh leaves for one running display."""
state = root / "phone_vd" / serial / owner_dir
state.mkdir(parents=True)
(state / "display").write_text(display)
(state / "sf").write_text("123")
(state / "package").write_text(package)