Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Tests: convert false guards to real assertions

Murtaza Nasircb8b71ced8b8
diff --git a/tests/test_admin_hotwords_and_bugfixes.py b/tests/test_admin_hotwords_and_bugfixes.py
index a99f3e19..7f739247 100644
--- a/tests/test_admin_hotwords_and_bugfixes.py
+++ b/tests/test_admin_hotwords_and_bugfixes.py
@@ -205,24 +205,36 @@ def test_azure_empty_choices():
"""Test handling of empty choices in streaming responses."""
print("\n=== Testing Azure Empty Choices Fix (#246) ===")
- def t1():
- """Empty choices array should be skipped, not crash."""
- # Simulate the streaming loop logic from inquire.py
- collected_content = ""
-
- # Create mock chunks - some with empty choices (Azure content filter)
- class MockDelta:
- def __init__(self, content=None):
- self.content = content
-
- class MockChoice:
- def __init__(self, content=None):
- self.delta = MockDelta(content)
-
- class MockChunk:
- def __init__(self, choices=None):
- self.choices = choices if choices is not None else []
+ # Exercise the REAL shared streaming consumer used by inquire.py for chat
+ # streaming. It is responsible for the empty-choices guard (Azure content
+ # filter returns chunks with choices=[]); a regression that dropped the
+ # `if chunk.choices and ...` guard would raise IndexError here.
+ from src.services.llm import process_streaming_with_thinking
+
+ class MockDelta:
+ def __init__(self, content=None):
+ self.content = content
+
+ class MockChoice:
+ def __init__(self, content=None):
+ self.delta = MockDelta(content)
+
+ class MockChunk:
+ def __init__(self, choices=None):
+ self.choices = choices if choices is not None else []
+
+ def _collect_deltas(chunks):
+ """Run chunks through the real generator and reassemble its 'delta' SSE payloads."""
+ collected = ""
+ for sse in process_streaming_with_thinking(iter(chunks)):
+ # SSE frames look like: "data: {json}\n\n"
+ payload = json.loads(sse[len("data: "):].strip())
+ if 'delta' in payload:
+ collected += payload['delta']
+ return collected
+ def t1():
+ """Empty choices array should be skipped by the real generator, not crash."""
chunks = [
MockChunk(choices=[]), # Empty choices (Azure filter)
MockChunk(choices=[MockChoice("Hello ")]), # Normal chunk
@@ -230,36 +242,16 @@ def test_azure_empty_choices():
MockChunk(choices=[MockChoice("world")]), # Normal chunk
MockChunk(choices=[MockChoice(None)]), # Choice with None content
]
-
- for chunk in chunks:
- if not chunk.choices:
- continue
- content = chunk.choices[0].delta.content
- if content:
- collected_content += content
-
+ collected_content = _collect_deltas(chunks)
assert collected_content == "Hello world", f"Expected 'Hello world', got '{collected_content}'"
- run_test("Empty choices array is safely skipped in streaming", t1)
+ run_test("Empty choices array is safely skipped in streaming (real generator)", t1)
def t2():
- """All-empty stream produces no output (doesn't crash)."""
- chunks = []
-
- class MockChunk:
- def __init__(self):
- self.choices = []
-
- chunks = [MockChunk(), MockChunk(), MockChunk()]
- collected = ""
- for chunk in chunks:
- if not chunk.choices:
- continue
- content = chunk.choices[0].delta.content
- if content:
- collected += content
-
- assert collected == ""
- run_test("All-empty-choices stream produces empty output", t2)
+ """All-empty stream produces no delta output (doesn't crash)."""
+ chunks = [MockChunk(choices=[]), MockChunk(choices=[]), MockChunk(choices=[])]
+ collected = _collect_deltas(chunks)
+ assert collected == "", f"Expected empty output, got '{collected}'"
+ run_test("All-empty-choices stream produces empty output (real generator)", t2)
# =============================================================================
diff --git a/tests/test_ffprobe_codec_detection.py b/tests/test_ffprobe_codec_detection.py
index be7f70b0..f67574a4 100644
--- a/tests/test_ffprobe_codec_detection.py
+++ b/tests/test_ffprobe_codec_detection.py
@@ -2,8 +2,11 @@
"""
Test script for ffprobe codec detection functionality.
-This script tests the new codec-based detection system to ensure it correctly
-identifies audio codecs, video files, and lossless formats.
+This script tests the codec-based detection system to ensure it correctly
+identifies audio codecs, video files, and lossless formats. It generates real
+media files with ffmpeg and probes them with the production ffprobe helpers in
+src/utils/ffprobe.py — nothing is mocked. If ffmpeg/ffprobe are unavailable the
+tests skip rather than passing vacuously.
"""
import os
@@ -12,6 +15,8 @@ import tempfile
import subprocess
from pathlib import Path
+import pytest
+
# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
@@ -23,7 +28,24 @@ from src.utils.ffprobe import (
needs_audio_conversion,
is_lossless_audio,
get_duration,
- FFProbeError
+ FFProbeError,
+)
+
+
+def _ffmpeg_available():
+ """Return True if both ffmpeg and ffprobe are runnable."""
+ try:
+ subprocess.run(['ffprobe', '-version'], capture_output=True, check=True)
+ subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
+ return True
+ except (FileNotFoundError, subprocess.CalledProcessError):
+ return False
+
+
+# Skip the whole module when ffmpeg/ffprobe are missing instead of passing silently.
+pytestmark = pytest.mark.skipif(
+ not _ffmpeg_available(),
+ reason="ffmpeg/ffprobe not found; required for codec-detection tests",
)
@@ -37,10 +59,10 @@ def create_test_audio_file(codec, output_path, duration=1.0):
'pcm_s16le': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'pcm_s16le', '-ar', '44100', output_path],
'vorbis': ['ffmpeg', '-f', 'lavfi', '-i', f'sine=frequency=440:duration={duration}', '-acodec', 'libvorbis', '-b:a', '128k', output_path],
}
-
+
if codec not in codec_map:
raise ValueError(f"Unknown codec: {codec}")
-
+
subprocess.run(codec_map[codec], check=True, capture_output=True)
@@ -55,9 +77,7 @@ def create_test_video_file(output_path, duration=1.0):
def test_codec_detection():
- """Test basic codec detection."""
- print("\n=== Testing Codec Detection ===\n")
-
+ """The detected audio codec must match the codec the file was encoded with."""
with tempfile.TemporaryDirectory() as tmpdir:
test_files = {
'mp3': 'test.mp3',
@@ -67,87 +87,50 @@ def test_codec_detection():
'pcm_s16le': 'test.wav',
'vorbis': 'test.ogg',
}
-
+
for codec, filename in test_files.items():
filepath = os.path.join(tmpdir, filename)
- try:
- print(f"Creating test file: {filename} with codec {codec}...")
- create_test_audio_file(codec, filepath)
-
- print(f" Probing {filename}...")
- codec_info = get_codec_info(filepath)
-
- detected_codec = codec_info['audio_codec']
- print(f" ✓ Detected codec: {detected_codec}")
- print(f" Has audio: {codec_info['has_audio']}")
- print(f" Has video: {codec_info['has_video']}")
- print(f" Format: {codec_info['format_name']}")
- print(f" Duration: {codec_info['duration']:.2f}s" if codec_info['duration'] else " Duration: N/A")
-
- if detected_codec != codec:
- print(f" ⚠️ Warning: Expected {codec}, got {detected_codec}")
-
- print()
-
- except Exception as e:
- print(f" ✗ Failed to test {codec}: {e}\n")
+ create_test_audio_file(codec, filepath)
+
+ codec_info = get_codec_info(filepath)
+
+ assert codec_info['has_audio'] is True, f"{filename}: audio stream not detected"
+ assert codec_info['has_video'] is False, f"{filename}: unexpected video stream"
+ assert codec_info['audio_codec'] == codec, (
+ f"{filename}: expected codec {codec}, got {codec_info['audio_codec']}"
+ )
+ assert codec_info['duration'] is not None and codec_info['duration'] > 0, (
+ f"{filename}: duration not detected"
+ )
+ # get_audio_codec is a thin wrapper and must agree.
+ assert get_audio_codec(filepath) == codec
+ assert is_audio_file(filepath) is True
def test_video_detection():
- """Test video file detection."""
- print("\n=== Testing Video Detection ===\n")
-
+ """A video file must be flagged as video; an audio-only file must not."""
with tempfile.TemporaryDirectory() as tmpdir:
video_path = os.path.join(tmpdir, 'test_video.mp4')
audio_path = os.path.join(tmpdir, 'test_audio.mp3')
-
- try:
- print("Creating test video file...")
- create_test_video_file(video_path)
-
- print("Creating test audio file...")
- create_test_audio_file('mp3', audio_path)
-
- print(f"\nProbing video file...")
- codec_info = get_codec_info(video_path)
- print(f" Audio codec: {codec_info['audio_codec']}")
- print(f" Video codec: {codec_info['video_codec']}")
- print(f" Has audio: {codec_info['has_audio']}")
- print(f" Has video: {codec_info['has_video']}")
-
- is_video = is_video_file(video_path)
- print(f" is_video_file(): {is_video}")
-
- if not is_video:
- print(" ✗ Video file not detected as video!")
- else:
- print(" ✓ Video file correctly detected")
-
- print(f"\nProbing audio file...")
- codec_info = get_codec_info(audio_path)
- print(f" Audio codec: {codec_info['audio_codec']}")
- print(f" Video codec: {codec_info['video_codec']}")
- print(f" Has audio: {codec_info['has_audio']}")
- print(f" Has video: {codec_info['has_video']}")
-
- is_video = is_video_file(audio_path)
- print(f" is_video_file(): {is_video}")
-
- if is_video:
- print(" ✗ Audio file incorrectly detected as video!")
- else:
- print(" ✓ Audio file correctly identified as audio-only")
-
- print()
-
- except Exception as e:
- print(f"✗ Failed to test video detection: {e}\n")
+
+ create_test_video_file(video_path)
+ create_test_audio_file('mp3', audio_path)
+
+ video_info = get_codec_info(video_path)
+ assert video_info['has_video'] is True, "video stream not detected in video file"
+ assert video_info['has_audio'] is True, "audio stream not detected in video file"
+ assert video_info['video_codec'] is not None
+ assert is_video_file(video_path) is True, "video file not detected as video"
+
+ audio_info = get_codec_info(audio_path)
+ assert audio_info['has_video'] is False, "unexpected video stream in audio file"
+ assert audio_info['has_audio'] is True
+ assert audio_info['video_codec'] is None
+ assert is_video_file(audio_path) is False, "audio file incorrectly detected as video"
def test_lossless_detection():
- """Test lossless audio detection."""
- print("\n=== Testing Lossless Detection ===\n")
-
+ """Lossless codecs must be reported as lossless; lossy codecs must not."""
with tempfile.TemporaryDirectory() as tmpdir:
test_cases = {
'pcm_s16le': ('test.wav', True),
@@ -156,164 +139,122 @@ def test_lossless_detection():
'aac': ('test.m4a', False),
'opus': ('test.opus', False),
}
-
+
for codec, (filename, expected_lossless) in test_cases.items():
filepath = os.path.join(tmpdir, filename)
- try:
- print(f"Creating {filename} with codec {codec}...")
- create_test_audio_file(codec, filepath)
-
- is_lossless = is_lossless_audio(filepath)
- status = "✓" if is_lossless == expected_lossless else "✗"
-
- print(f" {status} {codec}: is_lossless={is_lossless} (expected {expected_lossless})")
-
- except Exception as e:
- print(f" ✗ Failed to test {codec}: {e}")
-
- print()
+ create_test_audio_file(codec, filepath)
+
+ assert is_lossless_audio(filepath) is expected_lossless, (
+ f"{codec}: is_lossless_audio returned {not expected_lossless}, "
+ f"expected {expected_lossless}"
+ )
def test_conversion_check():
- """Test conversion requirement detection."""
- print("\n=== Testing Conversion Check ===\n")
-
+ """needs_audio_conversion must flag only codecs outside the supported list."""
with tempfile.TemporaryDirectory() as tmpdir:
- # Supported codecs for direct transcription
supported_codecs = ['pcm_s16le', 'mp3', 'flac', 'opus', 'aac']
-
+
test_cases = {
- 'mp3': ('test.mp3', False), # Supported, no conversion needed
- 'aac': ('test.m4a', False), # Supported, no conversion needed
+ 'mp3': ('test.mp3', False), # Supported, no conversion needed
+ 'aac': ('test.m4a', False), # Supported, no conversion needed
'opus': ('test.opus', False), # Supported, no conversion needed
- 'vorbis': ('test.ogg', True), # Not in supported list, needs conversion
+ 'vorbis': ('test.ogg', True), # Not supported, needs conversion
}
-
+
for codec, (filename, should_convert) in test_cases.items():
filepath = os.path.join(tmpdir, filename)
- try:
- print(f"Creating {filename} with codec {codec}...")
- create_test_audio_file(codec, filepath)
-
- needs_conversion, detected_codec = needs_audio_conversion(filepath, supported_codecs)
- status = "✓" if needs_conversion == should_convert else "✗"
-
- print(f" {status} {codec}: needs_conversion={needs_conversion} (expected {should_convert})")
- print(f" Detected codec: {detected_codec}")
-
- except Exception as e:
- print(f" ✗ Failed to test {codec}: {e}")
-
- print()
+ create_test_audio_file(codec, filepath)
+
+ needs_conversion, detected_codec = needs_audio_conversion(filepath, supported_codecs)
+
+ assert needs_conversion is should_convert, (
+ f"{codec}: needs_conversion={needs_conversion}, expected {should_convert}"
+ )
+ assert detected_codec == codec, (
+ f"{codec}: detected codec {detected_codec}"
+ )
def test_misnamed_file():
- """Test detection of files with wrong extensions."""
- print("\n=== Testing Misnamed File Detection ===\n")
-
+ """Codec detection must rely on stream contents, not the file extension."""
with tempfile.TemporaryDirectory() as tmpdir:
- # Create an MP3 file but name it .wav
+ # An MP3 file deliberately named .wav must still be detected as mp3.
wrong_name_path = os.path.join(tmpdir, 'actually_mp3.wav')
-
- try:
- print("Creating MP3 file with .wav extension...")
- create_test_audio_file('mp3', wrong_name_path)
-
- codec_info = get_codec_info(wrong_name_path)
- detected_codec = codec_info['audio_codec']
-
- print(f" Filename: actually_mp3.wav")
- print(f" Detected codec: {detected_codec}")
-
- if detected_codec == 'mp3':
- print(" ✓ Correctly detected MP3 codec despite .wav extension")
- else:
- print(f" ✗ Incorrectly detected as {detected_codec}")
-
- # Create a FLAC file but name it .mp3
- wrong_name_path2 = os.path.join(tmpdir, 'actually_flac.mp3')
- print("\nCreating FLAC file with .mp3 extension...")
- create_test_audio_file('flac', wrong_name_path2)
-
- codec_info = get_codec_info(wrong_name_path2)
- detected_codec = codec_info['audio_codec']
-
- print(f" Filename: actually_flac.mp3")
- print(f" Detected codec: {detected_codec}")
-
- if detected_codec == 'flac':
- print(" ✓ Correctly detected FLAC codec despite .mp3 extension")
- else:
- print(f" ✗ Incorrectly detected as {detected_codec}")
-
- print()
-
- except Exception as e:
- print(f"✗ Failed to test misnamed files: {e}\n")
+ create_test_audio_file('mp3', wrong_name_path)
+ assert get_codec_info(wrong_name_path)['audio_codec'] == 'mp3', (
+ "MP3 file misdetected because of .wav extension"
+ )
+
+ # A FLAC file deliberately named .mp3 must still be detected as flac.
+ # ffmpeg would normally infer the muxer from the .mp3 extension, so
+ # force the FLAC container explicitly to produce the misnamed file.
+ wrong_name_path2 = os.path.join(tmpdir, 'actually_flac.mp3')
+ subprocess.run([
+ 'ffmpeg', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=1.0',
+ '-acodec', 'flac', '-f', 'flac', wrong_name_path2
+ ], check=True, capture_output=True)
+ assert get_codec_info(wrong_name_path2)['audio_codec'] == 'flac', (
+ "FLAC file misdetected because of .mp3 extension"
+ )
def test_duration():
- """Test duration extraction."""
- print("\n=== Testing Duration Extraction ===\n")
-
+ """Extracted duration must match the encoded duration within tolerance."""
with tempfile.TemporaryDirectory() as tmpdir:
durations = [1.0, 2.5, 5.0]
-
+
for expected_duration in durations:
filepath = os.path.join(tmpdir, f'test_{expected_duration}s.mp3')
- try:
- print(f"Creating {expected_duration}s audio file...")
- create_test_audio_file('mp3', filepath, duration=expected_duration)
-
- detected_duration = get_duration(filepath)
-
- # Allow 0.1s tolerance for encoding variations
- if detected_duration and abs(detected_duration - expected_duration) < 0.1:
- print(f" ✓ Duration: {detected_duration:.2f}s (expected {expected_duration}s)")
- else:
- print(f" ✗ Duration: {detected_duration:.2f}s (expected {expected_duration}s)")
-
- except Exception as e:
- print(f" ✗ Failed to test duration: {e}")
-
- print()
+ create_test_audio_file('mp3', filepath, duration=expected_duration)
+
+ detected_duration = get_duration(filepath)
+
+ assert detected_duration is not None, (
+ f"{expected_duration}s file: no duration detected"
+ )
+ # Allow 0.1s tolerance for encoder padding/priming variations.
+ assert abs(detected_duration - expected_duration) < 0.1, (
+ f"duration {detected_duration:.2f}s, expected {expected_duration}s"
+ )
def main():
- """Run all tests."""
+ """Run all tests standalone (without pytest)."""
print("=" * 60)
print("FFProbe Codec Detection Test Suite")
print("=" * 60)
-
- # Check if ffmpeg/ffprobe are available
- try:
- subprocess.run(['ffprobe', '-version'], capture_output=True, check=True)
- subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
- except (FileNotFoundError, subprocess.CalledProcessError):
- print("\n✗ Error: ffmpeg/ffprobe not found. Please install ffmpeg to run tests.\n")
- return 1
-
- try:
- test_codec_detection()
- test_video_detection()
- test_lossless_detection()
- test_conversion_check()
- test_misnamed_file()
- test_duration()
-
- print("=" * 60)
- print("All tests completed!")
- print("=" * 60)
- print()
-
- return 0
-
- except Exception as e:
- print(f"\n✗ Test suite failed with error: {e}\n")
- import traceback
- traceback.print_exc()
+
+ if not _ffmpeg_available():
+ print("\nffmpeg/ffprobe not found. Please install ffmpeg to run tests.\n")
return 1
+ tests = [
+ test_codec_detection,
+ test_video_detection,
+ test_lossless_detection,
+ test_conversion_check,
+ test_misnamed_file,
+ test_duration,
+ ]
+
+ failed = False
+ for test in tests:
+ try:
+ test()
+ print(f"PASS - {test.__name__}")
+ except AssertionError as e:
+ print(f"FAIL - {test.__name__}: {e}")
+ failed = True
+ except Exception as e:
+ print(f"ERROR - {test.__name__}: {e}")
+ failed = True
+
+ print("=" * 60)
+ print("Some tests failed." if failed else "All tests completed!")
+ print("=" * 60)
+ return 1 if failed else 0
+
if __name__ == '__main__':
- sys.exit(main())
\ No newline at end of file
+ sys.exit(main())
diff --git a/tests/test_inquire_mode.py b/tests/test_inquire_mode.py
index 539766f7..2af5a308 100644
--- a/tests/test_inquire_mode.py
+++ b/tests/test_inquire_mode.py
@@ -1,187 +1,177 @@
#!/usr/bin/env python3
"""
-Test script for Inquire Mode functionality
+Test script for Inquire Mode functionality.
+
+These tests create their own isolated data inside the app context rather than
+relying on rows already present in the database, so they run correctly against
+the fresh, empty DB that conftest.py provisions for the pytest suite.
"""
import os
import sys
+import uuid
# Add the parent directory to the path to import app
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-from src.app import app, db, User, Recording, TranscriptChunk, InquireSession, Tag
+from src.app import app, db, User, Recording, TranscriptChunk, InquireSession
+
+
+def _unique_suffix():
+ """Return a short unique token so test rows never collide on unique cols."""
+ return uuid.uuid4().hex[:8]
+
def test_database_models():
- """Test that the new database models work correctly."""
+ """Create a user, recording, chunk and inquire session and assert they persist."""
with app.app_context():
- print("🔍 Testing Inquire Mode Database Models...")
-
- # Test that tables exist
+ # The schema must contain the inquire-mode tables.
from sqlalchemy import inspect
inspector = inspect(db.engine)
tables = inspector.get_table_names()
-
- required_tables = ['transcript_chunk', 'inquire_session']
- for table in required_tables:
- if table in tables:
- print(f"✅ Table '{table}' exists")
- else:
- print(f"❌ Table '{table}' missing")
- return False
-
- # Test creating sample data
+ assert 'transcript_chunk' in tables, "transcript_chunk table missing from schema"
+ assert 'inquire_session' in tables, "inquire_session table missing from schema"
+
+ suffix = _unique_suffix()
+ user = User(username=f"inq_{suffix}", email=f"inq_{suffix}@example.com")
+ db.session.add(user)
+ db.session.commit()
+
+ recording = Recording(
+ user_id=user.id,
+ title=f"Test Recording {suffix}",
+ status="COMPLETED",
+ )
+ db.session.add(recording)
+ db.session.commit()
+
+ chunk = TranscriptChunk(
+ recording_id=recording.id,
+ user_id=user.id,
+ chunk_index=0,
+ content="This is a test transcription chunk.",
+ start_time=0.0,
+ end_time=5.0,
+ speaker_name="Test Speaker",
+ )
+ db.session.add(chunk)
+
+ session = InquireSession(
+ user_id=user.id,
+ session_name="Test Session",
+ filter_tags='[]',
+ filter_speakers='["Test Speaker"]',
+ )
+ db.session.add(session)
+ db.session.commit()
+
try:
- # Create a test user (or get existing one)
- user = User.query.first()
- if not user:
- print("❌ No users found. Please create a user first.")
- return False
-
- print(f"📝 Using test user: {user.username}")
-
- # Create a test recording if none exist
- recording = Recording.query.filter_by(user_id=user.id).first()
- if not recording:
- print("❌ No recordings found. Please create a recording first.")
- return False
-
- print(f"🎵 Using test recording: {recording.title}")
-
- # Test TranscriptChunk creation
- chunk = TranscriptChunk(
- recording_id=recording.id,
- user_id=user.id,
- chunk_index=0,
- content="This is a test transcription chunk.",
- start_time=0.0,
- end_time=5.0,
- speaker_name="Test Speaker"
- )
-
- db.session.add(chunk)
-
- # Test InquireSession creation
- session = InquireSession(
- user_id=user.id,
- session_name="Test Session",
- filter_tags='[]',
- filter_speakers='["Test Speaker"]'
- )
-
- db.session.add(session)
- db.session.commit()
-
- print("✅ Successfully created test TranscriptChunk and InquireSession")
-
- # Clean up test data
+ # The rows must be retrievable and carry the data we stored.
+ stored_chunk = TranscriptChunk.query.get(chunk.id)
+ assert stored_chunk is not None
+ assert stored_chunk.recording_id == recording.id
+ assert stored_chunk.user_id == user.id
+ assert stored_chunk.content == "This is a test transcription chunk."
+ assert stored_chunk.speaker_name == "Test Speaker"
+
+ stored_session = InquireSession.query.get(session.id)
+ assert stored_session is not None
+ assert stored_session.user_id == user.id
+ assert stored_session.session_name == "Test Session"
+ assert stored_session.filter_speakers == '["Test Speaker"]'
+ finally:
+ # Clean up so repeat runs / other tests start from a clean slate.
db.session.delete(chunk)
db.session.delete(session)
+ db.session.delete(recording)
+ db.session.delete(user)
db.session.commit()
-
- print("✅ Test data cleaned up")
-
- except Exception as e:
- print(f"❌ Error testing models: {e}")
- return False
-
- return True
+
def test_chunking_functions():
- """Test the chunking and embedding functions."""
+ """Exercise the real chunking/embedding helpers re-exported by src.app."""
with app.app_context():
- print("🔧 Testing Chunking Functions...")
-
- try:
- from src.app import chunk_transcription, generate_embeddings, serialize_embedding, deserialize_embedding
-
- # Test chunking
- test_text = "This is a test sentence. This is another sentence for testing. And here's a third sentence to make sure chunking works properly with longer text that should be split into multiple chunks."
- chunks = chunk_transcription(test_text, max_chunk_length=100, overlap=20)
-
- if len(chunks) > 1:
- print(f"✅ Chunking works: {len(chunks)} chunks created")
- else:
- print("✅ Text too short for chunking (expected behavior)")
-
- # Test embeddings (will only work if sentence-transformers is installed)
- try:
- embeddings = generate_embeddings(["test sentence", "another test"])
- if len(embeddings) == 2:
- print("✅ Embedding generation works")
-
- # Test serialization
- if embeddings[0] is not None:
- serialized = serialize_embedding(embeddings[0])
- deserialized = deserialize_embedding(serialized)
- if deserialized is not None and len(deserialized) > 0:
- print("✅ Embedding serialization/deserialization works")
- else:
- print("❌ Embedding serialization/deserialization failed")
- else:
- print("❌ Embedding generation returned wrong number of embeddings")
-
- except Exception as e:
- print(f"⚠️ Embedding test skipped (sentence-transformers may not be installed): {e}")
-
- except Exception as e:
- print(f"❌ Error testing chunking functions: {e}")
- return False
-
- return True
-
-def test_api_imports():
- """Test that all API endpoints can be imported."""
- print("🔌 Testing API Endpoint Imports...")
-
- try:
from src.app import (
- get_inquire_sessions, create_inquire_session, inquire_search,
- inquire_chat, get_available_filters, process_recording_chunks_endpoint
+ chunk_transcription,
+ generate_embeddings,
+ serialize_embedding,
+ deserialize_embedding,
)
- print("✅ All inquire mode API endpoints imported successfully")
- return True
- except ImportError as e:
- print(f"❌ Failed to import API endpoints: {e}")
- return False
+
+ # chunk_transcription must split a long passage into ordered chunks.
+ test_text = (
+ "This is a test sentence. This is another sentence for testing. "
+ "And here's a third sentence to make sure chunking works properly "
+ "with longer text that should be split into multiple chunks."
+ )
+ chunks = chunk_transcription(test_text, max_chunk_length=100, overlap=20)
+ assert isinstance(chunks, list)
+ assert len(chunks) >= 1
+ # Every chunk must be a non-empty string.
+ assert all(isinstance(c, str) and c.strip() for c in chunks)
+ # With max_chunk_length=100 on this ~190-char passage we expect a split.
+ assert len(chunks) > 1, f"expected multiple chunks, got {len(chunks)}"
+
+ # Embeddings depend on sentence-transformers, which may not be installed
+ # in every environment. When it is unavailable generate_embeddings
+ # returns a list of None placeholders rather than raising.
+ embeddings = generate_embeddings(["test sentence", "another test"])
+ assert isinstance(embeddings, list)
+ assert len(embeddings) == 2
+
+ if embeddings[0] is not None:
+ serialized = serialize_embedding(embeddings[0])
+ assert serialized is not None
+ deserialized = deserialize_embedding(serialized)
+ assert deserialized is not None
+ assert len(deserialized) > 0
+
+
+def test_api_imports():
+ """The inquire-mode endpoint functions must be importable and callable."""
+ from src.api.inquire import (
+ get_inquire_sessions,
+ create_inquire_session,
+ inquire_search,
+ inquire_chat,
+ get_available_filters,
+ )
+ from src.api.recordings import process_recording_chunks_endpoint
+
+ for fn in (
+ get_inquire_sessions,
+ create_inquire_session,
+ inquire_search,
+ inquire_chat,
+ get_available_filters,
+ process_recording_chunks_endpoint,
+ ):
+ assert callable(fn), f"{getattr(fn, '__name__', fn)} is not callable"
+
def main():
- """Run all tests."""
- print("🚀 Starting Inquire Mode Tests...\n")
-
+ """Run all tests (standalone, without pytest)."""
+ print("Starting Inquire Mode Tests...\n")
+
tests = [
("Database Models", test_database_models),
- ("Chunking Functions", test_chunking_functions),
- ("API Imports", test_api_imports)
+ ("Chunking Functions", test_chunking_functions),
+ ("API Imports", test_api_imports),
]
-
- results = []
+
+ all_passed = True
for test_name, test_func in tests:
- print(f"\n--- {test_name} ---")
+ print(f"--- {test_name} ---")
try:
- success = test_func()
- results.append((test_name, success))
+ test_func()
+ print(f"PASS - {test_name}")
except Exception as e:
- print(f"❌ {test_name} failed with exception: {e}")
- results.append((test_name, False))
-
- print("\n" + "="*50)
- print("📊 Test Results Summary:")
- print("="*50)
-
- all_passed = True
- for test_name, success in results:
- status = "✅ PASS" if success else "❌ FAIL"
- print(f"{status} - {test_name}")
- if not success:
+ print(f"FAIL - {test_name}: {e}")
all_passed = False
-
- print("\n" + "="*50)
- if all_passed:
- print("🎉 All tests passed! Inquire Mode is ready to use.")
- else:
- print("⚠️ Some tests failed. Please check the output above.")
-
+
+ print("\nAll tests passed!" if all_passed else "\nSome tests failed.")
return all_passed
+
if __name__ == "__main__":
success = main()
- sys.exit(0 if success else 1)
\ No newline at end of file
+ sys.exit(0 if success else 1)
diff --git a/tests/test_job_queue_race_condition.py b/tests/test_job_queue_race_condition.py
index ae39b872..3f98761c 100644
--- a/tests/test_job_queue_race_condition.py
+++ b/tests/test_job_queue_race_condition.py
@@ -5,259 +5,204 @@ Test script for job queue race condition fix.
This script verifies that the atomic job claiming mechanism prevents
multiple workers from claiming the same job simultaneously.
-The fix uses an atomic UPDATE with WHERE clause to ensure only one
-worker can claim a job, even with multiple processes/threads.
+The fix lives in FairJobQueue._claim_next_job() in src/services/job_queue.py,
+which uses an atomic UPDATE ... WHERE status='queued' so only one worker can
+flip a job from 'queued' to 'processing'. These tests exercise that REAL
+method (via the module-level `job_queue` singleton) rather than re-implementing
+the claim SQL inline, so a regression in the source claim logic would fail here.
"""
import os
import sys
import threading
-import time
from pathlib import Path
-from datetime import datetime
from concurrent.futures import ThreadPoolExecutor, as_completed
# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
+def _make_user(db, User):
+ """Get the first existing user or create a throwaway test user."""
+ user = User.query.first()
+ if user:
+ return user, False
+ user = User(username='test_race_condition_user', email='[email protected]')
+ # Different User models expose different password attributes; set whatever
+ # exists so the row is valid without depending on a specific hashing path.
+ if hasattr(user, 'password_hash'):
+ user.password_hash = 'unused'
+ db.session.add(user)
+ db.session.commit()
+ return user, True
+
+
def test_atomic_job_claiming():
"""
- Test that only one worker can claim a job even with concurrent attempts.
-
- This simulates the race condition where multiple workers try to claim
- the same job simultaneously.
+ Test that only one worker can claim a single queued job even with
+ concurrent attempts against the REAL _claim_next_job method.
"""
- print("\n=== Testing Atomic Job Claiming ===\n")
+ print("\n=== Testing Atomic Job Claiming (real _claim_next_job) ===\n")
- # Import Flask app and models
from src.app import app
from src.database import db
from src.models import ProcessingJob, User, Recording
- from sqlalchemy import update
+ from src.services.job_queue import job_queue
+
+ # Bind the singleton queue to the test app so its internal _app_context()
+ # opens the test DB. We never call start(), so no worker threads spawn.
+ job_queue.init_app(app)
+ created_user = False
with app.app_context():
- # Use the first existing user for testing, or create a minimal test user
- test_user = User.query.first()
- if not test_user:
- test_user = User(
- username='test_race_condition_user',
- password='not_used' # Password not needed for this test
- )
- db.session.add(test_user)
- db.session.commit()
+ test_user, created_user = _make_user(db, User)
- # Create a test recording
- test_recording = Recording(
+ recording = Recording(
user_id=test_user.id,
title='Test Race Condition Recording',
audio_path='/tmp/test_audio.mp3',
- status='QUEUED'
+ status='QUEUED',
)
- db.session.add(test_recording)
+ db.session.add(recording)
db.session.commit()
- # Create a test job in 'queued' status
- test_job = ProcessingJob(
- recording_id=test_recording.id,
+ job = ProcessingJob(
+ recording_id=recording.id,
user_id=test_user.id,
job_type='transcribe',
- status='queued'
+ status='queued',
)
- db.session.add(test_job)
+ db.session.add(job)
db.session.commit()
- job_id = test_job.id
+ job_id = job.id
+ recording_id = recording.id
print(f"Created test job {job_id} with status 'queued'")
- # Track which threads successfully claimed the job
- successful_claims = []
- claim_lock = threading.Lock()
-
- def attempt_claim(worker_id):
- """Simulate a worker attempting to claim the job."""
- with app.app_context():
- try:
- # This is the atomic claim logic from the fix
- claim_time = datetime.utcnow()
- result = db.session.execute(
- update(ProcessingJob)
- .where(
- ProcessingJob.id == job_id,
- ProcessingJob.status == 'queued'
- )
- .values(status='processing', started_at=claim_time)
- )
-
- if result.rowcount == 1:
- db.session.commit()
- with claim_lock:
- successful_claims.append(worker_id)
- return f"Worker {worker_id}: Successfully claimed job"
- else:
- db.session.rollback()
- return f"Worker {worker_id}: Job already claimed (rowcount=0)"
-
- except Exception as e:
- db.session.rollback()
- return f"Worker {worker_id}: Error - {e}"
-
- # Spawn multiple threads to claim simultaneously
- num_workers = 10
- print(f"\nSpawning {num_workers} workers to claim job {job_id} simultaneously...")
-
- # Use a barrier to ensure all threads start at the same time
- barrier = threading.Barrier(num_workers)
-
- def worker_with_barrier(worker_id):
- barrier.wait() # Wait for all threads to be ready
- return attempt_claim(worker_id)
-
- with ThreadPoolExecutor(max_workers=num_workers) as executor:
- futures = {executor.submit(worker_with_barrier, i): i for i in range(num_workers)}
-
- for future in as_completed(futures):
- result = future.result()
- print(f" {result}")
-
- # Verify results
- print(f"\n=== Results ===")
- print(f"Total workers: {num_workers}")
- print(f"Successful claims: {len(successful_claims)}")
- print(f"Workers that claimed: {successful_claims}")
-
- # Check final job status
- db.session.expire_all()
- final_job = db.session.get(ProcessingJob, job_id)
- print(f"Final job status: {final_job.status}")
-
- # Cleanup
- db.session.delete(final_job)
- db.session.delete(test_recording)
- db.session.commit()
+ successful_claims = []
+ claim_lock = threading.Lock()
+ num_workers = 10
+ barrier = threading.Barrier(num_workers)
+
+ def worker(worker_id):
+ # Each worker calls the REAL claim method. Exactly one should win.
+ barrier.wait()
+ claimed = job_queue._claim_next_job(['transcribe'], 'transcription')
+ if claimed is not None and claimed.id == job_id:
+ with claim_lock:
+ successful_claims.append(worker_id)
+
+ print(f"\nSpawning {num_workers} workers to claim job {job_id} simultaneously...")
+ with ThreadPoolExecutor(max_workers=num_workers) as executor:
+ futures = [executor.submit(worker, i) for i in range(num_workers)]
+ for f in as_completed(futures):
+ f.result()
- # Assert only one worker claimed the job
- assert len(successful_claims) == 1, f"Expected 1 successful claim, got {len(successful_claims)}"
- assert final_job.status == 'processing', f"Expected status 'processing', got {final_job.status}"
+ try:
+ with app.app_context():
+ db.session.expire_all()
+ final_job = db.session.get(ProcessingJob, job_id)
+ final_status = final_job.status
+ print(f"\n=== Results ===")
+ print(f"Total workers: {num_workers}")
+ print(f"Successful claims: {len(successful_claims)} -> {successful_claims}")
+ print(f"Final job status: {final_status}")
+
+ assert len(successful_claims) == 1, \
+ f"Expected exactly 1 successful claim, got {len(successful_claims)}"
+ assert final_status == 'processing', \
+ f"Expected status 'processing', got {final_status}"
print("\n[PASS] Only one worker successfully claimed the job!")
- return True
+ finally:
+ with app.app_context():
+ j = db.session.get(ProcessingJob, job_id)
+ if j:
+ db.session.delete(j)
+ r = db.session.get(Recording, recording_id)
+ if r:
+ db.session.delete(r)
+ db.session.commit()
def test_multiple_jobs_fair_distribution():
"""
- Test that multiple jobs are distributed fairly across workers.
+ Test that the REAL _claim_next_job hands out N distinct jobs exactly once
+ each (no double-claims) when called repeatedly.
"""
- print("\n=== Testing Multiple Jobs Distribution ===\n")
+ print("\n=== Testing Multiple Jobs Distribution (real _claim_next_job) ===\n")
from src.app import app
from src.database import db
from src.models import ProcessingJob, User, Recording
- from sqlalchemy import update
+ from src.services.job_queue import job_queue
- with app.app_context():
- # Use the first existing user for testing
- test_user = User.query.first()
- if not test_user:
- test_user = User(
- username='test_distribution_user',
- password='not_used'
- )
- db.session.add(test_user)
- db.session.commit()
+ job_queue.init_app(app)
- # Create multiple test jobs
- num_jobs = 5
- job_ids = []
- recording_ids = []
+ num_jobs = 5
+ job_ids = []
+ recording_ids = []
+
+ with app.app_context():
+ test_user, _ = _make_user(db, User)
for i in range(num_jobs):
recording = Recording(
user_id=test_user.id,
title=f'Test Distribution Recording {i}',
audio_path=f'/tmp/test_audio_{i}.mp3',
- status='QUEUED'
+ status='QUEUED',
)
db.session.add(recording)
db.session.commit()
recording_ids.append(recording.id)
- job = ProcessingJob(
+ jb = ProcessingJob(
recording_id=recording.id,
user_id=test_user.id,
job_type='transcribe',
- status='queued'
+ status='queued',
)
- db.session.add(job)
+ db.session.add(jb)
db.session.commit()
- job_ids.append(job.id)
+ job_ids.append(jb.id)
print(f"Created {num_jobs} test jobs: {job_ids}")
- # Have workers claim jobs
- claimed_jobs = []
-
- def claim_any_job(worker_id):
- with app.app_context():
- # Find a queued job
- candidate = ProcessingJob.query.filter(
- ProcessingJob.status == 'queued',
- ProcessingJob.job_type == 'transcribe'
- ).first()
-
- if not candidate:
- return None
-
- # Atomic claim
- result = db.session.execute(
- update(ProcessingJob)
- .where(
- ProcessingJob.id == candidate.id,
- ProcessingJob.status == 'queued'
- )
- .values(status='processing', started_at=datetime.utcnow())
- )
-
- if result.rowcount == 1:
- db.session.commit()
- return candidate.id
- else:
- db.session.rollback()
- return None
-
- try:
- # Each "worker" claims one job
- for i in range(num_jobs + 2): # Extra attempts to ensure no double claims
- job_id = claim_any_job(i)
- if job_id:
- claimed_jobs.append(job_id)
- print(f" Worker {i} claimed job {job_id}")
- else:
- print(f" Worker {i} found no available jobs")
-
- print(f"\nClaimed jobs: {claimed_jobs}")
- print(f"Unique jobs claimed: {len(set(claimed_jobs))}")
-
- # Verify no duplicates
- assert len(claimed_jobs) == len(set(claimed_jobs)), "Duplicate job claims detected!"
- assert len(claimed_jobs) == num_jobs, f"Expected {num_jobs} claims, got {len(claimed_jobs)}"
-
- print("\n[PASS] All jobs claimed exactly once!")
- return True
- finally:
- # Cleanup must run even if the assertions above fail. Without this
- # the synthetic 'Test Distribution Recording N' rows leak into the
- # dev DB and show up in admin and user-facing recording lists.
- for job_id in job_ids:
- job = db.session.get(ProcessingJob, job_id)
- if job:
- db.session.delete(job)
- for rec_id in recording_ids:
- rec = db.session.get(Recording, rec_id)
- if rec:
- db.session.delete(rec)
+ claimed_jobs = []
+ try:
+ # Claim repeatedly via the real method until it returns None.
+ # Extra attempts beyond num_jobs must yield no further claims.
+ for i in range(num_jobs + 2):
+ claimed = job_queue._claim_next_job(['transcribe'], 'transcription')
+ if claimed is not None and claimed.id in job_ids:
+ claimed_jobs.append(claimed.id)
+ print(f" Attempt {i} claimed job {claimed.id}")
+ else:
+ # Could be None, or (in a shared dev DB) a pre-existing job from
+ # another user; ignore anything not in our created set.
+ print(f" Attempt {i} claimed no job of ours")
+
+ print(f"\nClaimed jobs: {claimed_jobs}")
+ print(f"Unique jobs claimed: {len(set(claimed_jobs))}")
+
+ assert len(claimed_jobs) == len(set(claimed_jobs)), "Duplicate job claims detected!"
+ assert len(claimed_jobs) == num_jobs, \
+ f"Expected {num_jobs} claims, got {len(claimed_jobs)}"
+
+ print("\n[PASS] All jobs claimed exactly once!")
+ finally:
+ # Cleanup must run even if the assertions above fail, so synthetic
+ # 'Test Distribution Recording N' rows don't leak into the dev DB.
+ with app.app_context():
+ for jid in job_ids:
+ j = db.session.get(ProcessingJob, jid)
+ if j:
+ db.session.delete(j)
+ for rid in recording_ids:
+ r = db.session.get(Recording, rid)
+ if r:
+ db.session.delete(r)
db.session.commit()
diff --git a/tests/test_json_standalone.py b/tests/test_json_standalone.py
deleted file mode 100644
index c08827bd..00000000
--- a/tests/test_json_standalone.py
+++ /dev/null
@@ -1,340 +0,0 @@
-#!/usr/bin/env python3
-"""
-Standalone test for JSON preprocessing functionality.
-Tests the safe_json_loads function with various malformed JSON scenarios.
-"""
-
-import json
-import re
-import ast
-from unittest.mock import Mock
-
-# Mock logger for testing
-class MockLogger:
- def warning(self, msg): print(f"WARNING: {msg}")
- def info(self, msg): print(f"INFO: {msg}")
- def debug(self, msg): print(f"DEBUG: {msg}")
- def error(self, msg): print(f"ERROR: {msg}")
-
-# Create mock app with logger
-class MockApp:
- logger = MockLogger()
-
-app = MockApp()
-
-def safe_json_loads(json_string, fallback_value=None):
- """
- Safely parse JSON with preprocessing to handle common LLM JSON formatting issues.
-
- Args:
- json_string (str): The JSON string to parse
- fallback_value: Value to return if parsing fails (default: None)
-
- Returns:
- Parsed JSON object or fallback_value if parsing fails
- """
- if not json_string or not isinstance(json_string, str):
- app.logger.warning(f"Invalid JSON input: {type(json_string)} - {json_string}")
- return fallback_value
-
- # Step 1: Clean the input string
- cleaned_json = json_string.strip()
-
- # Step 2: Extract JSON from markdown code blocks if present
- json_match = re.search(r'```(?:json)?\s*(.*?)\s*```', cleaned_json, re.DOTALL)
- if json_match:
- cleaned_json = json_match.group(1).strip()
-
- # Step 3: Try multiple parsing strategies
- parsing_strategies = [
- # Strategy 1: Direct parsing (for well-formed JSON)
- lambda x: json.loads(x),
-
- # Strategy 2: Fix common escape issues
- lambda x: json.loads(preprocess_json_escapes(x)),
-
- # Strategy 3: Use ast.literal_eval as fallback for simple cases
- lambda x: ast.literal_eval(x) if x.startswith(('{', '[')) else None,
-
- # Strategy 4: Extract JSON object/array using regex
- lambda x: json.loads(extract_json_object(x)),
- ]
-
- for i, strategy in enumerate(parsing_strategies):
- try:
- result = strategy(cleaned_json)
- if result is not None:
- if i > 0: # Log if we had to use a fallback strategy
- app.logger.info(f"JSON parsed successfully using strategy {i+1}")
- return result
- except (json.JSONDecodeError, ValueError, SyntaxError) as e:
- if i == 0: # Only log the first failure to avoid spam
- app.logger.debug(f"JSON parsing strategy {i+1} failed: {e}")
- continue
-
- # All strategies failed
- app.logger.error(f"All JSON parsing strategies failed for: {cleaned_json[:200]}...")
- return fallback_value
-
-def preprocess_json_escapes(json_string):
- """
- Preprocess JSON string to fix common escape issues from LLM responses.
- Uses a more sophisticated approach to handle nested quotes properly.
- """
- if not json_string:
- return json_string
-
- result = []
- i = 0
- in_string = False
- escape_next = False
- expecting_value = False # Track if we're expecting a value (after :)
-
- while i < len(json_string):
- char = json_string[i]
-
- if escape_next:
- # This character is escaped, add it as-is
- result.append(char)
- escape_next = False
- elif char == '\\':
- # This is an escape character
- result.append(char)
- escape_next = True
- elif char == ':' and not in_string:
- # We found a colon, next string will be a value
- result.append(char)
- expecting_value = True
- elif char == ',' and not in_string:
- # We found a comma, reset expecting_value
- result.append(char)
- expecting_value = False
- elif char == '"':
- if not in_string:
- # Starting a string
- in_string = True
- result.append(char)
- else:
- # We're in a string, check if this quote should be escaped
- # Look ahead to see if this is the end of the string value
- j = i + 1
- while j < len(json_string) and json_string[j].isspace():
- j += 1
-
- # For keys (not expecting_value), only end on colon
- # For values (expecting_value), end on comma, closing brace, or closing bracket
- if expecting_value:
- end_chars = ',}]'
- else:
- end_chars = ':'
-
- if j < len(json_string) and json_string[j] in end_chars:
- # This is the end of the string
- in_string = False
- result.append(char)
- if not expecting_value:
- # We just finished a key, next will be expecting value
- expecting_value = True
- else:
- # This is an inner quote that should be escaped
- result.append('\\"')
- else:
- result.append(char)
-
- i += 1
-
- return ''.join(result)
-
-def extract_json_object(text):
- """
- Extract the first complete JSON object or array from text using regex.
- """
- # Look for JSON object
- obj_match = re.search(r'\{.*\}', text, re.DOTALL)
- if obj_match:
- return obj_match.group(0)
-
- # Look for JSON array
- arr_match = re.search(r'\[.*\]', text, re.DOTALL)
- if arr_match:
- return arr_match.group(0)
-
- # Return original if no JSON structure found
- return text
-
-def run_comprehensive_test():
- """Run a comprehensive test with various malformed JSON examples."""
- print("🧪 Running comprehensive JSON preprocessing tests...\n")
-
- test_cases = [
- {
- "name": "Valid JSON",
- "input": '{"title": "Test", "summary": "Valid JSON"}',
- "should_succeed": True
- },
- {
- "name": "Unescaped quotes in title",
- "input": '{"title": "Meeting about "Project X"", "summary": "Discussion summary"}',
- "should_succeed": True
- },
- {
- "name": "Multiple unescaped quotes",
- "input": '{"title": "John said "Hello" and Mary replied "Hi there"", "summary": "Conversation log"}',
- "should_succeed": True
- },
- {
- "name": "Markdown code block",
- "input": '```json\n{"title": "Wrapped", "summary": "In code block"}\n```',
- "should_succeed": True
- },
- {
- "name": "Mixed quotes and apostrophes",
- "input": '{"title": "Alice\'s "big idea" presentation", "summary": "She said it\'s "revolutionary""}',
- "should_succeed": True
- },
- {
- "name": "JSON with newlines",
- "input": '{"title": "Multi-line", "summary": "Line 1\\nLine 2 with \\"quotes\\"\\nLine 3"}',
- "should_succeed": True
- },
- {
- "name": "LLM response with explanation",
- "input": '''Here's the JSON:
-```json
-{
- "title": "Q3 Planning",
- "summary": "We discussed the "new initiative" for next quarter."
-}
-```
-Hope this helps!''',
- "should_succeed": True
- },
- {
- "name": "Speaker identification with quotes",
- "input": '{"SPEAKER_00": "John Smith", "SPEAKER_01": "Jane "The Expert" Doe", "SPEAKER_02": "Bob"}',
- "should_succeed": True
- },
- {
- "name": "Completely malformed",
- "input": '{"title": "Test", "summary": this is not valid json at all}',
- "should_succeed": False
- },
- {
- "name": "Empty string",
- "input": "",
- "should_succeed": False
- }
- ]
-
- passed = 0
- failed = 0
-
- for i, test_case in enumerate(test_cases, 1):
- print(f"Test {i}: {test_case['name']}")
- print(f"Input: {test_case['input'][:100]}{'...' if len(test_case['input']) > 100 else ''}")
-
- try:
- result = safe_json_loads(test_case['input'], {"error": "fallback"})
-
- if test_case['should_succeed']:
- if result != {"error": "fallback"} and isinstance(result, (dict, list)):
- print("✅ PASSED - Successfully parsed JSON")
- print(f" Result: {result}")
- passed += 1
- else:
- print("❌ FAILED - Expected successful parsing but got fallback")
- failed += 1
- else:
- if result == {"error": "fallback"}:
- print("✅ PASSED - Correctly returned fallback for malformed JSON")
- passed += 1
- else:
- print("❌ FAILED - Expected fallback but got parsed result")
- print(f" Unexpected result: {result}")
- failed += 1
-
- except Exception as e:
- print(f"❌ FAILED - Exception occurred: {e}")
- failed += 1
-
- print("-" * 50)
-
- print(f"\n📊 Test Results: {passed} passed, {failed} failed")
- return failed == 0
-
-def test_preprocessing_function():
- """Test the preprocessing function directly."""
- print("\n🔧 Testing preprocessing function directly...\n")
-
- test_input = '{"title": "Meeting about "Project X"", "summary": "Discussion summary"}'
- print(f"Original: {test_input}")
-
- processed = preprocess_json_escapes(test_input)
- print(f"Processed: {processed}")
-
- try:
- result = json.loads(processed)
- print(f"✅ Successfully parsed: {result}")
- except json.JSONDecodeError as e:
- print(f"❌ Still failed: {e}")
-
-def test_specific_scenarios():
- """Test specific real-world scenarios."""
- print("\n🎯 Testing specific LLM response scenarios...\n")
-
- # Test case from the original issue
- gemini_response = '''{"title": "Meeting about "Project Phoenix" and budget allocation", "summary": "The team discussed John's "breakthrough idea" and Mary said "this will change everything" during the Q3 planning session."}'''
-
- print("Testing Gemini-style response with unescaped quotes:")
- print(f"Input: {gemini_response}")
-
- # Test preprocessing directly
- processed = preprocess_json_escapes(gemini_response)
- print(f"Processed: {processed}")
-
- result = safe_json_loads(gemini_response)
- if isinstance(result, dict) and "title" in result and "summary" in result:
- print("✅ SUCCESS - Parsed Gemini response correctly!")
- print(f"Title: {result['title']}")
- print(f"Summary: {result['summary'][:100]}...")
- else:
- print("❌ FAILED - Could not parse Gemini response")
- print(f"Result: {result}")
-
- print("-" * 50)
-
- # Test speaker identification scenario
- speaker_response = '''{"SPEAKER_00": "John "The Manager" Smith", "SPEAKER_01": "Alice Johnson", "SPEAKER_02": "Bob "Tech Lead" Wilson"}'''
-
- print("Testing speaker identification with quotes in names:")
- print(f"Input: {speaker_response}")
-
- # Test preprocessing directly
- processed = preprocess_json_escapes(speaker_response)
- print(f"Processed: {processed}")
-
- result = safe_json_loads(speaker_response)
- if isinstance(result, dict) and len(result) >= 3:
- print("✅ SUCCESS - Parsed speaker identification correctly!")
- for speaker, name in result.items():
- print(f" {speaker}: {name}")
- else:
- print("❌ FAILED - Could not parse speaker identification")
- print(f"Result: {result}")
-
-if __name__ == "__main__":
- print("🚀 Starting Standalone JSON Preprocessing Tests\n")
-
- # Test preprocessing function directly
- test_preprocessing_function()
-
- # Run the comprehensive test
- success = run_comprehensive_test()
-
- # Test specific scenarios
- test_specific_scenarios()
-
- if success:
- print("\n🎉 All tests completed successfully! JSON preprocessing should handle LLM response issues gracefully.")
- else:
- print("\n⚠️ Some tests failed. The implementation may need refinement.")