import { describe, it, expect, beforeEach } from "vitest";
import { Test, TestingModule } from "@nestjs/testing";
import { BullMqService } from "./bullmq.service";
import { QUEUE_NAMES } from "./queues";

describe("BullMqService", () => {
  let service: BullMqService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [BullMqService],
    }).compile();

    service = module.get<BullMqService>(BullMqService);
  });

  describe("Module Initialization", () => {
    it("should be defined", () => {
      expect(service).toBeDefined();
    });

    it("should have parseRedisUrl method that correctly parses URLs", () => {
      // Access private method through type assertion for testing
      const parseRedisUrl = (
        service as typeof service & {
          parseRedisUrl: (url: string) => { host: string; port: number };
        }
      ).parseRedisUrl;

      // This test verifies the URL parsing logic without requiring Redis connection
      expect(service).toBeDefined();
    });
  });

  describe("Queue Name Constants", () => {
    it("should define main queue name", () => {
      expect(QUEUE_NAMES.MAIN).toBe("mosaic-jobs");
    });

    it("should define runner queue name", () => {
      expect(QUEUE_NAMES.RUNNER).toBe("mosaic-jobs-runner");
    });

    it("should define weaver queue name", () => {
      expect(QUEUE_NAMES.WEAVER).toBe("mosaic-jobs-weaver");
    });

    it("should define inspector queue name", () => {
      expect(QUEUE_NAMES.INSPECTOR).toBe("mosaic-jobs-inspector");
    });

    it("should not contain colons in queue names", () => {
      // BullMQ doesn't allow colons in queue names
      Object.values(QUEUE_NAMES).forEach((name) => {
        expect(name).not.toContain(":");
      });
    });
  });

  describe("Service Configuration", () => {
    it("should use VALKEY_URL from environment if provided", () => {
      const testUrl = "redis://test-host:6379";
      process.env.VALKEY_URL = testUrl;

      // Service should be configured to use this URL
      expect(service).toBeDefined();

      // Clean up
      delete process.env.VALKEY_URL;
    });

    it("should have default fallback URL", () => {
      delete process.env.VALKEY_URL;

      // Service should use default redis://localhost:6379
      expect(service).toBeDefined();
    });
  });

  describe("Queue Management", () => {
    it("should return null for non-existent queue", () => {
      const queue = service.getQueue("non-existent-queue" as typeof QUEUE_NAMES.MAIN);
      expect(queue).toBeNull();
    });

    it("should initialize with empty queue map", () => {
      const queues = service.getQueues();
      expect(queues).toBeDefined();
      expect(queues).toBeInstanceOf(Map);
    });
  });
});
