// SPDX-License-Identifier: Apache-2.0
import { expect } from 'chai';
import sinon from 'sinon';
import { DefaultKindClient } from '../../../../../src/integration/kind/impl/default-kind-client.js';
import { KindExecutionBuilder } from '../../../../../src/integration/kind/execution/kind-execution-builder.js';
import { KindExecution } from '../../../../../src/integration/kind/execution/kind-execution.js';
import { ClusterDeleteResponse } from '../../../../../src/integration/kind/model/delete-cluster/cluster-delete-response.js';
describe('DefaultKindClient - deleteCluster', () => {
    let client;
    let executionBuilderStub;
    let executionStub;
    beforeEach(() => {
        client = new DefaultKindClient('/usr/local/bin/kind');
        executionBuilderStub = sinon.createStubInstance(KindExecutionBuilder);
        executionStub = sinon.createStubInstance(KindExecution);
        // Set up the builder stub
        executionBuilderStub.build.returns(executionStub);
        sinon.stub(KindExecutionBuilder.prototype, 'build').returns(executionStub);
    });
    afterEach(() => {
        sinon.restore();
    });
    it('should delete a cluster and parse the response correctly', async () => {
        const clusterName = 'test-cluster';
        executionStub.responseAs.callsFake((responseClass) => {
            const output = `Deleting cluster "${clusterName}" ...
Deleted nodes: ["${clusterName}-control-plane"]`;
            return Promise.resolve(new responseClass(output));
        });
        const result = await client.deleteCluster(clusterName);
        expect(result).to.be.instanceOf(ClusterDeleteResponse);
        expect(result.name).to.equal(clusterName);
        expect(result.deletedNodes).to.deep.equal([`${clusterName}-control-plane`]);
    });
    it('should throw if responseAs throws', async () => {
        executionStub.responseAs.rejects(new Error('fail'));
        try {
            await client.deleteCluster('test-cluster');
            expect.fail('Expected error');
        }
        catch (error) {
            expect(error.message).to.equal('fail');
        }
    });
    it('should handle output with multiple deleted nodes', async () => {
        const clusterName = 'multi-node-cluster';
        executionStub.responseAs.callsFake((responseClass) => {
            const output = `Deleting cluster "${clusterName}" ...
Deleted nodes: ["${clusterName}-control-plane", "${clusterName}-worker", "${clusterName}-worker2"]`;
            return Promise.resolve(new responseClass(output));
        });
        const result = await client.deleteCluster(clusterName);
        expect(result.name).to.equal(clusterName);
        expect(result.deletedNodes).to.deep.equal([
            `${clusterName}-control-plane`,
            `${clusterName}-worker`,
            `${clusterName}-worker2`,
        ]);
    });
    it('should handle output without deleted nodes information', async () => {
        const clusterName = 'no-nodes-cluster';
        executionStub.responseAs.callsFake((responseClass) => {
            const output = `Deleting cluster "${clusterName}" ...`;
            return Promise.resolve(new responseClass(output));
        });
        const result = await client.deleteCluster(clusterName);
        expect(result.name).to.equal(clusterName);
        expect(result.deletedNodes).to.be.an('array').that.is.empty;
    });
    it('should pass cluster name and options to execution builder', async () => {
        const clusterName = 'options-test-cluster';
        const options = {
            name: clusterName,
            kubeconfig: './custom-config.yaml',
        };
        // Create spies to track method calls
        const subcommandsSpy = sinon.spy(KindExecutionBuilder.prototype, 'subcommands');
        const argumentSpy = sinon.spy(KindExecutionBuilder.prototype, 'argument');
        await client.deleteCluster(clusterName, options);
        // Verify subcommands were called correctly
        expect(subcommandsSpy.calledWith('delete', 'cluster')).to.be.true;
        // Verify arguments were set correctly
        expect(argumentSpy.calledWith('name', clusterName)).to.be.true;
        expect(argumentSpy.calledWith('kubeconfig', options.kubeconfig)).to.be.true;
    });
    it('should handle no options provided', async () => {
        const clusterName = 'default-options-cluster';
        executionStub.responseAs.callsFake((responseClass) => {
            const output = `Deleting cluster "${clusterName}" ...
Deleted nodes: ["${clusterName}-control-plane"]`;
            return Promise.resolve(new responseClass(output));
        });
        // Create a spy to track argument calls
        const argumentSpy = sinon.spy(KindExecutionBuilder.prototype, 'argument');
        const result = await client.deleteCluster(clusterName);
        // Verify only name parameter was set
        expect(argumentSpy.calledWith('name', clusterName)).to.be.true;
        // Check number of calls to argument to verify no other arguments were set
        expect(argumentSpy.callCount).to.equal(1);
        // Response should be correct
        expect(result.name).to.equal(clusterName);
    });
});
//# sourceMappingURL=delete-cluster.test.js.map