119 lines
3.9 KiB
Python
119 lines
3.9 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from typer.testing import CliRunner
|
|
|
|
from reyna_cli.cli import app
|
|
from reyna_cli.config import Device, Registry, get_device, load_registry
|
|
from reyna_cli.utils import infer_capabilities, resolve_tool_name
|
|
|
|
runner = CliRunner()
|
|
|
|
|
|
def test_resolve_tool_name():
|
|
available_tools = [
|
|
{"name": "draw_text", "description": "Draw text on screen"},
|
|
{"name": "get_battery", "description": "Get battery status"},
|
|
{"name": "play_audio_base64", "description": "Play audio"},
|
|
]
|
|
assert resolve_tool_name("text", available_tools) == "draw_text"
|
|
assert resolve_tool_name("battery", available_tools) == "get_battery"
|
|
assert resolve_tool_name("speech", available_tools) == "play_audio_base64"
|
|
assert resolve_tool_name("unknown", available_tools) is None
|
|
|
|
|
|
def test_infer_capabilities():
|
|
caps = infer_capabilities([{"name": "draw_text"}, {"name": "screenshot"}])
|
|
assert caps["text"] == "draw_text"
|
|
assert caps["screenshot"] == "screenshot"
|
|
assert caps["speech"] is None
|
|
|
|
|
|
def test_registry_mapping_yaml(tmp_path, monkeypatch):
|
|
registry_path = tmp_path / "local_devices.yaml"
|
|
registry_path.write_text(
|
|
"""
|
|
version: 1
|
|
devices:
|
|
esp32_screen:
|
|
display_name: ESP32 Screen
|
|
host: esp32-screen.local
|
|
url: http://esp32-screen.local/api/mcp
|
|
fallback_url: http://192.168.68.123/api/mcp
|
|
capabilities: [display, audio]
|
|
""",
|
|
encoding="utf-8",
|
|
)
|
|
monkeypatch.setenv("REYNA_DEVICES_REGISTRY", str(registry_path))
|
|
registry = load_registry()
|
|
device = get_device("screen", registry)
|
|
assert device is not None
|
|
assert device.id == "esp32_screen"
|
|
assert device.candidate_urls() == ["http://esp32-screen.local/api/mcp", "http://192.168.68.123/api/mcp"]
|
|
|
|
|
|
def test_config_get_device():
|
|
registry = Registry(devices=[Device(id="iphone_mcp", url="http://localhost:8080/api/mcp")])
|
|
assert get_device("iphone", registry).url == "http://localhost:8080/api/mcp"
|
|
assert get_device("nonexistent", registry) is None
|
|
|
|
|
|
def test_cli_doctor():
|
|
result = runner.invoke(app, ["doctor", "--json"])
|
|
assert result.exit_code == 0
|
|
assert json.loads(result.stdout)["ok"] is True
|
|
|
|
|
|
def test_cli_devices_list(monkeypatch, tmp_path):
|
|
registry_path = tmp_path / "local_devices.yaml"
|
|
registry_path.write_text("devices: {}\n", encoding="utf-8")
|
|
monkeypatch.setenv("REYNA_DEVICES_REGISTRY", str(registry_path))
|
|
result = runner.invoke(app, ["devices", "list", "--json"])
|
|
assert result.exit_code == 0
|
|
payload = json.loads(result.stdout)
|
|
assert payload["ok"] is True
|
|
assert payload["devices"] == []
|
|
|
|
|
|
def test_cli_json_args_invalid_fails():
|
|
result = runner.invoke(app, ["devices", "call", "nonexistent", "tool", "--args", "{invalid}", "--json"])
|
|
assert result.exit_code != 0
|
|
assert json.loads(result.stdout)["ok"] is False
|
|
|
|
|
|
def test_screen_has_subcommands():
|
|
result = runner.invoke(app, ["screen", "--help"])
|
|
assert result.exit_code == 0
|
|
assert "text" in result.stdout
|
|
assert "speak" in result.stdout
|
|
|
|
|
|
def test_devices_screen_has_subcommands():
|
|
result = runner.invoke(app, ["devices", "screen", "--help"])
|
|
assert result.exit_code == 0
|
|
assert "text" in result.stdout
|
|
|
|
|
|
def test_devices_arm_has_subcommands():
|
|
result = runner.invoke(app, ["devices", "arm", "--help"])
|
|
assert result.exit_code == 0
|
|
assert "state" in result.stdout
|
|
assert "wave" in result.stdout
|
|
assert "home" in result.stdout
|
|
|
|
|
|
def test_immich_has_subcommands():
|
|
result = runner.invoke(app, ["immich", "--help"])
|
|
assert result.exit_code == 0
|
|
assert "stats" in result.stdout
|
|
assert "albums" in result.stdout
|
|
assert "search" in result.stdout
|
|
|
|
|
|
def test_mongo_has_subcommands():
|
|
result = runner.invoke(app, ["mongo", "--help"])
|
|
assert result.exit_code == 0
|
|
assert "dbs" in result.stdout
|
|
assert "collections" in result.stdout
|
|
assert "find" in result.stdout
|