|
4 | 4 | from typer.testing import CliRunner |
5 | 5 |
|
6 | 6 | from antares.cli import app |
| 7 | +from antares.errors import ConnectionError, SimulationError, SubscriptionError |
7 | 8 |
|
8 | 9 | runner = CliRunner() |
9 | 10 |
|
@@ -50,3 +51,86 @@ async def fake_sub(self): |
50 | 51 | result = runner.invoke(app, ["subscribe", "--config", fake_config]) |
51 | 52 | assert result.exit_code == 0 |
52 | 53 | assert "test-event" in result.output |
| 54 | + |
| 55 | + |
| 56 | +def test_handle_error_json(monkeypatch): |
| 57 | + result = runner.invoke(app, ["reset", "--json"], catch_exceptions=False) |
| 58 | + assert result.exit_code in {1, 2} |
| 59 | + assert "error" in result.output |
| 60 | + |
| 61 | + |
| 62 | +def test_build_client_fails(mocker): |
| 63 | + mocker.patch("antares.config_loader.load_config", side_effect=Exception("broken config")) |
| 64 | + result = runner.invoke(app, ["reset", "--config", "invalid.toml"]) |
| 65 | + assert result.exit_code == 1 |
| 66 | + assert "Failed to load configuration" in result.output |
| 67 | + |
| 68 | + |
| 69 | +def test_cli_reset_error_handling(mocker, fake_config): |
| 70 | + mocker.patch( |
| 71 | + "antares.client.rest.RestClient.reset_simulation", |
| 72 | + side_effect=ConnectionError("cannot connect"), |
| 73 | + ) |
| 74 | + result = runner.invoke(app, ["reset", "--config", fake_config]) |
| 75 | + expected_exit_code = 2 |
| 76 | + assert result.exit_code == expected_exit_code |
| 77 | + assert "cannot connect" in result.output |
| 78 | + |
| 79 | + |
| 80 | +def test_cli_add_ship_error_handling(mocker, fake_config): |
| 81 | + mocker.patch( |
| 82 | + "antares.client.rest.RestClient.add_ship", side_effect=SimulationError("ship rejected") |
| 83 | + ) |
| 84 | + result = runner.invoke(app, ["add-ship", "--x", "1", "--y", "2", "--config", fake_config]) |
| 85 | + expected_exit_code = 2 |
| 86 | + assert result.exit_code == expected_exit_code |
| 87 | + assert "ship rejected" in result.output |
| 88 | + |
| 89 | + |
| 90 | +def test_cli_subscribe_error(monkeypatch, fake_config): |
| 91 | + class FailingAsyncGenerator: |
| 92 | + def __aiter__(self): |
| 93 | + return self |
| 94 | + |
| 95 | + async def __anext__(self): |
| 96 | + raise SubscriptionError("stream failed") |
| 97 | + |
| 98 | + monkeypatch.setattr( |
| 99 | + "antares.client.tcp.TCPSubscriber.subscribe", lambda self: FailingAsyncGenerator() |
| 100 | + ) |
| 101 | + |
| 102 | + result = runner.invoke(app, ["subscribe", "--config", fake_config]) |
| 103 | + expected_exit_code = 3 |
| 104 | + assert result.exit_code == expected_exit_code |
| 105 | + assert "stream failed" in result.output |
| 106 | + |
| 107 | + |
| 108 | +def test_cli_verbose_prints_config(mocker, fake_config): |
| 109 | + mocker.patch("antares.client.tcp.TCPSubscriber.subscribe", return_value=iter([])) |
| 110 | + mocker.patch("antares.client.rest.RestClient.reset_simulation") |
| 111 | + |
| 112 | + result = runner.invoke(app, ["reset", "--config", fake_config, "--verbose"]) |
| 113 | + assert result.exit_code == 0 |
| 114 | + assert "Using settings" in result.output |
| 115 | + |
| 116 | + |
| 117 | +def test_cli_subscribe_json(monkeypatch, fake_config): |
| 118 | + class OneEventGen: |
| 119 | + def __init__(self): |
| 120 | + self.done = False |
| 121 | + |
| 122 | + def __aiter__(self): |
| 123 | + return self |
| 124 | + |
| 125 | + async def __anext__(self): |
| 126 | + if not self.done: |
| 127 | + self.done = True |
| 128 | + return {"event": "test"} |
| 129 | + raise StopAsyncIteration |
| 130 | + |
| 131 | + monkeypatch.setattr("antares.client.tcp.TCPSubscriber.subscribe", lambda self: OneEventGen()) |
| 132 | + |
| 133 | + result = runner.invoke(app, ["subscribe", "--config", fake_config, "--json"]) |
| 134 | + |
| 135 | + assert result.exit_code == 0 |
| 136 | + assert '{"event": "test"}' in result.output |
0 commit comments