// #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');
  });

  it('should limit recent tasks to 5 entries', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    // Add 7 tasks
    for (let i = 1; i <= 7; i++) {
      updateResourceTask(`resource-${i}`, `task-${i}`, i <= 3 ? 'RUNNING' : 'SUCCESS');
    }
    
    let state = null;
    const unsubscribe = activityStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.recentTasks.length).toBeLessThanOrEqual(5);
  });

  it('should decrease active count when a RUNNING task completes', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
    updateResourceTask('dashboard-2', 'task-2', 'RUNNING');
    
    let state1 = null;
    const unsub1 = activityStore.subscribe(s => { state1 = s; });
    unsub1();
    expect(state1.activeCount).toBe(2);
    
    // Complete one task
    updateResourceTask('dashboard-1', 'task-1', 'SUCCESS');
    
    let state2 = null;
    const unsub2 = activityStore.subscribe(s => { state2 = s; });
    unsub2();
    expect(state2.activeCount).toBe(1);
  });

  it('should handle unsubscribe correctly', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    const received: any[] = [];
    const unsub = activityStore.subscribe(s => received.push(s));
    
    updateResourceTask('dashboard-1', 'task-1', 'RUNNING');
    
    const countBeforeUnsub = received.length;
    unsub();
    
    updateResourceTask('dashboard-2', 'task-2', 'RUNNING');
    expect(received.length).toBe(countBeforeUnsub);
  });

  it('activityStore.get value should return current state', async () => {
    const { activityStore } = await import('../activity.svelte.js');
    
    const state = activityStore.value;
    expect(state.activeCount).toBe(0);
    expect(state.recentTasks).toEqual([]);
  });

  it('should handle mixed task statuses correctly', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    // Add RUNNING tasks — these stay in the map and count as active
    updateResourceTask('r1', 't1', 'RUNNING');
    updateResourceTask('r2', 't2', 'RUNNING');
    // WAITING_INPUT stays in map but is not active (not RUNNING)
    updateResourceTask('r3', 't3', 'WAITING_INPUT');
    
    let state = null;
    const unsubscribe = activityStore.subscribe(s => { state = s; });
    unsubscribe();
    
    // Note: IDLE/SUCCESS/ERROR tasks are removed from map by updateResourceTask
    expect(state.activeCount).toBe(2); // Only RUNNING tasks
    expect(state.recentTasks.length).toBe(3); // r1, r2, r3 in map
  });

  it('should track recentTasks with correct resourceIds', async () => {
    const { updateResourceTask } = await import('../taskDrawer.svelte.js');
    const { activityStore } = await import('../activity.svelte.js');
    
    updateResourceTask('dashboard-main', 'task-42', 'RUNNING');
    
    let state = null;
    const unsubscribe = activityStore.subscribe(s => { state = s; });
    unsubscribe();
    
    expect(state.recentTasks[0].resourceId).toBe('dashboard-main');
    expect(state.recentTasks[0].taskId).toBe('task-42');
    expect(state.recentTasks[0].status).toBe('RUNNING');
  });
});

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