import { describe, it, expect, vi, beforeEach } from "vitest";
import { NotificationType } from "$lib/notifications/types";

const mockPlay = vi.fn();

vi.mock("$lib/notifications", () => ({
  soundPlayer: {
    play: mockPlay,
  },
}));

describe("achievement sounds", () => {
  beforeEach(() => {
    mockPlay.mockReset();
  });

  describe("playAchievementSound", () => {
    it("plays the achievement notification sound", async () => {
      const { playAchievementSound } = await import("./achievement");
      playAchievementSound();
      expect(mockPlay).toHaveBeenCalledWith(NotificationType.ACHIEVEMENT);
    });
  });

  describe("testAchievementSound", () => {
    it("calls playAchievementSound without throwing", async () => {
      const { testAchievementSound } = await import("./achievement");
      expect(() => testAchievementSound()).not.toThrow();
      expect(mockPlay).toHaveBeenCalledWith(NotificationType.ACHIEVEMENT);
    });

    it("catches errors from the sound player gracefully", async () => {
      mockPlay.mockImplementation(() => {
        throw new Error("Audio not available");
      });
      const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});

      const { testAchievementSound } = await import("./achievement");
      expect(() => testAchievementSound()).not.toThrow();
      expect(consoleErrorSpy).toHaveBeenCalledWith(
        "Error playing achievement sound:",
        expect.any(Error)
      );

      consoleErrorSpy.mockRestore();
    });
  });
});
