Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

3.1 KB
Raw Blame
"""Timeout contracts for the file-location finder subprocess.

``open_file_location`` launches the platform file manager
(``xdg-open`` / macOS ``open``) with ``subprocess.run(..., shell=False)``
and a ``PathValidator``-checked directory. The argv shape and the path
validation are already pinned by ``test_bearer_security_fixes`` and
``test_utils``; what nothing pins is that the subprocess is **bounded**
— a desktop-environment hang (xdg-open waiting on a dead dbus session,
a mounted-but-unresponsive remote folder) pins the calling worker
indefinitely.
"""

from __future__ import annotations

import subprocess
from unittest.mock import patch

from local_deep_research.research_library import utils as rl_utils


def _recording_run(into):
    """Patch subprocess.run for the *platform* branch and record kwargs."""

    def fake_run(cmd, **kwargs):
        into.update(kwargs)
        into["cmd"] = cmd
        return subprocess.CompletedProcess(cmd, 0)

    return fake_run


class TestFinderSubprocessTimeout:
    def test_xdg_open_receives_a_timeout(self, monkeypatch, tmp_path):
        recorded: dict = {}
        monkeypatch.setattr(rl_utils.sys, "platform", "linux")
        monkeypatch.setattr(
            rl_utils.PathValidator,
            "validate_local_filesystem_path",
            classmethod(lambda cls, p: tmp_path),
        )
        with patch.object(
            rl_utils.subprocess,
            "run",
            side_effect=_recording_run(recorded),
        ):
            assert rl_utils.open_file_location(str(tmp_path)) is True

        assert recorded.get("timeout") is not None and (
            recorded["timeout"] >= 5
        ), f"xdg-open runs without a timeout: {recorded}"

    def test_open_darwin_receives_a_timeout(self, monkeypatch, tmp_path):
        recorded: dict = {}
        monkeypatch.setattr(rl_utils.sys, "platform", "darwin")
        monkeypatch.setattr(
            rl_utils.PathValidator,
            "validate_local_filesystem_path",
            classmethod(lambda cls, p: tmp_path),
        )
        with patch.object(
            rl_utils.subprocess,
            "run",
            side_effect=_recording_run(recorded),
        ):
            assert rl_utils.open_file_location(str(tmp_path)) is True

        assert recorded.get("timeout") is not None and (
            recorded["timeout"] >= 5
        ), f"open(1) runs without a timeout: {recorded}"

    def test_hang_degrades_to_false_not_an_exception(
        self, monkeypatch, tmp_path
    ):
        monkeypatch.setattr(rl_utils.sys, "platform", "linux")
        monkeypatch.setattr(
            rl_utils.PathValidator,
            "validate_local_filesystem_path",
            classmethod(lambda cls, p: tmp_path),
        )

        def hanging_run(cmd, **kwargs):
            raise subprocess.TimeoutExpired(cmd, kwargs.get("timeout", 0))

        with patch.object(rl_utils.subprocess, "run", side_effect=hanging_run):
            # Graceful False along the function's bool contract — not a
            # TimeoutExpired escaping to the caller.
            assert rl_utils.open_file_location(str(tmp_path)) is False