import copy import pytest from review_agent.application_form_fill.services.template_config import ( DEFAULT_CONFIG_PATH, compute_config_hash, load_template_config, validate_template_config, ) def test_template_config_loads_and_validates_default_yaml(settings): config = load_template_config() errors = validate_template_config(config) assert errors == [] assert config["version"] == "application_form_templates_v1" registration = next(item for item in config["templates"] if item["code"] == "registration_certificate") assert registration["file_format"] == "docx" assert {field["key"] for field in registration["fields"]} >= { "applicant_name", "product_name", "package_specification", "main_components", "intended_use", "storage_condition_and_validity", "attachments", } assert all(field["target"]["type"] == "table_row" for field in registration["fields"]) assert len(compute_config_hash(DEFAULT_CONFIG_PATH)) == 64 def test_template_config_reports_missing_source_dir(): config = load_template_config() config["source_dir"] = "docs/not-exists" errors = validate_template_config(config) assert any("source_dir 不存在" in error for error in errors) def test_template_config_reports_duplicate_code(): config = load_template_config() duplicate = copy.deepcopy(config["templates"][0]) config["templates"].append(duplicate) errors = validate_template_config(config) assert any("模板 code 重复" in error for error in errors) def test_template_config_reports_missing_source_file(): config = load_template_config() config["templates"][0]["source_file"] = "missing.docx" errors = validate_template_config(config) assert any("source_file 不存在" in error for error in errors) def test_template_config_reports_unsupported_target_type(): config = load_template_config() config["templates"][0]["fields"][0]["target"]["type"] = "content_control" errors = validate_template_config(config) assert any("target.type 不支持" in error for error in errors) def test_template_config_loads_custom_path(tmp_path): config_path = tmp_path / "templates.yaml" config_path.write_text( """ version: custom source_dir: . templates: - code: custom_template name: Custom source_file: source.docx output_label: Custom file_format: docx fields: - key: product_name label: 产品名称 target: type: table_row row_label: 产品名称 """.strip(), encoding="utf-8", ) (tmp_path / "source.docx").write_bytes(b"docx") config = load_template_config(config_path) assert validate_template_config(config, base_dir=tmp_path) == [] assert compute_config_hash(config_path)