my_voice_assistant_v3_jamulix/tests/test_ws.py
Dieter Schlüter 878bf785dd feat: Geräte-TTS, native Stimmen, Favicon, Auth-Gate, UI-Fixes
Web-UI / TTS:
- Geräte-TTS ("📱 Gerät"): Antwort wird on-device vorgelesen (Web Speech
  API), Server sendet nur Text (text_only) -> spart Bandbreite/Kosten.
  Mobil-Default, geräte-lokale Speicherung, iOS-Autoplay-Freischaltung.
- Vorlese-Symbol (🔊) je Bubble: Hybrid-Replay (Assistent-PCM gecacht,
  Eingabe via /api/speak); SVG-Icon mit kontrastreicher Farbe.
- Kombiniertes Sprachmenü (Flex + feste Sprachen) statt separatem Modus-Menü.
- "Neues Gespräch"-Button (frische Session gegen Sprach-Trägheit).
- Dark-Mode: lesbare <option>-Popups (Kontrast-Fix).
- Favicon (SVG + PNG-Fallbacks) aus mund.png.

TTS-Backend:
- Sprache wird an alle TTS-Provider durchgereicht; Piper-Stimme folgt der
  Sprache; Chatterbox mehrsprachig + cross-lingual.
- Native Referenz-Stimmen je Sprache (config/voices/<lang>.wav, FLEURS CC-BY),
  loudness-normalisiert.

LLM-Sprache:
- Antwort folgt zuverlässig der gewählten Sprache (verstärkte Anweisung +
  Erinnerung an der letzten Nutzer-Nachricht gegen History-Trägheit).

Admin / Auth:
- Wörterbuch: alle 8 Sprachen, Zeilen editierbar, alphabetische Sortierung.
- Web-UI hinter Auth-Gate (Redirect auf SSO_LOGIN_URL / 401); Favicons offen.
- Log-Tab: Hinweis, wenn der systemd-Dienst nicht aktiv ist.
- Einstellungen: Hinweis "pro Nutzer überschreibbar" bei Sprache/Modus/Qualität.

Doku (BEDIENUNGSANLEITUNG.md): Geräte-TTS §6.5.0, Fix/Flex §6.6, native
Stimmen §6.5.3, llama.cpp<->Ollama-Wechsel §4.7, Auth/SSO §7.4.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-20 13:12:04 +02:00

157 lines
6.1 KiB
Python

import pytest
from fastapi.testclient import TestClient
from starlette.websockets import WebSocketDisconnect
import app.dependencies as deps
from app.main import app
from app.config import settings
client = TestClient(app)
def _install_stubs(monkeypatch, captured):
class MemLLM:
async def complete(self, text, history=None, session_id=None, language=None):
captured["history"] = list(history or [])
return f"Echo: {text}"
class StubTTS:
async def synthesize(self, text, voice=None, audio_format="pcm", language=None):
return b"WSAUDIO"
monkeypatch.setitem(deps.LLM_REGISTRY, "mem", lambda s: MemLLM())
monkeypatch.setitem(deps.TTS_REGISTRY, "stub", lambda s: StubTTS())
return {"llm_provider": "mem", "tts_provider": "stub", "output_endpoint": "loopback"}
def _drain_turn(ws):
assert ws.receive_json()["type"] == "ack"
sem = ws.receive_json()
assert ws.receive_bytes() == b"WSAUDIO"
assert ws.receive_json()["type"] == "done"
return sem
def test_ws_streams_events_and_remembers(monkeypatch):
captured = {}
base = _install_stubs(monkeypatch, captured)
with client.websocket_connect("/ws/chat?session_id=wsconv") as ws:
ws.send_json({"text": "Hallo", **base})
sem1 = _drain_turn(ws)
assert sem1["type"] == "semantic" and sem1["text"] == "Echo: Hallo"
ws.send_json({"text": "Weiter", **base})
_drain_turn(ws)
# Zweiter Turn hat den Verlauf des ersten erhalten.
assert captured["history"] == [
{"role": "user", "content": "Hallo"},
{"role": "assistant", "content": "Echo: Hallo"},
]
def test_ws_unknown_provider_sends_error_event(monkeypatch):
captured = {}
_install_stubs(monkeypatch, captured)
with client.websocket_connect("/ws/chat") as ws:
ws.send_json({"text": "x", "llm_provider": "gibtsnicht"})
err = ws.receive_json()
assert err["type"] == "error" and err["status"] == 422
def test_ws_requires_token_when_auth_enabled(monkeypatch):
monkeypatch.setattr(settings, "auth_enabled", True)
monkeypatch.setattr(settings, "admin_api_key", "k")
with pytest.raises(WebSocketDisconnect):
with client.websocket_connect("/ws/chat"):
pass
def _install_voice_stubs(monkeypatch):
class StubSTT:
async def transcribe(self, audio_bytes, fmt, language=None):
return f"erkannt({len(audio_bytes)})"
class StubLLM:
async def complete(self, text, history=None, session_id=None, language=None):
return f"Antwort zu {text}"
class StubTTS:
async def synthesize(self, text, voice=None, audio_format="pcm", language=None):
return b"VOICEAUD"
monkeypatch.setitem(deps.STT_REGISTRY, "ss", lambda s: StubSTT())
monkeypatch.setitem(deps.LLM_REGISTRY, "ll", lambda s: StubLLM())
monkeypatch.setitem(deps.TTS_REGISTRY, "tt", lambda s: StubTTS())
return {
"stt_provider": "ss",
"llm_provider": "ll",
"tts_provider": "tt",
"output_endpoint": "loopback",
}
def test_ws_voice_transcribes_and_answers(monkeypatch):
opts = _install_voice_stubs(monkeypatch)
with client.websocket_connect("/ws/voice?session_id=v1") as ws:
ws.send_bytes(b"PCMDATA") # 7 Bytes
ws.send_bytes(b"MORE") # 4 Bytes -> insgesamt 11
ws.send_json({"type": "end", **opts})
transcript = ws.receive_json()
assert transcript["type"] == "transcript" and transcript["text"] == "erkannt(11)"
assert ws.receive_json()["type"] == "ack"
semantic = ws.receive_json()
assert semantic["type"] == "semantic" and semantic["text"] == "Antwort zu erkannt(11)"
assert ws.receive_bytes() == b"VOICEAUD"
assert ws.receive_json()["type"] == "done"
def test_ws_voice_start_frame_options_are_honored(monkeypatch):
# Provider stehen im START-Frame, der end-Frame ist leer -> muessen trotzdem gelten.
opts = _install_voice_stubs(monkeypatch)
with client.websocket_connect("/ws/voice") as ws:
ws.send_json({"type": "start", "format": "wav", **opts})
ws.send_bytes(b"PCMDATA") # 7 Bytes
ws.send_json({"type": "end"})
transcript = ws.receive_json()
# Stub-STT (aus dem START-Frame) liefert "erkannt(<bytes>)" -> beweist: Override greift
assert transcript["type"] == "transcript" and transcript["text"] == "erkannt(7)"
assert ws.receive_json()["type"] == "ack"
assert ws.receive_json()["type"] == "semantic"
ws.receive_bytes()
assert ws.receive_json()["type"] == "done"
def test_ws_voice_stream_text_emits_token_events(monkeypatch):
# stream:true im START-Frame -> der Server schickt token-Events (Live-Text).
opts = _install_voice_stubs(monkeypatch)
with client.websocket_connect("/ws/voice") as ws:
ws.send_json({"type": "start", "format": "wav", "stream": True, **opts})
ws.send_bytes(b"PCMDATA")
ws.send_json({"type": "end"})
assert ws.receive_json()["type"] == "transcript"
assert ws.receive_json()["type"] == "ack"
assert ws.receive_json()["type"] == "token" # Antworttext kommt als token-Event(e)
def test_ws_voice_audio_stream_default_on(monkeypatch):
# Server-Default an -> Audio wird gestreamt, auch ohne expliziten audio_stream-Flag.
monkeypatch.setattr(settings, "audio_stream_default", True)
opts = _install_voice_stubs(monkeypatch)
with client.websocket_connect("/ws/voice") as ws:
ws.send_json({"type": "start", "format": "wav", **opts}) # kein audio_stream gesetzt
ws.send_bytes(b"PCMDATA")
ws.send_json({"type": "end"})
assert ws.receive_json()["type"] == "transcript"
assert ws.receive_json()["type"] == "ack"
assert ws.receive_json()["type"] == "audio" # Audio VOR semantic -> Streaming aktiv
def test_ws_voice_empty_buffer_errors(monkeypatch):
opts = _install_voice_stubs(monkeypatch)
with client.websocket_connect("/ws/voice") as ws:
ws.send_json({"type": "end", **opts}) # kein Audio gesendet
err = ws.receive_json()
assert err["type"] == "error" and "no audio" in err["detail"]