# -*- coding: utf-8 -*- from types import SimpleNamespace from backend.explain import range_explainer def test_build_range_explanation_prefers_llm_text_when_available(monkeypatch): monkeypatch.setattr( range_explainer, "get_prices", lambda ticker, start_date, end_date: [ SimpleNamespace(open=100, close=98, high=102, low=97, volume=1000), SimpleNamespace(open=98, close=96, high=99, low=95, volume=1100), SimpleNamespace(open=96, close=97, high=98, low=94, volume=1200), ], ) monkeypatch.setattr( range_explainer, "analyze_range_with_llm", lambda payload: { "summary": "区间内整体偏弱,主题集中在盈利预期和供应链风险。", "trend_analysis": "前半段快速下探,后半段出现修复。", "bullish_factors": ["回调后出现承接"], "bearish_factors": ["盈利预期承压"], "model_label": "DASHSCOPE:qwen-max", }, ) result = range_explainer.build_range_explanation( ticker="AAPL", start_date="2026-03-10", end_date="2026-03-16", news_rows=[ { "id": "news-1", "trade_date": "2026-03-10", "title": "Apple margin pressure concerns grow", "summary": "Investors focused on weaker margin outlook.", "sentiment": "negative", "relevance": "high", "ret_t0": -0.02, "reason_decrease": "盈利预期承压", "category": "earnings", } ], ) assert result["analysis"]["summary"] == "区间内整体偏弱,主题集中在盈利预期和供应链风险。" assert result["analysis"]["trend_analysis"] == "前半段快速下探,后半段出现修复。" assert result["analysis"]["bullish_factors"] == ["回调后出现承接"] assert result["analysis"]["analysis_source"] == "llm" assert result["analysis"]["analysis_model_label"] == "DASHSCOPE:qwen-max" assert result["news_count"] == 1