"""Smoke tests for AI outfit panel defaults (offscreen Qt).""" import os import shutil import sys import tempfile import unittest from pathlib import Path os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") sys.path.insert(0, str(Path(__file__).parent.parent / "src")) import services.file_service as fs from services.config_service import DEFAULT_CONFIG class TestAiOutfitPanelDefaults(unittest.TestCase): def setUp(self): self.tmp = Path(tempfile.mkdtemp()) self._env = os.environ.get("CMBOT_DATA_DIR") os.environ["CMBOT_DATA_DIR"] = str(self.tmp / "data") self.app_dir = self.tmp / "app" self.app_dir.mkdir(parents=True) self._orig_get_app_dir = fs.get_app_dir fs.get_app_dir = lambda: self.app_dir self.app = self._app() def tearDown(self): fs.get_app_dir = self._orig_get_app_dir if self._env is None: os.environ.pop("CMBOT_DATA_DIR", None) else: os.environ["CMBOT_DATA_DIR"] = self._env shutil.rmtree(str(self.tmp), ignore_errors=True) def _app(self): from PySide6.QtWidgets import QApplication app = QApplication.instance() if app is None: app = QApplication([]) return app def _panel(self): from app.widgets.ai_outfit_panel import AiOutfitPanel return AiOutfitPanel() def test_empty_config_defaults_to_outfit_output_dir(self): panel = self._panel() cfg = dict(DEFAULT_CONFIG) cfg["outfit_output_dir"] = "" panel.apply_config(cfg) self.assertEqual(Path(panel._output_edit.text()), self.app_dir / "穿搭图片") def test_saved_outfit_output_dir_takes_precedence(self): panel = self._panel() saved = self.tmp / "custom-output" cfg = dict(DEFAULT_CONFIG) cfg["outfit_output_dir"] = str(saved) panel.apply_config(cfg) self.assertEqual(Path(panel._output_edit.text()), saved) def test_generation_setting_label_is_image_concurrency(self): from PySide6.QtWidgets import QLabel panel = self._panel() labels = [label.text() for label in panel.findChildren(QLabel)] self.assertIn("图片并发数", labels) def test_title_group_present_and_preview_removed(self): """标题生成组存在,预览块已移除(docs/11 §17)。""" from PySide6.QtWidgets import QLabel panel = self._panel() panel.apply_config(dict(DEFAULT_CONFIG)) labels = [label.text() for label in panel.findChildren(QLabel)] self.assertIn("标题生成提示词", labels) self.assertIn("标题模型", labels) self.assertNotIn("最终生成要求预览", labels) # The preview widgets and methods are gone. self.assertFalse(hasattr(panel, "_preview_view")) self.assertFalse(hasattr(panel, "_sample_combo")) self.assertTrue(hasattr(panel, "_title_btn")) self.assertTrue(hasattr(panel, "_title_model_combo")) def test_title_prompt_defaults_loaded(self): from services.config_service import DEFAULT_TITLE_PROMPT panel = self._panel() panel.apply_config(dict(DEFAULT_CONFIG)) self.assertEqual(panel._title_prompt_edit.toPlainText(), DEFAULT_TITLE_PROMPT) if __name__ == "__main__": unittest.main()