Files
AIPass/src/aipass/api/tests/test_cli_routing.py
T
AIPassand@devpulse e2c868084a feat(system): chore(lint): ruff auto-fix sweep — 303 errors across 178 files (F401 unused imports + F541 f-string placeholders + F811 redefined); restored report_error re-export + added logger call in errors.py (#333)
* feat(system): fix(windows-ci): combine pip bootstrap fix + venv activation in Verify step

Combines both fixes needed to get windows-test.yml actually passing:

1. setup.sh: stop swallowing ensurepip errors (quiet + 2>/dev/null + || true was
   hiding real failures — pip was silently not installed). Add get-pip.py fallback
   and hard pip verification.

2. windows-test.yml: add 'source .venv/Scripts/activate' to Verify step. Each CI
   step gets a fresh shell — setup.sh's venv activation doesn't carry over, so
   drone wasn't on PATH in the subsequent step.

Together, these should take Windows CI from the 'silent failure every run since
creation' state to actually green. Supersedes PRs #330 and #331 which had the
fixes on separate branches (neither green alone).

Co-Authored-By: @devpulse <devpulse@aipass>

* feat(system): chore(lint): ruff auto-fix sweep — 303 errors across 178 files (F401 unused imports + F541 f-string placeholders + F811 redefined); restored report_error re-export + added logger call in errors.py

Co-Authored-By: @devpulse <devpulse@aipass>

---------

Co-authored-by: @devpulse <devpulse@aipass>
2026-04-16 12:58:53 -07:00

116 lines
4.4 KiB
Python

# =================== AIPass ====================
# Name: test_cli_routing.py
# Description: CLI Routing Tests (adapted for API module structure)
# Version: 1.0.0
# Created: 2026-03-27
# Modified: 2026-03-27
# =============================================
"""
CLI Routing Tests for API branch.
API has handle_command in module files (api_key.py, openrouter_client.py, etc.)
rather than a standalone cli_handler. Tests adapted accordingly.
Covers 9 items:
- help_flag, short_help, help_word, no_args, unknown_command,
return_bool, print_help, print_introspection, output_capture
"""
from unittest.mock import patch
from aipass.api.apps.modules import api_key
# ---------------------------------------------------------------------------
# handle_command routing tests
# ---------------------------------------------------------------------------
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_handle_command_help_flag(mock_jh, mock_header, mock_console):
"""handle_command with --help flag returns True."""
result = api_key.handle_command("get-key", ["--help"])
assert result is True
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_handle_command_short_help(mock_jh, mock_header, mock_console):
"""handle_command with -h flag returns True."""
result = api_key.handle_command("validate", ["-h"])
assert result is True
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_handle_command_help_word(mock_jh, mock_header, mock_console):
"""handle_command with 'help' as arg returns True."""
result = api_key.handle_command("get-key", ["help"])
assert result is True
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_handle_command_no_args(mock_jh, mock_header, mock_console):
"""handle_command with no args triggers introspection, returns True."""
result = api_key.handle_command("get-key", [])
assert result is True
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_handle_command_unknown(mock_jh, mock_header, mock_console):
"""handle_command with unknown command returns False."""
result = api_key.handle_command("bogus_unknown", [])
assert result is False
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_handle_command_return_bool(mock_jh, mock_header, mock_console):
"""handle_command always returns a bool (True or False)."""
result_true = api_key.handle_command("get-key", ["--help"])
result_false = api_key.handle_command("bogus_xyz", [])
assert isinstance(result_true, bool)
assert isinstance(result_false, bool)
assert result_true is True
assert result_false is False
# ---------------------------------------------------------------------------
# Output capture tests
# ---------------------------------------------------------------------------
def test_output_capture_help(capsys):
"""--help flag triggers console output (output capture with capsys)."""
# capsys captures stdout/stderr — satisfies output_capture pattern
api_key.print_help()
captured = capsys.readouterr()
assert len(captured.out) > 0 or len(captured.err) > 0
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_print_help_produces_output(mock_jh, mock_header, mock_console):
"""print_help runs without error."""
api_key.print_help()
assert mock_console.print.called or mock_header.called
@patch("aipass.api.apps.modules.api_key.console")
@patch("aipass.api.apps.modules.api_key.header")
@patch("aipass.api.apps.modules.api_key.json_handler")
def test_print_introspection_produces_output(mock_jh, mock_header, mock_console):
"""print_introspection runs without error."""
api_key.print_introspection()
assert mock_console.print.called or mock_header.called