// #region ActivityTestModule [C:3] [TYPE Module] [SEMANTICS activity, store, test, task-count, badge]
// @BRIEF Unit tests validating activity store derived count and recent task tracking behavior.
// @RELATION DEPENDS_ON -> [ActivityStore]
// @RELATION DEPENDS_ON -> [TaskDrawerStore]

// #region ActivityTest:Module [TYPE Function]
// @PURPOSE: Unit tests for activity store
// @RELATION DEPENDS_ON -> [EXT:frontend:activity]
// @RELATION DEPENDS_ON -> [EXT:frontend:taskDrawer]

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

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

  it('should have zero active count initially', async () => {
    const { activityStore } = await import('../activity.svelte.js');
    
    let state = null;
    const unsubscribe = activityStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.activeCount).toBe(0);
    expect(state.recentTasks).toEqual([]);
  });

  it('should count RUNNING tasks as active', async () => {
    const { taskDrawerStore, updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.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('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.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 ERROR tasks as active', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    // Add an error task
    updateResourceTask('dashboard-1', 'task-1', 'ERROR');
    
    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('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.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);
  });

  it('should track multiple running tasks', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    // Add multiple running tasks
    updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
    updateResourceTask('dashboard-2', 'task-2', 'RUNNING');
    updateResourceTask('dataset-1', 'task-3', 'RUNNING');
    
    let state = null;
    const unsubscribe = activityStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.activeCount).toBe(3);
  });

  it('should return recent tasks', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    // Add multiple tasks
    updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
    updateResourceTask('dataset-1', 'task-2', 'SUCCESS');
    updateResourceTask('storage-1', 'task-3', 'ERROR');
    
    let state = null;
    const unsubscribe = activityStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.recentTasks.length).toBeGreaterThan(0);
    expect(state.recentTasks[0]).toHaveProperty('taskId');
    expect(state.recentTasks[0]).toHaveProperty('resourceId');
    expect(state.recentTasks[0]).toHaveProperty('status');
  });
});

// #endregion ActivityTest:Module
// #endregion ActivityTestModule
