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

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

  it("should throw error if no files provided", async () => {
    await expect(
      runImageOptimizer(
        { files: [] },
        { quality: 80, format: "image/webp" },
        mockContext
      )
    ).rejects.toThrow("No files provided for optimization.");
  });

  it("should throw error if file is not an image", async () => {
    const textFile = new File(["hello"], "hello.txt", { type: "text/plain" });

    await expect(
      runImageOptimizer(
        { files: [textFile] },
        { quality: 80, format: "image/webp" },
        mockContext
      )
    ).rejects.toThrow("File hello.txt is not an image.");
  });

  // Note: Testing actual Canvas API compression requires a full browser environment
  // or a mock canvas library in Vitest. For V1 MVP, we test the core validation logic.
});
