import { describe, it, expect, vi } from "vitest";
import { runQrCodeGenerator } from "./run";
import type { ToolContext } from "../../core/plugins/plugin-types";

describe("QR Code Generator Plugin", () => {
  const mockContext: ToolContext = {
    signal: new AbortController().signal,
    reportProgress: vi.fn(),
    logger: {
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    },
  };

  it("should generate SVG QR Code successfully", async () => {
    const result = await runQrCodeGenerator(
      { text: "https://plimi.app" },
      { size: 256, margin: 4, errorCorrection: "M", format: "svg" },
      mockContext
    );
    expect(result.type).toBe("files");
    if (result.type === "files") {
      expect(result.files).toHaveLength(1);
      expect(result.files[0].name).toBe("qrcode.svg");
      expect(result.files[0].mimeType).toBe("image/svg+xml");
      expect(result.files[0].blob).toBeInstanceOf(Blob);
    }
  });

  it("should generate PNG QR Code successfully", async () => {
    const result = await runQrCodeGenerator(
      { text: "https://plimi.app" },
      { size: 256, margin: 4, errorCorrection: "M", format: "png" },
      mockContext
    );
    expect(result.type).toBe("files");
    if (result.type === "files") {
      expect(result.files).toHaveLength(1);
      expect(result.files[0].name).toBe("qrcode.png");
      expect(result.files[0].mimeType).toBe("image/png");
      expect(result.files[0].blob).toBeInstanceOf(Blob);
    }
  });

  it("should throw error for empty input text", async () => {
    await expect(
      runQrCodeGenerator({ text: "" }, { size: 256, margin: 4, errorCorrection: "M", format: "svg" }, mockContext)
    ).rejects.toThrow("Please enter text or a URL to encode in the QR Code.");
  });
});
