Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Fix check_is_initialized() used by tests and fix checking service agreement (now always global) for tests in client projects (#774)

Louis Choquel0ac1f8feda19
diff --git a/pipelex/system/configuration/config_check.py b/pipelex/system/configuration/config_check.py
index d9a46aeb6..0cbdb0221 100644
--- a/pipelex/system/configuration/config_check.py
+++ b/pipelex/system/configuration/config_check.py
@@ -1,20 +1,19 @@
from rich.panel import Panel
-from pipelex.cli.commands.init.config_files import init_config
from pipelex.hub import get_console
-from pipelex.system.configuration.config_loader import config_manager
+from pipelex.system.configuration.config_loader import CONFIG_NAME, config_manager
from pipelex.tools.misc.file_utils import path_exists
from pipelex.urls import URLs
+# Config files that must be resolvable (in project or global dir) for pipelex to be initialized.
+PLXT_CONFIG_NAME = "plxt.toml"
-def check_is_initialized(print_warning_if_not: bool = True) -> bool:
- backends_toml_path = config_manager.backends_file_path
- routing_profiles_toml_path = config_manager.routing_profiles_file_path
- # Check critical files
- config_exists = init_config(reset=False, dry_run=True) == 0
- backends_exists = path_exists(backends_toml_path)
- routing_exists = path_exists(routing_profiles_toml_path)
+def check_is_initialized(print_warning_if_not: bool = True) -> bool:
+ # Use resolve_config_file for all checks — consistent with how backends and routing are resolved
+ config_exists = path_exists(config_manager.resolve_config_file(CONFIG_NAME)) and path_exists(config_manager.resolve_config_file(PLXT_CONFIG_NAME))
+ backends_exists = path_exists(config_manager.backends_file_path)
+ routing_exists = path_exists(config_manager.routing_profiles_file_path)
is_initialized = config_exists and backends_exists and routing_exists
diff --git a/pipelex/test_extras/shared_pytest_plugins.py b/pipelex/test_extras/shared_pytest_plugins.py
index 15b2a7e06..5ff47c7ea 100644
--- a/pipelex/test_extras/shared_pytest_plugins.py
+++ b/pipelex/test_extras/shared_pytest_plugins.py
@@ -103,7 +103,7 @@ def pytest_configure(config: Config) -> None:
if not is_pipelex_gateway_enabled():
return
- pipelex_service_config = load_pipelex_service_config_if_exists(config_dir=config_manager.pipelex_config_dir)
+ pipelex_service_config = load_pipelex_service_config_if_exists(config_dir=config_manager.global_config_dir)
if pipelex_service_config is None or not pipelex_service_config.agreement.terms_accepted:
console = Console()
diff --git a/tests/unit/pipelex/system/test_pipelex_check_initialization.py b/tests/unit/pipelex/system/test_pipelex_check_initialization.py
index 17d0973c5..546665d6b 100644
--- a/tests/unit/pipelex/system/test_pipelex_check_initialization.py
+++ b/tests/unit/pipelex/system/test_pipelex_check_initialization.py
@@ -3,174 +3,156 @@ from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
+ from collections.abc import Callable
from pathlib import Path
from pytest_mock import MockerFixture
-from pipelex.system.configuration.config_check import check_is_initialized
+from pipelex.system.configuration.config_check import CONFIG_NAME, PLXT_CONFIG_NAME, check_is_initialized
+
+
+def _make_resolve_config_file(config_dir: Path) -> Callable[[str], Path]:
+ def resolve_config_file(name: str) -> Path:
+ return config_dir / name
+
+ return resolve_config_file
class TestPipelexCheckInitialization:
"""Test the check_is_initialized function from config_check module."""
- def _mock_config_manager_paths(self, mocker: MockerFixture, backends_file: str, routing_file: str) -> None:
+ def _setup_config_dir(self, tmp_path: Path, *, config_files: bool = False) -> Path:
+ """Create the config directory structure and optionally create config files."""
+ config_dir = tmp_path / ".pipelex"
+ inference_dir = config_dir / "inference"
+ inference_dir.mkdir(parents=True)
+ if config_files:
+ (config_dir / CONFIG_NAME).write_text("[pipelex]\n")
+ (config_dir / PLXT_CONFIG_NAME).write_text("[plxt]\n")
+ return config_dir
+
+ def _mock_config_manager_paths(self, mocker: MockerFixture, config_dir: Path, backends_file: str, routing_file: str) -> None:
"""Mock config_manager properties used by config_check."""
mock_manager = mocker.MagicMock()
mock_manager.backends_file_path = backends_file
mock_manager.routing_profiles_file_path = routing_file
+ mock_manager.resolve_config_file = _make_resolve_config_file(config_dir)
mocker.patch("pipelex.system.configuration.config_check.config_manager", mock_manager)
def test_check_is_initialized_returns_true_when_all_files_exist(self, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test that check_is_initialized returns True when all required files exist."""
- # Setup test directories
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ config_dir = self._setup_config_dir(tmp_path, config_files=True)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
backends_file.write_text("[backends]\nconfig = 'value'")
routing_file.write_text("[routing]\nconfig = 'value'")
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
- # Test
result = check_is_initialized()
- # Verify
assert result is True
def test_check_is_initialized_returns_false_when_backends_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test that check_is_initialized returns False when backends.toml is missing."""
- # Setup test directories - only routing file exists
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ config_dir = self._setup_config_dir(tmp_path, config_files=True)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
routing_file.write_text("[routing]\nconfig = 'value'")
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
- # Test
result = check_is_initialized(print_warning_if_not=False)
- # Verify
assert result is False
def test_check_is_initialized_returns_false_when_routing_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test that check_is_initialized returns False when routing_profiles.toml is missing."""
- # Setup test directories - only backends file exists
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ config_dir = self._setup_config_dir(tmp_path, config_files=True)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
backends_file.write_text("[backends]\nconfig = 'value'")
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
- # Test
result = check_is_initialized(print_warning_if_not=False)
- # Verify
assert result is False
def test_check_is_initialized_returns_false_when_all_files_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
- """Test that check_is_initialized returns False when both required files are missing."""
- # Setup test directories - no files exist
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ """Test that check_is_initialized returns False when all required files are missing."""
+ config_dir = self._setup_config_dir(tmp_path, config_files=False)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
- # Test
result = check_is_initialized(print_warning_if_not=False)
- # Verify
assert result is False
def test_check_is_initialized_prints_warning_when_not_initialized(self, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test that check_is_initialized prints warning and returns False when not initialized and print_warning_if_not is True."""
- # Setup test directories - no files exist
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ config_dir = self._setup_config_dir(tmp_path, config_files=False)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
- # Mock console.print to suppress output during test
mock_console = mocker.MagicMock()
mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mock_console)
- # Test - should print warning and return False
result = check_is_initialized(print_warning_if_not=True)
- # Verify
assert result is False
assert mock_console.print.called
def test_check_is_initialized_returns_true_when_initialized_with_print_warning(self, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test that check_is_initialized returns True when initialized with print_warning_if_not=True."""
- # Setup test directories - all files exist
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ config_dir = self._setup_config_dir(tmp_path, config_files=True)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
backends_file.write_text("[backends]\nconfig = 'value'")
routing_file.write_text("[routing]\nconfig = 'value'")
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
- # Test
result = check_is_initialized(print_warning_if_not=True)
- # Verify
assert result is True
def test_check_is_initialized_returns_false_with_only_backends_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test that check_is_initialized returns False when only backends file is missing."""
- # Setup test directories - only routing file exists
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ config_dir = self._setup_config_dir(tmp_path, config_files=True)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
routing_file.write_text("[routing]\nconfig = 'value'")
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
-
- # Mock console.print to suppress output during test
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mocker.MagicMock())
- # Test
result = check_is_initialized(print_warning_if_not=True)
- # Verify
assert result is False
def test_check_is_initialized_returns_false_with_only_routing_missing(self, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test that check_is_initialized returns False when only routing file is missing."""
- # Setup test directories - only backends file exists
- config_dir = tmp_path / ".pipelex" / "inference"
- config_dir.mkdir(parents=True)
- backends_file = config_dir / "backends.toml"
- routing_file = config_dir / "routing_profiles.toml"
+ config_dir = self._setup_config_dir(tmp_path, config_files=True)
+ inference_dir = config_dir / "inference"
+ backends_file = inference_dir / "backends.toml"
+ routing_file = inference_dir / "routing_profiles.toml"
backends_file.write_text("[backends]\nconfig = 'value'")
- # Mock config_manager to point to temp directory
- self._mock_config_manager_paths(mocker, str(backends_file), str(routing_file))
-
- # Mock console.print to suppress output during test
+ self._mock_config_manager_paths(mocker, config_dir, str(backends_file), str(routing_file))
mocker.patch("pipelex.system.configuration.config_check.get_console", return_value=mocker.MagicMock())
- # Test
result = check_is_initialized(print_warning_if_not=True)
- # Verify
assert result is False