feat(application-form-fill): 实现自动填表模板选择

This commit is contained in:
2026-06-07 18:28:48 +08:00
parent 8694f2d43e
commit 72890783b3
4 changed files with 389 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
import pytest
from review_agent.application_form_fill.services.template_config import load_template_config
from review_agent.application_form_fill.services.template_repository import (
TemplateUnavailableError,
copy_template_to_batch,
resolve_source_template,
)
from review_agent.application_form_fill.services.template_select import select_templates
from review_agent.models import (
ApplicationFormFillArtifact,
ApplicationFormFillBatch,
Conversation,
FileSummaryBatch,
)
pytestmark = pytest.mark.django_db
def test_resolve_source_template_finds_registration_docx():
config = load_template_config()
specs, _risk_notes = select_templates(config, ["registration_certificate"], "首次注册")
path = resolve_source_template(specs[0], config)
assert path.exists()
assert path.name == "中华人民共和国医疗器械注册证(体外诊断试剂)(格式).docx"
def test_copy_template_to_batch_creates_artifact(settings, tmp_path, django_user_model):
settings.MEDIA_ROOT = tmp_path
user = django_user_model.objects.create_user(username="owner", password="pass")
conversation = Conversation.objects.create(user=user, title="会话")
summary = FileSummaryBatch.objects.create(conversation=conversation, user=user, batch_no="FS-REPO")
batch = ApplicationFormFillBatch.objects.create(
conversation=conversation,
user=user,
source_summary_batch=summary,
batch_no="AFF-REPO",
work_dir=str(tmp_path / "aff" / "AFF-REPO"),
)
config = load_template_config()
specs, _risk_notes = select_templates(config, ["registration_certificate"], "首次注册")
artifact = copy_template_to_batch(specs[0], batch, config)
assert artifact.artifact_type == ApplicationFormFillArtifact.ArtifactType.TEMPLATE_COPY
assert artifact.file_format == "docx"
assert artifact.content_hash
assert artifact.metadata["template_code"] == "registration_certificate"
assert artifact.storage_path.startswith(batch.work_dir)
def test_doc_template_without_working_docx_is_unavailable():
config = load_template_config()
specs, _risk_notes = select_templates(config, ["change_registration"], "变更注册")
with pytest.raises(TemplateUnavailableError):
resolve_source_template(specs[0], config)

View File

@@ -0,0 +1,114 @@
import pytest
from review_agent.application_form_fill.services.template_config import load_template_config
from review_agent.application_form_fill.services.template_select import (
detect_registration_type,
parse_requested_templates,
select_templates,
)
from review_agent.models import ApplicationFormFillBatch, Conversation, FileSummaryBatch, RegulatoryReviewBatch
pytestmark = pytest.mark.django_db
@pytest.mark.parametrize(
("message", "expected"),
[
("帮我填注册证", ["registration_certificate"]),
("生成变更注册备案文件", ["change_registration"]),
("生成安全和性能基本原则清单", ["essential_principles"]),
("请生成全部模板", ["registration_certificate", "change_registration", "essential_principles"]),
("普通聊天", []),
],
)
def test_parse_requested_templates(message, expected):
assert parse_requested_templates(message) == expected
def test_detect_registration_type_prefers_user_message(django_user_model):
user = django_user_model.objects.create_user(username="owner", password="pass")
conversation = Conversation.objects.create(user=user, title="会话")
summary = FileSummaryBatch.objects.create(conversation=conversation, user=user, batch_no="FS-SEL")
regulatory = RegulatoryReviewBatch.objects.create(
conversation=conversation,
user=user,
source_summary_batch=summary,
batch_no="RR-SEL",
condition_json={"confirmed_conditions": {"registration_type": "变更注册"}},
)
batch = ApplicationFormFillBatch.objects.create(
conversation=conversation,
user=user,
source_summary_batch=summary,
source_regulatory_batch=regulatory,
batch_no="AFF-SEL",
)
value, source = detect_registration_type(batch=batch, message="首次注册资料,请填注册证")
assert value == "首次注册"
assert source == ApplicationFormFillBatch.RegistrationTypeSource.USER_MESSAGE
def test_detect_registration_type_falls_back_to_regulatory_batch_and_file_candidates(django_user_model):
user = django_user_model.objects.create_user(username="owner", password="pass")
conversation = Conversation.objects.create(user=user, title="会话")
summary = FileSummaryBatch.objects.create(conversation=conversation, user=user, batch_no="FS-SEL-2")
regulatory = RegulatoryReviewBatch.objects.create(
conversation=conversation,
user=user,
source_summary_batch=summary,
batch_no="RR-SEL-2",
condition_json={"confirmed_conditions": {"registration_type": "变更注册"}},
)
batch = ApplicationFormFillBatch.objects.create(
conversation=conversation,
user=user,
source_summary_batch=summary,
source_regulatory_batch=regulatory,
batch_no="AFF-SEL-2",
)
regulatory_value, regulatory_source = detect_registration_type(batch=batch, message="")
file_value, file_source = detect_registration_type(
message="",
file_candidates={"registration_type": {"suggested": "备案"}},
)
assert (regulatory_value, regulatory_source) == (
"变更注册",
ApplicationFormFillBatch.RegistrationTypeSource.REGULATORY_BATCH,
)
assert (file_value, file_source) == (
"备案",
ApplicationFormFillBatch.RegistrationTypeSource.FILE_EXTRACT,
)
def test_select_default_templates_for_initial_registration():
config = load_template_config()
specs, risk_notes = select_templates(config, [], "首次注册")
assert [spec.code for spec in specs] == ["registration_certificate", "essential_principles"]
assert risk_notes == []
def test_select_default_templates_for_change_registration():
config = load_template_config()
specs, risk_notes = select_templates(config, [], "变更注册")
assert [spec.code for spec in specs] == ["change_registration", "essential_principles"]
assert risk_notes == []
def test_select_user_requested_mismatch_is_allowed_with_risk_note():
config = load_template_config()
specs, risk_notes = select_templates(config, ["change_registration"], "首次注册")
assert [spec.code for spec in specs] == ["change_registration"]
assert risk_notes
assert risk_notes[0]["type"] == "template_registration_mismatch"