// [DEF:frontend.src.lib.stores.__tests__.test_taskDrawer:Module]
// @TIER: STANDARD
// @SEMANTICS: task-drawer, store, mapping, tests
// @PURPOSE: Unit tests for task drawer store
// @LAYER: UI
// @RELATION: VERIFIES -> frontend.src.lib.stores.taskDrawer
// @INVARIANT: Store state transitions remain deterministic for open/close and task-status mapping.

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

describe('taskDrawer store', () => {
  beforeEach(async () => {
    vi.resetModules();
  });

  it('should have correct initial state', async () => {
    const { taskDrawerStore } = await import('../taskDrawer.js');
    
    let state = null;
    const unsubscribe = taskDrawerStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.isOpen).toBe(false);
    expect(state.activeTaskId).toBeNull();
    expect(state.resourceTaskMap).toEqual({});
  });

  it('should open drawer for specific task', async () => {
    const { taskDrawerStore, openDrawerForTask } = await import('../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('../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('../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();
  });

  it('should update resource-task mapping', async () => {
    const { taskDrawerStore, updateResourceTask } = await import('../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 task completion (SUCCESS)', async () => {
    const { taskDrawerStore, updateResourceTask } = await import('../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 task error', async () => {
    const { taskDrawerStore, updateResourceTask } = await import('../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 keep mapping for WAITING_INPUT status', async () => {
    const { taskDrawerStore, updateResourceTask } = await import('../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 get task for resource', async () => {
    const { updateResourceTask, getTaskForResource } = await import('../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('../taskDrawer.js');
    
    const taskInfo = getTaskForResource('non-existent');
    expect(taskInfo).toBeNull();
  });
});

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