-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Schreibstil-Presets ins Tray-Dropdown integrieren (Paket F) #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """Tests für das Tray-Submenu „Schreibstil-Vorlage" (Paket F). | ||
|
|
||
| Deckt das Zusammenspiel von Preset-Auswahl im Tray, Config-Persistenz und | ||
| LLM-Service-Neuaufbau ab: | ||
|
|
||
| * Auswahl im Submenu persistiert den Preset und baut den LLMService neu. | ||
| * Das Menü spiegelt jederzeit die ``config.writing_preset`` (Häkchen). | ||
| * Ein Settings-Save mit geändertem Preset gleicht das Häkchen wieder an. | ||
|
|
||
| GUI-gated über ``WHISPER_GUI_TESTS=1``: die echte ``BlitztextApp`` baut Tray + | ||
| QActionGroup auf und benötigt eine (Offscreen-)QApplication, analog zu | ||
| ``tests/test_state_machine.py``. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from app.config import BlitztextConfig | ||
| from app.writing_presets import DEFAULT_PRESET_KEY, WRITING_PRESET_KEYS | ||
|
|
||
| _GUI = os.environ.get("WHISPER_GUI_TESTS") == "1" | ||
| gui_only = pytest.mark.skipif(not _GUI, reason="benötigt WHISPER_GUI_TESTS=1 (Display)") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def tray_app(tmp_path): | ||
| """Echte App mit isolierter Config (tmp), ohne evdev-Thread. | ||
|
|
||
| Die Config wird nach dem Boot auf ein tmp-Verzeichnis umgehängt, damit der | ||
| Handler ``config.save()`` nie in die echte User-Config schreibt. Das | ||
| Preset-Menü wird danach neu synchronisiert. | ||
| """ | ||
| from PyQt6.QtWidgets import QApplication | ||
| from app.blitztext_linux import BlitztextApp | ||
|
|
||
| qapp = QApplication.instance() or QApplication([]) | ||
| app = BlitztextApp(qapp) | ||
| app.stop_hotkey_worker() # kein echter evdev-Thread im Test | ||
|
|
||
| app.config = BlitztextConfig(config_dir=tmp_path / ".config" / "blitztext-linux") | ||
| app._refresh_preset_menu() | ||
| yield app | ||
| app.stop_hotkey_worker() | ||
|
|
||
|
|
||
| def _other_key(current: str) -> str: | ||
| """Liefert einen vom aktuellen verschiedenen, gültigen Preset-Schlüssel.""" | ||
| return next(k for k in WRITING_PRESET_KEYS if k != current) | ||
|
|
||
|
|
||
| @gui_only | ||
| class TestPresetMenu: | ||
| def test_handler_persists_and_rebuilds_service(self, tray_app): | ||
| """Auswahl im Tray: Config gesetzt + gespeichert + Service neu gebaut.""" | ||
| target = _other_key(tray_app.config.writing_preset) | ||
| old_service = tray_app.llm_service | ||
|
|
||
| tray_app._on_writing_preset_selected(target) | ||
|
|
||
| assert tray_app.config.writing_preset == target | ||
| # Auf Platte persistiert: frische Instanz liest denselben Preset. | ||
| reloaded = BlitztextConfig(config_dir=tray_app.config.config_dir) | ||
| assert reloaded.writing_preset == target | ||
| # Service wurde neu konstruiert. | ||
| assert tray_app.llm_service is not old_service | ||
|
|
||
| def test_handler_noop_when_same_preset(self, tray_app): | ||
| """Erneute Auswahl des aktiven Presets schreibt nicht erneut (No-Op).""" | ||
| current = tray_app.config.writing_preset | ||
| old_service = tray_app.llm_service | ||
|
|
||
| tray_app._on_writing_preset_selected(current) | ||
|
|
||
| assert tray_app.config.writing_preset == current | ||
| assert tray_app.llm_service is old_service | ||
| # Kein Disk-Write erfolgt -> keine Config-Datei angelegt. | ||
| assert not tray_app.config.config_file.is_file() | ||
|
|
||
| def test_menu_mirrors_config(self, tray_app): | ||
| """Das Häkchen folgt der Config nach ``_refresh_preset_menu``.""" | ||
| target = _other_key(tray_app.config.writing_preset) | ||
| tray_app.config.writing_preset = target | ||
|
|
||
| tray_app._refresh_preset_menu() | ||
|
|
||
| assert tray_app.preset_actions[target].isChecked() is True | ||
| for key, action in tray_app.preset_actions.items(): | ||
| if key != target: | ||
| assert action.isChecked() is False | ||
|
|
||
| def test_refresh_falls_back_to_standard_for_unknown(self, tray_app): | ||
| """Ein nicht im Menü vorhandener Wert wählt den Standard-Preset.""" | ||
| # Direkter Eingriff am Backing-Store umgeht die Setter-Validierung, | ||
| # um einen "verwaisten" Config-Wert zu simulieren. | ||
| tray_app.config._data["workflows"]["writing_preset"] = "gibt_es_nicht" | ||
|
|
||
| tray_app._refresh_preset_menu() | ||
|
|
||
| assert tray_app.preset_actions[DEFAULT_PRESET_KEY].isChecked() is True | ||
|
|
||
| def test_settings_save_updates_check(self, tray_app, monkeypatch): | ||
| """Settings-Save mit geändertem Preset gleicht das Häkchen an.""" | ||
| from PyQt6.QtWidgets import QDialog | ||
| import app.blitztext_linux as mod | ||
|
|
||
| target = _other_key(tray_app.config.writing_preset) | ||
|
|
||
| class _FakeDialog: | ||
| def __init__(self, config): | ||
| # Dialog "speichert" den neuen Preset in dieselbe Config. | ||
| config.writing_preset = target | ||
|
|
||
| def exec(self): | ||
| return QDialog.DialogCode.Accepted | ||
|
|
||
| monkeypatch.setattr(mod, "SettingsDialog", _FakeDialog) | ||
|
|
||
| tray_app.show_settings_dialog() | ||
|
|
||
| assert tray_app.preset_actions[target].isChecked() is True | ||
|
|
||
| def test_submenu_disabled_when_llm_unavailable(self, tray_app, monkeypatch): | ||
| """Ohne nutzbaren LLM-Dienst wird das Preset-Submenu deaktiviert.""" | ||
| monkeypatch.setattr(tray_app.llm_service, "is_available", lambda: False) | ||
|
|
||
| tray_app.update_menu_availability() | ||
|
|
||
| assert tray_app.menu_preset.isEnabled() is False | ||
|
|
||
| def test_submenu_enabled_when_llm_available(self, tray_app, monkeypatch): | ||
| """Mit nutzbarem LLM-Dienst ist das Preset-Submenu auswählbar.""" | ||
| monkeypatch.setattr(tray_app.llm_service, "is_available", lambda: True) | ||
|
|
||
| tray_app.update_menu_availability() | ||
|
|
||
| assert tray_app.menu_preset.isEnabled() is True |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the config directory is unwritable or the atomic write fails,
Config.save()raises from this QAction slot after the QAction has already become checked but before the LLM service is rebuilt. In that environment the tray can show a newly selected preset that was neither persisted nor applied, with only an uncaught Qt-slot exception instead of the error handling used by the settings dialog; catch the save error and refresh/revert the menu state or notify the user.Useful? React with 👍 / 👎.