Files
aza/AzA march 2026/_test_translator_start_and_presentation_tile.py
2026-06-13 22:47:31 +02:00

146 lines
5.5 KiB
Python

# -*- coding: utf-8 -*-
"""Tests: Übersetzer-Start (bad screen distance) + Presentation-Maker-Platzhalter."""
from __future__ import annotations
import inspect
import os
import sys
import tkinter as tk
import unittest
from unittest import mock
import translate
import aza_addon_shell
class TestTranslatorStartFix(unittest.TestCase):
def setUp(self):
translate._clear_main_root()
def tearDown(self):
translate._clear_main_root()
for w in list(tk._default_root.winfo_children()) if tk._default_root else []:
try:
w.destroy()
except Exception:
pass
def test_addon_translator_callback_exists(self):
src = inspect.getsource(basis14.KGDesktopApp._open_uebersetzer)
self.assertIn("translate.main(parent=self)", src)
def test_no_tuple_pady_on_tk_frame_in_translate_main(self):
src = inspect.getsource(translate.main)
self.assertNotIn("tk.Frame(root, bg=_BG, padx=14, pady=(0, 4))", src)
self.assertNotIn("tk.Frame(root, bg=_BG, padx=14, pady=(0, 8))", src)
self.assertIn("umkehr_row.pack(fill=\"x\", padx=14, pady=(0, 4))", src)
self.assertIn("btn_frame.pack(fill=\"x\", padx=14, pady=(0, 8))", src)
def test_translator_window_instantiates_without_bad_screen_distance(self):
root = tk.Tk()
root.withdraw()
try:
with mock.patch.object(translate, "load_dotenv"), \
mock.patch("aza_ai_client.get_ai_client", return_value=object()), \
mock.patch.object(translate, "load_main_geometry", return_value="800x600"), \
mock.patch.object(translate, "load_main_languages", return_value=("de", "en")), \
mock.patch("aza_global_paste.start_global_right_click_paste_listener"), \
mock.patch.object(translate, "AudioRecorder"):
win = translate.main(parent=root)
self.assertIsNotNone(win)
self.assertTrue(win.winfo_exists())
finally:
translate._clear_main_root()
try:
root.destroy()
except Exception:
pass
def test_second_start_reuses_singleton(self):
root = tk.Tk()
root.withdraw()
patches = [
mock.patch.object(translate, "load_dotenv"),
mock.patch("aza_ai_client.get_ai_client", return_value=object()),
mock.patch.object(translate, "load_main_geometry", return_value="800x600"),
mock.patch.object(translate, "load_main_languages", return_value=("de", "en")),
mock.patch("aza_global_paste.start_global_right_click_paste_listener"),
mock.patch.object(translate, "AudioRecorder"),
]
try:
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
first = translate.main(parent=root)
second = translate.main(parent=root)
self.assertIs(first, second)
finally:
translate._clear_main_root()
try:
root.destroy()
except Exception:
pass
def test_focus_existing_translator_brings_window_front(self):
root = tk.Tk()
root.withdraw()
fake = tk.Toplevel(root)
translate._MAIN_ROOT = fake
with mock.patch("aza_ui_helpers.bring_tool_window_to_front") as bring, \
mock.patch("aza_ui_helpers.center_tool_window"):
out = translate.focus_existing_translator(parent=root)
self.assertIs(out, fake)
bring.assert_called_once()
translate._clear_main_root()
fake.destroy()
root.destroy()
class TestPresentationMakerTile(unittest.TestCase):
def test_four_tiles_in_addon_shell(self):
src = open(
os.path.join(os.path.dirname(__file__), "aza_addon_shell.py"),
encoding="utf-8",
).read()
self.assertIn("title=\"Diktat\"", src)
self.assertIn("title=\"Chat\"", src)
self.assertIn("title=\"Übersetzer\"", src)
self.assertIn("title=\"Presentation Maker\"", src)
def test_presentation_maker_disabled(self):
src = open(
os.path.join(os.path.dirname(__file__), "aza_addon_shell.py"),
encoding="utf-8",
).read()
self.assertIn("_make_tile_disabled", src)
self.assertIn("In Vorbereitung", src)
self.assertIn("open_presentation_maker_placeholder", src)
block = src.split("def _make_tile_disabled", 1)[1].split("\ndef ", 1)[0]
self.assertNotIn("Button-1", block)
def test_active_tiles_still_wired(self):
src = open(
os.path.join(os.path.dirname(__file__), "aza_addon_shell.py"),
encoding="utf-8",
).read()
self.assertIn('_safe_call(app, "open_diktat_window")', src)
self.assertIn('_safe_call(app, "_send_to_empfang")', src)
self.assertIn('_safe_call(app, "_open_uebersetzer")', src)
def test_horizontal_single_row_layout(self):
src = open(
os.path.join(os.path.dirname(__file__), "aza_addon_shell.py"),
encoding="utf-8",
).read()
self.assertIn(".grid(row=0, column=0", src)
self.assertIn(".grid(row=0, column=3", src)
self.assertNotIn(".grid(row=1, column=", src)
def test_placeholder_callback_is_noop(self):
self.assertIsNone(aza_addon_shell.open_presentation_maker_placeholder())
import basis14 # noqa: E402 — after test class defs that reference KGDesktopApp
if __name__ == "__main__":
unittest.main()