Files
temp/features/steps/a2a_facade_coverage_boost_steps.py
freemo f5d244cd37 fix(a2a): change A2aErrorDetail.code to int and map error constants to JSON-RPC 2.0 integer codes
Per JSON-RPC 2.0 specification (Section 5.1), error codes must be integers.
This commit fixes the protocol compliance defect where A2aErrorDetail.code
was typed as str and error constants were string literals.

Changes:
- src/cleveragents/a2a/models.py: Change A2aErrorDetail.code from str to int;
  update field_validator to only validate 'message' (code no longer needs
  non-empty string check; Pydantic enforces int type)
- src/cleveragents/a2a/errors.py: Change all error code constants from string
  literals to JSON-RPC 2.0 integer codes per docs/reference/a2a.md taxonomy:
    NOT_FOUND = -32001, AUTH_ERROR = -32002, FORBIDDEN = -32003,
    INVALID_STATE = -32004, PLAN_ERROR = -32008, CONFIGURATION_ERROR = -32009,
    VALIDATION_ERROR = -32602, INTERNAL_ERROR = -32603
  Update map_domain_error() return type from tuple[str, str] to tuple[int, str]
- features/steps/a2a_facade_steps.py: Update A2aErrorDetail construction to
  map symbolic string names to integer codes via _CODE_MAP
- features/steps/a2a_facade_wiring_steps.py: Update error code assertion to
  map symbolic names to integers for comparison
- features/steps/a2a_facade_coverage_boost_steps.py: Same as above
- features/steps/a2a_jsonrpc_wire_format_steps.py: Update all A2aErrorDetail
  constructions and JSON-RPC dict payloads to use integer codes
- robot/helper_a2a_facade_wiring.py: Update wired_error_mapping() to compare
  against integer codes
- robot/helper_a2a_jsonrpc_wire_format.py: Update response_error_wire_format()
  to use integer code -32001 instead of string 'NOT_FOUND'

Wire format now produces {"code": -32001, ...} instead of {"code": "NOT_FOUND", ...},
making it compliant with JSON-RPC 2.0 and interoperable with standards-conformant clients.

ISSUES CLOSED: #2746
2026-04-05 17:53:56 +00:00

142 lines
5.0 KiB
Python

"""Step definitions for A2A facade coverage-boost scenarios.
Targets uncovered lines in ``src/cleveragents/a2a/facade.py``:
| Line | Code path |
|------|--------------------------------------------------------|
| 82 | TypeError when ``services`` is not a dict or None |
| 256 | ValueError in plan.execute when plan_id is empty |
| 266 | ValueError in plan.status when plan_id is empty |
| 281 | ValueError in plan.diff when plan_id is empty |
| 295 | ValueError in plan.apply when plan_id is empty |
"""
from __future__ import annotations
import json
from typing import Any
from unittest.mock import MagicMock
from behave import given, then, use_step_matcher, when
from behave.runner import Context
try:
from cleveragents.a2a.facade import A2aLocalFacade
from cleveragents.a2a.models import A2aRequest
except ImportError:
pass # a2a module not available
from features.steps._a2a_code_map import A2A_CODE_MAP
use_step_matcher("re")
# ---------------------------------------------------------------------------
# Mock helpers (mirrors wiring steps — kept local to avoid coupling)
# ---------------------------------------------------------------------------
class _MockPlanIdentity:
def __init__(self, plan_id: str = "MOCK-PLAN-001") -> None:
self.plan_id = plan_id
class _MockPlan:
def __init__(self, plan_id: str = "MOCK-PLAN-001") -> None:
self.identity = _MockPlanIdentity(plan_id)
self.phase = MagicMock()
self.phase.value = "strategize"
self.state = MagicMock()
self.state.value = "queued"
def _build_mock_plan_lifecycle_service() -> MagicMock:
svc = MagicMock()
svc.use_action.return_value = _MockPlan()
svc.execute_plan.return_value = _MockPlan()
svc.get_plan.return_value = _MockPlan()
svc.apply_plan.return_value = _MockPlan()
return svc
# ---------------------------------------------------------------------------
# Given steps
# ---------------------------------------------------------------------------
@given(r"a coverage-boost facade with a mock PlanLifecycleService")
def step_cb_facade_plan(context: Context) -> None:
context.cb_facade = A2aLocalFacade(
services={"plan_lifecycle_service": _build_mock_plan_lifecycle_service()}
)
# ---------------------------------------------------------------------------
# When steps
# ---------------------------------------------------------------------------
@when(r"I try to create an A2aLocalFacade with a non-dict services argument")
def step_cb_create_facade_non_dict(context: Context) -> None:
context.cb_caught_error = None
try:
A2aLocalFacade(services=["not", "a", "dict"]) # type: ignore[arg-type]
except TypeError as exc:
context.cb_caught_error = exc
@when(
r'I dispatch coverage-boost operation "(?P<operation>[^"]+)" '
r"with params (?P<params_json>.+)"
)
def step_cb_dispatch(context: Context, operation: str, params_json: str) -> None:
params: dict[str, Any] = json.loads(params_json)
request = A2aRequest(method=operation, params=params)
context.cb_response = context.cb_facade.dispatch(request)
# ---------------------------------------------------------------------------
# Then steps
# ---------------------------------------------------------------------------
@then(r'a TypeError should be raised with message "(?P<msg>[^"]+)"')
def step_cb_type_error_raised(context: Context, msg: str) -> None:
assert isinstance(context.cb_caught_error, TypeError), (
f"Expected TypeError, got {type(context.cb_caught_error)}"
)
assert msg in str(context.cb_caught_error), (
f"Expected message containing '{msg}', got '{context.cb_caught_error}'"
)
@then(r'the coverage-boost response status should be "(?P<status>[^"]+)"')
def step_cb_response_status(context: Context, status: str) -> None:
assert (context.cb_response.error is None) == (status == "ok"), (
f"Expected status '{status}', got error={context.cb_response.error}"
)
@then(r'the coverage-boost response error code should be "(?P<code>[^"]+)"')
def step_cb_error_code(context: Context, code: str) -> None:
expected_code: int = A2A_CODE_MAP.get(
code, int(code) if code.lstrip("-").isdigit() else -1
)
assert context.cb_response.error is not None, "No error in response"
assert context.cb_response.error.code == expected_code, (
f"Expected error code {expected_code} ('{code}'), "
f"got '{context.cb_response.error.code}'"
)
@then(r'the coverage-boost response error message should contain "(?P<fragment>[^"]+)"')
def step_cb_error_message_contains(context: Context, fragment: str) -> None:
assert context.cb_response.error is not None, "No error in response"
assert fragment in context.cb_response.error.message, (
f"Expected message containing '{fragment}', "
f"got '{context.cb_response.error.message}'"
)
# Reset step matcher to parse (default) so subsequent step files are not affected
use_step_matcher("parse")