import fs from "node:fs/promises";
import path from "node:path";
import { describe, it } from "node:test";
import { fileURLToPath } from "node:url";
import { mergeOpenApiDocuments } from "@zuplo/openapi-tools";
import { expect } from "chai";
import { BASE_TEMPLATE } from "../source/import-openapi/handler.js";
import { addOperationIdsAsNecessary, parseOpenApiFile, } from "../source/import-openapi/utils.js";
import { baseRoutesFileOnImportTests, baseRoutesFileWOOperationIDs, } from "./oas-test-data.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const testAssetsPath = path.join(__dirname, "..", "..", "src", "__tests__", "test-assets");
describe("Import open api utils", async function () {
    it("should generate merge changes between two different oas files", async function () {
        const toImportPath = path.join(testAssetsPath, "toimport.oas.json");
        const toImportContent = await fs.readFile(toImportPath, "utf-8");
        const toImportOpenApiSpec = await parseOpenApiFile(".json", toImportContent);
        const originalDocument = (await parseOpenApiFile(".json", JSON.stringify(baseRoutesFileOnImportTests)));
        const { created, merged, retained } = mergeOpenApiDocuments(originalDocument, toImportOpenApiSpec, "path-method");
        const expectedMergedResult = new Set().add("post>/v2/second/{userUid}/nature");
        const expectedRetainedResult = new Set().add("post>/v1/second/{userUid}/nature");
        expect(created).to.deep.equals(new Set());
        expect(merged).to.deep.equals(expectedMergedResult);
        expect(retained).to.deep.equals(expectedRetainedResult);
    });
    it("should generate merge changes between a oas file and the default base template", async function () {
        const toImportPath = path.join(testAssetsPath, "toimport.oas.json");
        const toImportContent = await fs.readFile(toImportPath, "utf-8");
        const toImportOpenApiSpec = await parseOpenApiFile(".json", toImportContent);
        const originalDocument = (await parseOpenApiFile(".json", BASE_TEMPLATE));
        const { created, merged, retained } = mergeOpenApiDocuments(originalDocument, toImportOpenApiSpec, "path-method");
        expect(created).to.deep.equals(new Set().add("post>/v2/second/{userUid}/nature"));
        expect(merged).to.deep.equals(new Set());
        expect(retained).to.deep.equals(new Set());
    });
    it("should generate missing operation ids", async function () {
        const oasDocument = await parseOpenApiFile(".json", JSON.stringify(baseRoutesFileWOOperationIDs));
        addOperationIdsAsNecessary(oasDocument);
        expect(oasDocument.paths).to.not.be.empty;
        expect(oasDocument.paths["/v2/second/{userUid}/nature"]?.post?.operationId)
            .to.not.be.empty;
    });
    it("should not change existing operation ids", async function () {
        const oasDocument = await parseOpenApiFile(".json", JSON.stringify(baseRoutesFileOnImportTests));
        addOperationIdsAsNecessary(oasDocument);
        expect(oasDocument.paths).to.not.be.empty;
        expect(oasDocument.paths["/v1/second/{userUid}/nature"]?.post?.operationId).to.equal("12345");
        expect(oasDocument.paths["/v2/second/{userUid}/nature"]?.post?.operationId).to.equal("67920");
    });
    it("should successfully parse a yaml open api file", async function () {
        const toImportPath = path.join(testAssetsPath, "toimportv2.yaml");
        const toImportContent = await fs.readFile(toImportPath, "utf-8");
        const toImportOpenApiSpec = await parseOpenApiFile(".yaml", toImportContent);
        expect(toImportOpenApiSpec).to.not.be.empty;
        expect(toImportOpenApiSpec.info).to.deep.equals({
            title: "Protect API",
            description: "Buy & sell",
            version: "0.2.0",
            contact: {
                name: "Zuplo YAML Test",
                url: "https://zuplo.com/",
                email: "test@example.com",
            },
        });
        expect(toImportOpenApiSpec.servers).to.deep.equals([
            {
                url: "https://api.zuplo.com",
                description: "Production",
            },
        ]);
        expect(toImportOpenApiSpec.tags).to.deep.equals([
            {
                name: "Order",
            },
            {
                name: "Organization",
            },
            {
                name: "Site",
            },
            {
                name: "Supervisor",
            },
        ]);
        expect(toImportOpenApiSpec.paths).to.not.be.empty;
        expect(toImportOpenApiSpec.paths["/v1/orders"]).to.not.be.empty;
        expect(toImportOpenApiSpec.paths["/v1/orders/wa/{waaaa}"]).to.not.be.empty;
        expect(toImportOpenApiSpec.paths["/v1/orders/{order}"]).to.not.be.empty;
        expect(toImportOpenApiSpec.paths["/v1/orgs/{organization}"]).to.not.be
            .empty;
        expect(toImportOpenApiSpec.paths["/v1/sites"]).to.deep.equals({
            get: {
                operationId: "get-sites",
                summary: "Get Sites",
                description: "Sites are locations",
                tags: ["Site"],
                responses: {
                    "200": {
                        description: "OK",
                        content: {
                            "application/json": {
                                schema: {
                                    type: "array",
                                    items: {
                                        $ref: "#/components/schemas/Site",
                                    },
                                },
                            },
                        },
                        headers: {
                            "Cache-Control": {
                                required: true,
                                schema: {
                                    type: "string",
                                },
                                description: "Responses are cacheable following standard HTTP headers.",
                            },
                        },
                    },
                },
            },
        });
        expect(toImportOpenApiSpec.components).to.not.be.empty;
    });
});
//# sourceMappingURL=import-openapi-utils.test.js.map