from models.preset import Preset
import os

def test_preset():
    """Test Preset model CRUD operations."""
    # Clean up any existing test file
    if os.path.exists("Preset.json"):
        os.remove("Preset.json")
    
    presets = Preset()
    
    print("Testing create preset")
    preset_id = presets.create()
    print(f"Created preset with ID: {preset_id}")
    assert preset_id is not None
    assert preset_id in presets
    
    print("\nTesting read preset")
    preset = presets.read(preset_id)
    print(f"Read: {preset}")
    assert preset is not None
    assert preset["name"] == ""
    assert preset["pattern"] == ""
    
    print("\nTesting update preset")
    update_data = {
        "name": "test_preset",
        "pattern": "on",
        "colors": ["#FF0000", "#00FF00"],
        "delay": 100,
        "brightness": 127,
        "n1": 10,
        "n2": 20
    }
    result = presets.update(preset_id, update_data)
    assert result is True
    updated = presets.read(preset_id)
    assert updated["name"] == "test_preset"
    assert updated["pattern"] == "on"
    assert updated["delay"] == 100
    
    print("\nTesting list presets")
    preset_list = presets.list()
    print(f"Preset list: {preset_list}")
    assert preset_id in preset_list
    
    print("\nTesting delete preset")
    deleted = presets.delete(preset_id)
    assert deleted is True
    assert preset_id not in presets
    
    print("\nTesting read after delete")
    preset = presets.read(preset_id)
    assert preset is None
    
    print("\nAll preset tests passed!")


if __name__ == '__main__':
    test_preset()
