import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { testAllNotifications } from "./testNotifications";

vi.mock("./notificationManager", () => ({
  notificationManager: {
    notifySuccess: vi.fn().mockResolvedValue(undefined),
    notifyError: vi.fn().mockResolvedValue(undefined),
    notifyPermission: vi.fn().mockResolvedValue(undefined),
    notifyConnection: vi.fn().mockResolvedValue(undefined),
    notifyTaskStart: vi.fn().mockResolvedValue(undefined),
  },
}));

describe("testNotifications", () => {
  describe("window assignment", () => {
    it("assigns testAllNotifications to window.testNotifications", async () => {
      // The module-level if block runs on import — reimport to ensure it ran
      await import("./testNotifications");

      expect((window as unknown as { testNotifications: unknown }).testNotifications).toBe(
        testAllNotifications
      );
    });
  });

  describe("testAllNotifications", () => {
    beforeEach(() => {
      vi.useFakeTimers();
    });

    afterEach(() => {
      vi.useRealTimers();
    });

    it("is an async function", () => {
      expect(typeof testAllNotifications).toBe("function");
    });

    it("schedules all five notification type calls", async () => {
      const { notificationManager } = await import("./notificationManager");

      const consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => {});

      await testAllNotifications();
      await vi.runAllTimersAsync();

      expect(notificationManager.notifySuccess).toHaveBeenCalledWith("Test task completed!");
      expect(notificationManager.notifyError).toHaveBeenCalledWith("Test error occurred!");
      expect(notificationManager.notifyPermission).toHaveBeenCalledWith("Test permission request!");
      expect(notificationManager.notifyConnection).toHaveBeenCalledWith(
        "Test connection established!"
      );
      expect(notificationManager.notifyTaskStart).toHaveBeenCalledWith("Test task starting!");

      consoleLogSpy.mockRestore();
    });
  });
});
