Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,121 @@ async def test_aclose_really_closes_real_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_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
107 changes: 107 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""Tests for the centralized logger factory and CONCLAVE_LOG_LEVEL resolution.

``get_logger`` configures the root ``conclave`` logger exactly once (guarded by a
module ``_CONFIGURED`` flag) and returns either that root or a named child. These
tests prove the level is read from ``CONCLAVE_LOG_LEVEL`` (default ``WARNING``),
that an unrecognized value falls back to ``WARNING``, and that the factory's
root-vs-child contract holds. Each test resets the one-shot configuration state so
the env-var branch actually runs instead of short-circuiting on the import-time
configuration done by ``conclave.transport``.
"""

from __future__ import annotations

import logging

import pytest

import conclave.logging as logging_mod
from conclave.logging import get_logger


@pytest.fixture
def fresh_logging(monkeypatch):
"""Reset the one-shot logger config so a fresh get_logger() reconfigures.

Saves and restores ``_CONFIGURED`` plus the root ``conclave`` logger's
handlers/level/propagate so the global logging state is left exactly as found.
"""
root = logging.getLogger("conclave")
saved_configured = logging_mod._CONFIGURED
saved_handlers = root.handlers[:]
saved_level = root.level
saved_propagate = root.propagate

# Force reconfiguration on the next get_logger call.
monkeypatch.setattr(logging_mod, "_CONFIGURED", False)
root.handlers = []

yield root

# Restore prior state.
root.handlers = saved_handlers
root.setLevel(saved_level)
root.propagate = saved_propagate
logging_mod._CONFIGURED = saved_configured


def test_default_level_is_warning_when_env_unset(fresh_logging, monkeypatch):
"""With CONCLAVE_LOG_LEVEL unset the root logger is configured at WARNING."""
monkeypatch.delenv("CONCLAVE_LOG_LEVEL", raising=False)

logger = get_logger()

assert logger.name == "conclave"
assert logger.level == logging.WARNING
assert logger.propagate is False
assert len(logger.handlers) == 1
assert isinstance(logger.handlers[0], logging.StreamHandler)


def test_env_var_sets_level_case_insensitively(fresh_logging, monkeypatch):
"""A lowercase CONCLAVE_LOG_LEVEL is upper-cased and applied (DEBUG)."""
monkeypatch.setenv("CONCLAVE_LOG_LEVEL", "debug")

logger = get_logger()

assert logger.level == logging.DEBUG


def test_unknown_level_falls_back_to_warning(fresh_logging, monkeypatch):
"""An unrecognized level name falls back to WARNING rather than crashing."""
monkeypatch.setenv("CONCLAVE_LOG_LEVEL", "NOPE")

logger = get_logger()

assert logger.level == logging.WARNING


def test_named_logger_is_child_of_root(fresh_logging, monkeypatch):
"""A non-default name returns a child logger that inherits root config."""
monkeypatch.setenv("CONCLAVE_LOG_LEVEL", "INFO")

child = get_logger("transport")

assert child.name == "conclave.transport"
assert child.parent is logging.getLogger("conclave")
# Child has no handler of its own; it propagates to the configured root.
assert child.handlers == []
# Effective level is inherited from the root we just configured at INFO.
assert child.getEffectiveLevel() == logging.INFO


def test_configuration_happens_once(fresh_logging, monkeypatch):
"""Repeated calls do not stack handlers -- configuration is one-shot."""
monkeypatch.setenv("CONCLAVE_LOG_LEVEL", "ERROR")

first = get_logger()
assert len(first.handlers) == 1
assert logging_mod._CONFIGURED is True

# Changing the env now must have no effect -- the guard short-circuits.
monkeypatch.setenv("CONCLAVE_LOG_LEVEL", "DEBUG")
second = get_logger()

assert second is first
assert len(second.handlers) == 1 # not duplicated
assert second.level == logging.ERROR # unchanged from first config
Loading
Loading