import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
import { handlerRegistry, QueueManager } from '../src';

describe('QueueManager Simple Tests', () => {
  let queueManager: QueueManager;

  // Assumes Redis is running locally on default port
  const redisConfig = {
    host: 'localhost',
    port: 6379,
  };

  beforeEach(() => {
    handlerRegistry.clear();
  });

  afterEach(async () => {
    if (queueManager) {
      try {
        await queueManager.shutdown();
      } catch {
        // Ignore errors during cleanup
      }
    }
  });

  test('should create queue manager instance', () => {
    queueManager = new QueueManager({
      queueName: 'test-queue',
      redis: redisConfig,
    });

    expect(queueManager.queueName).toBe('test-queue');
  });

  test('should handle missing Redis gracefully', async () => {
    // Use a port that's likely not running Redis
    queueManager = new QueueManager({
      queueName: 'test-queue',
      redis: {
        host: 'localhost',
        port: 9999,
      },
    });

    await expect(queueManager.initialize()).rejects.toThrow();
  });

  test('handler registry should work', () => {
    const testHandler = async (payload: any) => {
      return { success: true, payload };
    };

    handlerRegistry.register('test-handler', {
      'test-op': testHandler,
    });

    const handler = handlerRegistry.getHandler('test-handler', 'test-op');
    expect(handler).toBe(testHandler);
  });

  test('handler registry should return null for missing handler', () => {
    const handler = handlerRegistry.getHandler('missing', 'op');
    expect(handler).toBe(null);
  });

  test('should get handler statistics', () => {
    handlerRegistry.register('handler1', {
      op1: async () => ({}),
      op2: async () => ({}),
    });

    handlerRegistry.register('handler2', {
      op1: async () => ({}),
    });

    const stats = handlerRegistry.getStats();
    expect(stats.handlers).toBe(2);
    expect(stats.totalOperations).toBe(3);
  });
});
