Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: use real audio plugin runtimes (#43577)

Asuka Minatoc07938b0007b
diff --git a/api/tests/unit_tests/audio_runtime_fixtures.py b/api/tests/unit_tests/audio_runtime_fixtures.py
new file mode 100644
index 00000000000..705f6507e26
--- /dev/null
+++ b/api/tests/unit_tests/audio_runtime_fixtures.py
@@ -0,0 +1,174 @@
+"""Audio tests use production model assembly; only provider lookup and daemon I/O are isolated."""
+
+from collections.abc import Callable, Iterable, Iterator
+from dataclasses import dataclass, field
+
+import pytest
+from redis import Redis
+
+from core.entities.model_entities import DefaultModelEntity, DefaultModelProviderEntity
+from core.entities.provider_configuration import ProviderConfiguration, ProviderModelBundle
+from core.entities.provider_entities import CustomConfiguration, CustomProviderConfiguration, SystemConfiguration
+from core.model_manager import ModelManager
+from core.plugin.entities.plugin_daemon import (
+ PluginStringResultResponse,
+ PluginTTSResultResponse,
+ PluginVoiceEntity,
+ PluginVoicesResponse,
+ TTSAudioChunk,
+)
+from core.plugin.impl.model import PluginModelClient
+from core.plugin.impl.model_runtime import PluginModelRuntime
+from core.plugin.impl.model_runtime_factory import create_model_type_instance
+from core.provider_manager import ProviderManager
+from extensions.ext_redis import RedisClientWrapper
+from graphon.model_runtime.entities.common_entities import I18nObject
+from graphon.model_runtime.entities.model_entities import AIModelEntity, FetchFrom, ModelPropertyKey, ModelType
+from graphon.model_runtime.entities.provider_entities import ProviderEntity
+from graphon.model_runtime.protocols.tts_runtime import TTSModelVoice
+from models.provider import ProviderType
+
+PROVIDER = "langgenius/openai/openai"
+
+
+def record_calls[**P, R](function: Callable[P, R], calls: list[dict[str, object]]) -> Callable[P, R]:
+ """Observe keyword arguments without replacing production behavior."""
+
+ def delegated(*args: P.args, **kwargs: P.kwargs) -> R:
+ calls.append(dict(kwargs))
+ return function(*args, **kwargs)
+
+ return delegated
+
+
+@dataclass
+class AudioRuntimeObservations:
+ """Transport response data and observations, never installed as a runtime dependency."""
+
+ speech_to_text_result: str = "Transcribed text"
+ tts_result: bytes | Iterable[bytes] = b"audio data"
+ tts_responses: list[Iterable[bytes]] = field(default_factory=list)
+ voices: list[TTSModelVoice] = field(default_factory=list)
+ # None means that the corresponding boundary succeeds.
+ voices_error: Exception | None = None
+ lookup_error: Exception | None = None
+ # False represents an absent default-model database record.
+ default_available: bool = True
+ model_properties: dict[ModelPropertyKey, object] = field(
+ default_factory=lambda: {ModelPropertyKey.AUDIO_TYPE: "mp3"}
+ )
+ speech_to_text_calls: list[bytes] = field(default_factory=list)
+ tts_calls: list[dict[str, str]] = field(default_factory=list)
+ voice_calls: list[tuple[str | None]] = field(default_factory=list)
+ manager_requests: list[dict[str, object]] = field(default_factory=list)
+ daemon_calls: list[dict[str, object]] = field(default_factory=list)
+ runtime_calls: list[dict[str, object]] = field(default_factory=list)
+ resolutions: list[tuple[str, ModelType]] = field(default_factory=list)
+
+
+def audio_runtime(monkeypatch: pytest.MonkeyPatch) -> Iterator[AudioRuntimeObservations]:
+ observed = AudioRuntimeObservations()
+ label = I18nObject(en_US="Audio provider")
+ provider_schema = ProviderEntity(
+ provider=PROVIDER,
+ label=label,
+ supported_model_types=[ModelType.TTS, ModelType.SPEECH2TEXT],
+ configurate_methods=[],
+ )
+
+ def default_model(_manager: ProviderManager, *, tenant_id: str, model_type: ModelType) -> DefaultModelEntity | None:
+ observed.resolutions.append((tenant_id, model_type))
+ if observed.lookup_error is not None:
+ raise observed.lookup_error
+ if not observed.default_available:
+ return None
+ return DefaultModelEntity(
+ model="audio-model",
+ model_type=model_type,
+ provider=DefaultModelProviderEntity(provider=PROVIDER, label=label),
+ )
+
+ def bundle(
+ manager: ProviderManager, *, tenant_id: str, provider: str, model_type: ModelType
+ ) -> ProviderModelBundle:
+ assert provider == PROVIDER
+ assert isinstance(manager._model_runtime, PluginModelRuntime)
+ assert manager._model_runtime.tenant_id == tenant_id
+ return ProviderModelBundle(
+ configuration=ProviderConfiguration(
+ tenant_id=tenant_id,
+ provider=provider_schema,
+ preferred_provider_type=ProviderType.CUSTOM,
+ using_provider_type=ProviderType.CUSTOM,
+ system_configuration=SystemConfiguration(enabled=False),
+ custom_configuration=CustomConfiguration(
+ provider=CustomProviderConfiguration(credentials={"api_key": "audio-token"})
+ ),
+ model_settings=[],
+ ),
+ model_type_instance=create_model_type_instance(
+ runtime=manager._model_runtime, provider_schema=provider_schema, model_type=model_type
+ ),
+ )
+
+ def schema(_client: PluginModelClient, **kwargs: object) -> AIModelEntity:
+ return AIModelEntity(
+ model="audio-model",
+ label=label,
+ model_type=ModelType(kwargs["model_type"]),
+ fetch_from=FetchFrom.PREDEFINED_MODEL,
+ model_properties=observed.model_properties,
+ )
+
+ def dispatch(
+ _client: PluginModelClient, **kwargs: object
+ ) -> Iterator[PluginStringResultResponse | PluginVoicesResponse]:
+ observed.daemon_calls.append(kwargs)
+ data = kwargs["data"]
+ assert isinstance(data, dict)
+ payload = data["data"]
+ assert payload["credentials"] == {"api_key": "audio-token"}
+ assert payload["provider"] == "openai"
+ assert payload["model"] == "audio-model"
+ path = kwargs["path"]
+ assert isinstance(path, str)
+ if path.endswith("/voices"):
+ observed.voice_calls.append((payload["language"],))
+ if observed.voices_error is not None:
+ raise observed.voices_error
+ yield PluginVoicesResponse(
+ voices=[PluginVoiceEntity(name=v.get("name", v["value"]), value=v["value"]) for v in observed.voices]
+ )
+ elif path.endswith("/speech2text/invoke"):
+ observed.speech_to_text_calls.append(bytes.fromhex(payload["file"]))
+ yield PluginStringResultResponse(result=observed.speech_to_text_result)
+ else:
+ assert path.endswith("/tts/invoke")
+ observed.tts_calls.append({"content_text": payload["content_text"], "voice": payload["voice"]})
+ response = observed.tts_responses.pop(0) if observed.tts_responses else observed.tts_result
+ chunks = [response] if isinstance(response, bytes) else response
+ for chunk in chunks:
+ yield PluginTTSResultResponse(
+ result=chunk.hex(), mime_type=chunk.mime_type if isinstance(chunk, TTSAudioChunk) else None
+ )
+
+ monkeypatch.setattr(ProviderManager, "get_default_model", default_model)
+ monkeypatch.setattr(ProviderManager, "get_provider_model_bundle", bundle)
+ monkeypatch.setattr(PluginModelClient, "get_model_schema", schema)
+ monkeypatch.setattr(PluginModelClient, "_request_with_plugin_daemon_response_stream", dispatch)
+ monkeypatch.setattr(ModelManager, "for_tenant", record_calls(ModelManager.for_tenant, observed.manager_requests))
+ monkeypatch.setattr(
+ PluginModelRuntime, "invoke_tts", record_calls(PluginModelRuntime.invoke_tts, observed.runtime_calls)
+ )
+ monkeypatch.setattr(
+ PluginModelRuntime,
+ "invoke_speech_to_text",
+ record_calls(PluginModelRuntime.invoke_speech_to_text, observed.runtime_calls),
+ )
+ with Redis() as client:
+ monkeypatch.setattr(client, "execute_command", lambda *_args, **_kwargs: None)
+ redis = RedisClientWrapper()
+ redis.initialize(client)
+ monkeypatch.setattr("core.plugin.impl.model_runtime.redis_client", redis)
+ yield observed
diff --git a/api/tests/unit_tests/conftest.py b/api/tests/unit_tests/conftest.py
index 9edfb9e779d..c1e7e9287b2 100644
--- a/api/tests/unit_tests/conftest.py
+++ b/api/tests/unit_tests/conftest.py
@@ -27,6 +27,8 @@ PROJECT_DIR = os.path.abspath(os.path.join(ABS_PATH, os.pardir, os.pardir))
CACHED_APP = Flask(__name__)
+pytest_plugins = ("tests.unit_tests.audio_runtime_fixtures",)
+
# set global mock for Redis client
redis_mock = MagicMock()
redis_mock.get = MagicMock(return_value=None)
diff --git a/api/tests/unit_tests/controllers/console/app/test_agent_audio_readonly.py b/api/tests/unit_tests/controllers/console/app/test_agent_audio_readonly.py
index ac2d8c808fb..439f4309e23 100644
--- a/api/tests/unit_tests/controllers/console/app/test_agent_audio_readonly.py
+++ b/api/tests/unit_tests/controllers/console/app/test_agent_audio_readonly.py
@@ -1,9 +1,8 @@
from collections.abc import Callable, Iterator
-from unittest.mock import Mock
from uuid import UUID
import pytest
-from flask import Flask, Response
+from flask import Flask
from sqlalchemy import Engine, event
from sqlalchemy.orm import Session
from werkzeug.exceptions import InternalServerError
@@ -21,7 +20,7 @@ from graphon.model_runtime.errors.invoke import InvokeError
from models.agent import Agent, AgentKind, AgentScope, AgentSource, AgentStatus
from models.model import App, AppMode
from services.agent.errors import AgentNotFoundError
-from services.audio_service import AudioService
+from tests.unit_tests.audio_runtime_fixtures import AudioRuntimeObservations
from tests.unit_tests.model_factories import make_account, make_app
AGENT_ID = UUID("019ef3d2-b24c-7803-b428-18b5ee8fb853")
@@ -65,19 +64,17 @@ def persistence_events(sqlite_engine: Engine) -> Iterator[list[str]]:
@pytest.fixture(params=["voices", "preview"])
def request_audio(
- request: pytest.FixtureRequest, app: Flask, monkeypatch: pytest.MonkeyPatch
-) -> tuple[Callable[[], object], Mock]:
+ request: pytest.FixtureRequest, app: Flask, audio_runtime: AudioRuntimeObservations
+) -> tuple[Callable[[], object], AudioRuntimeObservations]:
if request.param == "voices":
- provider = Mock(return_value=[{"name": "Echo", "value": "echo"}])
- monkeypatch.setattr(AudioService, "transcript_tts_voices", provider)
+ audio_runtime.voices = [{"name": "Echo", "value": "echo"}]
def invoke() -> object:
with app.test_request_context(f"/console/api/agent/{AGENT_ID}/text-to-audio/voices?language=en-US"):
return audio.AgentTextToSpeechVoicesApi().get(agent_id=AGENT_ID)
else:
- provider = Mock(return_value=Response(b"ID3audio", content_type="audio/mpeg"))
- monkeypatch.setattr(AudioService, "transcript_tts", provider)
+ audio_runtime.tts_result = b"ID3audio"
def invoke() -> object:
with app.test_request_context(
@@ -85,9 +82,16 @@ def request_audio(
method="POST",
json={"text": "Preview this voice", "voice": "echo"},
):
- return audio.AgentChatMessageTextApi().post(agent_id=AGENT_ID)
+ response = audio.AgentChatMessageTextApi().post(agent_id=AGENT_ID)
+ assert response is not None
+ try:
+ assert response.get_data() == b"ID3audio"
+ assert response.content_type == "audio/mpeg"
+ return response
+ finally:
+ response.close()
- return invoke, provider
+ return invoke, audio_runtime
def _agent(scope: AgentScope = AgentScope.WORKFLOW_ONLY) -> Agent:
@@ -112,7 +116,7 @@ def _agent(scope: AgentScope = AgentScope.WORKFLOW_ONLY) -> Agent:
@pytest.mark.parametrize("scope", [AgentScope.ROSTER, AgentScope.WORKFLOW_ONLY])
def test_audio_uses_existing_runtime_state_without_writes(
sqlite_session: Session,
- request_audio: tuple[Callable[[], object], Mock],
+ request_audio: tuple[Callable[[], object], AudioRuntimeObservations],
persistence_events: list[str],
scope: AgentScope,
) -> None:
@@ -125,14 +129,14 @@ def test_audio_uses_existing_runtime_state_without_writes(
response = invoke()
- provider.assert_called_once()
+ assert len(provider.resolutions) == 1
assert response is not None
assert persistence_events == []
def test_audio_does_not_materialize_a_missing_workflow_backing_app(
sqlite_session: Session,
- request_audio: tuple[Callable[[], object], Mock],
+ request_audio: tuple[Callable[[], object], AudioRuntimeObservations],
persistence_events: list[str],
) -> None:
agent = _agent()
@@ -146,7 +150,8 @@ def test_audio_does_not_materialize_a_missing_workflow_backing_app(
with pytest.raises(AgentNotFoundError):
invoke()
- provider.assert_not_called()
+ assert provider.resolutions == []
+ assert provider.daemon_calls == []
assert persistence_events == []
sqlite_session.refresh(agent)
assert agent.backing_app_id is None
@@ -156,7 +161,7 @@ def test_audio_does_not_materialize_a_missing_workflow_backing_app(
@pytest.mark.parametrize("foreign_owner", ["agent", "app"])
def test_audio_scopes_both_agent_and_runtime_app_to_the_current_tenant(
sqlite_session: Session,
- request_audio: tuple[Callable[[], object], Mock],
+ request_audio: tuple[Callable[[], object], AudioRuntimeObservations],
persistence_events: list[str],
foreign_owner: str,
) -> None:
@@ -174,7 +179,8 @@ def test_audio_scopes_both_agent_and_runtime_app_to_the_current_tenant(
with pytest.raises(AgentNotFoundError):
invoke()
- provider.assert_not_called()
+ assert provider.resolutions == []
+ assert provider.daemon_calls == []
assert persistence_events == []
@@ -191,7 +197,7 @@ def test_audio_scopes_both_agent_and_runtime_app_to_the_current_tenant(
)
def test_provider_failures_preserve_error_contract_and_readonly_state(
sqlite_session: Session,
- request_audio: tuple[Callable[[], object], Mock],
+ request_audio: tuple[Callable[[], object], AudioRuntimeObservations],
persistence_events: list[str],
provider_error: Exception,
expected_error: type[Exception],
@@ -200,10 +206,10 @@ def test_provider_failures_preserve_error_contract_and_readonly_state(
sqlite_session.commit()
persistence_events.clear()
invoke, provider = request_audio
- provider.side_effect = provider_error
+ provider.lookup_error = provider_error
with pytest.raises(expected_error):
invoke()
- provider.assert_called_once()
+ assert len(provider.resolutions) == 1
assert persistence_events == []
diff --git a/api/tests/unit_tests/controllers/web/test_audio.py b/api/tests/unit_tests/controllers/web/test_audio.py
index c6a36803f19..79c7ecc8589 100644
--- a/api/tests/unit_tests/controllers/web/test_audio.py
+++ b/api/tests/unit_tests/controllers/web/test_audio.py
@@ -1,13 +1,12 @@
-"""Unit tests for controllers.web.audio endpoints."""
-
-from __future__ import annotations
+"""Exercise web audio handlers through AudioService, SQLite and real plugin model runtimes."""
import inspect
+import json
+from decimal import Decimal
from io import BytesIO
-from unittest.mock import MagicMock, patch
import pytest
-from flask import Flask
+from flask import Flask, Response
from sqlalchemy.orm import Session
from controllers.web.audio import AudioApi, TextApi, TextToAudioPayload
@@ -24,178 +23,226 @@ from controllers.web.error import (
)
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
from graphon.model_runtime.errors.invoke import InvokeError
-from models.model import App, AppMode, EndUser, IconType
+from models.enums import ConversationFromSource
+from models.model import App, AppMode, AppModelConfig, EndUser
from services.app_ref_service import AppRef, MessageRef
-from services.errors.audio import (
- AudioTooLargeServiceError,
- NoAudioUploadedServiceError,
- ProviderNotSupportSpeechToTextServiceError,
- SpeechToTextDisabledServiceError,
- UnsupportedAudioTypeServiceError,
-)
-from tests.unit_tests.model_factories import make_end_user
-
-
-def _app_model() -> App:
- return App(
- id="app-1",
- tenant_id="tenant-1",
- name="Web App",
- description="",
- mode=AppMode.CHAT,
- icon_type=IconType.EMOJI,
- icon="robot",
- icon_background="#FFFFFF",
- enable_site=True,
- enable_api=False,
- max_active_requests=0,
+from services.audio_service import FILE_SIZE_LIMIT, AudioService
+from services.errors.audio import ProviderNotSupportSpeechToTextServiceError
+from tests.unit_tests.audio_runtime_fixtures import AudioRuntimeObservations, record_calls
+from tests.unit_tests.model_factories import make_app, make_end_user, make_message
+
+
+def web_app(sqlite_session: Session) -> App:
+ config = AppModelConfig(
+ app_id="app-1",
+ speech_to_text=json.dumps({"enabled": True}),
+ text_to_speech=json.dumps({"enabled": True, "voice": "alloy"}),
)
+ sqlite_session.add(config)
+ sqlite_session.flush()
+ model = make_app(app_id="app-1", tenant_id="tenant-1", mode=AppMode.CHAT)
+ model.app_model_config_id = config.id
+ sqlite_session.add(model)
+ sqlite_session.commit()
+ return model
+
+
+def service_calls(monkeypatch: pytest.MonkeyPatch) -> list[dict[str, object]]:
+ calls: list[dict[str, object]] = []
+ monkeypatch.setattr(AudioService, "transcript_asr", record_calls(AudioService.transcript_asr, calls))
+ monkeypatch.setattr(AudioService, "transcript_tts", record_calls(AudioService.transcript_tts, calls))
+ return calls
def _end_user() -> EndUser:
return make_end_user(end_user_id="eu-1", app_id="app-1", external_user_id="ext-1", name="Web User")
-# The @with_session decorator opens a request-scoped session and injects it as
-# the first argument after self; these undecorated handlers let tests pass a
-# session explicitly and assert it is forwarded to AudioService.
+# Unwrap only for the injected-session tests; normal tests execute the request-session decorator.
_audio_post = inspect.unwrap(AudioApi.post)
_text_post = inspect.unwrap(TextApi.post)
-# ---------------------------------------------------------------------------
-# AudioApi (audio-to-text)
-# ---------------------------------------------------------------------------
class TestAudioApi:
- @patch("controllers.web.audio.AudioService.transcript_asr", return_value={"text": "hello"})
- def test_happy_path(self, mock_asr: MagicMock, app: Flask) -> None:
+ @pytest.mark.parametrize("injected_session", [False, True])
+ def test_happy_path_and_session_forwarding(
+ self,
+ app: Flask,
+ sqlite_session: Session,
+ web_app: App,
+ audio_runtime: AudioRuntimeObservations,
+ service_calls: list[dict[str, object]],
+ injected_session: bool,
+ ) -> None:
+ audio_runtime.speech_to_text_result = "hello"
app.config["RESTX_MASK_HEADER"] = "X-Fields"
- data = {"file": (BytesIO(b"fake-audio"), "test.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- result = AudioApi().post(_app_model(), _end_user())
-
- assert result == {"text": "hello"}
- assert isinstance(mock_asr.call_args.kwargs["session"], Session)
-
- @patch("controllers.web.audio.AudioService.transcript_asr", return_value={"text": "hello"})
- def test_forwards_injected_session(self, mock_asr: MagicMock, app: Flask, sqlite_session: Session) -> None:
- data = {"file": (BytesIO(b"fake-audio"), "test.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- result = _audio_post(AudioApi(), sqlite_session, _app_model(), _end_user())
+ data = {"file": (BytesIO(b"uploaded audio"), "test.mp3", "audio/mp3")}
+ with app.test_request_context("/audio-to-text", method="POST", data=data):
+ if injected_session:
+ result = _audio_post(AudioApi(), sqlite_session, web_app, _end_user())
+ else:
+ result = AudioApi().post(web_app, _end_user())
assert result == {"text": "hello"}
- assert mock_asr.call_args.kwargs["session"] is sqlite_session
-
- @patch("controllers.web.audio.AudioService.transcript_asr", side_effect=NoAudioUploadedServiceError())
- def test_no_audio_uploaded(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b""), "empty.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(NoAudioUploadedError):
- AudioApi().post(_app_model(), _end_user())
-
- @patch("controllers.web.audio.AudioService.transcript_asr", side_effect=AudioTooLargeServiceError("too big"))
- def test_audio_too_large(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b"big"), "big.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(AudioTooLargeError):
- AudioApi().post(_app_model(), _end_user())
-
- @patch("controllers.web.audio.AudioService.transcript_asr", side_effect=UnsupportedAudioTypeServiceError())
- def test_unsupported_type(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b"bad"), "bad.xyz")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(UnsupportedAudioTypeError):
- AudioApi().post(_app_model(), _end_user())
-
- @patch(
- "controllers.web.audio.AudioService.transcript_asr",
- side_effect=ProviderNotSupportSpeechToTextServiceError(),
- )
- def test_provider_not_support(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b"x"), "x.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(ProviderNotSupportSpeechToTextError):
- AudioApi().post(_app_model(), _end_user())
-
- @patch(
- "controllers.web.audio.AudioService.transcript_asr",
- side_effect=SpeechToTextDisabledServiceError(),
+ assert len(service_calls) == 1
+ assert isinstance(service_calls[0]["session"], Session)
+ if injected_session:
+ assert service_calls[0]["session"] is sqlite_session
+ assert audio_runtime.speech_to_text_calls == [b"uploaded audio"]
+ assert audio_runtime.manager_requests[0]["user_id"] == "ext-1"
+
+ @pytest.mark.parametrize(
+ ("condition", "expected_error"),
+ [
+ ("missing", NoAudioUploadedError),
+ ("large", AudioTooLargeError),
+ ("unsupported", UnsupportedAudioTypeError),
+ ("disabled", SpeechToTextDisabledError),
+ ],
)
- def test_speech_to_text_disabled(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b"x"), "x.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(SpeechToTextDisabledError):
- AudioApi().post(_app_model(), _end_user())
-
- @patch(
- "controllers.web.audio.AudioService.transcript_asr",
- side_effect=ProviderTokenNotInitError(description="no token"),
+ def test_invalid_upload_or_disabled_feature(
+ self,
+ condition: str,
+ expected_error: type[Exception],
+ app: Flask,
+ web_app: App,
+ sqlite_session: Session,
+ audio_runtime: AudioRuntimeObservations,
+ ) -> None:
+ if condition == "disabled":
+ config = web_app.app_model_config_with_session(session=sqlite_session)
+ assert config is not None
+ config.speech_to_text = json.dumps({"enabled": False})
+ sqlite_session.commit()
+ content = b"x" * (FILE_SIZE_LIMIT + 1) if condition == "large" else b"audio"
+ mime = "video/mp4" if condition == "unsupported" else "audio/mp3"
+ data: dict[str, object] = {} if condition == "missing" else {"file": (BytesIO(content), "test.mp3", mime)}
+ with app.test_request_context("/audio-to-text", method="POST", data=data):
+ with pytest.raises(expected_error):
+ AudioApi().post(web_app, _end_user())
+ assert audio_runtime.resolutions == []
+ assert audio_runtime.daemon_calls == []
+
+ @pytest.mark.parametrize(
+ ("provider_error", "expected_error"),
+ [
+ (ProviderNotSupportSpeechToTextServiceError(), ProviderNotSupportSpeechToTextError),
+ (ProviderTokenNotInitError(description="no token"), ProviderNotInitializeError),
+ (QuotaExceededError(), ProviderQuotaExceededError),
+ (ModelCurrentlyNotSupportError(), ProviderModelCurrentlyNotSupportError),
+ ],
)
- def test_provider_not_init(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b"x"), "x.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(ProviderNotInitializeError):
- AudioApi().post(_app_model(), _end_user())
-
- @patch("controllers.web.audio.AudioService.transcript_asr", side_effect=QuotaExceededError())
- def test_quota_exceeded(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b"x"), "x.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(ProviderQuotaExceededError):
- AudioApi().post(_app_model(), _end_user())
-
- @patch("controllers.web.audio.AudioService.transcript_asr", side_effect=ModelCurrentlyNotSupportError())
- def test_model_not_support(self, mock_asr: MagicMock, app: Flask) -> None:
- data = {"file": (BytesIO(b"x"), "x.mp3")}
- with app.test_request_context("/audio-to-text", method="POST", data=data, content_type="multipart/form-data"):
- with pytest.raises(ProviderModelCurrentlyNotSupportError):
- AudioApi().post(_app_model(), _end_user())
-
-
-# ---------------------------------------------------------------------------
-# TextApi (text-to-audio)
-# ---------------------------------------------------------------------------
-class TestTextApi:
- @patch("controllers.web.audio.AudioService.transcript_tts", return_value="audio-bytes")
- def test_happy_path(self, mock_tts: MagicMock, app: Flask) -> None:
- with app.test_request_context("/text-to-audio", method="POST", json={"text": "hello", "voice": "alloy"}):
- result = TextApi().post(_app_model(), _end_user())
-
- assert result == "audio-bytes"
- mock_tts.assert_called_once()
- assert isinstance(mock_tts.call_args.kwargs["session"], Session)
+ def test_provider_failure_mapping(
+ self,
+ provider_error: Exception,
+ expected_error: type[Exception],
+ app: Flask,
+ web_app: App,
+ audio_runtime: AudioRuntimeObservations,
+ ) -> None:
+ audio_runtime.lookup_error = provider_error
+ with app.test_request_context(
+ "/audio-to-text", method="POST", data={"file": (BytesIO(b"audio"), "x.mp3", "audio/mp3")}
+ ):
+ with pytest.raises(expected_error):
+ AudioApi().post(web_app, _end_user())
+ assert len(audio_runtime.resolutions) == 1
+ assert audio_runtime.daemon_calls == []
- @patch("controllers.web.audio.AudioService.transcript_tts", return_value="audio-bytes")
- def test_forwards_injected_session(self, mock_tts: MagicMock, app: Flask, sqlite_session: Session) -> None:
- payload = TextToAudioPayload.model_validate({"text": "hello", "voice": "alloy"})
- with app.test_request_context("/text-to-audio", method="POST"):
- result = _text_post(TextApi(), payload, sqlite_session, _app_model(), _end_user())
+ def test_missing_default_model(self, app: Flask, web_app: App, audio_runtime: AudioRuntimeObservations) -> None:
+ audio_runtime.default_available = False
+ with app.test_request_context(
+ "/audio-to-text", method="POST", data={"file": (BytesIO(b"audio"), "x.mp3", "audio/mp3")}
+ ):
+ with pytest.raises(ProviderNotInitializeError):
+ AudioApi().post(web_app, _end_user())
- assert result == "audio-bytes"
- assert mock_tts.call_args.kwargs["session"] is sqlite_session
- @patch("controllers.web.audio.AudioService.transcript_tts", return_value="audio-bytes")
- def test_happy_path_with_message_ref(self, mock_tts: MagicMock, app: Flask) -> None:
+class TestTextApi:
+ @pytest.mark.parametrize("injected_session", [False, True])
+ def test_happy_path_and_session_forwarding(
+ self,
+ app: Flask,
+ sqlite_session: Session,
+ web_app: App,
+ audio_runtime: AudioRuntimeObservations,
+ service_calls: list[dict[str, object]],
+ injected_session: bool,
+ ) -> None:
+ audio_runtime.tts_result = b"audio-bytes"
+ with app.test_request_context("/text-to-audio", method="POST", json={"text": "hello", "voice": "alloy"}):
+ if injected_session:
+ payload = TextToAudioPayload.model_validate({"text": "hello", "voice": "alloy"})
+ result = _text_post(TextApi(), payload, sqlite_session, web_app, _end_user())
+ else:
+ result = TextApi().post(web_app, _end_user())
+ assert isinstance(result, Response)
+ try:
+ assert result.get_data() == b"audio-bytes"
+ assert result.content_type == "audio/mpeg"
+ finally:
+ result.close()
+
+ assert len(service_calls) == 1
+ assert isinstance(service_calls[0]["session"], Session)
+ if injected_session:
+ assert service_calls[0]["session"] is sqlite_session
+ assert audio_runtime.tts_calls == [{"content_text": "hello", "voice": "alloy"}]
+ assert audio_runtime.manager_requests[0]["user_id"] == "ext-1"
+
+ @pytest.mark.parametrize("owner", ["current", "other-app", "other-user"])
+ def test_owned_message_ref(
+ self,
+ owner: str,
+ app: Flask,
+ sqlite_session: Session,
+ web_app: App,
+ audio_runtime: AudioRuntimeObservations,
+ service_calls: list[dict[str, object]],
+ ) -> None:
message_id = "550e8400-e29b-41d4-a716-446655440000"
- app_model = _app_model()
-
+ sqlite_session.add(
+ make_message(
+ message_id=message_id,
+ app_id="other-app" if owner == "other-app" else web_app.id,
+ from_end_user_id="other-user" if owner == "other-user" else "eu-1",
+ answer="persisted answer",
+ inputs={},
+ query="Question",
+ message={"role": "user", "content": "Question"},
+ message_unit_price=Decimal(0),
+ answer_unit_price=Decimal(0),
+ currency="USD",
+ from_source=ConversationFromSource.API,
+ )
+ )
+ sqlite_session.commit()
with app.test_request_context(
- "/text-to-audio", method="POST", json={"text": "hello", "message_id": message_id}
+ "/text-to-audio", method="POST", json={"text": "ignored input", "message_id": message_id}
):
- result = TextApi().post(app_model, _end_user())
-
- assert result == "audio-bytes"
- assert mock_tts.call_args.kwargs["message_ref"] == MessageRef(
- AppRef("tenant-1", "app-1"),
- message_id,
- end_user_id="eu-1",
+ result = TextApi().post(web_app, _end_user())
+ if owner == "current":
+ assert isinstance(result, Response)
+ try:
+ assert result.get_data() == b"audio data"
+ finally:
+ result.close()
+ else:
+ assert result is None
+
+ assert service_calls[0]["message_ref"] == MessageRef(
+ AppRef("tenant-1", "app-1"), message_id, end_user_id="eu-1"
+ )
+ assert audio_runtime.tts_calls == (
+ [{"content_text": "persisted answer", "voice": "alloy"}] if owner == "current" else []
)
+ if owner != "current":
+ assert audio_runtime.resolutions == []
- @patch(
- "controllers.web.audio.AudioService.transcript_tts",
- side_effect=InvokeError(description="invoke failed"),
- )
- def test_invoke_error_mapped(self, mock_tts: MagicMock, app: Flask) -> None:
+ def test_invoke_error_mapped(self, app: Flask, web_app: App, audio_runtime: AudioRuntimeObservations) -> None:
+ audio_runtime.lookup_error = InvokeError(description="invoke failed")
with app.test_request_context("/text-to-audio", method="POST", json={"text": "hello"}):
with pytest.raises(CompletionRequestError):
- TextApi().post(_app_model(), _end_user())
+ TextApi().post(web_app, _end_user())
diff --git a/api/tests/unit_tests/core/base/test_app_generator_tts_publisher.py b/api/tests/unit_tests/core/base/test_app_generator_tts_publisher.py
index bdf6bffdb14..fc6418b843a 100644
--- a/api/tests/unit_tests/core/base/test_app_generator_tts_publisher.py
+++ b/api/tests/unit_tests/core/base/test_app_generator_tts_publisher.py
@@ -1,39 +1,35 @@
import base64
-from types import SimpleNamespace
-from unittest.mock import MagicMock
+from collections.abc import Iterator
import pytest
from pytest_mock import MockerFixture
-from core.app.entities.queue_entities import QueueNodeSucceededEvent, QueueTextChunkEvent, WorkflowQueueMessage
+from core.app.entities.queue_entities import (
+ MessageQueueMessage,
+ QueueNodeSucceededEvent,
+ QueueTextChunkEvent,
+ WorkflowQueueMessage,
+)
from core.base.tts.app_generator_tts_publisher import AppGeneratorTTSPublisher, AudioTrunk
+from core.credit_usage import CreditUsageAppType, CreditUsageCreatedBy
+from core.model_manager import ModelInstance, ModelManager
from core.plugin.entities.plugin_daemon import TTSAudioChunk
from graphon.enums import BuiltinNodeTypes
from graphon.model_runtime.entities.model_entities import ModelPropertyKey
from graphon.model_runtime.errors.invoke import InvokeBadRequestError
from libs.datetime_utils import naive_utc_now
from models.model import AppMode
+from tests.unit_tests.audio_runtime_fixtures import AudioRuntimeObservations
-def mock_model_instance(mocker: MockerFixture):
- model = mocker.MagicMock()
- model.invoke_tts.return_value = [b"audio1", b"audio2"]
- model.get_tts_voices.return_value = [{"value": "voice1"}, {"value": "voice2"}]
- model.get_model_schema.return_value = SimpleNamespace(model_properties={ModelPropertyKey.AUDIO_TYPE: "mp3"})
- return model
-
-
-def mock_model_manager(mocker: MockerFixture, mock_model_instance: MagicMock):
- manager = mocker.MagicMock()
- manager.get_default_model_instance.return_value = mock_model_instance
- mocker.patch("core.base.tts.app_generator_tts_publisher.ModelManager.for_tenant", return_value=manager)
- return manager
[email protected](autouse=True)
+def audio_responses(audio_runtime: AudioRuntimeObservations) -> None:
+ audio_runtime.tts_result = [b"audio1", b"audio2"]
+ audio_runtime.voices = [{"name": "One", "value": "voice1"}, {"name": "Two", "value": "voice2"}]
@pytest.fixture(autouse=True)
-def patch_threads(mocker: MockerFixture):
+def patch_threads(mocker: MockerFixture) -> None:
"""Run the worker explicitly in tests."""
mocker.patch("threading.Thread.start", return_value=None)
@@ -56,7 +52,7 @@ def _node_event(outputs: dict[str, object] | None) -> WorkflowQueueMessage:
return WorkflowQueueMessage(task_id="task", app_mode=AppMode.WORKFLOW, event=event)
-def _run(publisher: AppGeneratorTTSPublisher, *messages: WorkflowQueueMessage | MagicMock) -> None:
+def _run(publisher: AppGeneratorTTSPublisher, *messages: WorkflowQueueMessage) -> None:
for message in messages:
publisher._msg_queue.put(message)
publisher._msg_queue.put(None)
@@ -64,7 +60,7 @@ def _run(publisher: AppGeneratorTTSPublisher, *messages: WorkflowQueueMessage |
class TestAudioTrunk:
- def test_initialization(self):
+ def test_initialization(self) -> None:
error = RuntimeError("failed")
trunk = AudioTrunk("error", b"", error=error)
@@ -74,42 +70,45 @@ class TestAudioTrunk:
class TestAppGeneratorTTSPublisher:
- def test_initialization_valid_voice(self, mock_model_manager):
+ def test_initialization_valid_voice(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
+ assert type(publisher.model_manager) is ModelManager
+ assert type(publisher.model_instance) is ModelInstance
+ assert publisher.model_instance.credentials == {"api_key": "audio-token"}
assert publisher.voice == "voice1"
assert publisher.max_sentence == 2
assert publisher.msg_text == ""
- def test_initialization_invalid_voice_fallback(self, mock_model_manager):
+ def test_initialization_invalid_voice_fallback(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "invalid_voice")
assert publisher.voice == "voice1"
- def test_publish_puts_message_in_queue(self, mock_model_manager):
+ def test_publish_puts_message_in_queue(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
- message = MagicMock()
+ message = _text_event("queued text")
publisher.publish(message)
assert publisher._msg_queue.get() == message
- def test_cancel_discards_queued_text(self, mock_model_manager, mock_model_instance: MagicMock):
+ def test_cancel_discards_queued_text(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
publisher.publish(_text_event("First. Second."))
publisher.cancel()
publisher._runtime()
- mock_model_instance.invoke_tts.assert_not_called()
+ assert audio_runtime.tts_calls == []
assert publisher._audio_queue.empty()
- def test_check_and_get_audio_returns_none_without_audio(self, mock_model_manager):
+ def test_check_and_get_audio_returns_none_without_audio(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
assert publisher.check_and_get_audio() is None
- def test_check_and_get_audio_returns_the_resolved_mime_type(self, mock_model_manager):
+ def test_check_and_get_audio_returns_the_resolved_mime_type(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
trunk = AudioTrunk("responding", b"abc", audio_type="audio/wav")
publisher._audio_queue.put(trunk)
@@ -120,7 +119,9 @@ class TestAppGeneratorTTSPublisher:
assert result.audio_type == "audio/wav"
@pytest.mark.parametrize("status", ["finish", "error"])
- def test_check_and_get_audio_caches_terminal_events(self, mock_model_manager, status: str):
+ def test_check_and_get_audio_caches_terminal_events(
+ self, audio_runtime: AudioRuntimeObservations, status: str
+ ) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
terminal = AudioTrunk(status, b"", error=RuntimeError("failed") if status == "error" else None)
publisher._audio_queue.put(terminal)
@@ -137,7 +138,9 @@ class TestAppGeneratorTTSPublisher:
("", [], ""),
],
)
- def test_extract_sentence(self, mock_model_manager, text, expected_sentences, expected_remaining):
+ def test_extract_sentence(
+ self, audio_runtime: AudioRuntimeObservations, text: str, expected_sentences: list[str], expected_remaining: str
+ ) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
sentences, remaining = publisher._extract_sentence(text)
@@ -145,79 +148,82 @@ class TestAppGeneratorTTSPublisher:
assert sentences == expected_sentences
assert remaining == expected_remaining
- def test_runtime_generates_the_final_buffer(self, mock_model_manager, mock_model_instance: MagicMock):
+ def test_runtime_generates_the_final_buffer(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
publisher.msg_text = " Hello. "
_run(publisher)
- mock_model_instance.invoke_tts.assert_called_once_with(content_text="Hello.", voice="voice1")
+ assert audio_runtime.tts_calls == [{"content_text": "Hello.", "voice": "voice1"}]
+ assert len(audio_runtime.runtime_calls) == 1
+ assert audio_runtime.runtime_calls[0]["request_metadata"] == {
+ "app_type": CreditUsageAppType.UNKNOWN,
+ "created_by": CreditUsageCreatedBy.AUDIO,
+ }
+ assert audio_runtime.manager_requests[0]["user_id"] == "responding_tts"
assert publisher._audio_queue.get().audio == base64.b64encode(b"audio1")
assert publisher._audio_queue.get().audio == base64.b64encode(b"audio2")
assert publisher._audio_queue.get().status == "finish"
- def test_runtime_skips_an_empty_final_buffer(self, mock_model_manager, mock_model_instance: MagicMock):
+ def test_runtime_skips_an_empty_final_buffer(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
publisher.msg_text = " "
_run(publisher)
- mock_model_instance.invoke_tts.assert_not_called()
+ assert audio_runtime.tts_calls == []
assert publisher._audio_queue.get().status == "finish"
def test_runtime_generates_incremental_mp3_after_the_sentence_threshold(
- self, mock_model_manager, mock_model_instance: MagicMock
- ):
+ self, audio_runtime: AudioRuntimeObservations
+ ) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
_run(publisher, _text_event("Hello world. Second sentence."))
- mock_model_instance.invoke_tts.assert_called_once_with(
- content_text="Hello world. Second sentence.", voice="voice1"
- )
+ assert audio_runtime.tts_calls == [{"content_text": "Hello world. Second sentence.", "voice": "voice1"}]
def test_runtime_waits_for_terminal_when_the_schema_has_no_audio_type(
- self, mock_model_manager, mock_model_instance: MagicMock
- ):
- mock_model_instance.get_model_schema.return_value = SimpleNamespace(model_properties={})
+ self, audio_runtime: AudioRuntimeObservations, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ audio_runtime.model_properties = {}
wav = b"RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00audio-data"
- mock_model_instance.invoke_tts.return_value = [TTSAudioChunk(wav, "audio/wav")]
+ audio_runtime.tts_result = [TTSAudioChunk(wav, "audio/wav")]
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
event = _text_event("Hello. World.")
- messages = iter([event, None])
+ publisher._msg_queue.put(event)
+ publisher._msg_queue.put(None)
+ original_get = publisher._msg_queue.get
- def get_message():
- message = next(messages)
+ def get_message() -> WorkflowQueueMessage | MessageQueueMessage | None:
+ message = original_get()
if message is None:
- mock_model_instance.invoke_tts.assert_not_called()
+ assert audio_runtime.tts_calls == []
return message
- publisher._msg_queue = MagicMock()
- publisher._msg_queue.get.side_effect = get_message
+ monkeypatch.setattr(publisher._msg_queue, "get", get_message)
publisher._runtime()
- mock_model_instance.invoke_tts.assert_called_once_with(content_text="Hello. World.", voice="voice1")
+ assert audio_runtime.tts_calls == [{"content_text": "Hello. World.", "voice": "voice1"}]
assert publisher._audio_queue.get().audio_type == "audio/wav"
assert publisher._audio_queue.get().status == "finish"
def test_runtime_waits_for_terminal_when_the_model_declares_wav(
- self, mock_model_manager, mock_model_instance: MagicMock
- ):
- mock_model_instance.get_model_schema.return_value = SimpleNamespace(
- model_properties={ModelPropertyKey.AUDIO_TYPE: "wav"}
- )
+ self, audio_runtime: AudioRuntimeObservations
+ ) -> None:
+ audio_runtime.model_properties = {ModelPropertyKey.AUDIO_TYPE: "wav"}
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
_run(publisher, _text_event("Hello. World."))
- mock_model_instance.invoke_tts.assert_called_once_with(content_text="Hello. World.", voice="voice1")
+ assert audio_runtime.tts_calls == [{"content_text": "Hello. World.", "voice": "voice1"}]
def test_runtime_rejects_a_non_mp3_incremental_response_before_emitting_audio(
- self, mock_model_manager, mock_model_instance: MagicMock
- ):
+ self, audio_runtime: AudioRuntimeObservations
+ ) -> None:
wav = b"RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00audio-data"
- mock_model_instance.invoke_tts.return_value = [TTSAudioChunk(wav, "audio/wav")]
+ audio_runtime.tts_result = [TTSAudioChunk(wav, "audio/wav")]
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
_run(publisher, _text_event("First. Second. tail"))
@@ -228,11 +234,11 @@ class TestAppGeneratorTTSPublisher:
assert publisher._audio_queue.empty()
def test_runtime_rejects_mime_changes_between_audio_responses(
- self, mock_model_manager, mock_model_instance: MagicMock
- ):
+ self, audio_runtime: AudioRuntimeObservations
+ ) -> None:
mp3 = b"\xff\xfb" + b"\x00" * 30
wav = b"RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00audio-data"
- mock_model_instance.invoke_tts.side_effect = [
+ audio_runtime.tts_responses = [
[TTSAudioChunk(mp3, "audio/mpeg")],
[TTSAudioChunk(wav, "audio/wav")],
]
@@ -249,13 +255,13 @@ class TestAppGeneratorTTSPublisher:
assert publisher._audio_queue.empty()
def test_runtime_turns_a_lazy_provider_failure_into_an_error_terminal(
- self, mock_model_manager, mock_model_instance: MagicMock
- ):
- def failing_stream():
+ self, audio_runtime: AudioRuntimeObservations
+ ) -> None:
+ def failing_stream() -> Iterator[bytes]:
raise RuntimeError("provider failed")
yield b"" # pragma: no cover
- mock_model_instance.invoke_tts.return_value = failing_stream()
+ audio_runtime.tts_result = failing_stream()
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
publisher.msg_text = "Hello"
@@ -266,26 +272,31 @@ class TestAppGeneratorTTSPublisher:
assert isinstance(terminal.error, RuntimeError)
assert publisher._audio_queue.empty()
- def test_runtime_handles_node_succeeded_output(self, mock_model_manager, mock_model_instance: MagicMock):
+ def test_runtime_handles_node_succeeded_output(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
event = _node_event({"output": "Hello world."})
_run(publisher, event)
- mock_model_instance.invoke_tts.assert_called_once()
+ assert len(audio_runtime.tts_calls) == 1
- def test_runtime_ignores_node_succeeded_without_output(self, mock_model_manager, mock_model_instance: MagicMock):
+ def test_runtime_ignores_node_succeeded_without_output(self, audio_runtime: AudioRuntimeObservations) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
event = _node_event(None)
_run(publisher, event)
- mock_model_instance.invoke_tts.assert_not_called()
+ assert audio_runtime.tts_calls == []
- def test_runtime_turns_message_processing_failure_into_an_error_terminal(self, mock_model_manager):
+ def test_runtime_turns_message_processing_failure_into_an_error_terminal(
+ self, audio_runtime: AudioRuntimeObservations, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
publisher = AppGeneratorTTSPublisher("tenant", "voice1")
- publisher._msg_queue = MagicMock()
- publisher._msg_queue.get.side_effect = RuntimeError("failed")
+
+ def unavailable() -> None:
+ raise RuntimeError("failed")
+
+ monkeypatch.setattr(publisher._msg_queue, "get", unavailable)
publisher._runtime()
diff --git a/api/tests/unit_tests/core/base/tts/test_audio_mime.py b/api/tests/unit_tests/core/base/tts/test_audio_mime.py
index f233a11b272..071be49ab76 100644
--- a/api/tests/unit_tests/core/base/tts/test_audio_mime.py
+++ b/api/tests/unit_tests/core/base/tts/test_audio_mime.py
@@ -1,6 +1,5 @@
from collections.abc import Generator, Iterator
-from types import SimpleNamespace
-from typing import cast, override
+from typing import override
import pytest
@@ -10,10 +9,11 @@ from core.base.tts.audio_mime import (
resolve_audio_mime_type,
sniff_audio_mime_type,
)
-from core.model_manager import ModelInstance
+from core.model_manager import ModelManager
from core.plugin.entities.plugin_daemon import TTSAudioChunk
-from graphon.model_runtime.entities.model_entities import ModelPropertyKey
+from graphon.model_runtime.entities.model_entities import ModelPropertyKey, ModelType
from graphon.model_runtime.errors.invoke import InvokeBadRequestError
+from tests.unit_tests.audio_runtime_fixtures import AudioRuntimeObservations
def test_inspect_audio_stream_preserves_the_prefix_with_matching_chunk_mime_type() -> None:
@@ -41,12 +41,13 @@ def test_resolve_audio_mime_type_falls_back_to_the_declared_model_type() -> None
assert resolve_audio_mime_type(b"unrecognised", "audio/ogg") == "audio/ogg"
-def test_model_audio_mime_type_normalizes_plugin_metadata() -> None:
- model_instance = SimpleNamespace(
- get_model_schema=lambda: SimpleNamespace(model_properties={ModelPropertyKey.AUDIO_TYPE: "audio/x-wav"})
+def test_model_audio_mime_type_normalizes_plugin_metadata(audio_runtime: AudioRuntimeObservations) -> None:
+ audio_runtime.model_properties = {ModelPropertyKey.AUDIO_TYPE: "audio/x-wav"}
+ model_instance = ModelManager.for_tenant(tenant_id="tenant-1").get_default_model_instance(
+ tenant_id="tenant-1", model_type=ModelType.TTS
)
- assert get_model_audio_mime_type(cast(ModelInstance, model_instance)) == "audio/wav"
+ assert get_model_audio_mime_type(model_instance) == "audio/wav"
@pytest.mark.parametrize(
diff --git a/api/tests/unit_tests/core/plugin/impl/test_model_client.py b/api/tests/unit_tests/core/plugin/impl/test_model_client.py
index e0dee3266ad..3de7a2f061a 100644
--- a/api/tests/unit_tests/core/plugin/impl/test_model_client.py
+++ b/api/tests/unit_tests/core/plugin/impl/test_model_client.py
@@ -7,7 +7,7 @@ from types import SimpleNamespace
import pytest
from pytest_mock import MockerFixture
-from core.plugin.entities.plugin_daemon import PluginDaemonInnerError
+from core.plugin.entities.plugin_daemon import PluginDaemonInnerError, PluginStringResultResponse
from core.plugin.impl.exc import PluginInvokeError, PluginLLMPollingUnsupportedError
from core.plugin.impl.model import PluginModelClient
from graphon.model_runtime.entities.llm_entities import LLMPollingResult, LLMPollingStatus, LLMResult, LLMUsage
@@ -587,7 +587,7 @@ class TestPluginModelClient:
stream_mock = mocker.patch.object(
client,
"_request_with_plugin_daemon_response_stream",
- return_value=iter([SimpleNamespace(result="transcribed text")]),
+ return_value=iter([PluginStringResultResponse(result="transcribed text")]),
)
result = client.invoke_speech_to_text(
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 328f893c1ac..17bdca356be 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -255,7 +255,6 @@ project-excludes = [
"core/app/workflow/test_node_factory.py",
"core/app/workflow/test_observability_layer_extra.py",
"core/app/workflow/test_persistence_layer.py",
- "core/base/test_app_generator_tts_publisher.py",
"core/callback_handler/test_agent_tool_callback_handler.py",
"core/callback_handler/test_index_tool_callback_handler.py",
"core/callback_handler/test_workflow_tool_callback_handler.py",
@@ -599,7 +598,6 @@ project-excludes = [
"services/test_app_service.py",
"services/test_app_task_service.py",
"services/test_async_workflow_service.py",
- "services/test_audio_service.py",
"services/test_billing_service.py",
"services/test_clear_free_plan_expired_workflow_run_logs.py",
"services/test_clear_free_plan_tenant_expired_logs.py",
diff --git a/api/tests/unit_tests/services/test_audio_service.py b/api/tests/unit_tests/services/test_audio_service.py
index 730ea49ece8..f4f46bdb213 100644
--- a/api/tests/unit_tests/services/test_audio_service.py
+++ b/api/tests/unit_tests/services/test_audio_service.py
@@ -32,10 +32,10 @@ Tests available voice retrieval:
## Testing Approach
-- **Isolation Strategy**: ModelManager uses a recording implementation; uploads use real FileStorage byte streams,
- while database paths use isolated in-memory SQLite sessions
+- **Isolation Strategy**: Real ModelManager, model wrappers and plugin clients process audio; only provider
+ discovery and daemon transport are isolated. Uploads use FileStorage and persistence uses SQLite.
- **Factory Pattern**: AudioServiceTestDataFactory provides consistent test data
-- **Fixtures**: Recording provider objects are configured per test method
+- **Fixtures**: Daemon response data and observations are configured per test method
- **Assertions**: Each test verifies return values, side effects, and error conditions
## Key Concepts
@@ -54,11 +54,9 @@ Tests available voice retrieval:
"""
import json
-from collections.abc import Generator
-from dataclasses import dataclass, field
+from collections.abc import Generator, Iterator
from decimal import Decimal
from io import BytesIO
-from typing import Any
from uuid import uuid4
import pytest
@@ -67,11 +65,12 @@ from sqlalchemy.orm import Session
from werkzeug.datastructures import FileStorage
from core.credit_usage import CreditUsageAppType, CreditUsageCreatedBy
-from core.model_manager import ModelInstance, ModelManager
+from core.errors.error import ProviderTokenNotInitError
+from core.model_manager import ModelManager
from core.plugin.entities.plugin_daemon import TTSAudioChunk
from extensions.ext_database import db
-from graphon.model_runtime.entities.model_entities import ModelType
from graphon.model_runtime.errors.invoke import InvokeBadRequestError
+from graphon.model_runtime.protocols.tts_runtime import TTSModelVoice
from models.agent import Agent, AgentConfigSnapshot, AgentScope, AgentSource
from models.agent_config_entities import AgentSoulConfig
from models.enums import ConversationFromSource, MessageStatus
@@ -87,6 +86,7 @@ from services.errors.audio import (
SpeechToTextDisabledServiceError,
UnsupportedAudioTypeServiceError,
)
+from tests.unit_tests.audio_runtime_fixtures import AudioRuntimeObservations
from tests.unit_tests.model_factories import make_message
APP_ID = "11111111-1111-1111-1111-111111111111"
@@ -98,69 +98,11 @@ ACCOUNT_ID = "66666666-6666-6666-6666-666666666666"
OTHER_ID = "77777777-7777-7777-7777-777777777777"
-@dataclass
-class RecordingAudioModel(ModelInstance):
- """Concrete audio model with configurable results and recorded invocations."""
-
- speech_to_text_result: str = "Transcribed text"
- tts_result: bytes | Generator[bytes | TTSAudioChunk] = b"audio data"
- voices: list[dict[str, str]] = field(default_factory=list)
- voices_error: Exception | None = None
- speech_to_text_calls: list[bytes] = field(default_factory=list)
- tts_calls: list[dict[str, str]] = field(default_factory=list)
- voice_calls: list[tuple[str, ...]] = field(default_factory=list)
-
- def invoke_speech2text(self, *, file: BytesIO) -> str:
- self.speech_to_text_calls.append(file.read())
- return self.speech_to_text_result
-
- def invoke_tts(self, *, content_text: str, voice: str) -> bytes | Generator[bytes | TTSAudioChunk]:
- self.tts_calls.append({"content_text": content_text, "voice": voice})
- return self.tts_result
-
- def get_tts_voices(self, *args: str) -> list[dict[str, str]]:
- self.voice_calls.append(args)
- if self.voices_error is not None:
- raise self.voices_error
- return self.voices
-
-
-@dataclass
-class RecordingModelManager(ModelManager):
- """Concrete manager that returns the configured audio model."""
-
- model: RecordingAudioModel | None
- model_requests: list[tuple[str, ModelType]] = field(default_factory=list)
-
- def get_default_model_instance(self, tenant_id: str, model_type: ModelType) -> RecordingAudioModel | None:
- self.model_requests.append((tenant_id, model_type))
- return self.model
-
-
-@dataclass
-class AudioProviderHarness:
- """Provider assembly exposed to tests without dynamic stand-ins."""
-
- model: RecordingAudioModel
- manager: RecordingModelManager
- manager_requests: list[dict[str, Any]] = field(default_factory=list)
-
-
@pytest.fixture
-def audio_provider(monkeypatch: pytest.MonkeyPatch) -> AudioProviderHarness:
- model = RecordingAudioModel()
- harness = AudioProviderHarness(model=model, manager=RecordingModelManager(model=model))
-
- def for_tenant(
- *, tenant_id: str, user_id: str | None = None, request_metadata: dict[str, object] | None = None
- ) -> RecordingModelManager:
- harness.manager_requests.append(
- {"tenant_id": tenant_id, "user_id": user_id, "request_metadata": request_metadata}
- )
- return harness.manager
-
- monkeypatch.setattr(ModelManager, "for_tenant", staticmethod(for_tenant))
- return harness
+def audio_provider(audio_runtime: AudioRuntimeObservations, app: Flask) -> Iterator[AudioRuntimeObservations]:
+ # Real plugin TTS returns a stream, so the compatibility response needs a request context.
+ with app.test_request_context("/text-to-audio", method="POST"):
+ yield audio_runtime
def _message(*, answer: str = "Message answer") -> Message:
@@ -184,10 +126,7 @@ def _message(*, answer: str = "Message answer") -> Message:
class AudioServiceTestDataFactory:
"""
- Factory for creating test data and mock objects.
-
- Provides reusable methods to create consistent mock objects for testing
- audio-related operations.
+ Factory for persisted application configuration and real upload streams.
"""
def __init__(self, session: Session) -> None:
@@ -356,7 +295,7 @@ class TestAudioServiceASR:
self.session = sqlite_session
def test_transcript_asr_success_chat_mode(
- self, audio_provider: AudioProviderHarness, factory: AudioServiceTestDataFactory
+ self, audio_provider: AudioRuntimeObservations, factory: AudioServiceTestDataFactory
) -> None:
"""Test successful ASR transcription in CHAT mode."""
# Arrange
@@ -367,14 +306,18 @@ class TestAudioServiceASR:
)
file = factory.create_file_storage()
- audio_provider.model.speech_to_text_result = "Transcribed text"
+ audio_provider.speech_to_text_result = "Transcribed text"
# Act
result = AudioService.transcript_asr(app_model=app, file=file, session=self.session, end_user="user-123")
# Assert
assert result == {"text": "Transcribed text"}
- assert audio_provider.model.speech_to_text_calls == [b"fake audio content"]
+ assert audio_provider.speech_to_text_calls == [b"fake audio content"]
+ assert audio_provider.runtime_calls[0]["request_metadata"] == {
+ "app_type": CreditUsageAppType.CHATBOT,
+ "created_by": CreditUsageCreatedBy.AUDIO,
+ }
assert audio_provider.manager_requests == [
{
"tenant_id": app.tenant_id,
@@ -387,24 +330,24 @@ class TestAudioServiceASR:
]
def test_transcript_asr_accepts_x_m4a_mimetype(
- self, audio_provider: AudioProviderHarness, factory: AudioServiceTestDataFactory
+ self, audio_provider: AudioRuntimeObservations, factory: AudioServiceTestDataFactory
) -> None:
"""Test that the x-m4a MIME alias follows the normal m4a transcription flow."""
# Arrange
app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
app = factory.create_app_mock(mode=AppMode.CHAT, app_model_config=app_model_config)
file = factory.create_file_storage(filename="audio.m4a", mimetype="audio/x-m4a")
- audio_provider.model.speech_to_text_result = "M4A transcript"
+ audio_provider.speech_to_text_result = "M4A transcript"
# Act
result = AudioService.transcript_asr(app_model=app, file=file, session=self.session)
# Assert
assert result == {"text": "M4A transcript"}
- assert audio_provider.model.speech_to_text_calls == [b"fake audio content"]
+ assert audio_provider.speech_to_text_calls == [b"fake audio content"]
def test_transcript_asr_success_advanced_chat_mode(
- self, audio_provider: AudioProviderHarness, factory: AudioServiceTestDataFactory
+ self, audio_provider: AudioRuntimeObservations, factory: AudioServiceTestDataFactory
) -> None:
"""Test successful ASR transcription in ADVANCED_CHAT mode."""
# Arrange
@@ -415,7 +358,7 @@ class TestAudioServiceASR:
)
file = factory.create_file_storage()
- audio_provider.model.speech_to_text_result = "Workflow transcribed text"
+ audio_provider.speech_to_text_result = "Workflow transcribed text"
# Act
result = AudioService.transcript_asr(app_model=app, file=file, session=self.session)
@@ -425,7 +368,7 @@ class TestAudioServiceASR:
def test_transcript_asr_success_published_agent_mode(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
) -> None:
app = factory.create_app_mock(mode=AppMode.AGENT)
@@ -447,7 +390,7 @@ class TestAudioServiceASR:
self.session.flush()
agent.active_config_snapshot_id = snapshot.id
self.session.commit()
- audio_provider.model.speech_to_text_result = "Published Agent transcript"
+ audio_provider.speech_to_text_result = "Published Agent transcript"
result = AudioService.transcript_asr(app_model=app, file=file, session=self.session, end_user="end-user-1")
@@ -465,25 +408,25 @@ class TestAudioServiceASR:
def test_transcript_asr_legacy_agent_falls_back_to_app_model_config(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
) -> None:
app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)
file = factory.create_file_storage()
- audio_provider.model.speech_to_text_result = "Legacy Agent transcript"
+ audio_provider.speech_to_text_result = "Legacy Agent transcript"
result = AudioService.transcript_asr(app_model=app, file=file, session=self.session)
assert result == {"text": "Legacy Agent transcript"}
def test_transcript_agent_asr_uses_agent_soul_feature(
- self, audio_provider: AudioProviderHarness, factory: AudioServiceTestDataFactory
+ self, audio_provider: AudioRuntimeObservations, factory: AudioServiceTestDataFactory
) -> None:
app = factory.create_app_mock(mode=AppMode.AGENT)
file = factory.create_file_storage()
agent_soul = AgentSoulConfig.model_validate({"app_features": {"speech_to_text": {"enabled": True}}})
- audio_provider.model.speech_to_text_result = "Agent transcript"
+ audio_provider.speech_to_text_result = "Agent transcript"
result = AudioService.transcript_agent_asr(
app_model=app,
@@ -522,12 +465,12 @@ class TestAudioServiceASR:
AudioService.transcript_agent_asr(app_model=app, agent_soul=agent_soul, file=file, session=self.session)
def test_transcript_agent_asr_preserves_legacy_feature_fallback(
- self, audio_provider: AudioProviderHarness, factory: AudioServiceTestDataFactory
+ self, audio_provider: AudioRuntimeObservations, factory: AudioServiceTestDataFactory
) -> None:
app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})
app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)
file = factory.create_file_storage()
- audio_provider.model.speech_to_text_result = "Legacy feature transcript"
+ audio_provider.speech_to_text_result = "Legacy feature transcript"
result = AudioService.transcript_agent_asr(
app_model=app,
@@ -637,8 +580,13 @@ class TestAudioServiceASR:
with pytest.raises(AudioTooLargeServiceError, match="Audio size larger than 30 mb"):
AudioService.transcript_asr(app_model=app, file=file, session=self.session)
+ @pytest.mark.parametrize("legacy_null", [False, True])
def test_transcript_asr_raises_error_when_no_model_instance(
- self, audio_provider: AudioProviderHarness, factory: AudioServiceTestDataFactory
+ self,
+ audio_provider: AudioRuntimeObservations,
+ factory: AudioServiceTestDataFactory,
+ monkeypatch: pytest.MonkeyPatch,
+ legacy_null: bool,
) -> None:
"""Test that ASR raises error when no model instance is available."""
# Arrange
@@ -649,10 +597,13 @@ class TestAudioServiceASR:
)
file = factory.create_file_storage()
- audio_provider.manager.model = None
+ audio_provider.default_available = False
# Act & Assert
- with pytest.raises(ProviderNotSupportSpeechToTextServiceError):
+ # The real manager raises for absent defaults. Explicitly retain the legacy null-return guard too.
+ if legacy_null:
+ monkeypatch.setattr(ModelManager, "get_default_model_instance", lambda *_args, **_kwargs: None)
+ with pytest.raises(ProviderNotSupportSpeechToTextServiceError if legacy_null else ProviderTokenNotInitError):
AudioService.transcript_asr(app_model=app, file=file, session=self.session)
@@ -661,14 +612,14 @@ class TestAudioServiceTTS:
def test_legacy_tts_preserves_the_callers_uncommitted_transaction(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
) -> None:
app = factory.create_app_mock()
sqlite_session.add(_message())
sqlite_session.flush()
- audio_provider.model.tts_result = b"pending message audio"
+ audio_provider.tts_result = b"pending message audio"
result = AudioService.transcript_tts(
app_model=app,
@@ -682,13 +633,13 @@ class TestAudioServiceTTS:
assert result is not None
assert result.get_data() == b"pending message audio"
assert sqlite_session.in_transaction()
- assert audio_provider.model.tts_calls == [{"content_text": "Message answer", "voice": "pending-voice"}]
+ assert audio_provider.tts_calls == [{"content_text": "Message answer", "voice": "pending-voice"}]
sqlite_session.rollback()
assert sqlite_session.get(Message, MESSAGE_ID) is None
def test_transcript_tts_with_text_success(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
) -> None:
@@ -702,7 +653,7 @@ class TestAudioServiceTTS:
app_model_config=app_model_config,
)
- audio_provider.model.tts_result = b"audio data"
+ audio_provider.tts_result = b"audio data"
# Act
result = AudioService.transcript_tts(
@@ -727,11 +678,15 @@ class TestAudioServiceTTS:
},
},
]
- assert audio_provider.model.tts_calls == [{"content_text": "Hello world", "voice": "en-US-Neural"}]
+ assert audio_provider.tts_calls == [{"content_text": "Hello world", "voice": "en-US-Neural"}]
+ assert audio_provider.runtime_calls[0]["request_metadata"] == {
+ "app_type": CreditUsageAppType.CHATBOT,
+ "created_by": CreditUsageCreatedBy.AUDIO,
+ }
def test_transcript_tts_with_default_voice(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
) -> None:
@@ -745,7 +700,7 @@ class TestAudioServiceTTS:
app_model_config=app_model_config,
)
- audio_provider.model.tts_result = b"audio data"
+ audio_provider.tts_result = b"audio data"
# Act
result = AudioService.transcript_tts(
@@ -759,11 +714,11 @@ class TestAudioServiceTTS:
assert result.content_type == "audio/mpeg"
assert result.get_data() == b"audio data"
# Verify default voice was used
- assert audio_provider.model.tts_calls == [{"content_text": "Test", "voice": "default-voice"}]
+ assert audio_provider.tts_calls == [{"content_text": "Test", "voice": "default-voice"}]
def test_transcript_tts_gets_first_available_voice_when_none_configured(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
) -> None:
@@ -777,8 +732,8 @@ class TestAudioServiceTTS:
app_model_config=app_model_config,
)
- audio_provider.model.voices = [{"value": "auto-voice"}]
- audio_provider.model.tts_result = b"audio data"
+ audio_provider.voices = [{"name": "Automatic", "value": "auto-voice"}]
+ audio_provider.tts_result = b"audio data"
# Act
result = AudioService.transcript_tts(
@@ -791,11 +746,11 @@ class TestAudioServiceTTS:
assert result is not None
assert result.content_type == "audio/mpeg"
assert result.get_data() == b"audio data"
- assert audio_provider.model.tts_calls == [{"content_text": "Test", "voice": "auto-voice"}]
+ assert audio_provider.tts_calls == [{"content_text": "Test", "voice": "auto-voice"}]
def test_transcript_tts_workflow_mode_with_draft(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
) -> None:
@@ -806,7 +761,7 @@ class TestAudioServiceTTS:
mode=AppMode.WORKFLOW,
)
- audio_provider.model.tts_result = b"draft audio"
+ audio_provider.tts_result = b"draft audio"
# WorkflowService constructs its default repository from db.engine.
# Leave that database empty so the draft must come from sqlite_session.
flask_app = Flask(__name__)
@@ -828,11 +783,11 @@ class TestAudioServiceTTS:
assert result is not None
assert result.content_type == "audio/mpeg"
assert result.get_data() == b"draft audio"
- assert audio_provider.model.tts_calls == [{"content_text": "Draft test", "voice": "draft-voice"}]
+ assert audio_provider.tts_calls == [{"content_text": "Draft test", "voice": "draft-voice"}]
def test_transcript_tts_message_id_uses_provided_session(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
) -> None:
@@ -848,7 +803,7 @@ class TestAudioServiceTTS:
sqlite_session.add(_message())
sqlite_session.commit()
- audio_provider.model.tts_result = b"message audio"
+ audio_provider.tts_result = b"message audio"
# Act
for wrong_ref in (
@@ -892,11 +847,11 @@ class TestAudioServiceTTS:
assert result is not None
assert result.content_type == "audio/mpeg"
assert result.get_data() == b"message audio"
- assert audio_provider.model.tts_calls == [{"content_text": "Message answer", "voice": "message-voice"}]
+ assert audio_provider.tts_calls == [{"content_text": "Message answer", "voice": "message-voice"}]
def test_transcript_tts_uses_detected_wav_mime_type_for_streams(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
app: Flask,
@@ -905,9 +860,11 @@ class TestAudioServiceTTS:
text_to_speech_dict={"enabled": True, "voice": "en-US-Neural"}
)
app_model = factory.create_app_mock(mode=AppMode.CHAT, app_model_config=app_model_config)
- audio_provider.model.tts_result = (
+ audio_provider.tts_result = (
chunk for chunk in [b"RIFF\x24\x00\x00\x00WAVEfmt ", b"\x10\x00\x00\x00audio-data"]
)
+ # Legacy plugins without format metadata must use the detected container.
+ audio_provider.model_properties = {}
with app.test_request_context("/text-to-audio", method="POST"):
result = AudioService.transcript_tts(
@@ -923,7 +880,7 @@ class TestAudioServiceTTS:
def test_transcript_tts_returns_provider_output_error_for_mime_magic_mismatch(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
app: Flask,
@@ -932,12 +889,12 @@ class TestAudioServiceTTS:
text_to_speech_dict={"enabled": True, "voice": "en-US-Neural"}
)
app_model = factory.create_app_mock(mode=AppMode.CHAT, app_model_config=app_model_config)
- audio_provider.model.tts_result = (
+ audio_provider.tts_result = (
chunk for chunk in [TTSAudioChunk(b"RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00audio-data", "audio/mpeg")]
)
with app.test_request_context("/text-to-audio", method="POST"):
- with pytest.raises(InvokeBadRequestError, match="output MIME does not match"):
+ with pytest.raises(InvokeBadRequestError, match="declared audio/mpeg, detected audio/wav"):
AudioService.transcript_tts(
app_model=app_model,
session=sqlite_session,
@@ -960,7 +917,7 @@ class TestAudioServiceTTS:
def test_transcript_tts_raises_error_when_no_voices_available(
self,
- audio_provider: AudioProviderHarness,
+ audio_provider: AudioRuntimeObservations,
factory: AudioServiceTestDataFactory,
sqlite_session: Session,
) -> None:
@@ -974,7 +931,7 @@ class TestAudioServiceTTS:
app_model_config=app_model_config,
)
- audio_provider.model.voices = []
+ audio_provider.voices = []
# Act & Assert
with pytest.raises(ValueError, match="Sorry, no voice available"):
@@ -984,47 +941,50 @@ class TestAudioServiceTTS:
class TestAudioServiceTTSVoices:
"""Test TTS voice listing operations."""
- def test_transcript_tts_voices_success(self, audio_provider: AudioProviderHarness) -> None:
+ def test_transcript_tts_voices_success(self, audio_provider: AudioRuntimeObservations) -> None:
"""Test successful retrieval of TTS voices."""
# Arrange
tenant_id = "tenant-123"
language = "en-US"
- expected_voices = [
+ expected_voices: list[TTSModelVoice] = [
{"name": "Voice 1", "value": "voice-1"},
{"name": "Voice 2", "value": "voice-2"},
]
- audio_provider.model.voices = expected_voices
+ audio_provider.voices = expected_voices
# Act
result = AudioService.transcript_tts_voices(tenant_id=tenant_id, language=language)
# Assert
assert result == expected_voices
- assert audio_provider.model.voice_calls == [(language,)]
+ assert audio_provider.voice_calls == [(language,)]
+ @pytest.mark.parametrize("legacy_null", [False, True])
def test_transcript_tts_voices_raises_error_when_no_model_instance(
- self, audio_provider: AudioProviderHarness
+ self, audio_provider: AudioRuntimeObservations, monkeypatch: pytest.MonkeyPatch, legacy_null: bool
) -> None:
"""Test that TTS voices raises error when no model instance is available."""
# Arrange
tenant_id = "tenant-123"
language = "en-US"
- audio_provider.manager.model = None
+ audio_provider.default_available = False
# Act & Assert
- with pytest.raises(ProviderNotSupportTextToSpeechServiceError):
+ if legacy_null:
+ monkeypatch.setattr(ModelManager, "get_default_model_instance", lambda *_args, **_kwargs: None)
+ with pytest.raises(ProviderNotSupportTextToSpeechServiceError if legacy_null else ProviderTokenNotInitError):
AudioService.transcript_tts_voices(tenant_id=tenant_id, language=language)
- def test_transcript_tts_voices_propagates_exceptions(self, audio_provider: AudioProviderHarness) -> None:
+ def test_transcript_tts_voices_propagates_exceptions(self, audio_provider: AudioRuntimeObservations) -> None:
"""Test that TTS voices propagates exceptions from model instance."""
# Arrange
tenant_id = "tenant-123"
language = "en-US"
- audio_provider.model.voices_error = RuntimeError("Model error")
+ audio_provider.voices_error = RuntimeError("Model error")
# Act & Assert
with pytest.raises(RuntimeError, match="Model error"):