Files
031-flash-card/tests/unit/test_app_speech.py
T
Tirsvad f45d6036e4 MIL-008: Choose level
Review record RC-041 (Go, autonomous mode, not independent).

Task: MIL-008#1
Task: MIL-008#2
Task: MIL-008#3
Task: MIL-008#4
Task: MIL-008#5
Task: MIL-008#6
Task: MIL-008#7
Task: MIL-008#8
2026-10-14 23:58:31 +08:00

79 lines
2.2 KiB
Python

"""Tests of the speech wiring in the entry point."""
import importlib.util
from importlib.machinery import ModuleSpec
from pathlib import Path
import pytest
from flash_card.domain.value_objects.level import Level
from flash_card.infrastructure.speech.gtts_speech import GttsSpeech
from flash_card.infrastructure.speech.null_speech import NullSpeech
from flash_card.presentation import app
class _FakeRoot:
def mainloop(self) -> None:
return None
class _FakeApplication:
root = _FakeRoot()
exit_code = 0
def _found(name: str) -> ModuleSpec:
return ModuleSpec(name, None)
def test_build_speech_returns_null_speech_when_disabled(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(importlib.util, "find_spec", _found)
assert isinstance(app.build_speech(False), NullSpeech)
def test_build_speech_returns_null_speech_when_packages_are_missing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(importlib.util, "find_spec", lambda name: None)
assert isinstance(app.build_speech(True), NullSpeech)
def test_build_speech_returns_null_speech_when_one_package_is_missing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
importlib.util,
"find_spec",
lambda name: None if name == "playsound" else _found(name),
)
assert isinstance(app.build_speech(True), NullSpeech)
def test_build_speech_returns_gtts_speech_when_both_packages_are_found(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(importlib.util, "find_spec", _found)
assert isinstance(app.build_speech(True), GttsSpeech)
def test_main_passes_speech_disabled_with_no_speech_option(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
seen: list[bool] = []
def fake_create(
data_dir: Path | None = None,
speech_enabled: bool = True,
level: Level | None = None,
) -> _FakeApplication:
seen.append(speech_enabled)
return _FakeApplication()
monkeypatch.setattr(app, "create_application", fake_create)
assert app.main(["--data-dir", str(tmp_path), "--no-speech"]) == 0
assert app.main(["--data-dir", str(tmp_path)]) == 0
assert seen == [False, True]