import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, beforeEach, describe, it } from "node:test";
import { fileURLToPath } from "node:url";
import { expect } from "chai";
import { createSandbox } from "sinon";
import { importOpenApi } from "../source/import-openapi/handler.js";
import { baseRoutesFileOnImportTests } 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");
const testTmpPath = path.join(__dirname, "..", "..", "src", "__tests__", "test-tmp");
async function createOASFile(content, destinationPath) {
    try {
        const dir = path.dirname(destinationPath);
        await fs.mkdir(dir, { recursive: true });
        await fs.writeFile(destinationPath, content, "utf8");
        console.log(`File created successfully at ${destinationPath}`);
    }
    catch (error) {
        console.error(`Error creating file at ${destinationPath}:`, error);
    }
}
let sandbox;
describe("Import open api file", async function () {
    let printExit;
    beforeEach(async function () {
        sandbox = createSandbox();
        printExit = sandbox.stub(process, "exit");
    });
    afterEach(() => {
        sandbox.restore();
    });
    it("should merge two existing files correctly", async function () {
        const destinationPath = path.join(testTmpPath, "config", "routes-test-one.oas.json");
        await createOASFile(JSON.stringify(baseRoutesFileOnImportTests), destinationPath);
        await importOpenApi({
            dir: testAssetsPath,
            prompt: false,
            source: path.join(testAssetsPath, "toimport.oas.json"),
            destination: destinationPath,
            "source-url": "",
        });
        const mergedContent = await fs.readFile(destinationPath, "utf-8");
        const jsonContent = JSON.parse(mergedContent);
        expect(jsonContent.paths).to.deep.equals({
            "/v1/second/{userUid}/nature": {
                post: {
                    summary: "Test route",
                    operationId: "12345",
                    parameters: [
                        {
                            $ref: "#/components/parameters/userUid",
                        },
                    ],
                    responses: {
                        "200": {
                            description: "",
                        },
                    },
                    security: [{}],
                    servers: [
                        {
                            url: "https://api.stage.example.com",
                            description: "stage",
                        },
                        {
                            url: "https://api.example.com",
                            description: "production",
                        },
                    ],
                },
            },
            "/v2/second/{userUid}/nature": {
                post: {
                    operationId: "67920",
                    summary: "Test route v2",
                    parameters: [
                        {
                            $ref: "#/components/parameters/userUid",
                        },
                    ],
                    responses: {
                        "200": {
                            description: "",
                        },
                    },
                },
            },
        });
    });
    it("should import a file into a fs destination that does not yet exist", async function () {
        const destinationPath = path.join(testTmpPath, "routes-test-two.oas.json");
        await importOpenApi({
            dir: testAssetsPath,
            prompt: false,
            source: path.join(testAssetsPath, "toimport.oas.json"),
            destination: destinationPath,
            "source-url": "",
        });
        const mergedContent = await fs.readFile(destinationPath, "utf-8");
        const jsonContent = JSON.parse(mergedContent);
        expect(jsonContent.info).to.deep.equals({
            version: "v2",
            title: "Zuplo test API V2",
            description: "Test api v2",
            contact: {
                name: "Zuplo test",
                url: "www.zuplo.com",
            },
        });
        expect(jsonContent.openapi).to.equal("3.1.0");
        expect(jsonContent.components).to.deep.equals({
            parameters: {
                userUid: {
                    name: "userUid",
                    in: "path",
                    description: "Unique UUID of the user.",
                    required: true,
                    schema: {
                        type: "string",
                        format: "uuid",
                    },
                },
            },
            securitySchemes: {
                authorization: {
                    type: "http",
                    description: "Firebase JWT auth token",
                    scheme: "bearer",
                },
            },
        });
        expect(jsonContent.servers).to.deep.equals([
            {
                url: "https://api.stage.example.com",
                description: "Stage",
            },
            {
                url: "https://api.example.com",
                description: "Prod",
            },
        ]);
        expect(jsonContent.security).to.deep.equals([{}]);
        expect(jsonContent.tags).to.deep.equals([]);
        expect(jsonContent.paths).to.deep.equals({
            "/v2/second/{userUid}/nature": {
                post: {
                    summary: "Test route v2",
                    operationId: "67920",
                    parameters: [
                        {
                            $ref: "#/components/parameters/userUid",
                        },
                    ],
                    responses: {
                        "200": {
                            description: "",
                        },
                    },
                },
            },
        });
    });
});
//# sourceMappingURL=import-openapi.test.js.map