feat(file-summary): 接入文件汇总工作流触发
This commit is contained in:
32
tests/test_file_summary_trigger.py
Normal file
32
tests/test_file_summary_trigger.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import pytest
|
||||
|
||||
from review_agent.file_summary.workflow_trigger import evaluate_file_summary_trigger
|
||||
from review_agent.models import Conversation, FileAttachment
|
||||
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
def test_trigger_matches_keywords_only_when_active_attachment_exists(django_user_model):
|
||||
user = django_user_model.objects.create_user(username="owner", password="pass")
|
||||
conversation = Conversation.objects.create(user=user, title="会话")
|
||||
|
||||
no_file = evaluate_file_summary_trigger(conversation, "请自动汇总文件目录与页数")
|
||||
assert no_file.should_start is False
|
||||
assert no_file.reason == "missing_attachment"
|
||||
|
||||
FileAttachment.objects.create(
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
original_name="a.docx",
|
||||
storage_path="x/a.docx",
|
||||
file_size=1,
|
||||
)
|
||||
|
||||
matched = evaluate_file_summary_trigger(conversation, "请自动汇总文件目录与页数")
|
||||
assert matched.should_start is True
|
||||
assert matched.workflow_type == "file_summary"
|
||||
|
||||
normal = evaluate_file_summary_trigger(conversation, "你好,帮我解释法规")
|
||||
assert normal.should_start is False
|
||||
assert normal.reason == "not_matched"
|
||||
102
tests/test_file_summary_workflow.py
Normal file
102
tests/test_file_summary_workflow.py
Normal file
@@ -0,0 +1,102 @@
|
||||
import pytest
|
||||
|
||||
from review_agent.file_summary.workflow import create_file_summary_batch, start_file_summary_workflow
|
||||
from review_agent.models import (
|
||||
Conversation,
|
||||
FileAttachment,
|
||||
FileSummaryBatch,
|
||||
FileSummaryBatchAttachment,
|
||||
Message,
|
||||
WorkflowEvent,
|
||||
WorkflowNodeRun,
|
||||
)
|
||||
from review_agent.services import stream_message
|
||||
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
def test_create_batch_binds_active_attachments_and_initializes_nodes(django_user_model):
|
||||
user = django_user_model.objects.create_user(username="owner", password="pass")
|
||||
conversation = Conversation.objects.create(user=user, title="会话")
|
||||
message = Message.objects.create(conversation=conversation, role=Message.Role.USER, content="自动汇总")
|
||||
active = FileAttachment.objects.create(
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
original_name="a.docx",
|
||||
storage_path="x/a.docx",
|
||||
file_size=1,
|
||||
)
|
||||
FileAttachment.objects.create(
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
original_name="old.docx",
|
||||
is_active=False,
|
||||
storage_path="x/old.docx",
|
||||
file_size=1,
|
||||
)
|
||||
|
||||
batch = create_file_summary_batch(conversation=conversation, user=user, trigger_message=message)
|
||||
|
||||
assert batch.status == FileSummaryBatch.Status.PENDING
|
||||
assert FileSummaryBatchAttachment.objects.get(batch=batch).attachment == active
|
||||
active.refresh_from_db()
|
||||
assert active.upload_status == FileAttachment.UploadStatus.BOUND
|
||||
assert WorkflowNodeRun.objects.filter(batch=batch).count() >= 6
|
||||
assert WorkflowEvent.objects.filter(batch=batch, event_type="workflow_created").exists()
|
||||
|
||||
|
||||
def test_start_file_summary_workflow_runs_synchronously_for_tests(django_user_model):
|
||||
user = django_user_model.objects.create_user(username="owner", password="pass")
|
||||
conversation = Conversation.objects.create(user=user, title="会话")
|
||||
message = Message.objects.create(conversation=conversation, role=Message.Role.USER, content="自动汇总")
|
||||
FileAttachment.objects.create(
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
original_name="a.docx",
|
||||
storage_path="x/a.docx",
|
||||
file_size=1,
|
||||
)
|
||||
batch = create_file_summary_batch(conversation=conversation, user=user, trigger_message=message)
|
||||
|
||||
start_file_summary_workflow(batch, async_run=False)
|
||||
|
||||
batch.refresh_from_db()
|
||||
assert batch.status == FileSummaryBatch.Status.SUCCESS
|
||||
assert WorkflowEvent.objects.filter(batch=batch, event_type="workflow_completed").exists()
|
||||
|
||||
|
||||
def test_stream_message_returns_workflow_meta_when_triggered(settings, django_user_model):
|
||||
settings.FILE_SUMMARY_ASYNC = False
|
||||
user = django_user_model.objects.create_user(username="owner", password="pass")
|
||||
conversation = Conversation.objects.create(user=user, title="会话")
|
||||
FileAttachment.objects.create(
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
original_name="a.docx",
|
||||
storage_path="x/a.docx",
|
||||
file_size=1,
|
||||
)
|
||||
|
||||
frames = list(stream_message(conversation, "请自动汇总文件目录与页数"))
|
||||
|
||||
joined = "".join(frames)
|
||||
assert "workflow_started" in joined
|
||||
assert "\"workflow_type\": \"file_summary\"" in joined
|
||||
assert FileSummaryBatch.objects.filter(conversation=conversation).exists()
|
||||
|
||||
|
||||
def test_stream_message_uses_normal_llm_path_when_not_triggered(monkeypatch, django_user_model):
|
||||
user = django_user_model.objects.create_user(username="owner", password="pass")
|
||||
conversation = Conversation.objects.create(user=user, title="会话")
|
||||
|
||||
def fake_stream_reply(conversation, content):
|
||||
yield "普通回复"
|
||||
|
||||
monkeypatch.setattr("review_agent.services.stream_reply", fake_stream_reply)
|
||||
|
||||
frames = list(stream_message(conversation, "你好"))
|
||||
|
||||
joined = "".join(frames)
|
||||
assert "普通回复" in joined
|
||||
assert "workflow_started" not in joined
|
||||
Reference in New Issue
Block a user