from db.source_snapshot_enrichment import ENRICHMENT_PROFILES, merge_payload
from shopify.pull_query import extract_shopify_media


def test_merge_payload_custom_attributes_by_code():
    base = {"custom_attributes": [{"attribute_code": "door_style", "value": "100"}]}
    patch = {"custom_attributes": [{"attribute_code": "door_style", "value": "520"}, {"attribute_code": "color", "value": "white"}]}
    merged = merge_payload(base, patch)
    attrs = {item["attribute_code"]: item["value"] for item in merged["custom_attributes"]}
    assert attrs["door_style"] == "520"
    assert attrs["color"] == "white"


def test_extract_shopify_media_urls():
    patch = extract_shopify_media(
        {
            "featuredMedia": {"preview": {"image": {"url": "https://cdn.shopify.com/featured.jpg"}}},
            "media": {
                "nodes": [
                    {"preview": {"image": {"url": "https://cdn.shopify.com/featured.jpg"}}},
                    {"preview": {"image": {"url": "https://cdn.shopify.com/alt.jpg"}}},
                ]
            },
        }
    )
    assert patch["featured_image_url"] == "https://cdn.shopify.com/featured.jpg"
    assert patch["image_urls"] == [
        "https://cdn.shopify.com/featured.jpg",
        "https://cdn.shopify.com/alt.jpg",
    ]


def test_enrichment_profiles_defined():
    assert "images" in ENRICHMENT_PROFILES
    assert "magento_custom_attributes" in ENRICHMENT_PROFILES
