"""Тесты долговечных сессий и checkpoint-файлов.""" from datetime import UTC, datetime from decimal import Decimal from voice_dictation.core.models import TranscriptionResult from voice_dictation.core.paths import AppPaths from voice_dictation.core.session_store import SessionStore def test_chunk_checkpoint_roundtrip(tmp_paths: AppPaths) -> None: store = SessionStore(tmp_paths.sessions_dir) audio_path = store.create() audio_path.write_bytes(b"audio") store.mark_recorded(audio_path, 12.5) expected = TranscriptionResult( text="полный текст чанка", duration_sec=12.5, model="gpt-4o-mini-transcribe", language="ru", prompt_used="словарь", requested_at=datetime.now(tz=UTC), cost_usd=Decimal("0.00123"), ) store.save_chunk_result(audio_path.parent, 0, expected) actual = store.load_chunk_result(audio_path.parent, 0) assert actual == expected assert store.pending() == [audio_path.parent] def test_completed_session_is_not_pending(tmp_paths: AppPaths) -> None: store = SessionStore(tmp_paths.sessions_dir) audio_path = store.create() audio_path.write_bytes(b"audio") store.mark_recorded(audio_path, 1.0) store.complete(audio_path.parent, "готово", ()) assert store.pending() == [] assert (audio_path.parent / "transcript.txt").read_text(encoding="utf-8") == "готово" def test_interrupted_recording_is_pending_when_wav_exists(tmp_paths: AppPaths) -> None: store = SessionStore(tmp_paths.sessions_dir) audio_path = store.create() audio_path.write_bytes(b"RIFF interrupted audio") assert store.pending() == [audio_path.parent] def test_custom_storage_retains_discovery_after_location_changes(tmp_path): from voice_dictation.core.session_store import SessionStore default = tmp_path / "default" custom = tmp_path / "external" later = tmp_path / "later" store = SessionStore(default) old_audio = store.create() old_audio.write_bytes(b"synthetic") custom_audio = store.create(custom) custom_audio.write_bytes(b"synthetic") assert custom_audio.parent.parent == custom.resolve() last_audio = store.create(later) last_audio.write_bytes(b"synthetic") recovered = SessionStore(default).pending() assert set(recovered) == {old_audio.parent, custom_audio.parent, last_audio.parent} def test_final_output_separate_from_raw_transcript(tmp_path): from voice_dictation.core.session_store import SessionStore store = SessionStore(tmp_path) audio = store.create() store.complete(audio.parent, "raw", ()) store.save_final_output(audio.parent, "processed", 1.2, str(audio)) assert (audio.parent / "transcript.txt").read_text() == "raw" assert (audio.parent / "final.txt").read_text() == "processed" assert store.read(audio.parent)["processing_sec"] == 1.2 def test_custom_storage_never_cleans_unrelated_directories(tmp_path): import json store = SessionStore(tmp_path / "default") custom = tmp_path / "custom" store.create(custom) unrelated = custom / "another-app" unrelated.mkdir() (unrelated / "source.wav").write_bytes(b"unrelated") (unrelated / "manifest.json").write_text( json.dumps({"status": "completed", "completed_at": "2000-01-01T00:00:00+00:00"}) ) assert store.cleanup_completed_audio(1) == 0 assert (unrelated / "source.wav").read_bytes() == b"unrelated"