ReMe/tests/unit/test_reme_cli.py
jinliyl e7d44f6f3b
Some checks are pending
Pre-commit / run (ubuntu-latest) (push) Waiting to run
Tests ReMe / Unit Tests - py3.11 (push) Waiting to run
Tests ReMe / Unit Tests - py3.12 (push) Waiting to run
Tests ReMe / Unit Tests - py3.13 (push) Waiting to run
Windows Smoke / CLI smoke - py3.11 (push) Waiting to run
refactor(agent): unify agent subprocess env, sessions, skills, and MCP/service jobs (#382)
* feat(config): add environment variable configuration for agent subprocesses

- Add environment field to ApplicationConfig to store variables for agent subprocesses
- Remove dynamic loading of .env files in agent wrappers
- Introduce subprocess_environment property in base agent wrapper
- Pass application-level environment variables to Claude Code and Codex agents
- Load environment variables once at startup and pass to ReMe application
- Remove dependency on load_env utility in agent wrapper implementations
- Update tests to use configured environment instead of dynamic loading
- Remove unused environment loading utilities and related test cases

* refactor(mcp): remove channel notification system and related components

- Removed channel notification step implementation
- Removed claim channel step implementation
- Removed ChannelSink class from MCP service
- Removed channel-related documentation from AGENTS.md
- Removed channel instruction text from MCP service
- Removed all channel-related tests
- Updated application context metadata comment to remove channel sink reference
- Removed channel module initialization and imports

* feat(service): add job whitelisting capability to BaseService

- Add optional jobs parameter to BaseService.__init__ to configure job whitelist
- Store jobs as set in self.jobs attribute for efficient lookup operations
- Modify add_jobs method to filter jobs based on whitelist configuration
- Update documentation in both English and Chinese to describe new feature
- Add comprehensive unit tests for job whitelisting behavior
- Implement flowchart update showing new filtering logic
- Preserve existing enable_serve flag behavior alongside new whitelisting

* refactor(service): enhance service job validation and MCP tool injection

- Add strict validation for service jobs whitelist with detailed error messages
- Implement injected job arguments support for MCP services with conflict detection
- Add tool error handling for unsuccessful responses in MCP services
- Remove duplicate job names in Codex agent wrapper using dict.fromkeys
- Update MCP server argument format from single JSON array to repeated --job flags
- Add comprehensive test coverage for job injection and error handling scenarios
- Update documentation to reflect service job validation and MCP features
- Ensure application cleanup occurs even when service lifespan encounters errors

* feat(agent): update skill handling to preserve existing Claude skills

- Change skills parameter processing to use 'all' instead of filtered list
- Add logic to select project skills without restricting Claude's existing skills
- Update variable naming from 'skills' to 'selected_skills' for clarity
- Modify application context metadata documentation to clarify in-memory state usage
- Add test case to verify configured skills are added without filtering existing skills
- Update internal skill directory handling to use renamed variable consistently

* refactor(agent): restructure agent wrapper components and session storage

- Move CcFileSessionStore to separate module for better organization
- Add SDK package version logging in base agent wrapper
- Update Claude Code agent to use new session store structure with project keys
- Refactor Claude Code agent wrapper to use proper type hints and SDK integration
- Add support for server tool use events in Claude Code message processing
- Improve error handling and resource cleanup in streaming operations
- Update Codex agent wrapper with proper type annotations and configuration
- Remove deprecated system prompt mode handling from Claude Code wrapper
- Fix session path construction for Claude Code transcript storage
- Update dependency injection and configuration handling patterns

* fix(cc_agent_wrapper): resolve Claude Code SDK integration issues

- Added dataclass import and created _BlockState for content block metadata tracking
- Implemented proper MCP server name constant and tool context ID validation
- Fixed tool_context_id injection to prevent duplicate assignment errors
- Resolved skills parameter handling in build_options method
- Enhanced job tools integration with MCP servers mapping validation
- Replaced deprecated block_ids/block_types/tool_call_names with block_states dict
- Updated message_delta to emit USAGE chunks instead of REPLY_END
- Fixed stream result handling to ensure proper REPLY_END emission
- Improved error handling for session mirror failures and rate limits
- Added proper cleanup for expected trailing errors in streams
- Refactored Codex agent wrapper initialization and configuration management
- Removed obsolete system_prompt_mode from default config
- Enhanced test coverage for new block state and error handling features
- Fixed async generator handling with aclosing context manager
- Improved chunk type mapping for Claude Code SDK events

* refactor(tests): remove demo config tests from config parser test suite

- Removed test_demo_config_registers_llm_jobs function and its assertions
- Eliminated verification of LLM demo job configurations
- Removed checks for agent wrapper component settings
- Deleted assertions for model configurations and parameters
- Cleaned up deprecated test cases related to demo config parsing

* refactor(evolve): simplify Claude Code session store path structure

- Removed redundant project key subdirectory from session link generation
- Updated CcFileSessionStore initialization to use direct session directory path
- Maintained existing session layout compatibility for backward compatibility
- Added unit tests to verify session persistence behavior with existing transcripts
- Ensured UUID-based session files remain accessible at expected locations
- Preserved existing session directory structure without additional nesting

* refactor(agent): defer optional Codex SDK imports until first use

- Moved openai-codex imports inside functions to avoid mandatory dependencies
- Added TYPE_CHECKING guard for development time type checking only
- Implemented lazy loading mechanism with _get_async_codex_class function
- Updated AsyncCodex initialization to occur on demand rather than at module level
- Maintained backward compatibility while improving import performance
- Added test case to verify package import works without optional Codex SDK
- Updated agentscope dependency to version 2.0.4.post1 in pyproject.toml

* test(embedded): add compatibility tests for in-process ReMe embedding

- Add test suite for QwenPaw-style embedded configurations
- Verify optional defaults remain preserved in embedded configs
- Ensure in-process application API stays compatible
- Test model injection and lifecycle management compatibility
- Remove obsolete hermes agent plugin tests
- Update CLI import test to cover multiple optional SDKs
- Block claude_agent_sdk and openai_codex during import testing
2026-07-20 23:52:14 +08:00

340 lines
10 KiB
Python

"""Tests for the ReMe CLI entry helpers."""
import asyncio
from pathlib import Path
import subprocess
import sys
from types import SimpleNamespace
import pytest
from reme.components.service import cli_service
from reme.components.service.cli_service import CliService
from reme import reme as reme_module
def test_package_import_does_not_require_optional_agent_sdks():
"""The base package remains importable without Claude or Codex SDKs."""
script = """
import importlib.abc
import sys
class BlockOptionalAgentSDKs(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path, target=None):
blocked = ("claude_agent_sdk", "openai_codex")
if any(fullname == name or fullname.startswith(f"{name}.") for name in blocked):
raise ModuleNotFoundError(f"blocked optional SDK: {fullname}", name=fullname)
return None
sys.meta_path.insert(0, BlockOptionalAgentSDKs())
import reme
assert not any(
name == sdk or name.startswith(f"{sdk}.")
for name in sys.modules
for sdk in ("claude_agent_sdk", "openai_codex")
)
"""
result = subprocess.run(
[sys.executable, "-c", script],
cwd=Path(__file__).resolve().parents[2],
capture_output=True,
text=True,
check=False,
)
assert result.returncode == 0, result.stderr
def test_main_loads_env_before_calling_server(monkeypatch):
"""Client actions can resolve connection settings from the local .env."""
events = []
main_globals = reme_module.main.__globals__
monkeypatch.setitem(main_globals, "load_env", lambda: events.append("load_env"))
monkeypatch.setitem(main_globals, "parse_args", lambda *_args: ("shell", {"cmd": "pwd"}))
async def fake_call_server(action, **kwargs):
events.append(("call_server", action, kwargs))
monkeypatch.setitem(main_globals, "call_server", fake_call_server)
reme_module.main()
assert events == ["load_env", ("call_server", "shell", {"cmd": "pwd"})]
def test_main_saves_loaded_environment_in_start_config(monkeypatch):
"""The startup config keeps the environment captured by the single global load."""
observed = {}
class FakeReMe:
"""Capture the fully resolved application configuration."""
def __init__(self, **kwargs):
"""Record the application startup configuration."""
observed["config"] = kwargs
def run_app(self):
"""Record that application startup continued."""
observed["ran"] = True
main_globals = reme_module.main.__globals__
monkeypatch.setitem(main_globals, "load_env", lambda: {"TOOL_ENV": "configured"})
monkeypatch.setitem(main_globals, "parse_args", lambda *_args: ("start", {}))
monkeypatch.setitem(main_globals, "prepare_start_config", lambda _kwargs: {"service": {"backend": "cli"}})
monkeypatch.setitem(main_globals, "ReMe", FakeReMe)
reme_module.main()
assert observed == {
"config": {
"service": {"backend": "cli"},
"environment": {"TOOL_ENV": "configured"},
},
"ran": True,
}
def test_prepare_start_config_moves_unknown_start_args_to_job_args(monkeypatch):
"""``reme start job=...`` is translated into a one-shot cli service config."""
monkeypatch.setattr(
cli_service,
"resolve_app_config",
lambda **kwargs: {
**kwargs,
"service": {"backend": "http", "host": "127.0.0.1"},
},
)
cfg = cli_service.prepare_start_config(
{
"config": "jinli_lme",
"workspace_dir": "/tmp/reme",
"job": "search",
"query": "hello",
"limit": 3,
},
)
assert cfg["config"] == "jinli_lme"
assert cfg["workspace_dir"] == "/tmp/reme"
assert cfg["enable_logo"] is False
assert cfg["log_to_console"] is False
assert cfg["service"] == {
"backend": "cli",
"host": "127.0.0.1",
"job": "search",
"job_args": {"query": "hello", "limit": 3},
}
def test_should_precheck_start_skips_cli_service():
"""CLI service is local execution and should not run port prechecks."""
assert cli_service.should_precheck_start({"service": {"backend": "cli"}}) is False
assert cli_service.should_precheck_start({"service": {"backend": "http"}}) is True
def test_cli_service_runs_configured_job_and_closes_app(capsys):
"""CLI service runs one local job through app lifecycle and prints its answer."""
events = []
class FakeApp:
"""Minimal app stub for exercising CliService lifecycle."""
async def start(self):
"""Record app startup."""
events.append("start")
async def close(self):
"""Record app shutdown."""
events.append("close")
async def run_job(self, name, **kwargs):
"""Record job execution and return a successful response."""
events.append(("run_job", name, kwargs))
return SimpleNamespace(answer="found it", success=True, metadata={"hits": 1})
service = CliService(job="search", job_args={"query": "hello"})
service.start_service(FakeApp())
assert events == [
"start",
("run_job", "search", {"query": "hello"}),
"close",
]
assert capsys.readouterr().out == "found it\n"
def test_cli_service_can_print_metadata_from_service_config(capsys):
"""service.show_metadata controls optional CLI metadata output."""
class FakeApp:
"""Minimal app stub for exercising metadata output."""
async def start(self):
"""No-op app startup."""
async def close(self):
"""No-op app shutdown."""
async def run_job(self, _name, **_kwargs):
"""Return a successful response with metadata."""
return SimpleNamespace(answer="found it", success=True, metadata={"hits": 1})
service = CliService(job="search", show_metadata=True)
service.start_service(FakeApp())
assert capsys.readouterr().out == 'found it\n{"hits": 1}\n'
def test_cli_service_exits_nonzero_on_failed_response(capsys):
"""Failed local CLI jobs write to stderr and produce a failing process status."""
events = []
class FakeApp:
"""Minimal app stub for exercising failure handling."""
async def start(self):
"""Record app startup."""
events.append("start")
async def close(self):
"""Record app shutdown."""
events.append("close")
async def run_job(self, name, **kwargs):
"""Record job execution and return a failed response."""
events.append(("run_job", name, kwargs))
return SimpleNamespace(answer="boom", success=False, metadata={})
service = CliService(job="search", job_args={"query": "hello"})
with pytest.raises(SystemExit) as exc_info:
service.start_service(FakeApp())
assert exc_info.value.code == 1
assert events == [
"start",
("run_job", "search", {"query": "hello"}),
"close",
]
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == "boom\n"
def test_call_server_passes_client_kwargs_to_client(monkeypatch, capsys):
"""CLI helper forwards connection options to the selected client."""
seen = {}
class FakeClient:
"""Async client stub that records call arguments."""
def __init__(self, **kwargs):
seen["client_kwargs"] = kwargs
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
return None
async def __call__(self, action: str, **kwargs):
seen["action"] = action
seen["payload"] = kwargs
yield "ok"
monkeypatch.setattr(reme_module.R, "get", lambda component_type, backend: FakeClient)
monkeypatch.setattr(reme_module, "running_service_config", lambda: None)
async def run():
await reme_module.call_server(
"search",
backend="http",
host="127.0.0.2",
port=2444,
timeout=1.5,
query="hello",
)
asyncio.run(run())
assert seen["client_kwargs"] == {"host": "127.0.0.2", "port": 2444, "timeout": 1.5}
assert seen["action"] == "search"
assert seen["payload"] == {"query": "hello"}
assert capsys.readouterr().out == "ok\n"
def test_call_server_treats_show_metadata_as_client_kwarg(monkeypatch, capsys):
"""show_metadata controls client display and is not sent as a tool argument."""
seen = {}
class FakeClient:
"""Async client stub that records call arguments."""
def __init__(self, **kwargs):
seen["client_kwargs"] = kwargs
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
return None
async def __call__(self, action: str, **kwargs):
seen["action"] = action
seen["payload"] = kwargs
yield "ok"
monkeypatch.setattr(reme_module.R, "get", lambda component_type, backend: FakeClient)
monkeypatch.setattr(reme_module, "running_service_config", lambda: None)
async def run():
await reme_module.call_server("version", backend="http", show_metadata=True)
asyncio.run(run())
assert seen["client_kwargs"] == {"show_metadata": True}
assert seen["action"] == "version"
assert seen["payload"] == {}
assert capsys.readouterr().out == "ok\n"
def test_call_server_passes_shell_parameters_as_payload(monkeypatch, capsys):
"""Shell-specific parameter names do not collide with client options."""
seen = {}
class FakeClient:
"""Async client stub that records shell request arguments."""
def __init__(self, **kwargs):
seen["client_kwargs"] = kwargs
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
return None
async def __call__(self, action: str, **kwargs):
seen["action"] = action
seen["payload"] = kwargs
yield "ok"
monkeypatch.setattr(reme_module.R, "get", lambda component_type, backend: FakeClient)
monkeypatch.setattr(reme_module, "running_service_config", lambda: None)
async def run():
await reme_module.call_server("shell", backend="http", cmd="ls", shell_timeout=5)
asyncio.run(run())
assert seen["action"] == "shell"
assert seen["payload"] == {"cmd": "ls", "shell_timeout": 5}
assert capsys.readouterr().out == "ok\n"