import unit_test_guard # noqa: F401 import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import unittest from datetime import datetime, timedelta from zoneinfo import ZoneInfo from app.core.signal_health import funds_warning class FundsHealthTests(unittest.TestCase): def setUp(self): self.now=datetime(2026,11,16,14,10,tzinfo=ZoneInfo('Asia/Shanghai')) self.doc={'at':self.now.isoformat(),'as_of':(self.now-timedelta(minutes=1)).isoformat(), 'source':'redis_stock_flow_realtime','priority':{'300133.SZ':{'state':'available','money_flow_baseline_ready':True}}} def check(self, doc=None, codes=('300133.SZ',), **kw): return funds_warning(doc if doc is not None else self.doc,codes,self.now,**kw) def test_healthy_quiet_not_fake_signal(self): self.assertEqual(self.check(),'') def test_frozen_reader_even_live_heartbeat_warns(self): self.doc['as_of']=(self.now-timedelta(hours=3)).isoformat() self.assertIn('提醒可能延迟',self.check()) def test_missing_stale_and_future_status_visible(self): self.assertIn('提醒可能延迟',funds_warning(None,['300133.SZ'],self.now)) for delta in (-181,1): self.doc['at']=(self.now+timedelta(seconds=delta)).isoformat() self.assertIn('提醒可能延迟',self.check()) def test_missing_focus_detected(self): self.assertIn('提醒暂不可用',self.check(codes=('300133.SZ','601126.SH'))) def test_bad_funds_and_baseline_separate(self): self.doc['priority']['300133.SZ']={'state':'stale','money_flow_baseline_ready':False} text=self.check();self.assertIn('提醒暂不可用',text);self.assertNotIn('历史',text) def test_history_preparation_is_not_a_trader_diagnostic_banner(self): self.doc['priority']['300133.SZ']['money_flow_baseline_ready']=False self.assertEqual(self.check(),'') self.assertFalse(self.doc['priority']['300133.SZ']['money_flow_baseline_ready']) def test_malformed_internal_status_does_not_break_signal_page(self): self.doc['priority']['300133.SZ']=None self.assertIn('提醒可能延迟',self.check()) def test_closed_and_lunch_do_not_expect_new_ticks(self): for hour,minute in [(12,0),(18,0),(9,0)]: now=self.now.replace(hour=hour,minute=minute) self.assertEqual(funds_warning(None,['300133.SZ'],now),'') self.assertEqual(self.check({},trade_day=False),'') def test_no_timezone_or_wrong_source_not_healthy(self): self.doc['at']='2026-11-16T14:10:00' self.assertIn('提醒可能延迟',self.check()) if __name__=='__main__': unittest.main()