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.

9.9 KB
Raw Blame
"""The macOS app lives where Mac users look for apps (BUG-216).

A bundle kept only in ``~/Applications`` was installed and launchable, yet the
user could not see it among their apps: Finder's "Applications" item and
Launchpad show ``/Applications``. These tests pin that the installer prefers
``/Applications`` when the account may write there, moves an existing
per-user install over without touching its bytes (the signature, and with it
every TCC grant, survives a rename), and never loses the app on the way.
"""

from __future__ import annotations

import os
import plistlib
import sys
from pathlib import Path

import pytest

import dodaios.setup.macos_app_bundle as mab
from dodaios.setup.macos_app_bundle import (
    APP_DIR_NAME,
    ensure_macos_app_bundle,
    macos_applications_dir,
    remove_macos_app_bundle,
)

pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits")


def _bundle(root: Path, marker: str = "signed-bytes") -> Path:
    bundle = root / APP_DIR_NAME
    (bundle / "Contents" / "MacOS").mkdir(parents=True)
    (bundle / "Contents" / "MacOS" / "PersonalJarvis").write_text(marker, encoding="utf-8")
    return bundle


@pytest.fixture
def roots(tmp_path: Path) -> tuple[Path, Path]:
    system = tmp_path / "Applications"
    user = tmp_path / "home" / "Applications"
    system.mkdir()
    user.mkdir(parents=True)
    return system, user


@pytest.fixture
def read_only(roots: tuple[Path, Path]):
    system, _user = roots
    if os.geteuid() == 0:
        pytest.skip("root ignores directory permission bits")
    system.chmod(0o555)
    yield system
    system.chmod(0o755)


def test_a_fresh_install_goes_to_the_system_applications_folder(roots) -> None:
    system, user = roots
    assert macos_applications_dir(system_dir=system, user_dir=user) == system


def test_a_standard_account_falls_back_to_its_own_applications_folder(roots, read_only) -> None:
    _system, user = roots
    assert macos_applications_dir(system_dir=read_only, user_dir=user) == user


def test_an_existing_install_is_found_where_it_is(roots, read_only) -> None:
    system, user = roots
    _bundle(user)
    assert macos_applications_dir(system_dir=system, user_dir=user) == user


def test_a_copy_in_the_system_folder_wins(roots) -> None:
    system, user = roots
    _bundle(user)
    _bundle(system)
    assert macos_applications_dir(system_dir=system, user_dir=user) == system


def test_a_per_user_install_is_moved_byte_for_byte(roots) -> None:
    system, user = roots
    _bundle(user, marker="exact-signature")

    moved = mab._promote_to_system_applications(system_dir=system, user_dir=user)

    assert moved == system / APP_DIR_NAME
    assert not (user / APP_DIR_NAME).exists()
    executable = moved / "Contents" / "MacOS" / "PersonalJarvis"
    assert executable.read_text(encoding="utf-8") == "exact-signature"


def test_an_existing_system_copy_is_never_overwritten(roots) -> None:
    system, user = roots
    _bundle(user, marker="user")
    _bundle(system, marker="system")

    assert mab._promote_to_system_applications(system_dir=system, user_dir=user) is None
    assert (user / APP_DIR_NAME).is_dir()
    assert (system / APP_DIR_NAME / "Contents" / "MacOS" / "PersonalJarvis").read_text(
        encoding="utf-8"
    ) == "system"


def test_no_move_without_write_access(roots, read_only) -> None:
    _system, user = roots
    _bundle(user)

    assert mab._promote_to_system_applications(system_dir=read_only, user_dir=user) is None
    assert (user / APP_DIR_NAME).is_dir()


def test_a_failed_move_keeps_the_working_app(roots, monkeypatch: pytest.MonkeyPatch) -> None:
    system, user = roots
    _bundle(user)

    def _refuse(self, target):
        raise OSError("Cross-device link")

    monkeypatch.setattr(Path, "rename", _refuse)

    assert mab._promote_to_system_applications(system_dir=system, user_dir=user) is None
    assert (user / APP_DIR_NAME).is_dir()


def test_nothing_to_move_is_a_no_op(roots) -> None:
    system, user = roots
    assert mab._promote_to_system_applications(system_dir=system, user_dir=user) is None


def test_uninstall_clears_both_locations(roots, monkeypatch: pytest.MonkeyPatch) -> None:
    system, user = roots
    _bundle(system)
    _bundle(user)
    monkeypatch.setattr(mab.sys, "platform", "darwin")
    monkeypatch.setattr(mab, "SYSTEM_APPLICATIONS_DIR", system)
    monkeypatch.setattr(mab, "user_applications_dir", lambda: user)

    assert remove_macos_app_bundle() is True
    assert not (system / APP_DIR_NAME).exists()
    assert not (user / APP_DIR_NAME).exists()


def test_the_running_app_is_followed_to_its_new_location(
    roots, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    """The process keeps the path it launched from after the move; the repair
    must hand back — and register — where the app is now."""
    system, user = roots
    _bundle(user)
    launched_from = user / APP_DIR_NAME
    monkeypatch.setattr(mab.sys, "platform", "darwin")
    monkeypatch.setattr(mab, "SYSTEM_APPLICATIONS_DIR", system)
    monkeypatch.setattr(mab, "user_applications_dir", lambda: user)
    monkeypatch.setattr(mab, "ensure_local_signing_identity", lambda **_kw: None)
    monkeypatch.setattr(
        mab, "_running_managed_bundle", lambda *, install_root, **_kw: launched_from
    )
    monkeypatch.setattr(
        mab,
        "_install_native_bundle",
        lambda *_a, **_kw: pytest.fail("a moved running app must never be rebuilt"),
    )
    registered: list[Path] = []
    monkeypatch.setattr(mab, "register_with_launch_services", registered.append)

    result = ensure_macos_app_bundle(install_dir=tmp_path / "install")

    assert result == system / APP_DIR_NAME
    assert registered == [system / APP_DIR_NAME]
    assert not launched_from.exists()


def _foreign_bundle(root: Path) -> Path:
    """The notarized DMG build: same name, its own bundle id."""
    bundle = _bundle(root, marker="dmg-build")
    with (bundle / "Contents" / "Info.plist").open("wb") as stream:
        plistlib.dump({"CFBundleIdentifier": "ai.personaljarvis.desktop"}, stream)
    return bundle


def test_a_separately_installed_app_is_not_taken_for_ours(roots) -> None:
    system, user = roots
    _foreign_bundle(system)
    assert macos_applications_dir(system_dir=system, user_dir=user) == user


def test_uninstall_keeps_a_separately_installed_app(roots, monkeypatch: pytest.MonkeyPatch) -> None:
    system, user = roots
    foreign = _foreign_bundle(system)
    _bundle(user)
    monkeypatch.setattr(mab.sys, "platform", "darwin")
    monkeypatch.setattr(mab, "SYSTEM_APPLICATIONS_DIR", system)
    monkeypatch.setattr(mab, "user_applications_dir", lambda: user)

    assert remove_macos_app_bundle() is True
    assert foreign.is_dir()
    assert not (user / APP_DIR_NAME).exists()


def test_a_repair_never_replaces_a_separately_installed_app(
    roots, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    system, _user = roots
    foreign = _foreign_bundle(system)
    monkeypatch.setattr(mab, "ensure_local_signing_identity", lambda **_kw: None)
    monkeypatch.setattr(
        mab,
        "_install_native_bundle",
        lambda *_a, **_kw: pytest.fail("a foreign app must never be rebuilt over"),
    )

    assert (
        ensure_macos_app_bundle(install_dir=tmp_path / "install", applications_dir=system) is None
    )
    assert "separately installed" in (mab.last_error() or "")
    assert (foreign / "Contents" / "MacOS" / "PersonalJarvis").read_text() == "dmg-build"


def test_a_damaged_bundle_of_ours_is_still_ours(roots) -> None:
    """No Info.plist proves nothing about ownership — it must stay repairable."""
    system, user = roots
    _bundle(system)
    assert macos_applications_dir(system_dir=system, user_dir=user) == system


def test_the_login_item_follows_the_moved_app(
    roots, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    import dodaios.autostart.macos as autostart_macos
    from dodaios.autostart.macos import MacOSAutostart
    from dodaios.autostart.protocol import LaunchSpec

    system, user = roots
    _bundle(user)
    launched_from = user / APP_DIR_NAME
    agents = tmp_path / "LaunchAgents"
    monkeypatch.setattr(autostart_macos, "_agents_dir", lambda: agents)
    MacOSAutostart().install(
        LaunchSpec(
            program="/usr/bin/open",
            args=("-W", "-a", str(launched_from)),
            working_dir=str(tmp_path),
            minimized=False,
        )
    )
    monkeypatch.setattr(mab.sys, "platform", "darwin")
    monkeypatch.setattr(mab, "SYSTEM_APPLICATIONS_DIR", system)
    monkeypatch.setattr(mab, "user_applications_dir", lambda: user)
    monkeypatch.setattr(mab, "ensure_local_signing_identity", lambda **_kw: None)
    monkeypatch.setattr(
        mab, "_running_managed_bundle", lambda *, install_root, **_kw: launched_from
    )
    monkeypatch.setattr(mab, "register_with_launch_services", lambda _bundle: True)

    ensure_macos_app_bundle(install_dir=tmp_path / "install")

    with (agents / "com.personal-jarvis.autostart.plist").open("rb") as fh:
        arguments = plistlib.load(fh)["ProgramArguments"]
    assert arguments == ["/usr/bin/open", "-W", "-a", str(system / APP_DIR_NAME)]


def test_the_login_item_follows_even_when_the_repair_fails(
    roots, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
    """Seen live: signing failed after the move, and login kept the dead path."""
    import dodaios.autostart.macos as autostart_macos

    system, user = roots
    _bundle(user)
    monkeypatch.setattr(mab.sys, "platform", "darwin")
    monkeypatch.setattr(mab, "SYSTEM_APPLICATIONS_DIR", system)
    monkeypatch.setattr(mab, "user_applications_dir", lambda: user)

    def _signing_breaks(**_kw):
        raise RuntimeError("errSecInternalComponent")

    monkeypatch.setattr(mab, "ensure_local_signing_identity", _signing_breaks)
    retargeted: list[Path] = []
    monkeypatch.setattr(autostart_macos, "retarget_launch_agent", retargeted.append)

    assert ensure_macos_app_bundle(install_dir=tmp_path / "install") is None
    assert retargeted == [system / APP_DIR_NAME]