// @RELATION: VERIFIES -> frontend/src/lib/stores/sidebar.js
// [DEF:frontend.src.lib.stores.__tests__.sidebar:Module]
// @TIER: STANDARD
// @SEMANTICS: sidebar, store, tests, mobile, navigation
// @PURPOSE: Unit tests for sidebar store
// @LAYER: Domain (Tests)
// @INVARIANT: Sidebar store transitions must be deterministic across desktop/mobile toggles.

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { get } from 'svelte/store';
import { sidebarStore, toggleSidebar, setActiveItem, setMobileOpen, closeMobile, toggleMobileSidebar } from '../sidebar.js';

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

describe('SidebarStore', () => {
  beforeEach(() => {
    sidebarStore.set({
      isExpanded: true,
      activeCategory: 'dashboards',
      activeItem: '/dashboards',
      isMobileOpen: false
    });
  });

  // [DEF:test_sidebar_initial_state:Function]
  // @PURPOSE: Verify initial sidebar store values when no persisted state is available.
  // @TEST: Store initializes with default values
  // @PRE: No localStorage state
  // @POST: Default state is { isExpanded: true, activeCategory: 'dashboards', activeItem: '/dashboards', isMobileOpen: false }
  describe('initial state', () => {
    it('should have default values when no localStorage', () => {
      const state = get(sidebarStore);

      expect(state.isExpanded).toBe(true);
      expect(state.activeCategory).toBe('dashboards');
      expect(state.activeItem).toBe('/dashboards');
      expect(state.isMobileOpen).toBe(false);
    });
  });
  // [/DEF:test_sidebar_initial_state:Function]

  // [DEF:test_toggleSidebar:Function]
  // @PURPOSE: Verify desktop sidebar expansion toggles deterministically.
  // @TEST: toggleSidebar toggles isExpanded state
  // @PRE: Store is initialized
  // @POST: isExpanded is toggled from previous value
  describe('toggleSidebar', () => {
    it('should toggle isExpanded from true to false', () => {
      const initialState = get(sidebarStore);
      expect(initialState.isExpanded).toBe(true);

      toggleSidebar();

      const newState = get(sidebarStore);
      expect(newState.isExpanded).toBe(false);
    });

    it('should toggle isExpanded from false to true', () => {
      toggleSidebar(); // Now false
      toggleSidebar(); // Should be true again

      const state = get(sidebarStore);
      expect(state.isExpanded).toBe(true);
    });
  });
  // [/DEF:test_toggleSidebar:Function]

  // [DEF:test_setActiveItem:Function]
  // @TEST: setActiveItem updates activeCategory and activeItem
  // @PRE: Store is initialized
  // @POST: activeCategory and activeItem are updated
  describe('setActiveItem', () => {
    it('should update activeCategory and activeItem', () => {
      setActiveItem('datasets', '/datasets');

      const state = get(sidebarStore);
      expect(state.activeCategory).toBe('datasets');
      expect(state.activeItem).toBe('/datasets');
    });

    it('should update to admin category', () => {
      setActiveItem('admin', '/settings');

      const state = get(sidebarStore);
      expect(state.activeCategory).toBe('admin');
      expect(state.activeItem).toBe('/settings');
    });
  });
  // [/DEF:test_setActiveItem:Function]

  // [DEF:test_mobile_functions:Function]
  // @TEST: Mobile functions correctly update isMobileOpen
  // @PRE: Store is initialized
  // @POST: isMobileOpen is correctly updated
  describe('mobile functions', () => {
    it('should set isMobileOpen to true with setMobileOpen', () => {
      setMobileOpen(true);

      const state = get(sidebarStore);
      expect(state.isMobileOpen).toBe(true);
    });

    it('should set isMobileOpen to false with closeMobile', () => {
      setMobileOpen(true);
      closeMobile();

      const state = get(sidebarStore);
      expect(state.isMobileOpen).toBe(false);
    });

    it('should toggle isMobileOpen with toggleMobileSidebar', () => {
      const initialState = get(sidebarStore);
      const initialMobileOpen = initialState.isMobileOpen;

      toggleMobileSidebar();

      const state1 = get(sidebarStore);
      expect(state1.isMobileOpen).toBe(!initialMobileOpen);

      toggleMobileSidebar();

      const state2 = get(sidebarStore);
      expect(state2.isMobileOpen).toBe(initialMobileOpen);
    });
  });
  // [/DEF:test_mobile_functions:Function]
});

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