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

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

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

  describe('Initial State', () => {
    it('should have isOpen false initially', async () => {
      const { taskDrawerStore } = await import('$lib/stores/taskDrawer.js');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.isOpen).toBe(false);
    });

    it('should have null activeTaskId initially', async () => {
      const { taskDrawerStore } = await import('$lib/stores/taskDrawer.js');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.activeTaskId).toBeNull();
    });

    it('should have empty resourceTaskMap initially', async () => {
      const { taskDrawerStore } = await import('$lib/stores/taskDrawer.js');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.resourceTaskMap).toEqual({});
    });
  });

  describe('UX States - Open/Close', () => {
    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);
      expect(state.activeTaskId).toBeNull();
    });
  });

  describe('Resource-Task Mapping', () => {
    it('should update resource-task mapping', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('dashboard-1', 'task-123', 'RUNNING');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.resourceTaskMap['dashboard-1']).toEqual({
        taskId: 'task-123',
        status: 'RUNNING'
      });
    });

    it('should remove mapping on SUCCESS status', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      // First add a running task
      updateResourceTask('dashboard-1', 'task-123', 'RUNNING');
      
      let state = null;
      const unsub1 = taskDrawerStore.subscribe(s => { state = s; });
      unsub1();
      expect(state.resourceTaskMap['dashboard-1']).toBeDefined();
      
      // Complete the task
      updateResourceTask('dashboard-1', 'task-123', 'SUCCESS');
      
      const unsub2 = taskDrawerStore.subscribe(s => { state = s; });
      unsub2();
      expect(state.resourceTaskMap['dashboard-1']).toBeUndefined();
    });

    it('should remove mapping on ERROR status', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('dataset-1', 'task-456', 'RUNNING');
      
      let state = null;
      const unsub1 = taskDrawerStore.subscribe(s => { state = s; });
      unsub1();
      expect(state.resourceTaskMap['dataset-1']).toBeDefined();
      
      // Error the task
      updateResourceTask('dataset-1', 'task-456', 'ERROR');
      
      const unsub2 = taskDrawerStore.subscribe(s => { state = s; });
      unsub2();
      expect(state.resourceTaskMap['dataset-1']).toBeUndefined();
    });

    it('should remove mapping on IDLE status', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('storage-1', 'task-789', 'RUNNING');
      
      let state = null;
      const unsub1 = taskDrawerStore.subscribe(s => { state = s; });
      unsub1();
      expect(state.resourceTaskMap['storage-1']).toBeDefined();
      
      // Set to IDLE
      updateResourceTask('storage-1', 'task-789', 'IDLE');
      
      const unsub2 = taskDrawerStore.subscribe(s => { state = s; });
      unsub2();
      expect(state.resourceTaskMap['storage-1']).toBeUndefined();
    });

    it('should keep mapping for WAITING_INPUT status', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('dashboard-1', 'task-789', 'WAITING_INPUT');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.resourceTaskMap['dashboard-1']).toEqual({
        taskId: 'task-789',
        status: 'WAITING_INPUT'
      });
    });

    it('should keep mapping for RUNNING status', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('dashboard-1', 'task-abc', 'RUNNING');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(state.resourceTaskMap['dashboard-1']).toEqual({
        taskId: 'task-abc',
        status: 'RUNNING'
      });
    });
  });

  describe('Task Retrieval', () => {
    it('should get task for resource', async () => {
      const { updateResourceTask, getTaskForResource } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('dashboard-1', 'task-123', 'RUNNING');
      
      const taskInfo = getTaskForResource('dashboard-1');
      expect(taskInfo).toEqual({
        taskId: 'task-123',
        status: 'RUNNING'
      });
    });

    it('should return null for resource without task', async () => {
      const { getTaskForResource } = await import('$lib/stores/taskDrawer.js');
      
      const taskInfo = getTaskForResource('non-existent');
      expect(taskInfo).toBeNull();
    });
  });

  describe('Multiple Resources', () => {
    it('should handle multiple resource-task mappings', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
      updateResourceTask('dashboard-2', 'task-2', 'RUNNING');
      updateResourceTask('dataset-1', 'task-3', 'WAITING_INPUT');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      expect(Object.keys(state.resourceTaskMap).length).toBe(3);
    });

    it('should update existing mapping', async () => {
      const { taskDrawerStore, updateResourceTask } = await import('$lib/stores/taskDrawer.js');
      
      updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
      updateResourceTask('dashboard-1', 'task-2', 'SUCCESS');
      
      let state = null;
      const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
      unsubscribe();
      
      // Should be removed due to SUCCESS status
      expect(state.resourceTaskMap['dashboard-1']).toBeUndefined();
    });
  });
});

// [/DEF:__tests__/test_taskDrawer:Module]