from models.profile import Profile
import os

def test_profile():
    """Test Profile model CRUD operations."""
    # Clean up any existing test file
    if os.path.exists("Profile.json"):
        os.remove("Profile.json")
    
    profiles = Profile()
    
    print("Testing create profile")
    profile_id = profiles.create("test_profile")
    print(f"Created profile with ID: {profile_id}")
    assert profile_id is not None
    assert profile_id in profiles
    
    print("\nTesting read profile")
    profile = profiles.read(profile_id)
    print(f"Read: {profile}")
    assert profile is not None
    assert profile["name"] == "test_profile"
    assert "tabs" in profile
    assert "palette" in profile
    assert "tab_order" in profile
    
    print("\nTesting update profile")
    update_data = {
        "name": "updated_profile",
        "tabs": {"tab1": {"names": ["1"], "presets": []}},
        "palette": ["#FF0000", "#00FF00"],
        "tab_order": ["tab1"]
    }
    result = profiles.update(profile_id, update_data)
    assert result is True
    updated = profiles.read(profile_id)
    assert updated["name"] == "updated_profile"
    assert "tab1" in updated["tabs"]
    
    print("\nTesting list profiles")
    profile_list = profiles.list()
    print(f"Profile list: {profile_list}")
    assert profile_id in profile_list
    
    print("\nTesting delete profile")
    deleted = profiles.delete(profile_id)
    assert deleted is True
    assert profile_id not in profiles
    
    print("\nTesting read after delete")
    profile = profiles.read(profile_id)
    assert profile is None
    
    print("\nAll profile tests passed!")


if __name__ == '__main__':
    test_profile()
