import { beforeEach, describe, expect, it, vi } from "vitest";
import * as client from "./client";
import {
  createAgentProvider,
  deleteAgentProvider,
  fetchAgentProviders,
  updateAgentProvider,
} from "./agent-providers";

vi.mock("./client");

beforeEach((): void => {
  vi.clearAllMocks();
});

describe("fetchAgentProviders", (): void => {
  it("calls provider list endpoint", async (): Promise<void> => {
    vi.mocked(client.apiGet).mockResolvedValueOnce([] as never);

    await fetchAgentProviders();

    expect(client.apiGet).toHaveBeenCalledWith("/api/agent-providers");
  });
});

describe("createAgentProvider", (): void => {
  it("posts create payload", async (): Promise<void> => {
    vi.mocked(client.apiPost).mockResolvedValueOnce({ id: "provider-1" } as never);

    await createAgentProvider({
      name: "openclaw-primary",
      provider: "openclaw",
      gatewayUrl: "https://openclaw.example.com",
      credentials: {
        apiToken: "top-secret",
      },
      isActive: true,
    });

    expect(client.apiPost).toHaveBeenCalledWith("/api/agent-providers", {
      name: "openclaw-primary",
      provider: "openclaw",
      gatewayUrl: "https://openclaw.example.com",
      credentials: {
        apiToken: "top-secret",
      },
      isActive: true,
    });
  });
});

describe("updateAgentProvider", (): void => {
  it("sends PUT request with update payload", async (): Promise<void> => {
    vi.mocked(client.apiRequest).mockResolvedValueOnce({ id: "provider-1" } as never);

    await updateAgentProvider("provider-1", {
      gatewayUrl: "https://new-openclaw.example.com",
      isActive: false,
    });

    expect(client.apiRequest).toHaveBeenCalledWith("/api/agent-providers/provider-1", {
      method: "PUT",
      body: JSON.stringify({
        gatewayUrl: "https://new-openclaw.example.com",
        isActive: false,
      }),
    });
  });
});

describe("deleteAgentProvider", (): void => {
  it("calls delete endpoint", async (): Promise<void> => {
    vi.mocked(client.apiDelete).mockResolvedValueOnce(undefined as never);

    await deleteAgentProvider("provider-1");

    expect(client.apiDelete).toHaveBeenCalledWith("/api/agent-providers/provider-1");
  });
});
