|
| 1 | +"""Tests for the Anthropic brain adapter.""" |
| 2 | + |
| 3 | +from typing import Iterable |
| 4 | + |
| 5 | +import httpx |
| 6 | +import pytest |
| 7 | + |
| 8 | +from zhub.brains.base import ChatChunk |
| 9 | +from zhub.brains.anthropic import AnthropicAdapter |
| 10 | + |
| 11 | + |
| 12 | +class _FakeStream: |
| 13 | + def __init__(self, lines: Iterable[str]): |
| 14 | + self._lines = list(lines) |
| 15 | + |
| 16 | + async def aiter_lines(self): |
| 17 | + for line in self._lines: |
| 18 | + yield line |
| 19 | + |
| 20 | + async def __aenter__(self): |
| 21 | + return self |
| 22 | + |
| 23 | + async def __aexit__(self, *exc): |
| 24 | + return None |
| 25 | + |
| 26 | + |
| 27 | +class _FakeAsyncClient: |
| 28 | + def __init__(self, lines: Iterable[str]): |
| 29 | + self._lines = list(lines) |
| 30 | + self.last_call: dict | None = None |
| 31 | + |
| 32 | + def stream(self, method, url, **kw): |
| 33 | + self.last_call = {"method": method, "url": url, **kw} |
| 34 | + return _FakeStream(self._lines) |
| 35 | + |
| 36 | + async def aclose(self): |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +def test_try_init_none_without_key(monkeypatch): |
| 41 | + monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) |
| 42 | + assert AnthropicAdapter.try_init() is None |
| 43 | + |
| 44 | + |
| 45 | +def test_try_init_returns_adapter_when_probe_succeeds(monkeypatch): |
| 46 | + class R: |
| 47 | + status_code = 200 |
| 48 | + monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-xyz") |
| 49 | + monkeypatch.setattr(httpx, "get", |
| 50 | + lambda url, headers=None, timeout=None: R()) |
| 51 | + adapter = AnthropicAdapter.try_init() |
| 52 | + assert adapter is not None |
| 53 | + assert adapter.name == "anthropic" |
| 54 | + assert adapter.api_key == "sk-ant-xyz" |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.asyncio |
| 58 | +async def test_stream_parses_anthropic_sse(): |
| 59 | + """Anthropic Messages API streams `event:` + `data:` SSE pairs. |
| 60 | + The relevant event types are `content_block_delta` (text deltas) |
| 61 | + and `message_stop` (terminal).""" |
| 62 | + lines = [ |
| 63 | + 'event: message_start', |
| 64 | + 'data: {"type":"message_start","message":{"id":"msg_x"}}', |
| 65 | + '', |
| 66 | + 'event: content_block_delta', |
| 67 | + 'data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"Hi "}}', |
| 68 | + '', |
| 69 | + 'event: content_block_delta', |
| 70 | + 'data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"there"}}', |
| 71 | + '', |
| 72 | + 'event: message_delta', |
| 73 | + 'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}', |
| 74 | + '', |
| 75 | + 'event: message_stop', |
| 76 | + 'data: {"type":"message_stop"}', |
| 77 | + '', |
| 78 | + ] |
| 79 | + fake = _FakeAsyncClient(lines) |
| 80 | + adapter = AnthropicAdapter(api_key="sk-ant-xyz", |
| 81 | + model="claude-sonnet-4-5", |
| 82 | + http=fake) |
| 83 | + out = [c async for c in adapter.stream( |
| 84 | + [{"role": "user", "content": "hi"}], system="be brief" |
| 85 | + )] |
| 86 | + deltas = [c.delta for c in out if c.delta] |
| 87 | + assert "".join(deltas) == "Hi there" |
| 88 | + assert out[-1].done is True |
| 89 | + assert out[-1].finish_reason == "end_turn" |
| 90 | + |
| 91 | + body = fake.last_call["json"] |
| 92 | + headers = fake.last_call["headers"] |
| 93 | + assert body["model"] == "claude-sonnet-4-5" |
| 94 | + assert body["stream"] is True |
| 95 | + assert body["system"] == "be brief" |
| 96 | + assert body["messages"][-1] == {"role": "user", "content": "hi"} |
| 97 | + assert headers["x-api-key"] == "sk-ant-xyz" |
| 98 | + assert headers["anthropic-version"] |
| 99 | + |
| 100 | + |
| 101 | +def test_anthropic_in_default_registry(): |
| 102 | + from zhub.brains import REGISTRY |
| 103 | + names = [c.name for c in REGISTRY] |
| 104 | + assert "anthropic" in names |
0 commit comments