489 lines
15 KiB
Python
489 lines
15 KiB
Python
"""Step definitions for ACP facade and stubs Behave scenarios."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
from behave import given, then, use_step_matcher, when
|
|
from behave.runner import Context
|
|
from pydantic import ValidationError
|
|
|
|
from cleveragents.acp.errors import (
|
|
AcpError,
|
|
AcpNotAvailableError,
|
|
AcpOperationNotFoundError,
|
|
AcpVersionMismatchError,
|
|
)
|
|
from cleveragents.acp.events import AcpEventQueue
|
|
from cleveragents.acp.facade import AcpLocalFacade
|
|
from cleveragents.acp.models import (
|
|
AcpErrorDetail,
|
|
AcpEvent,
|
|
AcpRequest,
|
|
AcpResponse,
|
|
)
|
|
from cleveragents.acp.transport import AcpHttpTransport
|
|
from cleveragents.acp.versioning import AcpVersionNegotiator
|
|
from cleveragents.core.exceptions import CleverAgentsError
|
|
|
|
use_step_matcher("re")
|
|
|
|
|
|
# AcpLocalFacade — creation and service registration
|
|
|
|
|
|
@given(r"a new AcpLocalFacade with no services")
|
|
def step_facade_no_services(context: Context) -> None:
|
|
context.facade = AcpLocalFacade()
|
|
|
|
|
|
@given(r"a new AcpLocalFacade with services (?P<services_json>.+)")
|
|
def step_facade_with_services(context: Context, services_json: str) -> None:
|
|
services = json.loads(services_json)
|
|
context.facade = AcpLocalFacade(services=services)
|
|
|
|
|
|
@then(r"the facade should be created successfully")
|
|
def step_facade_created(context: Context) -> None:
|
|
assert context.facade is not None
|
|
|
|
|
|
@when(r'I register a service named "(?P<name>[^"]+)" on the facade')
|
|
def step_register_service(context: Context, name: str) -> None:
|
|
context.facade.register_service(name, object())
|
|
|
|
|
|
@then(r"the service should be registered successfully")
|
|
def step_service_registered(context: Context) -> None:
|
|
pass
|
|
|
|
|
|
@when(r"I try to register a service with an empty name")
|
|
def step_register_empty_name(context: Context) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
context.facade.register_service("", object())
|
|
except ValueError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
@then(r"an ACP ValueError should be raised")
|
|
def step_value_error_raised(context: Context) -> None:
|
|
assert isinstance(context.caught_error, ValueError), (
|
|
f"Expected ValueError, got {type(context.caught_error)}"
|
|
)
|
|
|
|
|
|
# AcpLocalFacade — dispatch operations
|
|
|
|
|
|
@when(r'I dispatch operation "(?P<operation>[^"]+)" with params (?P<params_json>.+)')
|
|
def step_dispatch_operation(context: Context, operation: str, params_json: str) -> None:
|
|
params: dict[str, Any] = json.loads(params_json)
|
|
request = AcpRequest(operation=operation, params=params)
|
|
context.response = context.facade.dispatch(request)
|
|
|
|
|
|
@then(r'the response status should be "(?P<status>[^"]+)"')
|
|
def step_response_status(context: Context, status: str) -> None:
|
|
assert context.response.status == status, (
|
|
f"Expected status '{status}', got '{context.response.status}'"
|
|
)
|
|
|
|
|
|
@then(r'response data includes key "(?P<key>[^"]+)"')
|
|
def step_response_data_has_key(context: Context, key: str) -> None:
|
|
assert key in context.response.data, (
|
|
f"Key '{key}' not found in response data: {context.response.data}"
|
|
)
|
|
|
|
|
|
@then(r'response data key "(?P<key>[^"]+)" equals "(?P<value>[^"]+)"')
|
|
def step_response_data_key_value(context: Context, key: str, value: str) -> None:
|
|
assert key in context.response.data, f"Key '{key}' not found in response data"
|
|
actual = context.response.data[key]
|
|
assert str(actual) == value, f"Expected '{value}', got '{actual}'"
|
|
|
|
|
|
# AcpLocalFacade — unknown operation
|
|
|
|
|
|
@when(r'I dispatch an unknown operation "(?P<operation>[^"]+)"')
|
|
def step_dispatch_unknown(context: Context, operation: str) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
request = AcpRequest(operation=operation)
|
|
context.facade.dispatch(request)
|
|
except AcpOperationNotFoundError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
@then(r"an AcpOperationNotFoundError should be raised")
|
|
def step_op_not_found_raised(context: Context) -> None:
|
|
assert isinstance(context.caught_error, AcpOperationNotFoundError)
|
|
|
|
|
|
@then(r'the error operation attribute should be "(?P<operation>[^"]+)"')
|
|
def step_error_operation_attr(context: Context, operation: str) -> None:
|
|
assert context.caught_error.operation == operation
|
|
|
|
|
|
# AcpLocalFacade — list_operations
|
|
|
|
|
|
@when(r"I call list_operations on the facade")
|
|
def step_list_operations(context: Context) -> None:
|
|
context.operations = context.facade.list_operations()
|
|
|
|
|
|
@then(r'the operations list should contain "(?P<operation>[^"]+)"')
|
|
def step_operations_contains(context: Context, operation: str) -> None:
|
|
assert operation in context.operations, f"'{operation}' not in {context.operations}"
|
|
|
|
|
|
@then(r"the operations list should have (?P<count>\d+) items")
|
|
def step_operations_count(context: Context, count: str) -> None:
|
|
expected = int(count)
|
|
assert len(context.operations) == expected, (
|
|
f"Expected {expected}, got {len(context.operations)}"
|
|
)
|
|
|
|
|
|
# AcpHttpTransport
|
|
|
|
|
|
@given(r"a new AcpHttpTransport")
|
|
def step_transport(context: Context) -> None:
|
|
context.transport = AcpHttpTransport()
|
|
|
|
|
|
@when(r"I try to send a request via the transport")
|
|
def step_transport_send(context: Context) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
request = AcpRequest(operation="test.op")
|
|
context.transport.send(request)
|
|
except AcpNotAvailableError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
@when(r'I try to connect via the transport to "(?P<url>[^"]+)"')
|
|
def step_transport_connect(context: Context, url: str) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
context.transport.connect(url)
|
|
except AcpNotAvailableError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
@when(r"I try to disconnect the transport")
|
|
def step_transport_disconnect(context: Context) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
context.transport.disconnect()
|
|
except AcpNotAvailableError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
@then(r"an AcpNotAvailableError should be raised")
|
|
def step_not_available_raised(context: Context) -> None:
|
|
assert isinstance(context.caught_error, AcpNotAvailableError), (
|
|
f"Expected AcpNotAvailableError, got {type(context.caught_error)}"
|
|
)
|
|
|
|
|
|
@then(r"the transport should not be connected")
|
|
def step_transport_not_connected(context: Context) -> None:
|
|
assert context.transport.is_connected() is False
|
|
|
|
|
|
# AcpEventQueue — local mode
|
|
|
|
|
|
@given(r"a new AcpEventQueue")
|
|
def step_event_queue(context: Context) -> None:
|
|
context.queue = AcpEventQueue()
|
|
context.callback_events: list[AcpEvent] = []
|
|
|
|
|
|
@given(r"I subscribe locally with a callback")
|
|
def step_subscribe_local(context: Context) -> None:
|
|
def _cb(event: AcpEvent) -> None:
|
|
context.callback_events.append(event)
|
|
|
|
context.subscription_id = context.queue.subscribe_local(_cb)
|
|
|
|
|
|
@when(r'I publish an event with type "(?P<event_type>[^"]+)"')
|
|
def step_publish_event(context: Context, event_type: str) -> None:
|
|
event = AcpEvent(event_type=event_type)
|
|
context.queue.publish(event)
|
|
|
|
|
|
@when(r"I publish (?P<count>\d+) events")
|
|
def step_publish_n_events(context: Context, count: str) -> None:
|
|
for i in range(int(count)):
|
|
event = AcpEvent(event_type=f"test.event.{i}")
|
|
context.queue.publish(event)
|
|
|
|
|
|
@when(r"I get events with limit (?P<limit>\d+)")
|
|
def step_get_events(context: Context, limit: str) -> None:
|
|
context.fetched_events = context.queue.get_events(limit=int(limit))
|
|
|
|
|
|
@then(r"the event queue should have (?P<count>\d+) event")
|
|
def step_queue_count(context: Context, count: str) -> None:
|
|
expected = int(count)
|
|
events = context.queue.get_events()
|
|
assert len(events) == expected, f"Expected {expected}, got {len(events)}"
|
|
|
|
|
|
@then(r'the callback should have been called with event type "(?P<event_type>[^"]+)"')
|
|
def step_callback_called(context: Context, event_type: str) -> None:
|
|
assert len(context.callback_events) > 0, "Callback was not called"
|
|
assert context.callback_events[-1].event_type == event_type
|
|
|
|
|
|
@when(r"I unsubscribe using the subscription id")
|
|
def step_unsubscribe(context: Context) -> None:
|
|
context.unsubscribe_result = context.queue.unsubscribe(context.subscription_id)
|
|
|
|
|
|
@when(r"I unsubscribe using a non-existent subscription id")
|
|
def step_unsubscribe_nonexistent(context: Context) -> None:
|
|
context.unsubscribe_result = context.queue.unsubscribe("NONEXISTENT_ID")
|
|
|
|
|
|
@then(r"the unsubscribe should return True")
|
|
def step_unsubscribe_true(context: Context) -> None:
|
|
assert context.unsubscribe_result is True
|
|
|
|
|
|
@then(r"the unsubscribe should return False")
|
|
def step_unsubscribe_false(context: Context) -> None:
|
|
assert context.unsubscribe_result is False
|
|
|
|
|
|
@then(r"I should receive (?P<count>\d+) events")
|
|
def step_received_count(context: Context, count: str) -> None:
|
|
expected = int(count)
|
|
assert len(context.fetched_events) == expected, (
|
|
f"Expected {expected}, got {len(context.fetched_events)}"
|
|
)
|
|
|
|
|
|
# AcpEventQueue — remote stub
|
|
|
|
|
|
@when(r'I try to subscribe remotely to "(?P<endpoint>[^"]+)"')
|
|
def step_remote_subscribe(context: Context, endpoint: str) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
context.queue.subscribe_remote(endpoint)
|
|
except AcpNotAvailableError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
# AcpVersionNegotiator
|
|
|
|
|
|
@given(r"a new AcpVersionNegotiator")
|
|
def step_version_negotiator(context: Context) -> None:
|
|
context.negotiator = AcpVersionNegotiator()
|
|
|
|
|
|
@when(r'I negotiate version "(?P<version>[^"]+)"')
|
|
def step_negotiate(context: Context, version: str) -> None:
|
|
context.negotiated_version = context.negotiator.negotiate(version)
|
|
|
|
|
|
@then(r'the negotiated version should be "(?P<version>[^"]+)"')
|
|
def step_negotiated(context: Context, version: str) -> None:
|
|
assert context.negotiated_version == version
|
|
|
|
|
|
@when(r'I try to negotiate version "(?P<version>[^"]+)"')
|
|
def step_negotiate_fail(context: Context, version: str) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
context.negotiator.negotiate(version)
|
|
except AcpVersionMismatchError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
@then(r"an AcpVersionMismatchError should be raised")
|
|
def step_version_mismatch_raised(context: Context) -> None:
|
|
assert isinstance(context.caught_error, AcpVersionMismatchError)
|
|
|
|
|
|
@then(r'the error requested_version should be "(?P<version>[^"]+)"')
|
|
def step_error_requested_version(context: Context, version: str) -> None:
|
|
assert context.caught_error.requested_version == version
|
|
|
|
|
|
@then(r'version "(?P<version>[^"]+)" should be supported')
|
|
def step_version_supported(context: Context, version: str) -> None:
|
|
assert context.negotiator.is_supported(version) is True
|
|
|
|
|
|
@then(r'version "(?P<version>[^"]+)" should not be supported')
|
|
def step_version_not_supported(context: Context, version: str) -> None:
|
|
assert context.negotiator.is_supported(version) is False
|
|
|
|
|
|
@then(r'the current version should be "(?P<version>[^"]+)"')
|
|
def step_current_version(context: Context, version: str) -> None:
|
|
assert context.negotiator.get_current() == version
|
|
|
|
|
|
# AcpRequest model validation
|
|
|
|
|
|
@when(r'I create an AcpRequest with operation "(?P<operation>[^"]+)"')
|
|
def step_create_request(context: Context, operation: str) -> None:
|
|
context.request = AcpRequest(operation=operation)
|
|
|
|
|
|
@then(r"the request should have a non-empty request_id")
|
|
def step_request_has_id(context: Context) -> None:
|
|
assert context.request.request_id, "request_id should not be empty"
|
|
|
|
|
|
@then(r'the request acp_version should be "(?P<version>[^"]+)"')
|
|
def step_request_version(context: Context, version: str) -> None:
|
|
assert context.request.acp_version == version
|
|
|
|
|
|
@when(r"I try to create an AcpRequest with empty operation")
|
|
def step_create_request_empty(context: Context) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
AcpRequest(operation="")
|
|
except ValidationError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
# Note: "a validation error should be raised" is defined in domain_models_steps.py
|
|
# Note: "the error message should contain" is defined in service_steps.py
|
|
# We reuse those shared step definitions.
|
|
|
|
|
|
# AcpResponse model validation
|
|
|
|
|
|
@when(
|
|
r'I create an AcpResponse with status "(?P<status>[^"]+)" and request_id "(?P<rid>[^"]+)"'
|
|
)
|
|
def step_create_response(context: Context, status: str, rid: str) -> None:
|
|
context.response = AcpResponse(request_id=rid, status=status)
|
|
|
|
|
|
@then(r"the response should be valid")
|
|
def step_response_valid(context: Context) -> None:
|
|
assert context.response is not None
|
|
|
|
|
|
@when(r'I try to create an AcpResponse with status "(?P<status>[^"]+)"')
|
|
def step_create_response_invalid(context: Context, status: str) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
AcpResponse(request_id="REQ", status=status)
|
|
except ValidationError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
# AcpErrorDetail model validation
|
|
|
|
|
|
@when(
|
|
r'I create an AcpErrorDetail with code "(?P<code>[^"]+)" and message "(?P<msg>[^"]+)"'
|
|
)
|
|
def step_create_error_detail(context: Context, code: str, msg: str) -> None:
|
|
context.error_detail = AcpErrorDetail(code=code, message=msg)
|
|
|
|
|
|
@then(r"the error detail should be valid")
|
|
def step_error_detail_valid(context: Context) -> None:
|
|
assert context.error_detail is not None
|
|
|
|
|
|
@when(r"I try to create an AcpErrorDetail with empty code")
|
|
def step_create_error_detail_empty(context: Context) -> None:
|
|
context.caught_error = None
|
|
context.error = None
|
|
try:
|
|
AcpErrorDetail(code="", message="test")
|
|
except ValidationError as exc:
|
|
context.caught_error = exc
|
|
context.error = exc
|
|
|
|
|
|
# AcpEvent model construction
|
|
|
|
|
|
@when(r'I create an AcpEvent with type "(?P<event_type>[^"]+)"')
|
|
def step_create_event(context: Context, event_type: str) -> None:
|
|
context.event = AcpEvent(event_type=event_type)
|
|
|
|
|
|
@then(r"the event should have a non-empty event_id")
|
|
def step_event_has_id(context: Context) -> None:
|
|
assert context.event.event_id, "event_id should not be empty"
|
|
|
|
|
|
@then(r"the event should have a non-empty timestamp")
|
|
def step_event_has_timestamp(context: Context) -> None:
|
|
assert context.event.timestamp, "timestamp should not be empty"
|
|
|
|
|
|
# Error hierarchy
|
|
|
|
|
|
@then(r"AcpError should be a subclass of CleverAgentsError")
|
|
def step_acp_error_hierarchy(context: Context) -> None:
|
|
assert issubclass(AcpError, CleverAgentsError)
|
|
|
|
|
|
@then(r"AcpNotAvailableError should be a subclass of AcpError")
|
|
def step_not_available_hierarchy(context: Context) -> None:
|
|
assert issubclass(AcpNotAvailableError, AcpError)
|
|
|
|
|
|
@then(r"AcpVersionMismatchError should be a subclass of AcpError")
|
|
def step_version_mismatch_hierarchy(context: Context) -> None:
|
|
assert issubclass(AcpVersionMismatchError, AcpError)
|
|
|
|
|
|
@then(r"AcpOperationNotFoundError should be a subclass of AcpError")
|
|
def step_op_not_found_hierarchy(context: Context) -> None:
|
|
assert issubclass(AcpOperationNotFoundError, AcpError)
|
|
|
|
|
|
@when(r"I create an AcpNotAvailableError with default message")
|
|
def step_create_not_available_default(context: Context) -> None:
|
|
context.caught_error = AcpNotAvailableError()
|
|
context.error = context.caught_error
|
|
|
|
|
|
# "the error message should contain" is provided by service_steps.py
|