from pathlib import Path from types import SimpleNamespace from voice_transcriptor.models import AppSettings, MediaInfo from voice_transcriptor.ui.main_window import MainWindow from voice_transcriptor.services.transcription import TranscriptionProgress class Repository: def __init__(self, root: Path): self.settings = AppSettings("model", "pt-BR", root) def load(self): return self.settings, None def save(self, settings): self.settings = settings class Credentials: def set_api_key(self, value): pass def get_api_key(self): return "test-key" class Probe: def probe(self, path): return MediaInfo(path, 10, 60.0, "aac", "60", True, False) class Preprocessor: def preprocess(self, *args, **kwargs): raise AssertionError("not started") def test_main_window_exposes_progress_and_cancel_controls(qtbot, tmp_path: Path) -> None: window = MainWindow(Probe(), Preprocessor(), Repository(tmp_path), Credentials()) qtbot.addWidget(window) assert window.prepare_button.text() == "Transcribe" assert window.cancel_button.text() == "Cancel" assert window.cancel_button.isEnabled() is False assert window.progress_bar.value() == 0 assert window.chunk_progress_label.text() == "0 / 0 chunks" assert window.current_chunk_label.text() == "Current chunk: —" assert window.elapsed_label.text() == "Elapsed: 00:00:00" def test_select_file_populates_media_and_enables_preprocessing(qtbot, tmp_path: Path) -> None: source = tmp_path / "recording.mp3"; source.write_bytes(b"x") window = MainWindow(Probe(), Preprocessor(), Repository(tmp_path), Credentials()) qtbot.addWidget(window) window.select_file(source) assert window.selected_media.path == source assert window.prepare_button.isEnabled() is True assert "recording.mp3" in window.file_label.text() def test_cancel_button_cancels_active_token(qtbot, tmp_path: Path) -> None: window = MainWindow(Probe(), Preprocessor(), Repository(tmp_path), Credentials()) qtbot.addWidget(window) window._begin_busy_state() window.cancel_preprocessing() assert window._cancellation_token.cancelled is True def test_transcription_progress_updates_required_status_fields(qtbot, tmp_path: Path) -> None: window = MainWindow(Probe(), Preprocessor(), Repository(tmp_path), Credentials()) qtbot.addWidget(window) window._on_progress(TranscriptionProgress(2, 5, 3, 65.0, "retrying", "Rate limited", "OpenAI API transient error (HTTP 429).")) assert window.chunk_progress_label.text() == "2 / 5 chunks" assert window.current_chunk_label.text() == "Current chunk: 3" assert window.elapsed_label.text() == "Elapsed: 00:01:05" assert "HTTP 429" in window.log.toPlainText() def test_transcription_job_runs_through_worker_and_restores_controls(qtbot, tmp_path: Path) -> None: source = tmp_path / "recording.mp3"; source.write_bytes(b"x") transcript = tmp_path / "transcript.txt" class DurablePreprocessor: def preprocess(self, *args, **kwargs): manifest = tmp_path / "manifest.json"; manifest.write_text("{}", encoding="utf-8") return SimpleNamespace(manifest_path=manifest) class Transcriber: def run(self, manifest_path, settings, token, progress): progress(TranscriptionProgress(1, 1, 1, 2.0, "transcribing", "Completed chunk 1 of 1")) transcript.write_text("Olá\n", encoding="utf-8") return transcript window = MainWindow(Probe(), DurablePreprocessor(), Repository(tmp_path), Credentials(), lambda key: Transcriber()) qtbot.addWidget(window); window.select_file(source); window.prepare_button.click() qtbot.waitUntil(lambda: "Transcription complete" in window.log.toPlainText()) assert window.progress_bar.value() == 100 assert window.browse_button.isEnabled() assert not window.cancel_button.isEnabled()