mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-23 17:04:21 +01:00
Refactor challenges to use cycle count instead of time (#4222)
Co-authored-by: Richard Beales <rich@richbeales.net>
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import contextlib
|
||||
import random
|
||||
from functools import wraps
|
||||
from typing import Optional
|
||||
from typing import Any, Callable, Dict, Generator, Optional, Tuple
|
||||
|
||||
import pytest
|
||||
|
||||
from autogpt.agent import Agent
|
||||
|
||||
|
||||
def get_level_to_run(
|
||||
user_selected_level: Optional[int],
|
||||
level_currently_beaten: Optional[int],
|
||||
user_selected_level: int,
|
||||
level_currently_beaten: int,
|
||||
max_level: int,
|
||||
) -> int:
|
||||
"""
|
||||
@@ -25,7 +28,7 @@ def get_level_to_run(
|
||||
ValueError: If the user-selected level is greater than the maximum level allowed.
|
||||
"""
|
||||
if user_selected_level is None:
|
||||
if level_currently_beaten is None:
|
||||
if level_currently_beaten == -1:
|
||||
pytest.skip(
|
||||
"No one has beaten any levels so we cannot run the test in our pipeline"
|
||||
)
|
||||
@@ -36,7 +39,7 @@ def get_level_to_run(
|
||||
return user_selected_level
|
||||
|
||||
|
||||
def generate_noise(noise_size) -> str:
|
||||
def generate_noise(noise_size: int) -> str:
|
||||
return "".join(
|
||||
random.choices(
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
||||
@@ -45,19 +48,46 @@ def generate_noise(noise_size) -> str:
|
||||
)
|
||||
|
||||
|
||||
def run_multiple_times(times):
|
||||
def run_multiple_times(times: int) -> Callable:
|
||||
"""
|
||||
Decorator that runs a test function multiple times.
|
||||
|
||||
:param times: The number of times the test function should be executed.
|
||||
"""
|
||||
|
||||
def decorator(test_func):
|
||||
def decorator(test_func: Callable[..., Any]) -> Callable[..., Any]:
|
||||
@wraps(test_func)
|
||||
def wrapper(*args, **kwargs):
|
||||
def wrapper(*args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> None:
|
||||
for _ in range(times):
|
||||
test_func(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def setup_mock_input(monkeypatch: pytest.MonkeyPatch, cycle_count: int) -> None:
|
||||
"""
|
||||
Sets up the mock input for testing.
|
||||
|
||||
:param monkeypatch: pytest's monkeypatch utility for modifying builtins.
|
||||
:param cycle_count: The number of cycles to mock.
|
||||
"""
|
||||
input_sequence = ["y"] * (cycle_count) + ["EXIT"]
|
||||
|
||||
def input_generator() -> Generator[str, None, None]:
|
||||
"""
|
||||
Creates a generator that yields input strings from the given sequence.
|
||||
"""
|
||||
yield from input_sequence
|
||||
|
||||
gen = input_generator()
|
||||
monkeypatch.setattr("builtins.input", lambda _: next(gen))
|
||||
|
||||
|
||||
def run_interaction_loop(
|
||||
monkeypatch: pytest.MonkeyPatch, agent: Agent, cycle_count: int
|
||||
) -> None:
|
||||
setup_mock_input(monkeypatch, cycle_count)
|
||||
with contextlib.suppress(SystemExit):
|
||||
agent.start_interaction_loop()
|
||||
|
||||
Reference in New Issue
Block a user