mirror of
https://github.com/open-jarvis/OpenJarvis.git
synced 2026-07-31 03:12:16 +00:00
Complete implementation across six development phases (v0.1 through v1.0): - Core: Registry system, config, event bus, types (Phase 0) - Intelligence + Inference: Model routing, Ollama/vLLM/llama.cpp/Cloud engines (Phase 1) - Memory: SQLite/FAISS/ColBERT/BM25/Hybrid backends, document ingest, context injection (Phase 2) - Agents: Simple/Orchestrator/Custom/OpenClaw agents, tool system (Phase 3) - Learning: HeuristicRouter, reward functions, GRPO stub, telemetry aggregation (Phase 4) - SDK: Jarvis class, OpenClaw protocol/transport, benchmarks, Docker deployment (Phase 5) 520 tests passing, 8 skipped (optional deps). Ruff lint clean. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
110 lines
4.0 KiB
Python
110 lines
4.0 KiB
Python
"""Tests for the --router CLI option in jarvis ask."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest import mock
|
|
|
|
from click.testing import CliRunner
|
|
|
|
from openjarvis.cli import cli
|
|
|
|
|
|
def _mock_engine():
|
|
"""Create a mock engine that returns a simple response."""
|
|
engine = mock.MagicMock()
|
|
engine.engine_id = "mock"
|
|
engine.health.return_value = True
|
|
engine.list_models.return_value = ["test-model"]
|
|
engine.generate.return_value = {
|
|
"content": "Hello!",
|
|
"usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8},
|
|
"model": "test-model",
|
|
"finish_reason": "stop",
|
|
}
|
|
return engine
|
|
|
|
|
|
def _patch_engine(engine):
|
|
"""Return context managers that patch engine discovery to use our mock."""
|
|
return (
|
|
mock.patch(
|
|
"openjarvis.cli.ask.get_engine",
|
|
return_value=("mock", engine),
|
|
),
|
|
mock.patch(
|
|
"openjarvis.cli.ask.discover_engines",
|
|
return_value={"mock": engine},
|
|
),
|
|
mock.patch(
|
|
"openjarvis.cli.ask.discover_models",
|
|
return_value={"mock": ["test-model"]},
|
|
),
|
|
mock.patch("openjarvis.cli.ask.register_builtin_models"),
|
|
mock.patch("openjarvis.cli.ask.merge_discovered_models"),
|
|
mock.patch("openjarvis.cli.ask.TelemetryStore"),
|
|
)
|
|
|
|
|
|
class TestAskRouter:
|
|
def test_default_uses_heuristic(self) -> None:
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
|
|
result = CliRunner().invoke(cli, ["ask", "Hello"])
|
|
assert result.exit_code == 0
|
|
assert "Hello!" in result.output
|
|
|
|
def test_explicit_heuristic(self) -> None:
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
|
|
result = CliRunner().invoke(cli, ["ask", "--router", "heuristic", "Hello"])
|
|
assert result.exit_code == 0
|
|
assert "Hello!" in result.output
|
|
|
|
def test_grpo_raises_error(self) -> None:
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
|
|
result = CliRunner().invoke(cli, ["ask", "--router", "grpo", "Hello"])
|
|
# GRPO raises NotImplementedError which should surface as an error
|
|
assert result.exit_code != 0
|
|
|
|
def test_unknown_router_falls_back(self) -> None:
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
|
|
result = CliRunner().invoke(
|
|
cli, ["ask", "--router", "nonexistent", "Hello"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "Hello!" in result.output
|
|
|
|
def test_model_flag_bypasses_router(self) -> None:
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
|
|
result = CliRunner().invoke(
|
|
cli, ["ask", "-m", "test-model", "Hello"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "Hello!" in result.output
|
|
|
|
def test_config_default_policy_respected(self) -> None:
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with (
|
|
patches[0], patches[1], patches[2], patches[3], patches[4], patches[5],
|
|
mock.patch(
|
|
"openjarvis.cli.ask.load_config",
|
|
) as mock_config,
|
|
):
|
|
cfg = mock_config.return_value
|
|
cfg.telemetry.enabled = False
|
|
cfg.intelligence.default_model = "test-model"
|
|
cfg.intelligence.fallback_model = ""
|
|
cfg.learning.default_policy = "heuristic"
|
|
cfg.memory.context_injection = False
|
|
result = CliRunner().invoke(cli, ["ask", "Hello"])
|
|
assert result.exit_code == 0
|