|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +"""`subtitle.create` has to say whether it made a file. |
| 3 | +
|
| 4 | +It used to return `None` when faster-whisper was unavailable and nothing at all |
| 5 | +on success, and the caller ignored both — so a machine without whisper ran |
| 6 | +`subtitle.correct()` over a path that was never written. That does not raise: |
| 7 | +`file_to_subtitles` answers `[]` for a missing file, so the run finishes with no |
| 8 | +subtitles, no error, and a log line saying "correcting subtitle" as though it |
| 9 | +had worked. A silent nothing is the worst of the available failures. |
| 10 | +
|
| 11 | +From upstream PR harry0703/MoneyPrinterTurbo#1244, which is the only one of the |
| 12 | +seven open there that still applies to this fork. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import app.services.subtitle as subtitle |
| 18 | + |
| 19 | + |
| 20 | +def test_create_returns_empty_when_whisper_is_unavailable(monkeypatch): |
| 21 | + monkeypatch.setattr(subtitle, "WhisperModel", None) |
| 22 | + assert subtitle.create(audio_file="does-not-matter.wav", subtitle_file="out.srt") == "" |
| 23 | + |
| 24 | + |
| 25 | +def test_create_is_annotated_as_returning_a_path(): |
| 26 | + """The annotation is the contract the caller now relies on.""" |
| 27 | + assert subtitle.create.__annotations__.get("return") is str |
| 28 | + |
| 29 | + |
| 30 | +def test_the_caller_stops_rather_than_correcting_a_file_that_was_never_written(monkeypatch, tmp_path): |
| 31 | + """The point of the return value: `correct()` must not run on nothing.""" |
| 32 | + from app.services import task |
| 33 | + |
| 34 | + corrected: list = [] |
| 35 | + monkeypatch.setattr(task.subtitle, "create", lambda **_: "") |
| 36 | + monkeypatch.setattr(task.subtitle, "correct", lambda **kw: corrected.append(kw)) |
| 37 | + # The provider is read from `config.app`, NOT from params. Setting it on the |
| 38 | + # params stub instead left this test taking the `edge` branch and returning |
| 39 | + # early, so it passed with the fix and without it — a test that guarded |
| 40 | + # nothing while looking like it guarded the thing it was named for. |
| 41 | + monkeypatch.setitem(task.config.app, "subtitle_provider", "whisper") |
| 42 | + |
| 43 | + params = type("P", (), {"subtitle_enabled": True})() |
| 44 | + result = task.generate_subtitle( |
| 45 | + task_id="t", params=params, video_script="a script", |
| 46 | + sub_maker=None, audio_file=str(tmp_path / "a.wav")) |
| 47 | + |
| 48 | + assert result == "" |
| 49 | + assert corrected == [], "correct() ran over a subtitle file that was never created" |
0 commit comments