astra-vision/tests/test_tools.py
Jeuner e7760688f0 feat: read German news from curated RSS feeds
Neues Werkzeug read_news, gleiche Trennung wie ComfyUI:

- astra/feeds.py: kuratierte deutsche Feed-Liste nach Thema (tech,
  nachrichten, wirtschaft) — 11 Feeds, alle live gegen die echten
  Endpunkte verifiziert (heise, Golem, t3n, netzpolitik.org,
  tagesschau.de, Zeit Online, Spiegel, SZ, Handelsblatt, WiWo,
  manager magazin).
- astra/rss.py: async Feed-Client (feedparser), holt Feeds eines
  Themas parallel ab, überspringt einzelne nicht erreichbare Feeds
  statt komplett zu scheitern.
- astra/tools.py: read_news als natives Ollama-Tool, gleiches
  tool_start/tool_result/tool_error-UI-Muster wie generate_image.

Mit zwei Tools gleichzeitig verfügbar sank die Zuverlässigkeit des
lokalen 9.7B-Modells spürbar (0/8 Tool-Aufrufe mit dem ursprünglichen,
langen System-Prompt) — reproduzierbar isoliert über direkte
Ollama-Requests. Ursache: Instruction-Dilution bei mehreren
Tool-Regeln in einem langen Prompt. Behoben durch gestrafften,
Tool-Regeln-zuerst-Prompt und Temperatur 0.6→0.4: Bildgenerierung
bleibt ~100% zuverlässig, Nachrichten-Tool bei ~60–75% je nach
Formulierung. Per echtem Browser-Test mit synthetisierter Sprache
verifiziert: echte aktuelle Schlagzeilen erscheinen im Transkript.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LVgSHNHdRx3UNTBodFmhRA
2026-09-07 17:55:24 +02:00

126 lines
4 KiB
Python

import pytest
import astra.tools as tools_module
from astra.comfyui import ComfyUIError
from astra.core import Settings
from astra.rss import Entry, RSSError
class FakeFunctionCallParams:
"""Stand-in for pipecat's FunctionCallParams: only what the handler touches."""
def __init__(self, arguments):
self.arguments = arguments
self.results = []
async def result_callback(self, result, *, properties=None):
self.results.append(result)
@pytest.mark.asyncio
async def test_generate_image_tool_stores_media_and_notifies(monkeypatch):
async def fake_generate_image(endpoint, prompt, **kwargs):
assert endpoint == "http://fake-comfyui"
assert prompt == "a cat"
return b"png-bytes"
monkeypatch.setattr(tools_module, "generate_image", fake_generate_image)
notifications = []
media_store: dict[str, bytes] = {}
schema = tools_module.build_tools(
Settings(comfyui_url="http://fake-comfyui"), notifications.append, media_store
).standard_tools[0]
params = FakeFunctionCallParams({"prompt": "a cat"})
await schema.handler(params)
assert len(media_store) == 1
image_id, image_bytes = next(iter(media_store.items()))
assert image_bytes == b"png-bytes"
assert [n["type"] for n in notifications] == ["activity", "tool_start", "image"]
assert "a cat" in notifications[1]["text"]
assert notifications[2] == {"type": "image", "url": f"/api/media/{image_id}"}
assert params.results[0]["status"] == "ok"
@pytest.mark.asyncio
async def test_generate_image_tool_reports_comfyui_errors_without_storing_media(monkeypatch):
async def failing_generate_image(endpoint, prompt, **kwargs):
raise ComfyUIError("ComfyUI ist nicht erreichbar.")
monkeypatch.setattr(tools_module, "generate_image", failing_generate_image)
notifications = []
media_store: dict[str, bytes] = {}
schema = tools_module.build_tools(
Settings(comfyui_url="http://fake-comfyui"), notifications.append, media_store
).standard_tools[0]
params = FakeFunctionCallParams({"prompt": "a cat"})
await schema.handler(params)
assert media_store == {}
assert "error" in params.results[0]
assert [n["type"] for n in notifications] == [
"activity",
"tool_start",
"activity",
"tool_error",
]
def _find(tools, name):
return next(t for t in tools if t.name == name)
@pytest.mark.asyncio
async def test_read_news_tool_reports_headlines(monkeypatch):
async def fake_read_topic(topic, **kwargs):
assert topic == "tech"
return [
Entry(source="heise online", title="KI-Durchbruch", summary="...", link="https://x"),
Entry(source="Golem.de", title="Neuer Chip", summary="...", link="https://y"),
]
monkeypatch.setattr(tools_module, "read_topic", fake_read_topic)
notifications = []
schema = _find(
tools_module.build_tools(Settings(), notifications.append, {}).standard_tools,
"read_news",
)
params = FakeFunctionCallParams({"topic": "tech"})
await schema.handler(params)
assert [n["type"] for n in notifications] == ["activity", "tool_start", "tool_result"]
assert "KI-Durchbruch" in notifications[2]["text"]
assert "Neuer Chip" in notifications[2]["text"]
assert params.results[0]["status"] == "ok"
assert len(params.results[0]["headlines"]) == 2
@pytest.mark.asyncio
async def test_read_news_tool_reports_rss_errors(monkeypatch):
async def failing_read_topic(topic, **kwargs):
raise RSSError('Keine Feeds für "tech" waren erreichbar.')
monkeypatch.setattr(tools_module, "read_topic", failing_read_topic)
notifications = []
schema = _find(
tools_module.build_tools(Settings(), notifications.append, {}).standard_tools,
"read_news",
)
params = FakeFunctionCallParams({"topic": "tech"})
await schema.handler(params)
assert [n["type"] for n in notifications] == [
"activity",
"tool_start",
"activity",
"tool_error",
]
assert "error" in params.results[0]