import { describe, expect, it } from 'bun:test';
import { RedisConnectionManager } from '../src/connection-manager';

describe('RedisConnectionManager', () => {
  it('should be a singleton', () => {
    const instance1 = RedisConnectionManager.getInstance();
    const instance2 = RedisConnectionManager.getInstance();
    expect(instance1).toBe(instance2);
  });

  it('should create connections', () => {
    const manager = RedisConnectionManager.getInstance();
    const connection = manager.getConnection({
      name: 'test',
      redisConfig: {
        host: 'localhost',
        port: 6379,
      },
    });

    expect(connection).toBeDefined();
    expect(connection.options.host).toBe('localhost');
    expect(connection.options.port).toBe(6379);
  });

  it('should reuse singleton connections', () => {
    const manager = RedisConnectionManager.getInstance();

    const conn1 = manager.getConnection({
      name: 'shared',
      singleton: true,
      redisConfig: {
        host: 'localhost',
        port: 6379,
      },
    });

    const conn2 = manager.getConnection({
      name: 'shared',
      singleton: true,
      redisConfig: {
        host: 'localhost',
        port: 6379,
      },
    });

    expect(conn1).toBe(conn2);
  });

  it('should create separate non-singleton connections', () => {
    const manager = RedisConnectionManager.getInstance();

    const conn1 = manager.getConnection({
      name: 'separate1',
      singleton: false,
      redisConfig: {
        host: 'localhost',
        port: 6379,
      },
    });

    const conn2 = manager.getConnection({
      name: 'separate2',
      singleton: false,
      redisConfig: {
        host: 'localhost',
        port: 6379,
      },
    });

    expect(conn1).not.toBe(conn2);
  });

  it('should close all connections', async () => {
    const manager = RedisConnectionManager.getInstance();

    // Create a few connections
    manager.getConnection({
      name: 'close-test-1',
      redisConfig: { host: 'localhost', port: 6379 },
    });

    manager.getConnection({
      name: 'close-test-2',
      redisConfig: { host: 'localhost', port: 6379 },
    });

    // Close all
    await RedisConnectionManager.closeAll();

    // Should not throw
    expect(true).toBe(true);
  });
});
