// [DEF:__tests__/test_topNavbar:Module]
// @TIER: STANDARD
// @PURPOSE: Unit tests for TopNavbar.svelte component
// @LAYER: UI
// @RELATION: VERIFIES -> frontend/src/lib/components/layout/TopNavbar.svelte

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

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

vi.mock('$app/stores', () => ({
  page: {
    subscribe: vi.fn((callback) => {
      callback({ url: { pathname: '/dashboards' } });
      return vi.fn();
    })
  }
}));

describe('TopNavbar Component Store Tests', () => {
  beforeEach(() => {
    vi.resetModules();
  });

  describe('Sidebar Store Integration', () => {
    it('should read isExpanded from sidebarStore', async () => {
      const { sidebarStore } = await import('$lib/stores/sidebar.js');
      
      let state = null;
      const unsubscribe = sidebarStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.isExpanded).toBe(true);
    });

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

  describe('Activity Store Integration', () => {
    it('should have zero activeCount initially', async () => {
      const { activityStore } = await import('$lib/stores/activity.js');
      
      let state = null;
      const unsubscribe = activityStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.activeCount).toBe(0);
    });

    it('should count RUNNING tasks as active', async () => {
      const { updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      const { activityStore } = await import('$lib/stores/activity.js');
      
      // Add a running task
      updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
      
      let state = null;
      const unsubscribe = activityStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.activeCount).toBe(1);
    });

    it('should not count SUCCESS tasks as active', async () => {
      const { updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      const { activityStore } = await import('$lib/stores/activity.js');
      
      // Add a success task
      updateResourceTask('dashboard-1', 'task-1', 'SUCCESS');
      
      let state = null;
      const unsubscribe = activityStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.activeCount).toBe(0);
    });

    it('should not count WAITING_INPUT as active', async () => {
      const { updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      const { activityStore } = await import('$lib/stores/activity.js');
      
      // Add a waiting input task - should NOT be counted as active per contract
      // Only RUNNING tasks count as active
      updateResourceTask('dashboard-1', 'task-1', 'WAITING_INPUT');
      
      let state = null;
      const unsubscribe = activityStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.activeCount).toBe(0);
    });
  });

  describe('Task Drawer Integration', () => {
    it('should open drawer for specific task', async () => {
      const { taskDrawerStore, openDrawerForTask } = await import('$lib/stores/taskDrawer.js');
      
      openDrawerForTask('task-123');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.isOpen).toBe(true);
      expect(state.activeTaskId).toBe('task-123');
    });

    it('should open drawer in list mode', async () => {
      const { taskDrawerStore, openDrawer } = await import('$lib/stores/taskDrawer.js');
      
      openDrawer();
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.isOpen).toBe(true);
      expect(state.activeTaskId).toBeNull();
    });

    it('should close drawer', async () => {
      const { taskDrawerStore, openDrawerForTask, closeDrawer } = await import('$lib/stores/taskDrawer.js');
      
      // First open drawer
      openDrawerForTask('task-123');
      
      let state = null;
      const unsub1 = taskDrawerStore.subscribe(s => { state = s; });
      unsub1();
      expect(state.isOpen).toBe(true);
      
      closeDrawer();
      
      const unsub2 = taskDrawerStore.subscribe(s => { state = s; });
      unsub2();
      expect(state.isOpen).toBe(false);
    });
  });

  describe('UX States', () => {
    it('should support activity badge with count > 0', async () => {
      const { updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      const { activityStore } = await import('$lib/stores/activity.js');
      
      updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
      updateResourceTask('dashboard-2', 'task-2', 'RUNNING');
      
      let state = null;
      const unsubscribe = activityStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.activeCount).toBe(2);
      expect(state.activeCount).toBeGreaterThan(0);
    });

    it('should show 9+ for counts exceeding 9', async () => {
      const { updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      const { activityStore } = await import('$lib/stores/activity.js');
      
      // Add 10 running tasks
      for (let i = 0; i < 10; i++) {
        updateResourceTask(`resource-${i}`, `task-${i}`, 'RUNNING');
      }
      
      let state = null;
      const unsubscribe = activityStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.activeCount).toBe(10);
    });
  });
});

// [/DEF:__tests__/test_topNavbar:Module]