import pytest from docx import Document from pathlib import Path from django.conf import settings from review_agent.models import Conversation, RegulatoryInfoPackageBatch from review_agent.regulatory_info_package.services.field_merge import merge_fields from review_agent.regulatory_info_package.services.package_generate import generate_package_documents from review_agent.regulatory_info_package.services.template_config import load_template_config pytestmark = pytest.mark.django_db def test_template_config_uses_clean_internal_templates(): config = load_template_config() source_dir = Path(config["source_dir"]) assert source_dir == settings.BASE_DIR / "review_agent" / "regulatory_info_package" / "templates" / "clean" assert source_dir.exists() assert len(config["templates"]) == 7 assert all((source_dir / item["source_file"]).exists() for item in config["templates"]) def test_clean_templates_expose_stable_fill_placeholders(): config = load_template_config() source_dir = Path(config["source_dir"]) expected_by_code = { "ch1_2_directory": {"{{product_name}}", "{{applicant_name}}"}, "ch1_4_application_form": {"{{product_name}}", "{{applicant_name}}"}, "ch1_5_product_list": {"{{product_name}}", "{{package_specification}}"}, "ch1_9_pre_submission": {"{{product_name}}", "{{applicant_name}}"}, "ch1_11_1_standards": {"{{standard_no}}", "{{product_name}}"}, "ch1_11_5_authenticity": {"{{product_name}}", "{{applicant_name}}"}, "ch1_11_6_conformity": {"{{product_name}}", "{{applicant_name}}"}, } for item in config["templates"]: document = Document(source_dir / item["source_file"]) text = _document_text(document) for placeholder in expected_by_code[item["code"]]: assert placeholder in text def test_generate_package_documents_creates_seven_results(django_user_model, tmp_path): user = django_user_model.objects.create_user(username="owner", password="pass") conversation = Conversation.objects.create(user=user, title="会话") batch = RegulatoryInfoPackageBatch.objects.create( conversation=conversation, user=user, batch_no="RIP-20260610154000-abcdef", work_dir=str(tmp_path), ) merged, _summary = merge_fields({"product_name": {"value": "测试产品", "label": "产品名称"}}, {}) results = generate_package_documents(batch, load_template_config(), merged) assert len(results) == 7 assert all(result.status in {"success", "fallback_success"} for result in results), [ (result.template_code, result.status, result.error_message) for result in results ] assert all(result.path for result in results) def test_generated_docx_has_visible_prefill_block_near_top(django_user_model, tmp_path): user = django_user_model.objects.create_user(username="owner", password="pass") conversation = Conversation.objects.create(user=user, title="会话") batch = RegulatoryInfoPackageBatch.objects.create( conversation=conversation, user=user, batch_no="RIP-20260610154100-abcdef", work_dir=str(tmp_path), ) merged, _summary = merge_fields({"product_name": {"value": "测试产品", "label": "产品名称"}}, {}) results = generate_package_documents(batch, load_template_config(), merged) docx_result = next(result for result in results if result.template_code == "ch1_2_directory") document = Document(docx_result.path) first_text = "\n".join(paragraph.text for paragraph in document.paragraphs[:8]) assert "预生成版" in first_text assert "测试产品" in first_text def test_generated_docx_replaces_sample_case_content(django_user_model, tmp_path): user = django_user_model.objects.create_user(username="owner", password="pass") conversation = Conversation.objects.create(user=user, title="会话") batch = RegulatoryInfoPackageBatch.objects.create( conversation=conversation, user=user, batch_no="RIP-20260610154200-abcdef", work_dir=str(tmp_path), ) merged, _summary = merge_fields( { "product_name": {"value": "测试产品", "label": "产品名称"}, "package_specification": {"value": "24人份/盒;48人份/盒", "label": "包装规格"}, }, {}, ) results = generate_package_documents(batch, load_template_config(), merged) docx_results = [result for result in results if result.actual_format == "docx"] for result in docx_results: document = Document(result.path) text = "\n".join(paragraph.text for paragraph in document.paragraphs) for table in document.tables: for row in table.rows: text += "\n" + "\t".join(cell.text for cell in row.cells) assert "呼吸道合胞病毒、肺炎支原体核酸检测试剂盒" not in text product_list = next(result for result in results if result.template_code == "ch1_5_product_list") product_doc = Document(product_list.path) table = product_doc.tables[0] assert table.rows[1].cells[0].text == "24人份/盒" assert table.rows[1].cells[1].text == "/" assert "6018003102" not in "\n".join(cell.text for row in table.rows for cell in row.cells) def test_generated_docs_fill_clean_template_body(django_user_model, tmp_path): user = django_user_model.objects.create_user(username="owner", password="pass") conversation = Conversation.objects.create(user=user, title="会话") batch = RegulatoryInfoPackageBatch.objects.create( conversation=conversation, user=user, batch_no="RIP-20260610154300-abcdef", work_dir=str(tmp_path), ) merged, _summary = merge_fields( { "product_name": {"value": "甲型流感病毒核酸检测试剂盒", "label": "产品名称"}, "applicant_name": {"value": "星河医疗科技有限公司", "label": "申请人名称"}, "package_specification": {"value": "24人份/盒;48人份/盒", "label": "包装规格"}, "standard_no": {"value": "GB/T 29791.1-2013", "label": "标准号"}, }, {}, ) results = generate_package_documents(batch, load_template_config(), merged) for code in ["ch1_2_directory", "ch1_4_application_form", "ch1_11_5_authenticity", "ch1_11_6_conformity"]: result = next(item for item in results if item.template_code == code) text = _document_text(Document(result.path)) assert "甲型流感病毒核酸检测试剂盒" in text assert "星河医疗科技有限公司" in text assert "{{" not in text assert "}}" not in text standards = next(item for item in results if item.template_code == "ch1_11_1_standards") standards_text = _document_text(Document(standards.path)) assert "GB/T 29791.1-2013" in standards_text product_list = next(item for item in results if item.template_code == "ch1_5_product_list") product_text = _document_text(Document(product_list.path)) assert "24人份/盒" in product_text assert "48人份/盒" in product_text def _document_text(document: Document) -> str: text = "\n".join(paragraph.text for paragraph in document.paragraphs) for table in document.tables: for row in table.rows: text += "\n" + "\t".join(cell.text for cell in row.cells) return text