5bbda89386
- Fix bare excepts (replace with except Exception:) - Remove wildcard star imports from test step files - Add explicit imports for ConfigurationError, AgentCreationError - Add 'from err' to raise statements in except blocks - Fix loop variable binding with default arguments (B023) - Organize imports, fix trailing whitespace and blank line whitespace - Bump Python to 3.13 and update tool configurations
107 lines
2.7 KiB
Python
107 lines
2.7 KiB
Python
"""
|
|
Step definitions for application utility methods tests.
|
|
"""
|
|
|
|
import asyncio
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from behave import given, then, when
|
|
from behave.runner import Context
|
|
|
|
from cleveractors.core.application import ReactiveCleverAgentsApp
|
|
|
|
# Given steps
|
|
|
|
|
|
@given("a clean test environment")
|
|
def step_utils_clean_test_environment(context: Context):
|
|
"""Initialize clean test environment for utilities."""
|
|
context.temp_dir = Path(tempfile.mkdtemp())
|
|
context.app = None
|
|
context.json_string = None
|
|
context.sanitized_json = None
|
|
|
|
try:
|
|
context.loop = asyncio.get_event_loop()
|
|
except RuntimeError:
|
|
context.loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(context.loop)
|
|
|
|
|
|
@given("a valid JSON string")
|
|
def step_utils_valid_json_string(context: Context):
|
|
"""Create valid JSON string for utility testing."""
|
|
context.json_string = '{"key": "value", "number": 123}'
|
|
|
|
|
|
@given("JSON with unescaped newlines")
|
|
def step_utils_json_with_newlines(context: Context):
|
|
"""Create JSON with newlines for utility testing."""
|
|
context.json_string = '{"key": "value\nwith newline"}'
|
|
|
|
|
|
# When steps
|
|
|
|
|
|
@when("sanitizing the JSON")
|
|
def step_utils_sanitize_json(context: Context):
|
|
"""Sanitize JSON string using utility method."""
|
|
# Create a basic app if not already created
|
|
if not context.app:
|
|
config = """
|
|
agents:
|
|
test_agent:
|
|
type: llm
|
|
config:
|
|
provider: openai
|
|
model: gpt-3.5-turbo
|
|
|
|
routes:
|
|
main:
|
|
type: stream
|
|
stream_type: cold
|
|
operators:
|
|
- type: map
|
|
params:
|
|
agent: test_agent
|
|
publications:
|
|
- __output__
|
|
|
|
merges:
|
|
- sources: [__input__]
|
|
target: main
|
|
"""
|
|
config_file = context.temp_dir / "config.yaml"
|
|
config_file.write_text(config)
|
|
context.app = ReactiveCleverAgentsApp([config_file])
|
|
|
|
context.sanitized_json = context.app._sanitize_json_string(context.json_string)
|
|
|
|
|
|
# Then steps
|
|
|
|
|
|
@then("JSON remains unchanged")
|
|
def step_utils_json_unchanged(context: Context):
|
|
"""Verify JSON unchanged by sanitization."""
|
|
assert context.sanitized_json == context.json_string
|
|
|
|
|
|
@then("newlines are escaped")
|
|
def step_utils_newlines_escaped(context: Context):
|
|
"""Verify newlines are escaped in sanitized JSON."""
|
|
assert "\\n" in context.sanitized_json
|
|
assert "\n" not in context.sanitized_json or context.sanitized_json.count("\n") == 0
|
|
|
|
|
|
@then("JSON becomes valid")
|
|
def step_utils_json_becomes_valid(context: Context):
|
|
"""Verify JSON is valid after sanitization."""
|
|
try:
|
|
json.loads(context.sanitized_json)
|
|
assert True
|
|
except json.JSONDecodeError as e:
|
|
raise AssertionError("JSON is not valid") from e
|