-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
377 lines (279 loc) · 14 KB
/
Copy pathtest_cli.py
File metadata and controls
377 lines (279 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Tests for the CLI exit-code contract and HTTP-client lifecycle wiring.
These exercise ``conclave.cli.ask`` through Typer's ``CliRunner`` (no real keys,
no network). Two concerns are pinned here:
* **Exit-code contract (#17).** A run that produces zero *usable* member answers
exits non-zero (code 1) on both the human and ``--json`` paths, and under
``--json`` the full JSON payload is still emitted to stdout so a script can
parse the result *and* detect the failure via the exit code. A run with at
least one usable answer exits 0.
* **Pooled-client lifecycle (#20).** The synchronous council wrappers close the
shared httpx client when the run completes, so ``transport.aclose()`` is
actually invoked and no client leaks past the CLI command.
"""
from __future__ import annotations
import json
import pytest
from typer.testing import CliRunner
from conclave import cli
from conclave.config import ConclaveConfig
runner = CliRunner()
def _config() -> ConclaveConfig:
"""A deterministic config independent of any on-disk ~/.conclave file."""
return ConclaveConfig(
models={
"grok": "xai/grok-4.3",
"gemini": "gemini/gemini-2.5-pro",
"claude": "anthropic/claude-sonnet-4-6",
"perplexity": "perplexity/sonar-pro",
},
councils={"default": ["grok", "gemini", "claude", "perplexity"]},
synthesizer="claude",
)
@pytest.fixture
def patch_cli_config(monkeypatch):
"""Make ``conclave.cli.load_config`` return the deterministic test config."""
monkeypatch.setattr(cli, "load_config", _config)
def test_no_members_human_exits_one(clear_keys, patch_cli_config):
"""Plain (human) path: zero usable answers -> exit code 1."""
result = runner.invoke(cli.app, ["ask", "hello"])
assert result.exit_code == 1
assert "No usable council answers" in result.output
def test_no_members_json_exits_one_but_emits_json(clear_keys, patch_cli_config):
"""JSON path: zero usable answers -> exit 1, yet valid JSON still on stdout."""
result = runner.invoke(cli.app, ["ask", "hello", "--json"])
assert result.exit_code == 1
payload = json.loads(result.stdout)
assert payload["answers"] == []
assert payload["prompt"] == "hello"
def test_all_members_failed_exits_one(monkeypatch, patch_cli_config, patch_call_model):
"""Keys present but every member errors -> zero usable answers -> exit 1."""
for var in ("XAI_API_KEY", "GEMINI_API_KEY", "ANTHROPIC_API_KEY", "PERPLEXITY_API_KEY"):
monkeypatch.setenv(var, "dummy-key")
def handler(model, messages, **kwargs):
raise RuntimeError("provider down")
patch_call_model(handler)
result = runner.invoke(cli.app, ["ask", "hello", "--council", "grok,gemini", "--mode", "raw"])
assert result.exit_code == 1
def test_successful_run_exits_zero(monkeypatch, patch_cli_config, patch_call_model):
"""At least one usable answer -> exit 0, JSON payload carries the answers."""
for var in ("XAI_API_KEY", "GEMINI_API_KEY", "ANTHROPIC_API_KEY", "PERPLEXITY_API_KEY"):
monkeypatch.setenv(var, "dummy-key")
def handler(model, messages, **kwargs):
from tests.conftest import make_response
return make_response(f"answer from {model}")
patch_call_model(handler)
result = runner.invoke(
cli.app,
["ask", "hello", "--council", "grok,gemini", "--mode", "raw", "--json"],
)
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert len(payload["answers"]) == 2
assert all(a["answer"].startswith("answer from") for a in payload["answers"])
def test_unknown_mode_exits_two(patch_cli_config):
"""Usage error (bad mode) keeps its distinct exit code 2."""
result = runner.invoke(cli.app, ["ask", "hello", "--mode", "bogus"])
assert result.exit_code == 2
def test_unresolved_council_exits_two(patch_cli_config):
"""Usage error (empty council selector resolves to zero members) -> exit 2."""
result = runner.invoke(cli.app, ["ask", "hello", "--council", " , "])
assert result.exit_code == 2
assert "No council members resolved" in result.output
def test_sync_run_closes_pooled_client(monkeypatch, patch_call_model):
"""The sync wrapper invokes transport.aclose() so the client never leaks."""
import conclave.council as council_mod
from conclave import Council
for var in ("XAI_API_KEY", "GEMINI_API_KEY", "ANTHROPIC_API_KEY", "PERPLEXITY_API_KEY"):
monkeypatch.setenv(var, "dummy-key")
calls = {"aclose": 0}
async def fake_aclose():
calls["aclose"] += 1
monkeypatch.setattr(council_mod.transport, "aclose", fake_aclose)
def handler(model, messages, **kwargs):
from tests.conftest import make_response
return make_response("ok")
patch_call_model(handler)
Council(models=["grok"], synthesizer="grok", config=_config()).ask_sync(
"hello", synthesize=False
)
assert calls["aclose"] == 1
def test_close_sync_invokes_aclose(monkeypatch):
"""Council.close_sync drives transport.aclose without re-closing recursively."""
import conclave.council as council_mod
from conclave import Council
calls = {"aclose": 0}
async def fake_aclose():
calls["aclose"] += 1
monkeypatch.setattr(council_mod.transport, "aclose", fake_aclose)
Council(models=["grok"], config=_config()).close_sync()
# close_sync passes close_client=False, so aclose fires exactly once (the body),
# not twice (body + finally).
assert calls["aclose"] == 1
async def test_aclose_really_closes_real_client():
"""End-to-end: a real pooled client gets created then closed by aclose()."""
from conclave import transport
client = transport._get_client()
assert not client.is_closed
await transport.aclose()
assert client.is_closed
# --------------------------------------------------------------------------- #
# Human (rich) renderers + the providers command. These cover the panel/table
# rendering paths that the JSON exit-code tests bypass via model_dump.
# --------------------------------------------------------------------------- #
def _all_keys(monkeypatch) -> None:
for var in ("XAI_API_KEY", "GEMINI_API_KEY", "ANTHROPIC_API_KEY", "PERPLEXITY_API_KEY"):
monkeypatch.setenv(var, "dummy-key")
def test_synthesize_human_render_prints_synthesis(monkeypatch, patch_cli_config, patch_call_model):
"""The default synthesize mode renders member panels and a SYNTHESIS panel."""
_all_keys(monkeypatch)
def handler(model, messages, **kwargs):
from tests.conftest import make_response
# Every call (members and the synthesizer) returns a usable answer, so the
# synthesis runs and its panel is rendered.
return make_response(f"ans-{model}")
patch_call_model(handler)
result = runner.invoke(cli.app, ["ask", "hello", "--council", "grok,gemini"])
assert result.exit_code == 0
# Member panel titles and the synthesis header are present in the rendered output.
assert "grok" in result.output
assert "SYNTHESIS" in result.output
def test_raw_human_render_prints_member_panels(monkeypatch, patch_cli_config, patch_call_model):
"""Raw mode renders each member's answer panel and no synthesis header."""
_all_keys(monkeypatch)
def handler(model, messages, **kwargs):
from tests.conftest import make_response
return make_response(f"raw-{model}")
patch_call_model(handler)
result = runner.invoke(cli.app, ["ask", "hello", "--council", "grok,gemini", "--mode", "raw"])
assert result.exit_code == 0
assert "grok" in result.output
assert "gemini" in result.output
def test_debate_human_render_prints_rounds(monkeypatch, patch_cli_config, patch_call_model):
"""Debate mode renders a Round rule per round plus the FINAL SYNTHESIS panel."""
_all_keys(monkeypatch)
def handler(model, messages, **kwargs):
from tests.conftest import make_response
return make_response(f"debate-{model}")
patch_call_model(handler)
result = runner.invoke(
cli.app,
["ask", "hello", "--council", "grok,gemini", "--mode", "debate", "--rounds", "2"],
)
assert result.exit_code == 0
assert "Round" in result.output
assert "SYNTHESIS" in result.output
def test_adversarial_human_render_prints_proposal_and_verdict(
monkeypatch, patch_cli_config, patch_call_model
):
"""Adversarial mode renders the proposal, critiques, and a VERDICT panel."""
_all_keys(monkeypatch)
def handler(model, messages, **kwargs):
from tests.conftest import make_response
return make_response(f"adv-{model}")
patch_call_model(handler)
result = runner.invoke(
cli.app,
["ask", "hello", "--council", "grok,gemini", "--mode", "adversarial"],
)
assert result.exit_code == 0
assert "Proposal" in result.output
assert "VERDICT" in result.output
def test_skipped_members_warning_is_printed(monkeypatch, patch_cli_config, patch_call_model):
"""A member with no key is skipped and surfaced via the yellow warning line."""
# Only grok has a key; gemini will be skipped for lack of GEMINI_API_KEY.
monkeypatch.delenv("GEMINI_API_KEY", raising=False)
monkeypatch.setenv("XAI_API_KEY", "dummy-key")
def handler(model, messages, **kwargs):
from tests.conftest import make_response
return make_response(f"ans-{model}")
patch_call_model(handler)
result = runner.invoke(cli.app, ["ask", "hello", "--council", "grok,gemini", "--mode", "raw"])
assert result.exit_code == 0
assert "Skipped (no key)" in result.output
assert "gemini" in result.output
def test_cache_flag_serves_second_run_from_cache(monkeypatch, patch_cli_config, tmp_path):
"""`--cache` makes a second identical CLI run a hit (providers not re-called)."""
import conclave.council as council_mod
from conclave.models import ModelAnswer
monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path))
_all_keys(monkeypatch)
counter = {"n": 0}
async def fake_call_model(name, model_id, messages, *, temperature=0.7, timeout=120.0):
counter["n"] += 1
return ModelAnswer(name=name, model_id=model_id, answer=f"ans-{model_id}")
monkeypatch.setattr(council_mod, "call_model", fake_call_model)
args = ["ask", "what is 2+2?", "--council", "grok", "--mode", "raw", "--cache", "--json"]
first = runner.invoke(cli.app, args)
assert first.exit_code == 0
n_after_first = counter["n"]
assert n_after_first > 0
assert json.loads(first.stdout)["cached"] is False
second = runner.invoke(cli.app, args)
assert second.exit_code == 0
assert counter["n"] == n_after_first # no new provider calls -> served from cache
assert json.loads(second.stdout)["cached"] is True
def test_no_cache_flag_runs_live_each_time(monkeypatch, patch_cli_config, tmp_path):
"""`--no-cache` forces a live run even if config enabled caching."""
import conclave.council as council_mod
from conclave.models import ModelAnswer
monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path))
_all_keys(monkeypatch)
counter = {"n": 0}
async def fake_call_model(name, model_id, messages, *, temperature=0.7, timeout=120.0):
counter["n"] += 1
return ModelAnswer(name=name, model_id=model_id, answer="ans")
monkeypatch.setattr(council_mod, "call_model", fake_call_model)
args = ["ask", "p", "--council", "grok", "--mode", "raw", "--no-cache", "--json"]
runner.invoke(cli.app, args)
n = counter["n"]
runner.invoke(cli.app, args)
assert counter["n"] == n * 2 # ran live both times
def test_providers_command_lists_keys_without_values(monkeypatch, patch_cli_config):
"""`conclave providers` prints a table marking present/absent keys, no values."""
monkeypatch.setenv("XAI_API_KEY", "super-secret-value")
monkeypatch.delenv("GEMINI_API_KEY", raising=False)
result = runner.invoke(cli.app, ["providers"])
assert result.exit_code == 0
assert "conclave providers" in result.output
# Provider names and the env-var column appear; the secret VALUE never does.
assert "grok" in result.output
assert "XAI_API_KEY" in result.output
assert "super-secret-value" not in result.output
def test_providers_command_lists_new_first_class_providers(monkeypatch, tmp_path):
"""`conclave providers` includes the issue-#5 direct-key providers + env vars.
Uses the real default config (no patched cli.load_config) so the registry's
DEFAULT_MODELS drive the table; an empty CONCLAVE_CONFIG path means the
built-in defaults are what appear.
"""
monkeypatch.setenv("CONCLAVE_CONFIG", str(tmp_path / "missing.yml"))
# The env-var column only renders a NAME when a key is present (otherwise it
# shows '-'), so set each new provider's key. The values are obvious fakes and
# must never appear in the output (BYO-keys name-only posture).
secrets = {
"GROQ_API_KEY": "groq-secret-value",
"DEEPSEEK_API_KEY": "deepseek-secret-value",
"MISTRAL_API_KEY": "mistral-secret-value",
"TOGETHER_API_KEY": "together-secret-value",
}
for var, val in secrets.items():
monkeypatch.setenv(var, val)
# Force a wide console so Rich does not ellipsize the (long) env-var / model
# columns; the module-level console fixes its width at import time, so swap in
# a wide one. Otherwise the assertions would test rendering width, not content.
from rich.console import Console
monkeypatch.setattr(cli, "console", Console(width=200))
from conclave.config import clear_config_cache
clear_config_cache()
result = runner.invoke(cli.app, ["providers"])
assert result.exit_code == 0
for name, env_var in (
("groq", "GROQ_API_KEY"),
("deepseek", "DEEPSEEK_API_KEY"),
("mistral", "MISTRAL_API_KEY"),
("together", "TOGETHER_API_KEY"),
):
assert name in result.output
assert env_var in result.output
# No secret VALUE ever appears (only the env-var NAME does).
for val in secrets.values():
assert val not in result.output