Files
OpenJarvis/tests/cli/test_ask_e2e.py
T
c7f451fbc8 fix(ask): honor agent.default_agent from config when --agent omitted
`jarvis ask "..."` (no `--agent` flag) routed straight to
`engine.generate()` regardless of `agent.default_agent` in the user's
config. As a result the persona stack — `agent.default_system_prompt`,
SOUL.md, MEMORY.md, USER.md — was silently bypassed for the most
common command.

Behavior now:
- `--agent X`              → use agent X (unchanged)
- `--agent ""` (empty)     → explicit opt-out, direct-to-engine mode
- (omitted)                → fall back to `config.agent.default_agent`,
                             which dataclass-defaults to `"simple"`,
                             so persona settings finally take effect

Tests:
- Rename `test_no_agent_uses_direct_mode` to
  `test_no_agent_flag_falls_back_to_config_default_agent` and update
  its docstring to document the new behavior.
- Add `test_explicit_empty_agent_opts_out_of_agent_mode`.
- Add `test_no_agent_with_blank_config_default_uses_direct_mode` to
  cover the case where the user clears `default_agent`.
- Update `_patch_engine` (test_ask_router) and `_patch_ask`
  (test_ask_e2e) to re-register `SimpleAgent` after the autouse
  `_clean_registries` conftest fixture, since the agent path now
  runs in tests that previously short-circuited to direct mode.
- Add explicit `cfg.agent.default_agent = ""` to two
  `test_ask_router` tests that mock load_config with a MagicMock
  (so `cfg.agent.default_agent` doesn't auto-create as a truthy mock).

Note: `tests/cli/test_ask_context.py` has 2 unrelated pre-existing
failures on origin/main (memory backend returns None on Windows);
those are out of scope for this PR.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 03:37:09 +00:00

103 lines
3.5 KiB
Python

"""End-to-end tests for ``jarvis ask``."""
from __future__ import annotations
import importlib
import json
from pathlib import Path
from unittest import mock
from click.testing import CliRunner
from openjarvis.cli import cli
from openjarvis.core.config import JarvisConfig
# Import the actual module (not the Click command attribute)
_ask_mod = importlib.import_module("openjarvis.cli.ask")
def _mock_engine_response():
"""Return a mock engine that generates a fixed response."""
return {
"content": "The answer is 4.",
"usage": {
"prompt_tokens": 10,
"completion_tokens": 5,
"total_tokens": 15,
},
"model": "test-model",
"finish_reason": "stop",
}
def _patch_ask(monkeypatch, tmp_path, *, engine_result=None, no_engine=False):
"""Set up common mocks for ask tests."""
# Re-register SimpleAgent after the autouse `_clean_registries` conftest
# fixture clears it. ``JarvisConfig().agent.default_agent`` defaults to
# ``"simple"``, so ``jarvis ask "..."`` (no --agent) routes through it.
from openjarvis.agents.simple import SimpleAgent
from openjarvis.core.registry import AgentRegistry
if not AgentRegistry.contains("simple"):
AgentRegistry.register_value("simple", SimpleAgent)
cfg = JarvisConfig()
cfg.telemetry.db_path = str(tmp_path / "telemetry.db")
monkeypatch.setattr(_ask_mod, "load_config", lambda: cfg)
if no_engine:
monkeypatch.setattr(_ask_mod, "get_engine", lambda *a, **kw: None)
else:
fake_engine = mock.MagicMock()
fake_engine.engine_id = "mock"
fake_engine.health.return_value = True
fake_engine.generate.return_value = engine_result or _mock_engine_response()
fake_engine.list_models.return_value = ["test-model"]
monkeypatch.setattr(
_ask_mod,
"get_engine",
lambda *a, **kw: ("mock", fake_engine),
)
monkeypatch.setattr(
_ask_mod,
"discover_engines",
lambda c: [("mock", fake_engine)],
)
monkeypatch.setattr(
_ask_mod,
"discover_models",
lambda e: {"mock": ["test-model"]},
)
class TestAskCommand:
def test_basic_response(self, monkeypatch, tmp_path: Path) -> None:
_patch_ask(monkeypatch, tmp_path)
result = CliRunner().invoke(cli, ["ask", "What is 2+2?"])
assert result.exit_code == 0
assert "The answer is 4" in result.output
def test_no_engine_error(self, monkeypatch, tmp_path: Path) -> None:
_patch_ask(monkeypatch, tmp_path, no_engine=True)
result = CliRunner().invoke(cli, ["ask", "Hello"])
assert result.exit_code != 0
def test_model_override(self, monkeypatch, tmp_path: Path) -> None:
_patch_ask(monkeypatch, tmp_path)
result = CliRunner().invoke(cli, ["ask", "-m", "custom-model", "Hello"])
assert result.exit_code == 0
def test_json_output(self, monkeypatch, tmp_path: Path) -> None:
_patch_ask(monkeypatch, tmp_path)
result = CliRunner().invoke(cli, ["ask", "--json", "Hello"])
assert result.exit_code == 0
data = json.loads(result.output)
assert "content" in data
def test_telemetry_recorded(self, monkeypatch, tmp_path: Path) -> None:
_patch_ask(monkeypatch, tmp_path)
CliRunner().invoke(cli, ["ask", "Hello"])
db_path = tmp_path / "telemetry.db"
assert db_path.exists()