"""Tests para diff_relations."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))
from diff_relations import diff_relations


def test_relacion_anadida():
    before = [{"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 1.0}]
    after = [
        {"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 1.0},
        {"source_id": "C", "target_id": "D", "relation_type": "owns", "weight": 0.5},
    ]
    result = diff_relations(before, after)
    assert len(result["added"]) == 1
    assert result["added"][0]["source_id"] == "C"
    assert result["removed"] == []
    assert result["unchanged"] == 1
    assert "1 added" in result["summary"]


def test_relacion_eliminada():
    before = [
        {"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 1.0},
        {"source_id": "C", "target_id": "D", "relation_type": "owns", "weight": 0.5},
    ]
    after = [{"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 1.0}]
    result = diff_relations(before, after)
    assert result["added"] == []
    assert len(result["removed"]) == 1
    assert result["removed"][0]["source_id"] == "C"
    assert result["unchanged"] == 1
    assert "1 removed" in result["summary"]


def test_relacion_con_metadata_modificada_mismo_source_target_type_distinto_weight():
    before = [{"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 1.0}]
    after = [{"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 5.0}]
    result = diff_relations(before, after)
    assert result["added"] == []
    assert result["removed"] == []
    assert len(result["modified"]) == 1
    mod = result["modified"][0]
    assert mod["key"] == "A|B|knows"
    assert "weight" in mod["changes"]
    assert mod["changes"]["weight"]["old"] == 1.0
    assert mod["changes"]["weight"]["new"] == 5.0
    assert result["unchanged"] == 0


def test_key_compuesta_funciona_correctamente():
    # Misma pareja A->B pero diferente tipo de relacion -> dos relaciones distintas
    before = [
        {"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 1.0},
        {"source_id": "A", "target_id": "B", "relation_type": "owns", "weight": 0.5},
    ]
    after = [
        {"source_id": "A", "target_id": "B", "relation_type": "knows", "weight": 1.0},
        {"source_id": "A", "target_id": "B", "relation_type": "trusts", "weight": 0.8},
    ]
    result = diff_relations(before, after)
    # owns eliminada, trusts añadida, knows sin cambios
    assert len(result["added"]) == 1
    assert result["added"][0]["relation_type"] == "trusts"
    assert len(result["removed"]) == 1
    assert result["removed"][0]["relation_type"] == "owns"
    assert result["unchanged"] == 1
    assert result["modified"] == []


if __name__ == "__main__":
    test_relacion_anadida()
    test_relacion_eliminada()
    test_relacion_con_metadata_modificada_mismo_source_target_type_distinto_weight()
    test_key_compuesta_funciona_correctamente()
    print("All tests passed.")
