import json from pathlib import Path import pytest from rpa_yisaiqi.apa_browser_adapter import ApaBrowserAdapter from rpa_yisaiqi.contract_create_node import ( ContractCreateError, ContractCreateInput, PdfMergeUnsupported, build_spec_from_data, create_contract_in_contract_system, ) from rpa_yisaiqi.contract_draft import is_placeholder_customer_code, load_contract_draft from rpa_yisaiqi.pdf_utils import ensure_merged_pdf def test_load_contract_draft_maps_full_contract_inputs(tmp_path): draft_path = tmp_path / "contract-draft.json" draft_path.write_text( json.dumps( { "supplierName": "重庆亿昊药业", "orderDate": "2026-08-11", "sourceType": "YYC", "templateType": "NON_FORMAT", "templateName": "非格式--采购协议", "customerCode": "CUST-001", "customerName": "重庆亿昊药业", "contractCategory": "采购协议", "settlementType": "付款", "contractBackground": "按订单自动起草采购协议", "contractNature": "主合同", "amountCurrency": "人民币", "amount": "12000.50", "contractEndDate": "2026-12-31", "isMajorContract": "否", "isPrepayment": "否", "belongYear": "2026", "paymentMethod": "承兑", "paymentDays": "45", "contractStatus": "正式合同", "containsBloodProducts": "否", "platformPdfPaths": ["C:/jobs/JOB-1/a.pdf", "C:/jobs/JOB-1/b.pdf"], "goodsLines": [ { "operationCode": "ADD", "goodsCode": "YP-001", "goodsName": "阿卡波糖片", "approvalNo": "国药准字H00000001", "spec": "50mg*30片", "unit": "盒", "quantity": 12, } ], }, ensure_ascii=False, ), encoding="utf-8", ) draft = load_contract_draft(draft_path) assert draft.supplier_name == "重庆亿昊药业" assert draft.template_type == "NON_FORMAT" assert draft.template_name == "非格式--采购协议" assert draft.customer_code == "CUST-001" assert draft.customer_name == "重庆亿昊药业" assert draft.contract_category == "采购协议" assert draft.settlement_type == "付款" assert draft.contract_background == "按订单自动起草采购协议" assert draft.amount == "12000.50" assert draft.contract_end_date == "2026-12-31" assert draft.belong_year == "2026" assert draft.payment_method == "承兑" assert draft.contract_status == "正式合同" assert draft.contains_blood_products == "否" assert draft.platform_pdf_paths == ["C:/jobs/JOB-1/a.pdf", "C:/jobs/JOB-1/b.pdf"] assert draft.goods_lines[0].goods_code == "YP-001" def test_placeholder_customer_code_is_rejected_before_opening_browser(): assert is_placeholder_customer_code("CUST-YYC-001") assert is_placeholder_customer_code("test-001") assert not is_placeholder_customer_code("DW0100046361") with pytest.raises(ContractCreateError, match="占位符"): build_spec_from_data( { "jobId": "JOB-1", "supplierName": "重庆亿昊药业有限公司", "orderDate": "2026-08-11", "sourceType": "XYY", "customerCode": "CUST-YYC-001", "customerName": "重庆亿昊药业有限公司", "amount": "100", "submitMode": "dry_run", "goodsLines": [ { "goodsCode": "YP-001", "goodsName": "阿卡波糖片", "quantity": 1, } ], } ) def test_yyc_requires_pdf_inputs_before_contract_drafting(tmp_path): draft_path = tmp_path / "draft.json" draft_path.write_text( json.dumps( { "supplierName": "重庆亿昊药业", "orderDate": "2026-08-11", "sourceType": "YYC", "goodsLines": [], "platformPdfPaths": [], }, ensure_ascii=False, ), encoding="utf-8", ) spec = ContractCreateInput( job_id="JOB-1", supplier_name="重庆亿昊药业", order_date="2026-08-11", source_type="YYC", draft_path=str(draft_path), ) with pytest.raises(PdfMergeUnsupported): create_contract_in_contract_system(spec) def test_ensure_merged_pdf_uses_single_pdf_without_merge(tmp_path): source = tmp_path / "one.pdf" source.write_bytes(b"%PDF-1.4\n%one\n") assert ensure_merged_pdf([str(source)], str(tmp_path / "merged.pdf")) == str(source) def test_create_contract_uses_apa_browser_adapter(tmp_path, monkeypatch): draft_path = tmp_path / "draft.json" draft_path.write_text( json.dumps( { "supplierName": "重庆亿昊药业有限公司", "orderDate": "2026-08-11", "sourceType": "XYY", "customerCode": "DW0100046361", "customerName": "重庆亿昊药业有限公司", "goodsLines": [], "platformPdfPaths": [], }, ensure_ascii=False, ), encoding="utf-8", ) spec = ContractCreateInput( job_id="JOB-1", supplier_name="重庆亿昊药业有限公司", order_date="2026-08-11", source_type="XYY", draft_path=str(draft_path), ) captured = {} def fake_run(browser, ctx): captured["browser"] = browser captured["ctx"] = ctx return "HT-1" monkeypatch.setattr( "rpa_yisaiqi.contract_create_node.run_contract_create_flow", fake_run, ) assert create_contract_in_contract_system(spec) == "HT-1" assert isinstance(captured["browser"], ApaBrowserAdapter)