import { describe, it, expect, vi, beforeEach } from "vitest";
import { QuickActionBusyManagerImpl } from "./quick-action-busy-manager";
import { BROKER_EVENTS } from "../broker/broker-events";

describe("QuickActionBusyManagerImpl", () => {
  let brokerMock: any;
  let manager: QuickActionBusyManagerImpl;

  beforeEach(() => {
    brokerMock = {
      publish: vi.fn(),
    };
    manager = new QuickActionBusyManagerImpl(brokerMock);
  });

  describe("addTask", () => {
    it("should add a task and emit event", () => {
      const task = { taskId: "qa1" };
      manager.addTask(task);
      expect(brokerMock.publish).toHaveBeenCalledWith(BROKER_EVENTS.shell.quickActionBusyChanged, {
        busy: true,
      });
    });

    it("should not add duplicate task with same taskId", () => {
      const task = { taskId: "qa1" };
      manager.addTask(task);
      manager.addTask(task);

      // Should only emit once for the first addition
      expect(brokerMock.publish).toHaveBeenCalledTimes(1);
    });
  });

  describe("removeTask", () => {
    it("should remove a task and emit event", () => {
      const task = { taskId: "qa1" };
      manager.addTask(task);
      manager.removeTask("qa1");
      expect(brokerMock.publish).toHaveBeenCalledWith(BROKER_EVENTS.shell.quickActionBusyChanged, {
        busy: false,
      });
    });

    it("should do nothing and not emit event if task does not exist", () => {
      manager.removeTask("non-existent");
      expect(brokerMock.publish).not.toHaveBeenCalled();
    });
  });

  describe("isBusy", () => {
    it("should return false when no tasks exist", () => {
      expect(manager.isBusy()).toBe(false);
    });

    it("should return true when tasks exist", () => {
      manager.addTask({ taskId: "qa1" });
      expect(manager.isBusy()).toBe(true);
    });
  });

  describe("clearAll", () => {
    it("should clear all tasks", () => {
      manager.addTask({ taskId: "qa1" });
      manager.clearAll();
      expect(manager.isBusy()).toBe(false);
    });
  });
});
