import nock from "nock";
import yargs from "yargs/yargs";
import listCommand from "../../cmds/list.js";
import { setupTestEnvironment, cleanupTest, setupAuthenticatedNock, RequestCapture, TEST_API_BASE, TEST_ACCOUNT_NAME, TEST_PROJECT_NAME, TEST_AUTH_TOKEN, } from "./test-utils.js";
async function executeListCommand(args) {
    const yargsInstance = yargs([])
        .command(listCommand)
        .help(false)
        .version(false)
        .exitProcess(false);
    const commandArgs = ["list"];
    if (args.account) {
        commandArgs.push("--account", args.account);
    }
    if (args.project) {
        commandArgs.push("--project", args.project);
    }
    if (args["api-key"]) {
        commandArgs.push("--api-key", args["api-key"]);
    }
    if (args["self-hosted-endpoint"]) {
        commandArgs.push("--self-hosted-endpoint", args["self-hosted-endpoint"]);
    }
    return await yargsInstance.parse(commandArgs);
}
const mockPrintResult = jest.mocked(require("../../common/output.js").printResultToConsoleAndExitGracefully);
const mockPrintDiagnostics = jest.mocked(require("../../common/output.js").printDiagnosticsToConsole);
describe("List Command Integration Tests", () => {
    let requestCapture;
    beforeEach(() => {
        setupTestEnvironment();
        requestCapture = new RequestCapture();
        nock.disableNetConnect();
        jest.clearAllMocks();
    });
    afterEach(() => {
        cleanupTest();
        requestCapture.clear();
    });
    describe("SaaS version", () => {
        it("should intercept GET request to list deployments with account and project", async () => {
            const mockResponse = {
                data: [
                    { url: "https://test-project-production.zuplo.app" },
                    { url: "https://test-project-staging.zuplo.app" },
                ],
            };
            const scope = setupAuthenticatedNock(nock(TEST_API_BASE))
                .get(`/v1/accounts/${TEST_ACCOUNT_NAME}/projects/${TEST_PROJECT_NAME}/deployments`)
                .reply(200, mockResponse);
            scope.on("request", (req, interceptor, body) => {
                requestCapture.capture(req, interceptor, body);
            });
            await executeListCommand({
                account: TEST_ACCOUNT_NAME,
                project: TEST_PROJECT_NAME,
                "api-key": TEST_AUTH_TOKEN,
            });
            expect(scope.isDone()).toBe(true);
            const expectedOutput = "https://test-project-production.zuplo.app\nhttps://test-project-staging.zuplo.app";
            expect(mockPrintResult).toHaveBeenCalledWith(expectedOutput);
            const capturedRequests = requestCapture.getRequests();
            expect(capturedRequests).toMatchSnapshot("list-deployments-saas-requests");
        });
        it("should handle empty deployments list", async () => {
            const mockResponse = { data: [] };
            const scope = setupAuthenticatedNock(nock(TEST_API_BASE))
                .get(`/v1/accounts/${TEST_ACCOUNT_NAME}/projects/${TEST_PROJECT_NAME}/deployments`)
                .reply(200, mockResponse);
            scope.on("request", (req, interceptor, body) => {
                requestCapture.capture(req, interceptor, body);
            });
            await executeListCommand({
                account: TEST_ACCOUNT_NAME,
                project: TEST_PROJECT_NAME,
                "api-key": TEST_AUTH_TOKEN,
            });
            expect(scope.isDone()).toBe(true);
            expect(mockPrintResult).toHaveBeenCalledWith("");
            const capturedRequests = requestCapture.getRequests();
            expect(capturedRequests).toMatchSnapshot("list-deployments-empty-requests");
        });
        it("should handle API error responses", async () => {
            const errorResponse = {
                error: "Not Found",
                message: "Project not found",
                statusCode: 404,
            };
            const scope = setupAuthenticatedNock(nock(TEST_API_BASE))
                .get(`/v1/accounts/${TEST_ACCOUNT_NAME}/projects/${TEST_PROJECT_NAME}/deployments`)
                .reply(404, errorResponse);
            scope.on("request", (req, interceptor, body) => {
                requestCapture.capture(req, interceptor, body);
            });
            await executeListCommand({
                account: TEST_ACCOUNT_NAME,
                project: TEST_PROJECT_NAME,
                "api-key": TEST_AUTH_TOKEN,
            });
            expect(scope.isDone()).toBe(true);
            expect(mockPrintDiagnostics).toHaveBeenCalled();
            const capturedRequests = requestCapture.getRequests();
            expect(capturedRequests).toMatchSnapshot("list-deployments-error-requests");
        });
    });
    describe("Self-hosted version", () => {
        it("should intercept GET request to self-hosted endpoint", async () => {
            const selfHostedEndpoint = "https://my-zuplo.company.com";
            const mockResponse = {
                data: [
                    { deploymentUrl: "https://my-zuplo.company.com/project1" },
                    { deploymentUrl: "https://my-zuplo.company.com/project2" },
                ],
            };
            const scope = setupAuthenticatedNock(nock(selfHostedEndpoint))
                .get("/v1/deployments")
                .reply(200, mockResponse);
            scope.on("request", (req, interceptor, body) => {
                requestCapture.capture(req, interceptor, body);
            });
            await executeListCommand({
                account: TEST_ACCOUNT_NAME,
                project: TEST_PROJECT_NAME,
                "api-key": TEST_AUTH_TOKEN,
                "self-hosted-endpoint": selfHostedEndpoint,
            });
            expect(scope.isDone()).toBe(true);
            const expectedOutput = "https://my-zuplo.company.com/project1\nhttps://my-zuplo.company.com/project2";
            expect(mockPrintResult).toHaveBeenCalledWith(expectedOutput);
            const capturedRequests = requestCapture.getRequests();
            expect(capturedRequests).toMatchSnapshot("list-deployments-self-hosted-requests");
        });
    });
    describe("Request headers validation", () => {
        it("should include correct authorization headers", async () => {
            const mockResponse = { data: [{ url: "https://test.zuplo.app" }] };
            const scope = nock(TEST_API_BASE)
                .get(`/v1/accounts/${TEST_ACCOUNT_NAME}/projects/${TEST_PROJECT_NAME}/deployments`)
                .matchHeader("authorization", `Bearer ${TEST_AUTH_TOKEN}`)
                .reply(200, mockResponse);
            scope.on("request", (req, interceptor, body) => {
                requestCapture.capture(req, interceptor, body);
            });
            await executeListCommand({
                account: TEST_ACCOUNT_NAME,
                project: TEST_PROJECT_NAME,
                "api-key": TEST_AUTH_TOKEN,
            });
            expect(scope.isDone()).toBe(true);
            expect(mockPrintResult).toHaveBeenCalledWith("https://test.zuplo.app");
            const capturedRequests = requestCapture.getRequests();
            expect(capturedRequests).toMatchSnapshot("list-deployments-headers-validation");
        });
    });
});
//# sourceMappingURL=list.integration.test.js.map