Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

10.3 KB
Raw Blame
from __future__ import annotations

import logging
import os
import signal
import subprocess
import sys
from logging.handlers import RotatingFileHandler
from pathlib import Path

import main
import pytest
from vibe import runtime


def test_build_logging_handlers_excludes_stdout_when_disabled(monkeypatch, tmp_path):
    monkeypatch.setenv("VIBE_DISABLE_STDOUT_LOGGING", "1")

    handlers = main._build_logging_handlers(str(tmp_path))

    assert len(handlers) == 1
    assert isinstance(handlers[0], RotatingFileHandler)
    assert handlers[0].maxBytes == main.APPLICATION_LOG_MAX_BYTES
    assert handlers[0].backupCount == main.APPLICATION_LOG_BACKUP_COUNT


def test_build_logging_handlers_keeps_stdout_by_default(monkeypatch, tmp_path):
    monkeypatch.delenv("VIBE_DISABLE_STDOUT_LOGGING", raising=False)

    handlers = main._build_logging_handlers(str(tmp_path))

    assert len(handlers) == 2
    assert isinstance(handlers[0], logging.StreamHandler)
    assert isinstance(handlers[1], RotatingFileHandler)


def test_application_log_rotates_at_configured_limit(monkeypatch, tmp_path):
    monkeypatch.setattr(main, "APPLICATION_LOG_MAX_BYTES", 128)
    monkeypatch.setattr(main, "APPLICATION_LOG_BACKUP_COUNT", 2)
    monkeypatch.setenv("VIBE_DISABLE_STDOUT_LOGGING", "1")
    handler = main._build_logging_handlers(str(tmp_path))[0]
    handler.setFormatter(logging.Formatter("%(message)s"))
    record = logging.LogRecord("rotation-test", logging.INFO, __file__, 1, "x" * 80, (), None)
    try:
        for _ in range(10):
            handler.emit(record)
    finally:
        handler.close()

    assert (tmp_path / "vibe_remote.log.1").exists()
    assert len(list(tmp_path.glob("vibe_remote.log*"))) == 3


def test_start_service_disables_stdout_logging_for_background_process(monkeypatch, tmp_path):
    captured: dict[str, object] = {}
    pid_path = tmp_path / "vibe.pid"

    monkeypatch.setattr(runtime.paths, "get_runtime_pid_path", lambda: pid_path)
    monkeypatch.setattr(runtime, "pid_alive", lambda pid: False)
    monkeypatch.setattr(runtime, "get_service_main_path", lambda: Path("/tmp/main.py"))
    monkeypatch.setattr(runtime, "service_instance_lock_available", lambda **kwargs: (True, 0))
    # Exercise the generic (non-scoped) spawn path deterministically: on a Linux
    # dev host maybe_systemd_scope_prefix() is truthy and would route through the
    # scoped poll-and-adopt path instead of wait_for_service_pid.
    monkeypatch.setattr(runtime, "maybe_systemd_scope_prefix", lambda: [])
    # Stub the real spawn so we never fork a real vibe service, and short-circuit
    # the post-spawn lock wait. This captures the env start_service would launch with.
    monkeypatch.setattr(runtime, "wait_for_service_pid", lambda pid, *args, **kwargs: True)
    # And the readiness gate after it: this test is about the env a spawn is
    # handed, not about whether the process that gets it comes up. The stubbed
    # process never reports a running phase, so the real gate would sit here for
    # the full slow-start timeout and then raise.
    monkeypatch.setattr(runtime, "wait_for_service_ready", lambda pid, timeout=None: pid)

    def fake_spawn_service_background_process(args, stdout_name, stderr_name, env=None, *, hand_over, **kwargs):
        captured["args"] = args
        captured["stdout_name"] = stdout_name
        captured["stderr_name"] = stderr_name
        captured["env"] = env
        process = type("Process", (), {"pid": 12345, "poll": lambda self: None})()
        # The real primitive hands the child over before it returns.
        hand_over(process)
        return process

    monkeypatch.setattr(runtime, "spawn_service_background_process", fake_spawn_service_background_process)

    pid = runtime.start_service()

    assert pid == 12345
    assert captured["stdout_name"] == "service_stdout.log"
    assert captured["stderr_name"] == "service_stderr.log"
    assert isinstance(captured["env"], dict)
    assert captured["env"]["VIBE_DISABLE_STDOUT_LOGGING"] == "1"


def test_spawn_background_detaches_stdin(monkeypatch, tmp_path):
    captured: dict[str, object] = {}
    sink_paths: list[Path] = []

    monkeypatch.setattr(runtime.paths, "get_runtime_dir", lambda: tmp_path)
    monkeypatch.setattr(runtime, "isolated_subprocess_kwargs", lambda *, console=True: {"creationflags": 0x200})

    class FakeSink:
        def __init__(self, path: Path):
            self.stdin = path.open("wb")

    def fake_sinks(stdout_path: Path, stderr_path: Path):
        sink_paths.extend((stdout_path, stderr_path))
        return FakeSink(tmp_path / "stdout.pipe"), FakeSink(tmp_path / "stderr.pipe")

    monkeypatch.setattr(runtime, "_spawn_runtime_log_sinks", fake_sinks)

    class FakePopen:
        pid = 12345

        def __init__(self, args, **kwargs):
            captured["args"] = args
            captured["kwargs"] = kwargs

    monkeypatch.setattr(subprocess, "Popen", FakePopen)

    process = runtime.spawn_service_background_process(["python3", "service.py"], "stdout.log", "stderr.log")

    assert process.pid == 12345
    stdin = captured["kwargs"]["stdin"]
    assert stdin.name == os.devnull
    assert stdin.closed is True
    assert captured["kwargs"]["creationflags"] == 0x200
    assert "start_new_session" not in captured["kwargs"]
    assert captured["kwargs"]["stdout"].closed is True
    assert captured["kwargs"]["stderr"].closed is True
    assert sink_paths == [tmp_path / "stdout.log", tmp_path / "stderr.log"]


def test_runtime_log_sink_uses_platform_isolation(monkeypatch, tmp_path):
    captured: dict[str, object] = {}

    class FakePopen:
        def __init__(self, args, **kwargs):
            captured["args"] = args
            captured["kwargs"] = kwargs

    monkeypatch.setattr(runtime.subprocess, "Popen", FakePopen)
    monkeypatch.setattr(runtime, "isolated_subprocess_kwargs", lambda *, console=True: {"creationflags": 0x200})

    runtime._spawn_runtime_log_sink(tmp_path / "runtime.log")

    assert captured["kwargs"]["creationflags"] == 0x200
    assert "start_new_session" not in captured["kwargs"]


def test_generic_background_spawn_uses_platform_isolation(monkeypatch, tmp_path):
    captured: dict[str, object] = {}
    sink_paths: list[Path] = []

    monkeypatch.setattr(runtime.paths, "get_runtime_dir", lambda: tmp_path)
    monkeypatch.setattr(runtime, "isolated_subprocess_kwargs", lambda *, console=True: {"creationflags": 0x200})

    class FakeSink:
        def __init__(self, path: Path):
            self.stdin = path.open("wb")

    def fake_sinks(stdout_path: Path, stderr_path: Path):
        sink_paths.extend((stdout_path, stderr_path))
        return FakeSink(tmp_path / "generic-stdout.pipe"), FakeSink(tmp_path / "generic-stderr.pipe")

    class FakePopen:
        pid = 24680

        def __init__(self, args, **kwargs):
            captured["args"] = args
            captured["kwargs"] = kwargs

    monkeypatch.setattr(runtime, "_spawn_runtime_log_sinks", fake_sinks)
    monkeypatch.setattr(runtime.subprocess, "Popen", FakePopen)
    pid_path = tmp_path / "ui.pid"

    assert runtime.spawn_background(["python3", "ui.py"], pid_path, "ui-out.log", "ui-err.log") == 24680
    assert captured["kwargs"]["creationflags"] == 0x200
    assert "start_new_session" not in captured["kwargs"]
    assert pid_path.read_text(encoding="utf-8") == "24680"
    assert sink_paths == [tmp_path / "ui-out.log", tmp_path / "ui-err.log"]


def test_main_import_does_not_load_controller() -> None:
    code = "import sys; import main; raise SystemExit(1 if 'core.controller' in sys.modules else 0)"
    result = subprocess.run([sys.executable, "-c", code], cwd=Path(__file__).resolve().parents[1], check=False)

    assert result.returncode == 0


def test_main_acquires_lock_before_loading_config(monkeypatch):
    events = []

    monkeypatch.setattr(main, "acquire_service_instance_lock", lambda: events.append("lock"))
    monkeypatch.setattr(main, "load_config", lambda: events.append("load_config") or (_ for _ in ()).throw(RuntimeError("stop")))
    monkeypatch.setattr(main, "release_service_instance_lock", lambda: events.append("release"))

    with pytest.raises(SystemExit) as exc:
        main.main()

    assert exc.value.code == 1
    assert events == ["lock", "load_config", "release"]


def test_shutdown_intent_missing_is_logged_not_ignored(monkeypatch, caplog):
    monkeypatch.setattr(main, "shutdown_intent_required", lambda: True)
    monkeypatch.setattr(main, "consume_shutdown_intent", lambda pid, signum: None)

    logger = logging.getLogger("test.shutdown")
    with caplog.at_level(logging.WARNING, logger=logger.name):
        main._log_shutdown_intent(logger, signal.SIGTERM)

    assert "honoring signal" in caplog.text


def test_only_unmanaged_service_shutdown_reaps_the_desktop_runtime_ui():
    assert main._should_reap_desktop_runtime_ui(None) is True
    assert main._should_reap_desktop_runtime_ui({"reason": "stop_service"}) is False
    assert main._should_reap_desktop_runtime_ui({"reason": "stop_pid"}) is False
    assert main._should_reap_desktop_runtime_ui({"reason": "service_restart"}) is False
    assert main._should_reap_desktop_runtime_ui({"reason": "full_stop"}) is False
    assert main._should_reap_desktop_runtime_ui({"reason": "restart"}) is False
    assert main._should_reap_desktop_runtime_ui({"reason": "future_managed_reason"}) is False


def test_shutdown_signal_logging_is_lightweight(monkeypatch, caplog):
    monkeypatch.setattr(main.os, "getpid", lambda: 123)
    monkeypatch.setattr(main.os, "getppid", lambda: 1)
    monkeypatch.setattr(main.os, "getpgid", lambda pid: 123)
    monkeypatch.setattr(main.os, "getsid", lambda pid: 123)

    logger = logging.getLogger("test.shutdown")
    with caplog.at_level(logging.INFO, logger=logger.name):
        main._log_shutdown_signal(logger, signal.SIGTERM)

    assert "Received signal 15 pid=123 ppid=1 pgid=123 sid=123" in caplog.text


def test_request_controller_loop_stop_schedules_cleanup_via_run_finally():
    callbacks = []

    class Loop:
        def is_closed(self):
            return False

        def is_running(self):
            return True

        def stop(self):
            callbacks.append("stop")

        def call_soon_threadsafe(self, callback):
            callbacks.append(callback)

    loop = Loop()
    controller = type("Controller", (), {"_loop": loop})()

    assert main._request_controller_loop_stop(controller) is True
    assert callbacks == [loop.stop]