|
| 1 | +"""Integration tests for the `uipath list-models` CLI command. |
| 2 | +
|
| 3 | +The command renders a rich table grouped by vendor (one column per vendor) |
| 4 | +for human terminal use, and falls through to the shared `format_output` |
| 5 | +pipeline for `--format json|csv` and `--output <file>`. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 10 | + |
| 11 | +import pytest |
| 12 | +from click.testing import CliRunner |
| 13 | + |
| 14 | +from uipath._cli import cli |
| 15 | +from uipath.platform.agenthub import LlmModel |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def runner(): |
| 20 | + """Provide a Click CLI test runner.""" |
| 21 | + return CliRunner() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def mock_client(): |
| 26 | + """Provide a mocked UiPath client with an async agenthub service.""" |
| 27 | + with patch("uipath.platform._uipath.UiPath") as mock: |
| 28 | + client_instance = MagicMock() |
| 29 | + mock.return_value = client_instance |
| 30 | + |
| 31 | + client_instance.agenthub = MagicMock() |
| 32 | + client_instance.agenthub.get_available_llm_models_async = AsyncMock() |
| 33 | + |
| 34 | + yield client_instance |
| 35 | + |
| 36 | + |
| 37 | +def _make_models() -> list[LlmModel]: |
| 38 | + """Build a small list of LlmModel instances spanning multiple vendors.""" |
| 39 | + return [ |
| 40 | + LlmModel(model_name="gpt-4o-mini", vendor="OpenAi"), |
| 41 | + LlmModel(model_name="gpt-4.1", vendor="OpenAi"), |
| 42 | + LlmModel(model_name="claude-sonnet-4-5", vendor="Anthropic"), |
| 43 | + LlmModel(model_name="gemini-2.5-flash", vendor="VertexAi"), |
| 44 | + ] |
| 45 | + |
| 46 | + |
| 47 | +class TestRichTable: |
| 48 | + def test_renders_each_model_and_vendor(self, runner, mock_client, mock_env_vars): |
| 49 | + """All models and vendor headers appear in the rendered table.""" |
| 50 | + mock_client.agenthub.get_available_llm_models_async.return_value = ( |
| 51 | + _make_models() |
| 52 | + ) |
| 53 | + |
| 54 | + result = runner.invoke(cli, ["list-models"]) |
| 55 | + |
| 56 | + assert result.exit_code == 0 |
| 57 | + for model in _make_models(): |
| 58 | + assert model.model_name in result.output |
| 59 | + assert (model.vendor or "") in result.output |
| 60 | + mock_client.agenthub.get_available_llm_models_async.assert_awaited_once() |
| 61 | + |
| 62 | + def test_table_title(self, runner, mock_client, mock_env_vars): |
| 63 | + """The rich table renders its title for orientation.""" |
| 64 | + mock_client.agenthub.get_available_llm_models_async.return_value = ( |
| 65 | + _make_models() |
| 66 | + ) |
| 67 | + |
| 68 | + result = runner.invoke(cli, ["list-models"]) |
| 69 | + |
| 70 | + assert result.exit_code == 0 |
| 71 | + assert "Available LLM Models" in result.output |
| 72 | + |
| 73 | + def test_missing_vendor_grouped_under_unknown( |
| 74 | + self, runner, mock_client, mock_env_vars |
| 75 | + ): |
| 76 | + """A model with no vendor lands in an 'Unknown' column.""" |
| 77 | + mock_client.agenthub.get_available_llm_models_async.return_value = [ |
| 78 | + LlmModel(model_name="custom-model", vendor=None), |
| 79 | + ] |
| 80 | + |
| 81 | + result = runner.invoke(cli, ["list-models"]) |
| 82 | + |
| 83 | + assert result.exit_code == 0 |
| 84 | + assert "custom-model" in result.output |
| 85 | + assert "Unknown" in result.output |
| 86 | + |
| 87 | + def test_empty(self, runner, mock_client, mock_env_vars): |
| 88 | + """An empty model list renders the title without rows or errors.""" |
| 89 | + mock_client.agenthub.get_available_llm_models_async.return_value = [] |
| 90 | + |
| 91 | + result = runner.invoke(cli, ["list-models"]) |
| 92 | + |
| 93 | + assert result.exit_code == 0 |
| 94 | + assert "Available LLM Models" in result.output |
| 95 | + |
| 96 | + |
| 97 | +class TestMachineReadableFormats: |
| 98 | + def test_json_format(self, runner, mock_client, mock_env_vars): |
| 99 | + """--format json bypasses the rich table and emits parseable JSON.""" |
| 100 | + mock_client.agenthub.get_available_llm_models_async.return_value = ( |
| 101 | + _make_models() |
| 102 | + ) |
| 103 | + |
| 104 | + result = runner.invoke(cli, ["list-models", "--format", "json"]) |
| 105 | + |
| 106 | + assert result.exit_code == 0 |
| 107 | + payload = json.loads(result.output) |
| 108 | + assert isinstance(payload, list) |
| 109 | + assert {m["model_name"] for m in payload} == { |
| 110 | + "gpt-4o-mini", |
| 111 | + "gpt-4.1", |
| 112 | + "claude-sonnet-4-5", |
| 113 | + "gemini-2.5-flash", |
| 114 | + } |
| 115 | + |
| 116 | + def test_csv_format(self, runner, mock_client, mock_env_vars): |
| 117 | + """--format csv emits a header row and one row per model.""" |
| 118 | + mock_client.agenthub.get_available_llm_models_async.return_value = ( |
| 119 | + _make_models() |
| 120 | + ) |
| 121 | + |
| 122 | + result = runner.invoke(cli, ["list-models", "--format", "csv"]) |
| 123 | + |
| 124 | + assert result.exit_code == 0 |
| 125 | + lines = [line for line in result.output.splitlines() if line.strip()] |
| 126 | + assert "model_name" in lines[0] |
| 127 | + assert "vendor" in lines[0] |
| 128 | + assert any("gpt-4o-mini" in line for line in lines[1:]) |
| 129 | + |
| 130 | + def test_global_json_flag(self, runner, mock_client, mock_env_vars): |
| 131 | + """The cli-group --format json is honored too.""" |
| 132 | + mock_client.agenthub.get_available_llm_models_async.return_value = ( |
| 133 | + _make_models() |
| 134 | + ) |
| 135 | + |
| 136 | + result = runner.invoke(cli, ["--format", "json", "list-models"]) |
| 137 | + |
| 138 | + assert result.exit_code == 0 |
| 139 | + payload = json.loads(result.output) |
| 140 | + assert len(payload) == 4 |
| 141 | + |
| 142 | + def test_output_writes_through_plain_formatter( |
| 143 | + self, runner, mock_client, mock_env_vars, tmp_path |
| 144 | + ): |
| 145 | + """--output writes through format_output (not the rich path).""" |
| 146 | + mock_client.agenthub.get_available_llm_models_async.return_value = ( |
| 147 | + _make_models() |
| 148 | + ) |
| 149 | + out_file = tmp_path / "models.json" |
| 150 | + |
| 151 | + result = runner.invoke( |
| 152 | + cli, |
| 153 | + ["list-models", "--format", "json", "--output", str(out_file)], |
| 154 | + ) |
| 155 | + |
| 156 | + assert result.exit_code == 0 |
| 157 | + assert out_file.exists() |
| 158 | + payload = json.loads(out_file.read_text(encoding="utf-8")) |
| 159 | + assert {m["model_name"] for m in payload} == { |
| 160 | + "gpt-4o-mini", |
| 161 | + "gpt-4.1", |
| 162 | + "claude-sonnet-4-5", |
| 163 | + "gemini-2.5-flash", |
| 164 | + } |
| 165 | + |
| 166 | + def test_output_file_alias( |
| 167 | + self, runner, mock_client, mock_env_vars, tmp_path |
| 168 | + ): |
| 169 | + """`--output-file` works as an alias for `--output` (matches `run`).""" |
| 170 | + mock_client.agenthub.get_available_llm_models_async.return_value = ( |
| 171 | + _make_models() |
| 172 | + ) |
| 173 | + out_file = tmp_path / "models.json" |
| 174 | + |
| 175 | + result = runner.invoke( |
| 176 | + cli, |
| 177 | + ["list-models", "--format", "json", "--output-file", str(out_file)], |
| 178 | + ) |
| 179 | + |
| 180 | + assert result.exit_code == 0 |
| 181 | + assert out_file.exists() |
| 182 | + payload = json.loads(out_file.read_text(encoding="utf-8")) |
| 183 | + assert len(payload) == 4 |
| 184 | + |
| 185 | + |
| 186 | +class TestErrorPaths: |
| 187 | + def test_service_error(self, runner, mock_client, mock_env_vars): |
| 188 | + """Exceptions from the service are surfaced as click errors.""" |
| 189 | + mock_client.agenthub.get_available_llm_models_async.side_effect = RuntimeError( |
| 190 | + "boom" |
| 191 | + ) |
| 192 | + |
| 193 | + result = runner.invoke(cli, ["list-models"]) |
| 194 | + |
| 195 | + assert result.exit_code != 0 |
| 196 | + assert "boom" in result.output |
| 197 | + |
| 198 | + def test_missing_url(self, runner, monkeypatch): |
| 199 | + """Missing UIPATH_URL surfaces an auth-configuration error.""" |
| 200 | + monkeypatch.delenv("UIPATH_URL", raising=False) |
| 201 | + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "mock_token") |
| 202 | + |
| 203 | + result = runner.invoke(cli, ["list-models"]) |
| 204 | + |
| 205 | + assert result.exit_code != 0 |
| 206 | + assert "UIPATH_URL not configured" in result.output |
| 207 | + |
| 208 | + def test_missing_token(self, runner, monkeypatch): |
| 209 | + """Missing UIPATH_ACCESS_TOKEN surfaces an auth-configuration error.""" |
| 210 | + monkeypatch.setenv("UIPATH_URL", "https://cloud.uipath.com/org/tenant") |
| 211 | + monkeypatch.delenv("UIPATH_ACCESS_TOKEN", raising=False) |
| 212 | + |
| 213 | + result = runner.invoke(cli, ["list-models"]) |
| 214 | + |
| 215 | + assert result.exit_code != 0 |
| 216 | + assert "Authentication required" in result.output |
| 217 | + |
| 218 | + |
| 219 | +class TestRegistration: |
| 220 | + def test_help_text(self, runner): |
| 221 | + """--help surfaces the command description and options.""" |
| 222 | + result = runner.invoke(cli, ["list-models", "--help"]) |
| 223 | + |
| 224 | + assert result.exit_code == 0 |
| 225 | + assert "List available LLM models" in result.output |
| 226 | + assert "--format" in result.output |
| 227 | + assert "--output" in result.output |
| 228 | + assert "--output-file" in result.output |
| 229 | + |
| 230 | + def test_registered_in_cli(self, runner): |
| 231 | + """The command is wired into the top-level CLI group.""" |
| 232 | + result = runner.invoke(cli, ["--help"]) |
| 233 | + |
| 234 | + assert result.exit_code == 0 |
| 235 | + assert "list-models" in result.output |
0 commit comments