import { describe, expect, it } from "vitest";
import {
  extractOpenApiEndpoints,
  formatOpenApiConnectorSystemPrompt,
} from "./openapi_utils";

describe("extractOpenApiEndpoints", () => {
  it("extracts endpoints from a JSON OpenAPI document", () => {
    const endpoints = extractOpenApiEndpoints({
      openapi: "3.0.3",
      info: {
        title: "Pets",
      },
      paths: {
        "/pets": {
          get: {
            summary: "List pets",
            operationId: "listPets",
            tags: ["pets"],
          },
          post: {
            summary: "Create pet",
            operationId: "createPet",
            tags: ["pets"],
          },
        },
      },
    });

    expect(endpoints).toEqual([
      {
        id: "listPets",
        method: "GET",
        path: "/pets",
        operationId: "listPets",
        summary: "List pets",
        description: null,
        tags: ["pets"],
      },
      {
        id: "createPet",
        method: "POST",
        path: "/pets",
        operationId: "createPet",
        summary: "Create pet",
        description: null,
        tags: ["pets"],
      },
    ]);
  });

  it("supports Swagger-style documents", () => {
    const endpoints = extractOpenApiEndpoints({
      swagger: "2.0",
      paths: {
        "/users/{id}": {
          delete: {
            summary: "Delete user",
          },
        },
      },
    });

    expect(endpoints).toEqual([
      {
        id: "DELETE /users/{id}",
        method: "DELETE",
        path: "/users/{id}",
        operationId: null,
        summary: "Delete user",
        description: null,
        tags: [],
      },
    ]);
  });

  it("formats a connector summary for the system prompt", () => {
    const prompt = formatOpenApiConnectorSystemPrompt({
      name: "Weather API",
      sourceUrl: "https://example.com/openapi.json",
      description: "Forecast data",
      specVersion: "3.1.0",
      endpoints: [
        {
          id: "getForecast",
          method: "GET",
          path: "/forecast",
          operationId: "getForecast",
          summary: "Get forecast",
          description: null,
          tags: ["weather"],
        },
      ],
    });

    expect(prompt).toContain("Connector: Weather API");
    expect(prompt).toContain("Spec URL: https://example.com/openapi.json");
    expect(prompt).toContain("- GET /forecast (getForecast) - Get forecast");
    expect(prompt).toContain(
      "Prefer these endpoints over inventing new API routes.",
    );
  });
});
