everything working again after cleanup
This commit is contained in:
+18
-13
@@ -117,17 +117,22 @@ def mock_kokoro_tts() -> Generator[dict[str, Any]]:
|
||||
mock_samples = np.array([0.1, 0.2, 0.3], dtype=np.float32)
|
||||
mock_process = MagicMock(return_value=(mock_samples, 24000))
|
||||
|
||||
with patch("vibe_bot.tts.Kokoro", return_value=mock_kokoro_instance): # noqa: SIM117
|
||||
with patch("vibe_bot.tts.chunk_text", mock_chunk):
|
||||
with patch("vibe_bot.tts.process_chunk_sequential", mock_process):
|
||||
yield {
|
||||
"Kokoro": mock_kokoro,
|
||||
"chunk_text": mock_chunk,
|
||||
"process_chunk_sequential": mock_process,
|
||||
"kokoro_instance": mock_kokoro_instance,
|
||||
"mock_samples": mock_samples,
|
||||
"mock_sr": 24000,
|
||||
}
|
||||
with (
|
||||
patch(
|
||||
"vibe_bot.tts.Kokoro",
|
||||
return_value=mock_kokoro_instance,
|
||||
),
|
||||
patch("vibe_bot.tts.chunk_text", mock_chunk),
|
||||
):
|
||||
with patch("vibe_bot.tts.process_chunk_sequential", mock_process):
|
||||
yield {
|
||||
"Kokoro": mock_kokoro,
|
||||
"chunk_text": mock_chunk,
|
||||
"process_chunk_sequential": mock_process,
|
||||
"kokoro_instance": mock_kokoro_instance,
|
||||
"mock_samples": mock_samples,
|
||||
"mock_sr": 24000,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -143,7 +148,7 @@ def mock_discord() -> Generator[dict[str, MagicMock]]:
|
||||
mock_bot_instance.user.name = "test-bot"
|
||||
mock_bot_instance.user.id = "123456789"
|
||||
|
||||
with patch("vibe_bot.main.discord") as mock_discord_module: # noqa: SIM117
|
||||
with patch("vibe_bot.main.discord") as mock_discord_module:
|
||||
with patch("vibe_bot.main.commands", MagicMock()):
|
||||
with patch("vibe_bot.main.commands.Bot", mock_bot_class):
|
||||
mock_bot_class.return_value = mock_bot_instance
|
||||
@@ -162,7 +167,7 @@ def mock_tts_engine() -> Generator[MagicMock]:
|
||||
"""Provide a mock TTSEngine."""
|
||||
mock_engine = MagicMock()
|
||||
mock_engine.generate_audio.return_value = MagicMock()
|
||||
with patch("vibe_bot.main.tts_engine", mock_engine): # noqa: SIM117
|
||||
with patch("vibe_bot.main.tts_engine", mock_engine):
|
||||
with patch("vibe_bot.main.tts.TTSEngine", return_value=mock_engine):
|
||||
yield mock_engine
|
||||
|
||||
|
||||
@@ -106,9 +106,9 @@ except Exception as e:
|
||||
timeout=30,
|
||||
)
|
||||
output = result.stdout.strip()
|
||||
assert output.startswith("ERROR:") and expected_error in output, ( # noqa: PT018
|
||||
f"Expected error '{expected_error}' but got: {output}"
|
||||
)
|
||||
assert (
|
||||
output.startswith("ERROR:") and expected_error in output
|
||||
), f"Expected error '{expected_error}' but got: {output}"
|
||||
|
||||
|
||||
def test_config_missing_discord_token() -> None:
|
||||
|
||||
@@ -129,13 +129,22 @@ def test_get_recent_messages(
|
||||
) -> None:
|
||||
"""Test retrieving recent messages."""
|
||||
chat_db.add_message(
|
||||
message_id="msg-1", user_id="u1", username="alice", content="First",
|
||||
message_id="msg-1",
|
||||
user_id="u1",
|
||||
username="alice",
|
||||
content="First",
|
||||
)
|
||||
chat_db.add_message(
|
||||
message_id="msg-2", user_id="u2", username="bob", content="Second",
|
||||
message_id="msg-2",
|
||||
user_id="u2",
|
||||
username="bob",
|
||||
content="Second",
|
||||
)
|
||||
chat_db.add_message(
|
||||
message_id="msg-3", user_id="u1", username="alice", content="Third",
|
||||
message_id="msg-3",
|
||||
user_id="u1",
|
||||
username="alice",
|
||||
content="Third",
|
||||
)
|
||||
|
||||
messages = chat_db.get_recent_messages(limit=2)
|
||||
@@ -167,10 +176,16 @@ def test_clear_all_messages(
|
||||
) -> None:
|
||||
"""Test clearing all messages."""
|
||||
chat_db.add_message(
|
||||
message_id="msg-1", user_id="u1", username="alice", content="Hello",
|
||||
message_id="msg-1",
|
||||
user_id="u1",
|
||||
username="alice",
|
||||
content="Hello",
|
||||
)
|
||||
chat_db.add_message(
|
||||
message_id="msg-2", user_id="u2", username="bob", content="World",
|
||||
message_id="msg-2",
|
||||
user_id="u2",
|
||||
username="bob",
|
||||
content="World",
|
||||
)
|
||||
|
||||
chat_db.clear_all_messages()
|
||||
@@ -185,7 +200,10 @@ def test_get_user_history(
|
||||
) -> None:
|
||||
"""Test retrieving user message history."""
|
||||
chat_db.add_message(
|
||||
message_id="msg-1", user_id="u1", username="alice", content="User question",
|
||||
message_id="msg-1",
|
||||
user_id="u1",
|
||||
username="alice",
|
||||
content="User question",
|
||||
)
|
||||
chat_db.add_message(
|
||||
message_id="msg-1_response",
|
||||
@@ -422,7 +440,9 @@ def test_custom_bot_delete_with_error(
|
||||
) -> None:
|
||||
"""Test that delete_custom_bot returns False on error."""
|
||||
with patch.object(
|
||||
custom_bot_manager, "_initialize_custom_bots_table", side_effect=Exception("db error"), # noqa: E501
|
||||
custom_bot_manager,
|
||||
"_initialize_custom_bots_table",
|
||||
side_effect=Exception("db error"),
|
||||
):
|
||||
pass
|
||||
result = custom_bot_manager.delete_custom_bot("nonexistent")
|
||||
@@ -433,6 +453,7 @@ def test_database_get_database_singleton(temp_db_path: str) -> None:
|
||||
"""Test that get_database returns the same instance."""
|
||||
import vibe_bot.database as db_module
|
||||
from vibe_bot.database import ChatDatabase, get_database
|
||||
|
||||
db_module._chat_db = None
|
||||
|
||||
db1 = get_database()
|
||||
@@ -453,6 +474,7 @@ def test_database_init_creates_tables(temp_db_path: str) -> None:
|
||||
db.client.close()
|
||||
|
||||
import sqlite3
|
||||
|
||||
conn = sqlite3.connect(temp_db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
||||
|
||||
@@ -6,6 +6,7 @@ import base64
|
||||
import tempfile
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import numpy as np
|
||||
@@ -106,24 +107,24 @@ EMBEDDING_SIMILARITY_LOW = 0.5
|
||||
|
||||
def test_embeddings() -> None:
|
||||
"""Test embedding similarity for similar and different texts."""
|
||||
with patch("vibe_bot.llama_wrapper.openai.OpenAI") as mock_openai:
|
||||
mock_horse_vec = [0.8] * 1024 + [0.6] * 1024
|
||||
mock_horse_also_vec = [0.79] * 1024 + [0.61] * 1024
|
||||
mock_donkey_vec = [-0.8] * 1024 + [-0.6] * 1024
|
||||
mock_horse_vec = [0.8] * 1024 + [0.6] * 1024
|
||||
mock_horse_also_vec = [0.79] * 1024 + [0.61] * 1024
|
||||
mock_donkey_vec = [-0.8] * 1024 + [-0.6] * 1024
|
||||
|
||||
mock_response1 = MagicMock()
|
||||
mock_response1.data = [MagicMock(embedding=mock_horse_vec)]
|
||||
mock_response2 = MagicMock()
|
||||
mock_response2.data = [MagicMock(embedding=mock_horse_also_vec)]
|
||||
mock_response3 = MagicMock()
|
||||
mock_response3.data = [MagicMock(embedding=mock_donkey_vec)]
|
||||
|
||||
mock_openai.return_value.embeddings.create.side_effect = [
|
||||
mock_response1,
|
||||
mock_response2,
|
||||
mock_response3,
|
||||
]
|
||||
def mock_post(*args: Any, **kwargs: Any) -> MagicMock:
|
||||
json_data = kwargs.get("json", {})
|
||||
text = json_data["input"][0]
|
||||
if "horse" in text and "donkey" not in text and "also" not in text:
|
||||
embedding_data = mock_horse_vec
|
||||
elif "also" in text:
|
||||
embedding_data = mock_horse_also_vec
|
||||
else:
|
||||
embedding_data = mock_donkey_vec
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.json.return_value = {"data": [{"embedding": embedding_data}]}
|
||||
return mock_resp
|
||||
|
||||
with patch("vibe_bot.llama_wrapper.requests.post", side_effect=mock_post):
|
||||
result1 = embedding(
|
||||
"this is a horse",
|
||||
openai_url=EMBEDDING_ENDPOINT,
|
||||
|
||||
@@ -125,7 +125,9 @@ def test_custom_bot_command_success(
|
||||
|
||||
asyncio.run(
|
||||
main_module.custom_bot(
|
||||
mock_ctx, bot_name="alfred", personality="you are a british butler",
|
||||
mock_ctx,
|
||||
bot_name="alfred",
|
||||
personality="you are a british butler",
|
||||
),
|
||||
)
|
||||
|
||||
@@ -199,7 +201,9 @@ def test_custom_bot_command_create_fails(
|
||||
|
||||
asyncio.run(
|
||||
main_module.custom_bot(
|
||||
mock_ctx, bot_name="alfred", personality="you are a british butler",
|
||||
mock_ctx,
|
||||
bot_name="alfred",
|
||||
personality="you are a british butler",
|
||||
),
|
||||
)
|
||||
call_args = mock_ctx.send.call_args[0][0]
|
||||
@@ -347,7 +351,9 @@ def test_handle_chat_success(
|
||||
|
||||
import vibe_bot.main as main_module
|
||||
|
||||
mock_llama_wrapper.chat_completion_with_history.return_value = "This is a bot response" # noqa: E501
|
||||
mock_llama_wrapper.chat_completion_with_history.return_value = (
|
||||
"This is a bot response"
|
||||
)
|
||||
|
||||
asyncio.run(
|
||||
main_module.handle_chat(
|
||||
|
||||
@@ -63,9 +63,15 @@ def test_generate_audio_multiple_chunks(mock_kokoro_tts: MagicMock) -> None:
|
||||
|
||||
from vibe_bot.tts import TTSEngine
|
||||
|
||||
mock_kokoro_tts["chunk_text"].return_value = ["chunk one", "chunk two", "chunk three"] # noqa: E501
|
||||
mock_kokoro_tts["chunk_text"].return_value = [
|
||||
"chunk one",
|
||||
"chunk two",
|
||||
"chunk three",
|
||||
]
|
||||
engine = TTSEngine("/tmp/test-model.onnx", "/tmp/test-voices.bin")
|
||||
result = engine.generate_audio("this text is long enough to be split into multiple chunks") # noqa: E501
|
||||
result = engine.generate_audio(
|
||||
"this text is long enough to be split into multiple chunks",
|
||||
)
|
||||
|
||||
assert isinstance(result, BytesIO)
|
||||
assert mock_kokoro_tts["process_chunk_sequential"].call_count == 3
|
||||
@@ -88,7 +94,11 @@ def test_generate_audio_chunk_failure(mock_kokoro_tts: MagicMock) -> None:
|
||||
raise Exception("processing error")
|
||||
return np.array([0.1, 0.2], dtype=np.float32), 24000
|
||||
|
||||
mock_kokoro_tts["chunk_text"].return_value = ["good chunk", "bad chunk", "another good"] # noqa: E501
|
||||
mock_kokoro_tts["chunk_text"].return_value = [
|
||||
"good chunk",
|
||||
"bad chunk",
|
||||
"another good",
|
||||
]
|
||||
mock_kokoro_tts["process_chunk_sequential"].side_effect = process_with_failure
|
||||
|
||||
engine = TTSEngine("/tmp/test-model.onnx", "/tmp/test-voices.bin")
|
||||
|
||||
Reference in New Issue
Block a user