|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import types |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +# ========================================== |
| 8 | +# 1. Environment Setup |
| 9 | +# ========================================== |
| 10 | +current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 11 | +plugin_root = os.path.abspath(os.path.join(current_dir, "..")) |
| 12 | +sys.path.insert(0, plugin_root) |
| 13 | + |
| 14 | +# Mock astrbot.api |
| 15 | +astrbot_api = types.ModuleType("astrbot.api") |
| 16 | + |
| 17 | + |
| 18 | +class MockLogger: |
| 19 | + def info(self, msg, *args, **kwargs): |
| 20 | + print(f"[INFO] {msg}") |
| 21 | + |
| 22 | + def error(self, msg, *args, **kwargs): |
| 23 | + print(f"[ERROR] {msg}") |
| 24 | + |
| 25 | + def warning(self, msg, *args, **kwargs): |
| 26 | + print(f"[WARN] {msg}") |
| 27 | + |
| 28 | + def debug(self, msg, *args, **kwargs): |
| 29 | + pass |
| 30 | + |
| 31 | + def log(self, level, msg, *args, **kwargs): |
| 32 | + pass |
| 33 | + |
| 34 | + def isEnabledFor(self, level): |
| 35 | + return True |
| 36 | + |
| 37 | + |
| 38 | +astrbot_api.logger = MockLogger() |
| 39 | +astrbot_api.AstrBotConfig = dict |
| 40 | +sys.modules["astrbot.api"] = astrbot_api |
| 41 | + |
| 42 | +# Mock astrbot.core.utils.astrbot_path |
| 43 | +astrbot_core_utils = types.ModuleType("astrbot.core.utils") |
| 44 | +astrbot_path = types.ModuleType("astrbot.core.utils.astrbot_path") |
| 45 | +astrbot_path.get_astrbot_data_path = lambda: Path(".") |
| 46 | +sys.modules["astrbot.core.utils"] = astrbot_core_utils |
| 47 | +sys.modules["astrbot.core.utils.astrbot_path"] = astrbot_path |
| 48 | + |
| 49 | +from src.domain.models.data_models import ( # noqa: E402 |
| 50 | + ActivityVisualization, |
| 51 | + EmojiStatistics, |
| 52 | + GroupStatistics, |
| 53 | + QualityReview, |
| 54 | + TokenUsage, |
| 55 | +) |
| 56 | +from src.infrastructure.reporting.generators import ReportGenerator # noqa: E402 |
| 57 | + |
| 58 | + |
| 59 | +class MockConfigManager: |
| 60 | + def __init__(self, source: str) -> None: |
| 61 | + self.source = source |
| 62 | + |
| 63 | + def get_report_template(self) -> str: |
| 64 | + return "simple" |
| 65 | + |
| 66 | + def get_max_topics(self) -> int: |
| 67 | + return 5 |
| 68 | + |
| 69 | + def get_max_user_titles(self) -> int: |
| 70 | + return 5 |
| 71 | + |
| 72 | + def get_max_golden_quotes(self) -> int: |
| 73 | + return 5 |
| 74 | + |
| 75 | + def get_html_output_dir(self) -> str: |
| 76 | + return "data/html" |
| 77 | + |
| 78 | + def get_html_filename_format(self) -> str: |
| 79 | + return "report.html" |
| 80 | + |
| 81 | + def get_enable_user_card(self) -> bool: |
| 82 | + return True |
| 83 | + |
| 84 | + def get_t2i_font_source(self) -> str: |
| 85 | + return self.source |
| 86 | + |
| 87 | + def get_t2i_google_fonts_mirror(self) -> str: |
| 88 | + return ( |
| 89 | + "https://fonts.loli.net" |
| 90 | + if self.source == "Mainland" |
| 91 | + else "https://fonts.googleapis.com" |
| 92 | + ) |
| 93 | + |
| 94 | + def get_t2i_gstatic_mirror(self) -> str: |
| 95 | + return ( |
| 96 | + "https://gstatic.loli.net" |
| 97 | + if self.source == "Mainland" |
| 98 | + else "https://fonts.gstatic.com" |
| 99 | + ) |
| 100 | + |
| 101 | + def get_t2i_atri_font_mirror(self) -> str: |
| 102 | + return "https://tc.ciallo.ccwu.cc" |
| 103 | + |
| 104 | + def get_profile_display_mode(self) -> str: |
| 105 | + return "mbti" |
| 106 | + |
| 107 | + def get_profile_image_opacity(self) -> float: |
| 108 | + return 0.2 |
| 109 | + |
| 110 | + def get_profile_image_size_mode(self) -> str: |
| 111 | + return "contain" |
| 112 | + |
| 113 | + def get_profile_mapping_config(self) -> str: |
| 114 | + return "" |
| 115 | + |
| 116 | + def get_t2i_max_concurrent(self) -> int: |
| 117 | + return 4 |
| 118 | + |
| 119 | + def get_llm_max_concurrent(self) -> int: |
| 120 | + return 2 |
| 121 | + |
| 122 | + def get_t2i_rendering_strategies(self) -> list: |
| 123 | + return [] |
| 124 | + |
| 125 | + def get_html_base_url(self) -> str: |
| 126 | + return "" |
| 127 | + |
| 128 | + |
| 129 | +async def mock_get_user_avatar(user_id: str) -> str: |
| 130 | + return f"https://q4.qlogo.cn/headimg_dl?dst_uin={user_id}&spec=640" |
| 131 | + |
| 132 | + |
| 133 | +async def verify_rendering(source: str): |
| 134 | + print(f"\n--- Verifying {source} Rendering ---") |
| 135 | + config = MockConfigManager(source) |
| 136 | + data_dir = Path("data/test") |
| 137 | + data_dir.mkdir(parents=True, exist_ok=True) |
| 138 | + generator = ReportGenerator(config, data_dir) |
| 139 | + |
| 140 | + # Mock avatar cache |
| 141 | + class MockCache(dict): |
| 142 | + def __getitem__(self, key): |
| 143 | + return self.get(key, "") |
| 144 | + |
| 145 | + def set(self, key, value, expire=None): |
| 146 | + self[key] = value |
| 147 | + |
| 148 | + generator._avatar_cache = MockCache() |
| 149 | + |
| 150 | + stats = GroupStatistics( |
| 151 | + message_count=100, |
| 152 | + total_characters=1000, |
| 153 | + participant_count=5, |
| 154 | + most_active_period="12:00", |
| 155 | + golden_quotes=[], |
| 156 | + emoji_count=0, |
| 157 | + emoji_statistics=EmojiStatistics(0, 0), |
| 158 | + activity_visualization=ActivityVisualization({}), |
| 159 | + token_usage=TokenUsage(0, 0, 0), |
| 160 | + chat_quality_review=QualityReview("Test", "Test", [], "Summary"), |
| 161 | + ) |
| 162 | + analysis_result = { |
| 163 | + "statistics": stats, |
| 164 | + "topics": [], |
| 165 | + "user_titles": [], |
| 166 | + "user_analysis": {}, |
| 167 | + "chat_quality_review": stats.chat_quality_review, |
| 168 | + "analysis_date": "2026-04-25", |
| 169 | + "group_id": "123", |
| 170 | + "group_name": "Test Group", |
| 171 | + } |
| 172 | + |
| 173 | + render_payload = await generator._prepare_render_data( |
| 174 | + analysis_result, mock_get_user_avatar |
| 175 | + ) |
| 176 | + html = generator.html_templates.render_template( |
| 177 | + "image_template.html", **render_payload |
| 178 | + ) |
| 179 | + |
| 180 | + filename = f"test_{source.lower()}.html" |
| 181 | + Path(filename).write_text(html, encoding="utf-8") |
| 182 | + |
| 183 | + # Verification |
| 184 | + expected_lang = "zh-CN" if source == "Mainland" else "zh-Hant" |
| 185 | + expected_font = ( |
| 186 | + "https://fonts.loli.net" |
| 187 | + if source == "Mainland" |
| 188 | + else "https://fonts.googleapis.com" |
| 189 | + ) |
| 190 | + expected_gstatic = ( |
| 191 | + "https://gstatic.loli.net" |
| 192 | + if source == "Mainland" |
| 193 | + else "https://fonts.gstatic.com" |
| 194 | + ) |
| 195 | + |
| 196 | + success = True |
| 197 | + if f'lang="{expected_lang}"' not in html: |
| 198 | + print(f'[FAIL] Expected lang="{expected_lang}" not found.') |
| 199 | + success = False |
| 200 | + if expected_font not in html: |
| 201 | + print(f'[FAIL] Expected font mirror "{expected_font}" not found.') |
| 202 | + success = False |
| 203 | + if expected_gstatic not in html: |
| 204 | + print(f'[FAIL] Expected gstatic mirror "{expected_gstatic}" not found.') |
| 205 | + success = False |
| 206 | + |
| 207 | + if success: |
| 208 | + print(f"[PASS] {source} rendering verified. Output saved to {filename}") |
| 209 | + |
| 210 | + await generator.close() |
| 211 | + |
| 212 | + |
| 213 | +async def main(): |
| 214 | + await verify_rendering("Mainland") |
| 215 | + await verify_rendering("Overseas") |
| 216 | + |
| 217 | + |
| 218 | +if __name__ == "__main__": |
| 219 | + asyncio.run(main()) |
0 commit comments