from models.squence import Sequence
import os

def test_sequence():
    """Test Sequence model CRUD operations."""
    # Clean up any existing test file
    if os.path.exists("Sequence.json"):
        os.remove("Sequence.json")
    
    sequences = Sequence()
    
    print("Testing create sequence")
    sequence_id = sequences.create("test_group", ["preset1", "preset2"])
    print(f"Created sequence with ID: {sequence_id}")
    assert sequence_id is not None
    assert sequence_id in sequences
    
    print("\nTesting read sequence")
    sequence = sequences.read(sequence_id)
    print(f"Read: {sequence}")
    assert sequence is not None
    assert sequence["group_name"] == "test_group"
    assert len(sequence["presets"]) == 2
    assert "sequence_duration" in sequence
    assert "sequence_loop" in sequence
    
    print("\nTesting update sequence")
    update_data = {
        "group_name": "updated_group",
        "presets": ["preset3", "preset4", "preset5"],
        "sequence_duration": 5000,
        "sequence_transition": 1000,
        "sequence_loop": True,
        "sequence_repeat_count": 3
    }
    result = sequences.update(sequence_id, update_data)
    assert result is True
    updated = sequences.read(sequence_id)
    assert updated["group_name"] == "updated_group"
    assert len(updated["presets"]) == 3
    assert updated["sequence_duration"] == 5000
    assert updated["sequence_loop"] is True
    
    print("\nTesting list sequences")
    sequence_list = sequences.list()
    print(f"Sequence list: {sequence_list}")
    assert sequence_id in sequence_list
    
    print("\nTesting delete sequence")
    deleted = sequences.delete(sequence_id)
    assert deleted is True
    assert sequence_id not in sequences
    
    print("\nTesting read after delete")
    sequence = sequences.read(sequence_id)
    assert sequence is None
    
    print("\nAll sequence tests passed!")


if __name__ == '__main__':
    test_sequence()
