import { describe, it, expect, vi, beforeEach } from "vitest";

const invoke = vi.fn();
vi.mock("@tauri-apps/api/core", () => ({
  invoke: (...args: unknown[]) => invoke(...args),
}));

import { TauriPluginGateway } from "./plugin";

describe("TauriPluginGateway invoke payloads", () => {
  beforeEach(() => invoke.mockReset().mockResolvedValue({}));

  it("reviewPackage sends the package DTO under the Tauri input arg", async () => {
    await new TauriPluginGateway().reviewPackage({
      sourceKind: "archive",
      path: "/tmp/example.ideaplug",
    });

    expect(invoke).toHaveBeenCalledWith("plugin_review_package", {
      input: {
        sourceKind: "archive",
        path: "/tmp/example.ideaplug",
      },
    });
  });

  it("normalizes backend review DTOs for the Plugins panel view-model", async () => {
    invoke.mockResolvedValueOnce({
      id: "dev.acme.gitgraph",
      displayName: "Git Graph",
      publisher: "Acme",
      version: "1.2.3",
      description: "Graph",
      sourceKind: "archive",
      sourceLabel: "/tmp/gitgraph.ideaplug",
      contentHash: "abc",
      trustLevel: "full",
      contributionSummary: { topLevelMenus: 1, menuItems: 2, layouts: 3, mcpServers: 4 },
      contributes: { menus: [], menuItems: [], layouts: [], mcpServers: [] },
    });

    const review = await new TauriPluginGateway().reviewPackage({
      sourceKind: "archive",
      path: "/tmp/gitgraph.ideaplug",
    });

    expect(review.issues).toEqual([]);
    expect(review.installable).toBe(true);
  });
});
