from magento.sync_service import _duplicate_axis_signatures, _image_content_type


def test_duplicate_axis_signatures_detects_children_with_same_axis_values():
    duplicates = _duplicate_axis_signatures(
        ["CSG-WDC2436MI", "CSG-WDC2436RIGHT", "CSG-WDC3042"],
        [
            {"height": "36", "width": "24"},
            {"height": "36", "width": "24"},
            {"height": "42", "width": "30"},
        ],
        ["width", "height"],
    )

    assert duplicates == [
        {
            "axis_values": {"width": "24", "height": "36"},
            "children": ["CSG-WDC2436MI", "CSG-WDC2436RIGHT"],
        }
    ]


def test_image_content_type_rejects_html_and_executable_payloads():
    assert _image_content_type("https://example.test/x.jpg", b"<!doctype html><html>")[2] == (
        "download returned HTML, not an image"
    )
    assert _image_content_type("https://example.test/x.jpg", b"MZfake-binary")[2] == (
        "downloaded content looks executable/archive-like, not an image"
    )


def test_image_content_type_prefers_bytes_over_url_extension():
    ext, mime, error = _image_content_type("https://example.test/wrong.jpg", b"\x89PNG\r\n\x1a\nrest")

    assert error is None
    assert ext == "png"
    assert mime == "image/png"
