// [DEF:frontend.src.lib.stores.__tests__.test_sidebar:Module]
// @TIER: STANDARD
// @PURPOSE: Unit tests for sidebar store
// @LAYER: UI
// @RELATION: VERIFIES -> frontend.src.lib.stores.sidebar

import { describe, it, expect, beforeEach, vi } from 'vitest';

// Mock browser environment
vi.mock('$app/environment', () => ({
  browser: true
}));

// Mock localStorage
const localStorageMock = (() => {
  let store = {};
  return {
    getItem: vi.fn((key) => store[key] || null),
    setItem: vi.fn((key, value) => { store[key] = value; }),
    clear: () => { store = {}; }
  };
})();

Object.defineProperty(global, 'localStorage', { value: localStorageMock });

describe('sidebar store', () => {
  // Reset modules to get fresh store
  beforeEach(async () => {
    localStorageMock.clear();
    vi.clearAllMocks();
    vi.resetModules();
  });

  it('should have correct initial state', async () => {
    const { sidebarStore } = await import('../sidebar.js');
    
    let state = null;
    const unsubscribe = sidebarStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.isExpanded).toBe(true);
    expect(state.activeCategory).toBe('dashboards');
    expect(state.activeItem).toBe('/dashboards');
    expect(state.isMobileOpen).toBe(false);
  });

  it('should toggle sidebar expansion', async () => {
    const { sidebarStore, toggleSidebar } = await import('../sidebar.js');
    
    let state = null;
    const unsub1 = sidebarStore.subscribe(s => { state = s; });
    unsub1();
    expect(state.isExpanded).toBe(true);
    
    toggleSidebar();
    
    const unsub2 = sidebarStore.subscribe(s => { state = s; });
    unsub2();
    expect(state.isExpanded).toBe(false);
    expect(localStorageMock.setItem).toHaveBeenCalled();
  });

  it('should set active category and item', async () => {
    const { sidebarStore, setActiveItem } = await import('../sidebar.js');
    
    setActiveItem('datasets', '/datasets');
    
    let state = null;
    const unsub = sidebarStore.subscribe(s => { state = s; });
    unsub();
    expect(state.activeCategory).toBe('datasets');
    expect(state.activeItem).toBe('/datasets');
    expect(localStorageMock.setItem).toHaveBeenCalled();
  });

  it('should set mobile open state', async () => {
    const { sidebarStore, setMobileOpen } = await import('../sidebar.js');
    
    setMobileOpen(true);
    
    let state = null;
    const unsub = sidebarStore.subscribe(s => { state = s; });
    unsub();
    expect(state.isMobileOpen).toBe(true);
  });

  it('should close mobile sidebar', async () => {
    const { sidebarStore, closeMobile } = await import('../sidebar.js');
    
    // First open mobile
    let state = null;
    sidebarStore.update(s => ({ ...s, isMobileOpen: true }));
    const unsub1 = sidebarStore.subscribe(s => { state = s; });
    unsub1();
    expect(state.isMobileOpen).toBe(true);
    
    closeMobile();
    
    const unsub2 = sidebarStore.subscribe(s => { state = s; });
    unsub2();
    expect(state.isMobileOpen).toBe(false);
  });

  it('should toggle mobile sidebar', async () => {
    const { sidebarStore, toggleMobileSidebar } = await import('../sidebar.js');
    
    toggleMobileSidebar();
    
    let state = null;
    const unsub1 = sidebarStore.subscribe(s => { state = s; });
    unsub1();
    expect(state.isMobileOpen).toBe(true);
    
    toggleMobileSidebar();
    
    const unsub2 = sidebarStore.subscribe(s => { state = s; });
    unsub2();
    expect(state.isMobileOpen).toBe(false);
  });

  it('should load state from localStorage', async () => {
    localStorageMock.getItem.mockReturnValue(JSON.stringify({
      isExpanded: false,
      activeCategory: 'storage',
      activeItem: '/storage',
      isMobileOpen: true
    }));
    
    // Re-import with localStorage populated
    vi.resetModules();
    const { sidebarStore } = await import('../sidebar.js');
    
    let state = null;
    const unsub = sidebarStore.subscribe(s => { state = s; });
    unsub();
    expect(state.isExpanded).toBe(false);
    expect(state.activeCategory).toBe('storage');
    expect(state.isMobileOpen).toBe(true);
  });
});

// [/DEF:frontend.src.lib.stores.__tests__.test_sidebar:Module]