from concurrent.futures import Future, ThreadPoolExecutor import pytest from psi.runtime.swa_speech import SwaSpeechWorker, IncompleteSpeechError def worker(error): result=SwaSpeechWorker.__new__(SwaSpeechWorker) result.pool=ThreadPoolExecutor(1) good,bad=Future(),Future() good.set_result({'text':'complete answer'}) bad.set_exception(error) result.jobs=[good,bad] return result def test_keep_completed_answers_but_explicitly_report_codec_limit(): speech=worker(IncompleteSpeechError('codec limit')) assert speech.finish(allow_incomplete=True)==[{'text':'complete answer'}] assert speech.failures==[{'ordinal':1,'error':'codec limit'}] def test_legacy_strict_behavior_remains(): speech=worker(IncompleteSpeechError('codec limit')) with pytest.raises(IncompleteSpeechError):speech.finish() def test_model_or_cache_failure_is_never_swallowed(): speech=worker(RuntimeError('bad KV')) with pytest.raises(RuntimeError,match='bad KV'):speech.finish(allow_incomplete=True)