import { describe, it, expect, beforeEach } from "vitest";
import { getOpenApiDocument, clearOpenApiCache } from "./generator";
// Import app so all routes and health endpoint register paths into the OpenAPI registry
import "../../app";

describe("OpenAPI Generator (2026 Production-Ready)", () => {
  beforeEach(() => {
    clearOpenApiCache();
  });

  it("should generate a valid OpenAPI 3.1.0 document with caching", () => {
    const doc = getOpenApiDocument();

    expect(doc).toBeDefined();
    expect(doc.openapi).toBe("3.1.0");
    expect(doc.info.title).toBe("Taxfend API");
    expect(doc.info.version).toBe("1.0.0");
    expect(doc.servers).toBeDefined();
    expect(doc.servers.length).toBeGreaterThanOrEqual(1);

    // Verify paths exist
    expect(doc.paths).toBeDefined();
    expect(doc.paths["/health"]).toBeDefined();
    expect(doc.paths["/health"].get.summary).toBe("Health check endpoint");

    // Verify auth endpoints registered
    expect(doc.paths["/auth/register"]).toBeDefined();
    expect(doc.paths["/auth/login"]).toBeDefined();
    expect(doc.paths["/auth/me"]).toBeDefined();
    expect(doc.paths["/auth/sessions"]).toBeDefined();

    // Verify security components registered
    expect(doc.components?.securitySchemes?.bearerAuth).toBeDefined();
    expect(doc.components?.securitySchemes?.bearerAuth).toEqual({
      type: "http",
      scheme: "bearer",
      bearerFormat: "JWT",
      description:
        "Enter your JWT access token below (e.g. from /api/v1/auth/login)",
    });

    // Verify caching works exactly (returns same reference on subsequent calls)
    const secondDoc = getOpenApiDocument();
    expect(secondDoc).toBe(doc);
  });
});
