84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
from backend.app.services.adaptive_rag_service import (
|
||
ACTION_MODE_READ,
|
||
ACTION_MODE_READ_WRITE,
|
||
ACTION_MODE_WRITE,
|
||
AdaptiveRAGService,
|
||
)
|
||
|
||
|
||
def test_resolve_action_mode_as_write_for_requirement_command():
|
||
mode = AdaptiveRAGService._resolve_action_mode(
|
||
requested_mode="auto",
|
||
question="给健身项目新增一个信号:各类健身动作消耗的大概卡路里",
|
||
effective_question="给健身项目新增一个信号:各类健身动作消耗的大概卡路里",
|
||
)
|
||
assert mode == ACTION_MODE_WRITE
|
||
|
||
|
||
def test_resolve_action_mode_as_read_write_for_requirement_question():
|
||
mode = AdaptiveRAGService._resolve_action_mode(
|
||
requested_mode="auto",
|
||
question="要不要给健身项目新增一个信号:各类健身动作消耗的大概卡路里?",
|
||
effective_question="要不要给健身项目新增一个信号:各类健身动作消耗的大概卡路里?",
|
||
)
|
||
assert mode == ACTION_MODE_READ_WRITE
|
||
|
||
|
||
def test_resolve_action_mode_keeps_read_for_plain_question():
|
||
mode = AdaptiveRAGService._resolve_action_mode(
|
||
requested_mode="auto",
|
||
question="健身项目现在有哪些阻塞项?",
|
||
effective_question="健身项目现在有哪些阻塞项?",
|
||
)
|
||
assert mode == ACTION_MODE_READ
|
||
|
||
|
||
def test_build_requirement_ack_text_variants():
|
||
created = AdaptiveRAGService._build_requirement_ack_text(
|
||
action_taken="signal_created",
|
||
signal_id=123,
|
||
project_id=5,
|
||
error_reason="",
|
||
)
|
||
deduped = AdaptiveRAGService._build_requirement_ack_text(
|
||
action_taken="signal_deduped",
|
||
signal_id=123,
|
||
project_id=5,
|
||
error_reason="",
|
||
)
|
||
failed = AdaptiveRAGService._build_requirement_ack_text(
|
||
action_taken="signal_failed",
|
||
signal_id=None,
|
||
project_id=5,
|
||
error_reason="db unavailable",
|
||
)
|
||
assert created == "已入库:已为项目#5创建信号 #123,并进入待处理队列。"
|
||
assert deduped == "已入库:检测到重复需求,已复用项目#5的信号 #123。"
|
||
assert "未完成入库" in failed
|
||
|
||
|
||
def test_extract_ack_metadata_from_sources():
|
||
meta = AdaptiveRAGService._extract_ack_metadata_from_sources(
|
||
[
|
||
{
|
||
"source": "pmflow",
|
||
"label": "Signal Queue",
|
||
"score": 0.98,
|
||
"metadata": {
|
||
"action_taken": "signal_created",
|
||
"signal_id": 77,
|
||
"trace_id": "rag-ack-1",
|
||
"processing_stage": "ack_only",
|
||
"ack_latency_ms": 320,
|
||
},
|
||
}
|
||
],
|
||
answer="已入库:已为项目#5创建信号 #77,并进入待处理队列。",
|
||
)
|
||
assert meta["action_taken"] == "signal_created"
|
||
assert meta["signal_id"] == 77
|
||
assert meta["reply_required"] is True
|
||
assert meta["processing_stage"] == "ack_only"
|
||
assert meta["ack_latency_ms"] == 320
|
||
assert meta["reply_text"] == "已入库:已为项目#5创建信号 #77,并进入待处理队列。"
|