File size: 4,838 Bytes
fd11263 | 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 | from types import SimpleNamespace
import pytest
from agent_evaluation import cli
from agent_evaluation.integrations.openai import OpenAIResponsesBackend
def _args(*extra: str) -> list[str]:
return [
"--skill", "skill",
"--dataset", "dataset",
"--artifacts", "artifacts",
"--adapter", "simulator",
*extra,
]
def test_cli_requires_llm_backend_and_model_together(tmp_path, monkeypatch):
empty_env = tmp_path / "empty.env"
empty_env.write_text("", encoding="utf-8")
monkeypatch.setattr(cli, "DEFAULT_ENV_FILE", empty_env)
monkeypatch.delenv("SKILL_EVAL_LLM_MODEL", raising=False)
monkeypatch.delenv("LLM_JUDGE_MODEL", raising=False)
with pytest.raises(SystemExit) as exc_info:
cli.main(_args("--llm-backend", "openai"))
assert exc_info.value.code == 2
def test_cli_rejects_non_positive_llm_timeout():
with pytest.raises(SystemExit) as exc_info:
cli.main(_args("--llm-timeout", "0"))
assert exc_info.value.code == 2
def test_cli_builds_openai_backend_and_partial_fails_hard_gate(monkeypatch):
captured = {}
class Loop:
def __init__(self, adapter, *, llm_backend=None, **kwargs):
captured["backend"] = llm_backend
def run(self, *args, **kwargs):
return SimpleNamespace(
scores=[SimpleNamespace(result_kind="assessment", status="success", score=0.5)],
experiment_id="experiment-test",
summary=lambda: {"pass_rate": 0.5},
)
monkeypatch.setattr(cli, "EvaluationLoop", Loop)
monkeypatch.setattr(
cli,
"create_native_adapters",
lambda database, endpoint: SimpleNamespace(experiment_backend=object(), trace_store=object()),
)
result = cli.main(_args(
"--llm-backend", "openai",
"--llm-model", "explicit-model",
"--llm-timeout", "17",
))
assert result == 1
assert isinstance(captured["backend"], OpenAIResponsesBackend)
assert captured["backend"].model == "explicit-model"
assert captured["backend"].timeout_seconds == 17
def test_cli_loads_default_local_env_file_for_openai_backend(tmp_path, monkeypatch):
env_file = tmp_path / ".env"
env_file.write_text(
"OPENAI_API_KEY=test-key\n"
"OPENAI_BASE_URL=https://example.test/v1\n"
"LLM_JUDGE_MODEL=env-model\n",
encoding="utf-8",
)
monkeypatch.setattr(cli, "DEFAULT_ENV_FILE", env_file)
for name in ("OPENAI_API_KEY", "OPENAI_BASE_URL", "SKILL_EVAL_LLM_MODEL", "LLM_JUDGE_MODEL"):
monkeypatch.delenv(name, raising=False)
captured = {}
class Loop:
def __init__(self, adapter, *, llm_backend=None, **kwargs):
captured["backend"] = llm_backend
def run(self, *args, **kwargs):
return SimpleNamespace(
scores=[SimpleNamespace(result_kind="assessment", status="success", score=1.0)],
experiment_id="experiment-test",
summary=lambda: {"pass_rate": 1.0},
)
monkeypatch.setattr(cli, "EvaluationLoop", Loop)
monkeypatch.setattr(
cli,
"create_native_adapters",
lambda database, endpoint: SimpleNamespace(experiment_backend=object(), trace_store=object()),
)
assert cli.main(_args()) == 0
assert isinstance(captured["backend"], OpenAIResponsesBackend)
assert captured["backend"].model == "env-model"
assert cli.os.environ["OPENAI_API_KEY"] == "test-key"
assert cli.os.environ["OPENAI_BASE_URL"] == "https://example.test/v1"
def test_cli_flags_override_env_model_and_shell_overrides_env_file(tmp_path, monkeypatch):
env_file = tmp_path / "shared.env"
env_file.write_text(
"OPENAI_API_KEY=file-key\nLLM_JUDGE_MODEL=file-model\n",
encoding="utf-8",
)
monkeypatch.setenv("OPENAI_API_KEY", "shell-key")
monkeypatch.delenv("LLM_JUDGE_MODEL", raising=False)
captured = {}
class Loop:
def __init__(self, adapter, *, llm_backend=None, **kwargs):
captured["backend"] = llm_backend
def run(self, *args, **kwargs):
return SimpleNamespace(
scores=[SimpleNamespace(result_kind="assessment", status="success", score=1.0)],
experiment_id="experiment-test",
summary=lambda: {"pass_rate": 1.0},
)
monkeypatch.setattr(cli, "EvaluationLoop", Loop)
monkeypatch.setattr(
cli,
"create_native_adapters",
lambda database, endpoint: SimpleNamespace(experiment_backend=object(), trace_store=object()),
)
assert cli.main(_args(
"--env-file", str(env_file), "--llm-model", "flag-model",
)) == 0
assert captured["backend"].model == "flag-model"
assert cli.os.environ["OPENAI_API_KEY"] == "shell-key"
|