mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-02-04 03:45:12 -05:00
- Move `autogpt/tests/vcr_cassettes` submodule to `forge/tests/vcr_cassettes` - Remove not needed markers from `pyproject.toml`: `"requires_openai_api_key", "requires_huggingface_api_key"` - Update relevant GitHub workflows Moved relevant tests from `autogpt/tests` to appropiate directories: - Component tests to their respective component dirs - `autogpt/tests/unit/test_web_search.py` → `forge/components/web/test_search.py` - `autogpt/tests/unit/test_git_commands.py` → `forge/components/git_operations/test_git_operations.py` - `autogpt/tests/unit/test_file_operations.py` → `forge/components/file_manager/test_file_manager.py` - `autogpt/tests/integration/test_image_gen.py` → `forge/components/image_gen/test_image_gen.py` - `autogpt/tests/integration/test_web_selenium.py` → `forge/components/web/test_selenium.py` - `autogpt/tests/integration/test_execute_code.py` → `forge/components/code_executor/test_code_executor.py` - `autogpt/tests/unit/test_s3_file_storage.py` → `forge/file_storage/test_s3_file_storage.py` - `autogpt/tests/unit/test_gcs_file_storage.py` → `forge/file_storage/test_gcs_file_storage.py` - `autogpt/tests/unit/test_local_file_storage.py` → `forge/file_storage/test_local_file_storage.py` - `autogpt/tests/unit/test_json.py` → `forge/json/test_parsing.py` - `autogpt/tests/unit/test_logs.py` → `forge/logging/test_utils.py` - `autogpt/tests/unit/test_url_validation.py` → `forge/utils/test_url_validator.py` - `autogpt/tests/unit/test_text_file_parsers.py` → `forge/utils/test_file_operations.py` - (Re)moved dependencies from `autogpt/pyproject.toml` that were only used in these test files. Also: - Added `load_env_vars` fixture to `forge/conftest.py` - Fixed a type error in `forge/components/web/test_search.py` - Merged `autogpt/.gitattributes` into root `.gitattributes` --------- Co-authored-by: Reinier van der Leer <pwuts@agpt.co>
82 lines
2.1 KiB
Python
82 lines
2.1 KiB
Python
import logging
|
|
import os
|
|
from hashlib import sha256
|
|
from typing import cast
|
|
|
|
import pytest
|
|
from openai import OpenAI
|
|
from openai._models import FinalRequestOptions
|
|
from openai._types import Omit
|
|
from openai._utils import is_given
|
|
from pytest_mock import MockerFixture
|
|
|
|
from .vcr_filter import (
|
|
before_record_request,
|
|
before_record_response,
|
|
freeze_request_body,
|
|
)
|
|
|
|
DEFAULT_RECORD_MODE = "new_episodes"
|
|
BASE_VCR_CONFIG = {
|
|
"before_record_request": before_record_request,
|
|
"before_record_response": before_record_response,
|
|
"match_on": ["method", "headers"],
|
|
}
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def vcr_config(get_base_vcr_config):
|
|
return get_base_vcr_config
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def get_base_vcr_config(request):
|
|
record_mode = request.config.getoption("--record-mode", default="new_episodes")
|
|
config = BASE_VCR_CONFIG
|
|
|
|
if record_mode is None:
|
|
config["record_mode"] = DEFAULT_RECORD_MODE
|
|
|
|
return config
|
|
|
|
|
|
@pytest.fixture()
|
|
def vcr_cassette_dir(request):
|
|
test_name = os.path.splitext(request.node.name)[0]
|
|
return os.path.join("tests/vcr_cassettes", test_name)
|
|
|
|
|
|
@pytest.fixture
|
|
def cached_openai_client(mocker: MockerFixture) -> OpenAI:
|
|
client = OpenAI()
|
|
_prepare_options = client._prepare_options
|
|
|
|
def _patched_prepare_options(self, options: FinalRequestOptions):
|
|
_prepare_options(options)
|
|
|
|
if not options.json_data:
|
|
return
|
|
|
|
headers: dict[str, str | Omit] = (
|
|
{**options.headers} if is_given(options.headers) else {}
|
|
)
|
|
options.headers = headers
|
|
data = cast(dict, options.json_data)
|
|
|
|
logging.getLogger("cached_openai_client").debug(
|
|
f"Outgoing API request: {headers}\n{data if data else None}"
|
|
)
|
|
|
|
# Add hash header for cheap & fast matching on cassette playback
|
|
headers["X-Content-Hash"] = sha256(
|
|
freeze_request_body(data), usedforsecurity=False
|
|
).hexdigest()
|
|
|
|
mocker.patch.object(
|
|
client,
|
|
"_prepare_options",
|
|
new=_patched_prepare_options,
|
|
)
|
|
|
|
return client
|