From dc16b4e418e2515929485d95f956b3765d0901c0 Mon Sep 17 00:00:00 2001 From: Mohammad Hijjawi Date: Sun, 11 Oct 2026 06:56:13 +0100 Subject: [PATCH] test(executor): pin repeat-conversion output path and source-stem fallback Run execute_conversion on a plan from build_conversion_plan and check that an existing book.wav stays untouched while the new output goes to book_2.wav, that a third run gets book_3.wav, that project mode checks its audio subfolder, and that a request without original_filename is named after the source file. Note both changes in CHANGELOG.md. --- CHANGELOG.md | 4 + tests/test_conversion_executor_unified.py | 95 +++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a613ab0..6d7df74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Unreleased +- Fixed repeat conversions overwriting the previous audiobook and its subtitles. The merged output now gets a `_2`, `_3`, ... suffix when a file with the same name already exists, issue mentioned in [#203](https://github.com/denizsafak/abogen/issues/203). +- When no original filename is set, the merged output is now named after the source file (e.g. `my_novel.wav`) instead of `output.wav`. + # 1.3.0 - Special thanks to [@jeremiahsb](https://github.com/jeremiahsb) for his [massive contribution](https://github.com/denizsafak/abogen/pull/120) (>55k lines!) that brought the Web UI, EPUB 3 pipeline, and core architectural improvements to life. - Added an EPUB 3 packaging pipeline that builds media-overlay EPUBs from generated audio and chunk metadata. diff --git a/tests/test_conversion_executor_unified.py b/tests/test_conversion_executor_unified.py index b700312..858647c 100644 --- a/tests/test_conversion_executor_unified.py +++ b/tests/test_conversion_executor_unified.py @@ -505,6 +505,101 @@ class TestExecuteConversion: assert result.metadata["author"] == "Author" +class TestRepeatConversionOutputPath: + """Repeat conversions must not overwrite an existing merged output.""" + + @staticmethod + def _run(req: ConversionRequest): + from abogen.application.conversion_planner import build_conversion_plan + + plan = build_conversion_plan(req) + return execute_conversion( + plan, + FakeEvents(), + FakePipelineProvider(), + FakeVoiceResolver(), + TTSContext(), + ) + + @staticmethod + def _request(output_dir: Path, **save_kwargs: Any) -> ConversionRequest: + return ConversionRequest( + direct_text="Hello world", + voice="M1", + save=SaveConfig( + mode="custom_folder", output_folder=output_dir, **save_kwargs + ), + original_filename="book.txt", + output_format="wav", + ) + + def test_existing_output_is_not_overwritten(self): + with tempfile.TemporaryDirectory() as tmpdir: + existing = Path(tmpdir) / "book.wav" + existing.write_bytes(b"previous output") + + result = self._run(self._request(Path(tmpdir))) + + assert result.audio_path == Path(tmpdir) / "book_2.wav" + assert result.audio_path.exists() + assert existing.read_bytes() == b"previous output" + + def test_third_conversion_gets_suffix_3(self): + with tempfile.TemporaryDirectory() as tmpdir: + first = Path(tmpdir) / "book.wav" + second = Path(tmpdir) / "book_2.wav" + first.write_bytes(b"first output") + second.write_bytes(b"second output") + + result = self._run(self._request(Path(tmpdir))) + + assert result.audio_path == Path(tmpdir) / "book_3.wav" + assert first.read_bytes() == b"first output" + assert second.read_bytes() == b"second output" + + def test_project_mode_checks_audio_subfolder(self, monkeypatch): + import functools + + from abogen.application import output_layout_service + from abogen.domain.output_paths import resolve_project_layout + + # Pin the project folder timestamp so both runs share one project folder. + monkeypatch.setattr( + output_layout_service, + "resolve_project_layout", + functools.partial( + resolve_project_layout, timestamp_fn=lambda: "20260101_000000" + ), + ) + with tempfile.TemporaryDirectory() as tmpdir: + req = self._request(Path(tmpdir), save_as_project=True) + + first = self._run(req) + first_bytes = first.audio_path.read_bytes() + second = self._run(req) + + assert first.audio_path.parent.name == "audio" + assert first.audio_path.name == "book.wav" + assert second.audio_path == first.audio_path.with_name("book_2.wav") + assert first.audio_path.read_bytes() == first_bytes + + def test_fallback_name_uses_source_stem(self): + """Without original_filename the merged file is named after the source.""" + with tempfile.TemporaryDirectory() as tmpdir: + source = Path(tmpdir) / "my_novel.txt" + source.write_text("Hello world", encoding="utf-8") + req = ConversionRequest( + source_path=source, + voice="M1", + save=SaveConfig(mode="save_next_to_input"), + output_format="wav", + ) + + result = self._run(req) + + assert result.audio_path == Path(tmpdir) / "my_novel.wav" + + class TestHeadingDedup: """Tests for heading dedup in executor."""