from models.tab import Tab
import os

def test_tab():
    """Test Tab model CRUD operations."""
    # Clean up any existing test file
    if os.path.exists("Tab.json"):
        os.remove("Tab.json")
    
    tabs = Tab()
    
    print("Testing create tab")
    tab_id = tabs.create("test_tab", ["1", "2", "3"], ["preset1", "preset2"])
    print(f"Created tab with ID: {tab_id}")
    assert tab_id is not None
    assert tab_id in tabs
    
    print("\nTesting read tab")
    tab = tabs.read(tab_id)
    print(f"Read: {tab}")
    assert tab is not None
    assert tab["name"] == "test_tab"
    assert len(tab["names"]) == 3
    assert len(tab["presets"]) == 2
    
    print("\nTesting update tab")
    update_data = {
        "name": "updated_tab",
        "names": ["4", "5"],
        "presets": ["preset3"]
    }
    result = tabs.update(tab_id, update_data)
    assert result is True
    updated = tabs.read(tab_id)
    assert updated["name"] == "updated_tab"
    assert len(updated["names"]) == 2
    assert len(updated["presets"]) == 1
    
    print("\nTesting list tabs")
    tab_list = tabs.list()
    print(f"Tab list: {tab_list}")
    assert tab_id in tab_list
    
    print("\nTesting delete tab")
    deleted = tabs.delete(tab_id)
    assert deleted is True
    assert tab_id not in tabs
    
    print("\nTesting read after delete")
    tab = tabs.read(tab_id)
    assert tab is None
    
    print("\nAll tab tests passed!")


if __name__ == '__main__':
    test_tab()
