diff --git a/tests/test_audio_ocr_helpers.py b/tests/test_audio_ocr_helpers.py index 29988a7..da455e7 100644 --- a/tests/test_audio_ocr_helpers.py +++ b/tests/test_audio_ocr_helpers.py @@ -1,5 +1,7 @@ +from types import SimpleNamespace + from llm_workflows.audio_text import ( - convert_time_to_srt_format, create_multiline_srt, split_line_by_char_limit, + convert_time_to_srt_format, create_multiline_srt, format_segments, split_line_by_char_limit, ) from utils.full_multi_updated2 import detect_languages_in_text, save_results @@ -22,6 +24,35 @@ def test_multiline_srt_file(tmp_path): assert out.read_text(encoding="utf-8") == "1\n00:00:00,000 --> 00:00:01,250\nhello world\nagain\n\n" +def test_format_segments_handles_empty_and_optional_word_timestamps(): + assert format_segments([]) == ([], [], "") + + segments = [ + SimpleNamespace(text=" first sentence ", start=0.0, end=1.2), + SimpleNamespace( + text=" second sentence ", + start=1.2, + end=2.5, + words=[ + SimpleNamespace(word=" first ", start=1.2, end=1.5), + SimpleNamespace(word=" second ", start=1.6, end=2.0), + ], + ), + ] + + sentences, words, transcript = format_segments(segments) + + assert sentences == [ + {"id": 0, "text": "first sentence", "start": 0.0, "end": 1.2}, + {"id": 1, "text": "second sentence", "start": 1.2, "end": 2.5}, + ] + assert words == [ + {"word": "first", "start": 1.2, "end": 1.5}, + {"word": "second", "start": 1.6, "end": 2.0}, + ] + assert transcript == "first sentence second sentence" + + def test_detect_languages(): assert detect_languages_in_text("hello") == ["Latin_Script"] assert detect_languages_in_text("ଓଡ଼ିଆ") == ["Odia"]