import pytest from backend.app.services.adaptive_rag_service import AdaptiveRAGService from backend.app.services.feishu_signal_pipeline import FeishuSignalPipelineService from backend.app.services.signal_capture import ProactiveSignalCaptureService from backend.app.services.context_cache import context_cache_service @pytest.mark.asyncio async def test_sync_feishu_recent_cache_skip_when_rag_result_present(monkeypatch): service = ProactiveSignalCaptureService(db=None) calls: list[dict] = [] async def _fake_append_session_messages(**kwargs): calls.append(kwargs) monkeypatch.setattr(context_cache_service, "append_session_messages", _fake_append_session_messages) await service._sync_feishu_recent_cache( tenant_id=1, group_chat_id="oc-chat-1", sender="测试用户", sender_open_id=None, message="这个需求要不要支持飞书同步?", pipeline_result={"status": "captured", "rag_result": {"answer": "已回答"}}, ) assert calls == [] @pytest.mark.asyncio async def test_sync_feishu_recent_cache_write_external_session(monkeypatch): service = ProactiveSignalCaptureService(db=None) calls: list[dict] = [] async def _fake_append_session_messages(**kwargs): calls.append(kwargs) monkeypatch.setattr(context_cache_service, "append_session_messages", _fake_append_session_messages) await service._sync_feishu_recent_cache( tenant_id=1, group_chat_id="oc-chat-2", sender="测试用户", sender_open_id=None, message="需要支持飞书同步", pipeline_result={"status": "captured"}, ) assert len(calls) == 1 call = calls[0] assert call["tenant_id"] == 1 assert call["question"] == "需要支持飞书同步" assert "已收到并记录该需求" in call["answer"] key = FeishuSignalPipelineService._build_rag_conversation_key( source_chat_id="oc-chat-2", source_sender_open_id=None, sender="测试用户", ) expected_sid = AdaptiveRAGService._external_session_id_from_key(key) assert call["session_id"] == expected_sid def test_build_recent_cache_ack_text_variants(): assert ProactiveSignalCaptureService._build_recent_cache_ack_text({"status": "captured"}) == "已收到并记录该需求,已进入信号处理队列。" assert ProactiveSignalCaptureService._build_recent_cache_ack_text({"status": "ignored"}) == "已收到该消息,本次未进入需求处理队列。" assert "系统处理中出现异常" in ProactiveSignalCaptureService._build_recent_cache_ack_text({"status": "error"})