|
18 | 18 |
|
19 | 19 |
|
20 | 20 | @contextmanager |
21 | | -def install_stub_modules(): |
| 21 | +def install_stub_modules(*, notify_lang="en"): |
22 | 22 | flask_module = types.ModuleType("flask") |
23 | 23 |
|
24 | 24 | class Flask: |
@@ -67,7 +67,7 @@ def run(self, *args, **kwargs): |
67 | 67 | market_timezone="Asia/Hong_Kong", |
68 | 68 | symbol_suffix=".HK", |
69 | 69 | trading_currency="HKD", |
70 | | - notify_lang="en", |
| 70 | + notify_lang=notify_lang, |
71 | 71 | tg_token=None, |
72 | 72 | tg_chat_id="shared-chat-id", |
73 | 73 | dry_run_only=False, |
@@ -186,8 +186,8 @@ def run(self, *args, **kwargs): |
186 | 186 | sys.modules[name] = previous |
187 | 187 |
|
188 | 188 |
|
189 | | -def load_module(): |
190 | | - with install_stub_modules(): |
| 189 | +def load_module(*, notify_lang="en"): |
| 190 | + with install_stub_modules(notify_lang=notify_lang): |
191 | 191 | with patch.dict( |
192 | 192 | os.environ, |
193 | 193 | { |
@@ -277,6 +277,32 @@ def fake_post(_url, *, json, timeout): |
277 | 277 | self.assertIn("LongBridge strategy run failed", observed["payloads"][0][0]["text"]) |
278 | 278 | self.assertIn("RuntimeError: boom", observed["payloads"][0][0]["text"]) |
279 | 279 |
|
| 280 | + def test_handle_trigger_runtime_error_fallback_uses_chinese_copy(self): |
| 281 | + module = load_module(notify_lang="zh") |
| 282 | + observed = {"payloads": []} |
| 283 | + |
| 284 | + class FakeResponse: |
| 285 | + status_code = 200 |
| 286 | + |
| 287 | + def fake_post(_url, *, json, timeout): |
| 288 | + observed["payloads"].append((json, timeout)) |
| 289 | + return FakeResponse() |
| 290 | + |
| 291 | + module.TG_TOKEN = "token-1" |
| 292 | + module.TG_CHAT_ID = "chat-1" |
| 293 | + module.requests.post = fake_post |
| 294 | + module.run_strategy = lambda: (_ for _ in ()).throw(RuntimeError("boom")) |
| 295 | + |
| 296 | + with module.app.test_request_context("/", method="POST"): |
| 297 | + body, status = module.handle_trigger() |
| 298 | + |
| 299 | + self.assertEqual(status, 500) |
| 300 | + self.assertEqual(body, "Error") |
| 301 | + text = observed["payloads"][0][0]["text"] |
| 302 | + self.assertIn("LongBridge 策略运行失败", text) |
| 303 | + self.assertIn("服务:", text) |
| 304 | + self.assertIn("错误: RuntimeError: boom", text) |
| 305 | + |
280 | 306 | def test_handle_trigger_allows_get(self): |
281 | 307 | module = load_module() |
282 | 308 | observed = {"called": False} |
|
0 commit comments