Download testing/agent_evaluation/tests/test_cli.py from yuhanqin/ut-evaluation: direct link, hf CLI and curl.
- Browser
- Download file 4.84 kB
-
https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/agent_evaluation/tests/test_cli.py
- Command line
-
hf download hf://datasets/yuhanqin/ut-evaluation/testing/agent_evaluation/tests/test_cli.py
-
curl -L -o test_cli.py https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/agent_evaluation/tests/test_cli.py
4.84 kB
| 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" | |