Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

2.7 KB
Raw Blame
"""The screen recorder's macOS Screen Recording gate (dodaios.platform.screen_access)."""

from __future__ import annotations

import sys

import pytest

from dodaios.platform import permission_service, screen_access
from dodaios.platform.permissions import PermissionId
from tests.fakes.fake_permission_service import FakePermissionService


@pytest.fixture
def gate(monkeypatch: pytest.MonkeyPatch):
    def use(*outcomes: str) -> FakePermissionService:
        fake = FakePermissionService()
        fake.script(PermissionId.SCREEN_RECORDING, *outcomes)
        monkeypatch.setattr(screen_access, "permission_gate", lambda: fake)
        monkeypatch.setattr(permission_service, "get_permission_service", lambda: fake)
        return fake

    return use


@pytest.mark.parametrize(
    ("outcome", "allowed"),
    [
        ("granted", True),
        ("not_required", True),
        ("denied", False),
        ("pending", False),
        ("unavailable", False),
    ],
)
def test_only_a_live_grant_lets_a_capture_run(gate, outcome: str, allowed: bool) -> None:
    gate(outcome)
    assert screen_access.state_allows_capture(screen_access.screen_recording_state()) is allowed


async def test_a_granted_recording_starts_without_asking(gate) -> None:
    fake = gate("granted")
    await screen_access.require_screen_recording_async("appshot")
    assert fake.ensure_calls(PermissionId.SCREEN_RECORDING) == []


async def test_starting_a_recording_asks_macos_once(gate) -> None:
    fake = gate("pending")
    with pytest.raises(screen_access.ScreenCaptureRefused):
        await screen_access.require_screen_recording_async("appshot")
    asks = fake.ensure_calls(PermissionId.SCREEN_RECORDING)
    assert len(asks) == 1
    assert asks[0].interactive is True


async def test_a_refused_grant_names_the_permission(gate) -> None:
    gate("denied")
    with pytest.raises(screen_access.ScreenCaptureRefused) as refused:
        await screen_access.require_screen_recording_async("appshot")
    assert "Screen Recording" in refused.value.user_detail


def test_the_recording_status_on_a_mac_without_the_grant(
    monkeypatch: pytest.MonkeyPatch, gate
) -> None:
    """The status route on macOS: an honest refusal, never an import error."""
    # A Linux runner without PulseAudio raises ImportError (libpulse), not ModuleNotFound.
    pytest.importorskip("PySide6.QtMultimedia", exc_type=ImportError)
    pytest.importorskip("av")
    from dodaios.appshot import recording

    gate("denied")
    monkeypatch.setattr(sys, "platform", "darwin")
    monkeypatch.setattr("dodaios.platform.probes.display_present", lambda: True)

    status = recording.capability()

    assert status["available"] is False
    assert status["permission_required"] is True
    assert "Screen Recording" in status["detail"]