from models.pallet import Palette
import os

def test_palette():
    """Test Palette model CRUD operations."""
    # Clean up any existing test file
    if os.path.exists("Palette.json"):
        os.remove("Palette.json")
    
    palettes = Palette()
    
    print("Testing create palette")
    colors = ["#FF0000", "#00FF00", "#0000FF", "#FFFF00"]
    palette_id = palettes.create("test_palette", colors)
    print(f"Created palette with ID: {palette_id}")
    assert palette_id is not None
    assert palette_id in palettes
    
    print("\nTesting read palette")
    palette = palettes.read(palette_id)
    print(f"Read: {palette}")
    assert palette is not None
    assert palette["name"] == "test_palette"
    assert len(palette["colors"]) == 4
    assert "#FF0000" in palette["colors"]
    
    print("\nTesting update palette")
    update_data = {
        "name": "updated_palette",
        "colors": ["#FF00FF", "#00FFFF", "#FFA500"]
    }
    result = palettes.update(palette_id, update_data)
    assert result is True
    updated = palettes.read(palette_id)
    assert updated["name"] == "updated_palette"
    assert len(updated["colors"]) == 3
    assert "#FF00FF" in updated["colors"]
    
    print("\nTesting list palettes")
    palette_list = palettes.list()
    print(f"Palette list: {palette_list}")
    assert palette_id in palette_list
    
    print("\nTesting delete palette")
    deleted = palettes.delete(palette_id)
    assert deleted is True
    assert palette_id not in palettes
    
    print("\nTesting read after delete")
    palette = palettes.read(palette_id)
    assert palette is None
    
    print("\nAll palette tests passed!")


if __name__ == '__main__':
    test_palette()
