from services.credit_payload_service import CreditPayloadService


def test_generate_payload_should_be_stable_for_same_input():
    service = CreditPayloadService("config/credit_feature_schema.json")
    identity = {
        "staff_name": "张三",
        "staff_id_card": "330101198801010011",
    }

    payload1 = service.generate_payload(
        model="LXCUSTALL",
        h_type="PERSON",
        filename="credit-report-a.html",
        subject_identity=identity,
    )
    payload2 = service.generate_payload(
        model="LXCUSTALL",
        h_type="PERSON",
        filename="credit-report-a.html",
        subject_identity=identity,
    )

    assert payload1 == payload2
    assert set(payload1.keys()) == {"lx_header", "lx_debt", "lx_publictype"}
    assert len(payload1["lx_debt"]) == 21
    assert len(payload1["lx_publictype"]) == 6
    assert payload1["lx_header"]["query_cust_name"] == "张三"
    assert payload1["lx_header"]["query_cert_no"] == "330101198801010011"


def test_generate_payload_should_use_schema_type_rules():
    service = CreditPayloadService("config/credit_feature_schema.json")

    payload = service.generate_payload(
        model="LXCUSTALL",
        h_type="ENTERPRISE",
        filename="credit-report-b.html",
        subject_identity={
            "staff_name": "李四",
            "staff_id_card": "330101199001010022",
        },
    )

    assert payload["lx_debt"]["uncle_bank_house_state"] in {"正常", "逾期", "不良"}
    assert payload["lx_header"]["report_time"].count("-") == 2
    assert payload["lx_publictype"]["civil_cnt"].isdigit()
