| 1 | from __future__ import annotations |
| 2 | |
| 3 | import importlib |
| 4 | import json |
| 5 | import sys |
| 6 | import types |
| 7 | from pathlib import Path |
| 8 | |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parents[1] |
| 11 | if str(ROOT) not in sys.path: |
| 12 | sys.path.insert(0, str(ROOT)) |
| 13 | |
| 14 | |
| 15 | def _load_fasta2a_server(monkeypatch): |
| 16 | settings_stub = types.ModuleType("helpers.settings") |
| 17 | settings_stub.get_settings = lambda: { |
| 18 | "a2a_server_enabled": True, |
| 19 | "mcp_server_token": "test-token", |
| 20 | } |
| 21 | monkeypatch.setitem(sys.modules, "helpers.settings", settings_stub) |
| 22 | |
| 23 | projects_stub = types.ModuleType("helpers.projects") |
| 24 | projects_stub.activate_project = lambda *args, **kwargs: None |
| 25 | monkeypatch.setitem(sys.modules, "helpers.projects", projects_stub) |
| 26 | |
| 27 | print_style_stub = types.ModuleType("helpers.print_style") |
| 28 | |
| 29 | class _PrintStyle: |
| 30 | def __init__(self, *args, **kwargs): |
| 31 | pass |
| 32 | |
| 33 | def print(self, *args, **kwargs): |
| 34 | pass |
| 35 | |
| 36 | print_style_stub.PrintStyle = _PrintStyle |
| 37 | monkeypatch.setitem(sys.modules, "helpers.print_style", print_style_stub) |
| 38 | |
| 39 | starlette_stub = types.ModuleType("starlette") |
| 40 | starlette_responses_stub = types.ModuleType("starlette.responses") |
| 41 | |
| 42 | class _Response: |
| 43 | def __init__(self, content=b"", media_type=None, *args, **kwargs): |
| 44 | self.body = content if isinstance(content, bytes) else str(content).encode() |
| 45 | self.media_type = media_type |
| 46 | |
| 47 | starlette_responses_stub.Response = _Response |
| 48 | starlette_requests_stub = types.ModuleType("starlette.requests") |
| 49 | starlette_requests_stub.Request = object |
| 50 | monkeypatch.setitem(sys.modules, "starlette", starlette_stub) |
| 51 | monkeypatch.setitem(sys.modules, "starlette.responses", starlette_responses_stub) |
| 52 | monkeypatch.setitem(sys.modules, "starlette.requests", starlette_requests_stub) |
| 53 | |
| 54 | agent_stub = types.ModuleType("agent") |
| 55 | agent_stub.AgentContext = type( |
| 56 | "AgentContext", |
| 57 | (), |
| 58 | {"remove": staticmethod(lambda *args, **kwargs: None)}, |
| 59 | ) |
| 60 | agent_stub.UserMessage = lambda **kwargs: types.SimpleNamespace(**kwargs) |
| 61 | agent_stub.AgentContextType = types.SimpleNamespace(BACKGROUND="background") |
| 62 | monkeypatch.setitem(sys.modules, "agent", agent_stub) |
| 63 | |
| 64 | initialize_stub = types.ModuleType("initialize") |
| 65 | initialize_stub.initialize_agent = lambda: {} |
| 66 | monkeypatch.setitem(sys.modules, "initialize", initialize_stub) |
| 67 | |
| 68 | persist_chat_stub = types.ModuleType("helpers.persist_chat") |
| 69 | persist_chat_stub.remove_chat = lambda *args, **kwargs: None |
| 70 | monkeypatch.setitem(sys.modules, "helpers.persist_chat", persist_chat_stub) |
| 71 | |
| 72 | sys.modules.pop("helpers.fasta2a_server", None) |
| 73 | return importlib.import_module("helpers.fasta2a_server") |
| 74 | |
| 75 | |
| 76 | def test_a2a_agent_card_streaming_capability_is_enabled_by_default(monkeypatch): |
| 77 | module = _load_fasta2a_server(monkeypatch) |
| 78 | |
| 79 | updated = module._enable_streaming_capability( |
| 80 | b'{"name":"Agent Zero","capabilities":{"streaming":false,"pushNotifications":false}}' |
| 81 | ) |
| 82 | |
| 83 | agent_card = json.loads(updated) |
| 84 | assert agent_card["capabilities"]["streaming"] is True |
| 85 | assert agent_card["capabilities"]["pushNotifications"] is False |
| 86 | |
| 87 | |
| 88 | def test_a2a_agent_card_streaming_capability_creates_missing_block(monkeypatch): |
| 89 | module = _load_fasta2a_server(monkeypatch) |
| 90 | |
| 91 | updated = module._enable_streaming_capability(b'{"name":"Agent Zero"}') |
| 92 | |
| 93 | assert json.loads(updated)["capabilities"] == {"streaming": True} |
| 94 | |
| 95 | |
| 96 | def test_a2a_proxy_uses_streaming_enabled_fast_a2a_wrapper(monkeypatch): |
| 97 | module = _load_fasta2a_server(monkeypatch) |
| 98 | proxy = object.__new__(module.DynamicA2AProxy) |
| 99 | |
| 100 | proxy._configure() |
| 101 | |
| 102 | assert isinstance(proxy.app, module.AgentZeroFastA2A) |