Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Fix JSON parser greedy extraction and newline handling, update video retention test patterns
Murtaza Nasir30f147823c59
diff --git a/src/utils/json_parser.py b/src/utils/json_parser.pyindex f98533a6..116ac25d 100644--- a/src/utils/json_parser.py+++ b/src/utils/json_parser.py@@ -131,6 +131,15 @@ def preprocess_json_escapes(json_string):else:# This is an inner quote that should be escapedresult.append('\\"')+ elif char == '\n' and in_string:+ # Literal newlines inside JSON strings must be escaped+ result.append('\\n')+ elif char == '\r' and in_string:+ # Literal carriage returns inside JSON strings must be escaped+ result.append('\\r')+ elif char == '\t' and in_string:+ # Literal tabs inside JSON strings must be escaped+ result.append('\\t')else:result.append(char)@@ -141,20 +150,56 @@ def preprocess_json_escapes(json_string):def extract_json_object(text):"""- Extract the first complete JSON object or array from text using regex.+ Extract the first complete JSON object or array from text using brace counting.+ This is more robust than regex as it properly handles nested structures and+ doesn't over-match when there's text after the JSON."""- # 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+ # Find the first { or [ that starts a JSON structure+ start = None+ close_char = None+ for i, ch in enumerate(text):+ if ch == '{':+ start = i+ close_char = '}'+ break+ elif ch == '[':+ start = i+ close_char = ']'+ break++ if start is None:+ return text++ # Walk through counting braces/brackets, respecting strings+ depth = 0+ in_string = False+ escape_next = False++ for i in range(start, len(text)):+ ch = text[i]++ if escape_next:+ escape_next = False+ continue++ if ch == '\\' and in_string:+ escape_next = True+ continue++ if ch == '"':+ in_string = not in_string+ continue++ if not in_string:+ if ch in ('{', '['):+ depth += 1+ elif ch in ('}', ']'):+ depth -= 1+ if depth == 0:+ return text[start:i + 1]++ # If we never balanced, return from start to end (best effort)+ return text[start:]def safe_json_loads(json_string, fallback_value=None):diff --git a/tests/test_video_retention.py b/tests/test_video_retention.pyindex 5428d57b..c4a25e70 100644--- a/tests/test_video_retention.py+++ b/tests/test_video_retention.py@@ -109,7 +109,7 @@ class TestUploadHandlerVideoRetention(unittest.TestCase):def test_upload_handler_skips_conversion_for_video_retention(self):"""Upload handler skips convert_if_needed for videos when retention is on."""- self.assertIn('VIDEO_RETENTION and has_video', self.content)+ self.assertIn('(VIDEO_RETENTION or VIDEO_PASSTHROUGH_ASR) and has_video', self.content)self.assertIn('skipping conversion', self.content)def test_upload_handler_has_video_from_codec_info(self):@@ -138,8 +138,8 @@ class TestFileMonitorVideoRetention(unittest.TestCase):def test_video_retention_skips_conversion(self):"""When VIDEO_RETENTION=True and has_video=True, convert_if_needed is skipped."""- # Should have the guard: if VIDEO_RETENTION and has_video: ... skip conversion- self.assertIn('VIDEO_RETENTION and has_video', self.content)+ # Should have the guard: if (VIDEO_PASSTHROUGH_ASR or VIDEO_RETENTION) and has_video: ... skip conversion+ self.assertIn('VIDEO_PASSTHROUGH_ASR or VIDEO_RETENTION) and has_video', self.content)self.assertIn('skipping conversion', self.content)def test_no_double_extraction(self):@@ -150,7 +150,7 @@ class TestFileMonitorVideoRetention(unittest.TestCase):found_convert_in_else = Falsefor i, line in enumerate(lines):- if 'VIDEO_RETENTION and has_video' in line and 'if' in line:+ if 'VIDEO_PASSTHROUGH_ASR or VIDEO_RETENTION) and has_video' in line and 'if' in line:in_retention_skip_block = Trueelif in_retention_skip_block and 'else:' in line:in_retention_skip_block = False@@ -194,12 +194,12 @@ class TestSendFileConditional(unittest.TestCase):"""Streaming send_file in recordings.py has conditional=True."""content = self._read_file('src/api/recordings.py')# Find the non-download send_file call- self.assertIn('send_file(recording.audio_path, conditional=True)', content)+ self.assertIn('send_file(recording.audio_path, mimetype=recording.mime_type, conditional=True)', content)def test_recordings_download_has_conditional(self):"""Download send_file in recordings.py has conditional=True."""content = self._read_file('src/api/recordings.py')- self.assertIn('as_attachment=True, download_name=filename, conditional=True', content)+ self.assertIn('as_attachment=True, download_name=filename, mimetype=recording.mime_type, conditional=True', content)def test_shares_has_conditional(self):"""send_file in shares.py has conditional=True."""@@ -331,7 +331,7 @@ class TestVideoRetentionMatrix(unittest.TestCase):def test_file_monitor_has_both_branches(self):"""file_monitor.py has both video retention skip and normal conversion paths."""content = self._read_file('src/file_monitor.py')- self.assertIn('VIDEO_RETENTION and has_video', content)+ self.assertIn('VIDEO_PASSTHROUGH_ASR or VIDEO_RETENTION) and has_video', content)# convert_if_needed should still exist in the else pathself.assertIn('convert_if_needed(', content)@@ -348,8 +348,8 @@ class TestVideoRetentionMatrix(unittest.TestCase):def test_all_three_entry_points_skip_for_video_retention(self):"""All entry points (upload, file monitor, processing) handle VIDEO_RETENTION."""for rel_path, marker in [- ('src/api/recordings.py', 'VIDEO_RETENTION and has_video'),- ('src/file_monitor.py', 'VIDEO_RETENTION and has_video'),+ ('src/api/recordings.py', '(VIDEO_RETENTION or VIDEO_PASSTHROUGH_ASR) and has_video'),+ ('src/file_monitor.py', '(VIDEO_PASSTHROUGH_ASR or VIDEO_RETENTION) and has_video'),('src/tasks/processing.py', 'if VIDEO_RETENTION:'),]:content = self._read_file(rel_path)