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"