from models.group import Group
import os

def test_group():
    """Test Group model CRUD operations."""
    # Clean up any existing test file
    if os.path.exists("Group.json"):
        os.remove("Group.json")
    
    groups = Group()
    
    print("Testing create group")
    group_id = groups.create("test_group")
    print(f"Created group with ID: {group_id}")
    assert group_id is not None
    assert group_id in groups
    
    print("\nTesting read group")
    group = groups.read(group_id)
    print(f"Read: {group}")
    assert group is not None
    assert group["name"] == "test_group"
    assert "devices" in group
    assert "pattern" in group
    assert "colors" in group
    
    print("\nTesting update group")
    update_data = {
        "name": "updated_group",
        "devices": ["device1", "device2"],
        "pattern": "rainbow",
        "colors": ["#FF0000", "#00FF00", "#0000FF"],
        "brightness": 80,
        "delay": 50
    }
    result = groups.update(group_id, update_data)
    assert result is True
    updated = groups.read(group_id)
    assert updated["name"] == "updated_group"
    assert len(updated["devices"]) == 2
    assert updated["pattern"] == "rainbow"
    
    print("\nTesting list groups")
    group_list = groups.list()
    print(f"Group list: {group_list}")
    assert group_id in group_list
    
    print("\nTesting delete group")
    deleted = groups.delete(group_id)
    assert deleted is True
    assert group_id not in groups
    
    print("\nTesting read after delete")
    group = groups.read(group_id)
    assert group is None
    
    print("\nAll group tests passed!")


if __name__ == '__main__':
    test_group()
