from db.source_snapshot_export import flatten_payload_for_export, ordered_csv_columns


def test_flatten_magento_custom_attributes():
    payload = {
        "sku": "ABC-1",
        "name": "Widget",
        "custom_attributes": [
            {"attribute_code": "color", "value": "Red"},
            {"attribute_code": "meta_title", "value": "Buy Widget"},
        ],
    }
    flat = flatten_payload_for_export(payload, channel="magento")
    assert flat["name"] == "Widget"
    assert flat["magento_color"] == "Red"
    assert flat["magento_meta_title"] == "Buy Widget"


def test_flatten_plytix_scalar_payload():
    payload = {"sku": "ABC-1", "title": "Widget", "nested": {"a": 1}}
    flat = flatten_payload_for_export(payload, channel="plytix")
    assert flat["title"] == "Widget"
    assert flat["nested"] == '{"a": 1}'


def test_ordered_csv_columns_sku_first():
    rows = [{"title": "A", "sku": "1"}, {"price": "9.99", "sku": "2"}]
    assert ordered_csv_columns(rows)[0] == "sku"
    assert "price" in ordered_csv_columns(rows)
