// #region SidebarTest [C:3] [TYPE Module] [SEMANTICS sidebar, store, test, navigation, toggle]
// @BRIEF Unit tests for sidebar store validating expansion toggles, active item selection, and mobile open/close transitions.
// @RELATION DEPENDS_ON -> [EXT:frontend:sidebar]

// #region SidebarTest:Module [TYPE Function]
// @SEMANTICS: sidebar, store, tests, mobile, navigation
// @PURPOSE: Unit tests for sidebar store
// @LAYER Tests
// @RELATION DEPENDS_ON -> [EXT:frontend:sidebar]
// @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.svelte.js';

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

describe('SidebarStore', () => {
  beforeEach(() => {
    // Reset sidebar to default state via available functions
    setActiveItem('dashboards', '/dashboards');
    closeMobile();
    // isExpanded defaults to true; if previous test toggled it, toggle back
    if (sidebarStore.value && !sidebarStore.value.isExpanded) {
      toggleSidebar();
    }
  });

  // #region test_sidebar_initial_state:Function [TYPE 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);
    });
  });
  // #endregion test_sidebar_initial_state:Function

  // #region test_toggleSidebar:Function [TYPE 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);
    });
  });
  // #endregion test_toggleSidebar:Function

  // #region test_setActiveItem:Function [TYPE Function]
// @PURPOSE: Verify active-category updates remain deterministic after item selection.
  // @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');
    });
  });
  // #endregion test_setActiveItem:Function

  // #region test_mobile_functions:Function [TYPE Function]
// @PURPOSE: Verify mobile sidebar helpers update open-state transitions predictably.
  // @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);
    });
  });
  // #endregion test_mobile_functions:Function
});

// #endregion SidebarTest:Module
// #endregion SidebarTest
