"""
Unit tests for Salesforce ID validation
========================================
"""
import pytest
import sys
sys.path.insert(0, 'src')

from src.fmatch.saas.api.v2.l2a import _valid_sf_ids


class TestSalesforceIdValidation:
    """Test Salesforce ID validation function"""
    
    def test_valid_15_char_id(self):
        """Test valid 15-character Salesforce ID"""
        ids = ["001xx000003DGb1"]
        result = _valid_sf_ids(ids)
        assert result == ["001xx000003DGb1"]
    
    def test_valid_18_char_id(self):
        """Test valid 18-character Salesforce ID"""
        ids = ["001xx000003DGb1AAO"]
        result = _valid_sf_ids(ids)
        assert result == ["001xx000003DGb1AAO"]
    
    def test_invalid_id_dropped(self):
        """Test invalid IDs are dropped"""
        ids = ["001xx000003DGb1AAO", "test-lead-001", "invalid"]
        result = _valid_sf_ids(ids)
        assert result == ["001xx000003DGb1AAO"]
    
    def test_empty_list(self):
        """Test empty list returns empty"""
        result = _valid_sf_ids([])
        assert result == []
    
    def test_none_input(self):
        """Test None input returns empty list"""
        result = _valid_sf_ids(None)
        assert result == []
    
    def test_duplicates_removed(self):
        """Test duplicate IDs are removed"""
        ids = ["001xx000003DGb1AAO", "001xx000003DGb1AAO", "002xx000003DGb2BBB"]
        result = _valid_sf_ids(ids)
        assert len(result) == 2
        assert "001xx000003DGb1AAO" in result
        assert "002xx000003DGb2BBB" in result
    
    def test_special_characters_invalid(self):
        """Test IDs with special characters are invalid"""
        ids = ["001xx000003DGb!", "001xx000003DGb-", "001xx000003DGb#AAO"]
        result = _valid_sf_ids(ids)
        assert result == []
    
    def test_wrong_length_invalid(self):
        """Test IDs with wrong length are invalid"""
        ids = ["001xx00", "001xx000003DGb1AAOTOOLONG", "001xx000003DGb12"]  # 16 chars
        result = _valid_sf_ids(ids)
        assert result == []
    
    def test_mixed_valid_invalid(self):
        """Test mixture of valid and invalid IDs"""
        ids = [
            "001xx000003DGb1AAO",  # Valid 18-char
            "test-lead-001",        # Invalid format
            "002yy000004EHc2",      # Valid 15-char  
            "",                     # Empty string
            None,                   # None value (should be filtered)
            "003zz000005FId3CCC",   # Valid 18-char
            "not-an-id",           # Invalid
        ]
        result = _valid_sf_ids([id for id in ids if id is not None])  # Filter None before passing
        assert len(result) == 3
        assert "001xx000003DGb1AAO" in result
        assert "002yy000004EHc2" in result
        assert "003zz000005FId3CCC" in result
    
    def test_case_sensitive(self):
        """Test that IDs are case-sensitive"""
        ids = ["001XX000003DGB1AAO", "001xx000003DGb1AAO"]  # Different cases
        result = _valid_sf_ids(ids)
        # Both should be valid as Salesforce IDs can have mixed case
        assert len(result) == 2


def test_quick_validation():
    """Quick test to verify the function works"""
    assert _valid_sf_ids(["001xx000003DGb1AAO", "test-lead-001"]) == ["001xx000003DGb1AAO"]
    print("✓ Quick validation test passed")


if __name__ == "__main__":
    # Run quick test
    test_quick_validation()
    
    # Run full test suite
    pytest.main([__file__, "-v"])