import { describe, it, expect, vi } from "vitest";
import { platform } from "@tauri-apps/plugin-os";
import { invoke } from "@tauri-apps/api/core";
import { sendWSLNotification } from "./wslNotificationHelper";

// platform() is mocked in vitest.setup.ts to return "linux" by default

describe("sendWSLNotification", () => {
  it("invokes send_windows_notification when platform is windows", async () => {
    vi.mocked(platform).mockResolvedValueOnce("windows" as Awaited<ReturnType<typeof platform>>);

    await sendWSLNotification("Test Title", "Test body");

    expect(invoke).toHaveBeenCalledWith("send_windows_notification", {
      title: "Test Title",
      body: "Test body",
    });
  });

  it("does not invoke on non-Windows platforms", async () => {
    // Default mock returns "linux"
    await sendWSLNotification("Test Title", "Test body");

    expect(invoke).not.toHaveBeenCalled();
  });

  it("handles invoke errors gracefully and logs them", async () => {
    vi.mocked(platform).mockResolvedValueOnce("windows" as Awaited<ReturnType<typeof platform>>);
    vi.mocked(invoke).mockRejectedValueOnce(new Error("Windows notification failed"));
    const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});

    await sendWSLNotification("Title", "Body");

    expect(errorSpy).toHaveBeenCalledWith(
      "Failed to send Windows notification:",
      expect.any(Error)
    );
    errorSpy.mockRestore();
  });
});
