// SPDX-License-Identifier: Apache-2.0
import { expect } from 'chai';
import { afterEach, beforeEach, describe, it } from 'mocha';
import sinon from 'sinon';
import { SharedResourceManager } from '../../../../src/core/shared-resources/shared-resource-manager.js';
import { NamespaceName } from '../../../../src/types/namespace/namespace-name.js';
import * as constants from '../../../../src/core/constants.js';
describe('SharedResourceManager', () => {
    const namespace = NamespaceName.of('test-namespace');
    const context = 'test-context';
    const chartVersion = '1.0.0';
    let loggerStub;
    let helmStub;
    let chartManagerStub;
    let manager;
    beforeEach(() => {
        loggerStub = sinon.stub();
        loggerStub.info = sinon.stub();
        loggerStub.error = sinon.stub();
        helmStub = sinon.stub();
        chartManagerStub = sinon.stub();
        chartManagerStub.isChartInstalled = sinon.stub().resolves(false);
        chartManagerStub.install = sinon.stub().resolves(true);
        chartManagerStub.uninstall = sinon.stub().resolves(true);
        manager = new SharedResourceManager(loggerStub, helmStub, chartManagerStub);
    });
    afterEach(() => {
        sinon.restore();
    });
    describe('installChart()', () => {
        it('skips installation when chart is already installed', async () => {
            chartManagerStub.isChartInstalled.resolves(true);
            await manager.installChart(namespace, '', chartVersion, context);
            expect(chartManagerStub.install).to.not.have.been.called;
        });
        it('installs chart when not already installed', async () => {
            await manager.installChart(namespace, '', chartVersion, context);
            expect(chartManagerStub.install).to.have.been.calledOnce;
        });
        it('uses SOLO_TESTING_CHART_URL when chartDirectory is empty', async () => {
            await manager.installChart(namespace, '', chartVersion, context);
            const repoName = chartManagerStub.install.firstCall.args[3];
            expect(repoName).to.equal(constants.SOLO_TESTING_CHART_URL);
        });
        it('uses provided chartDirectory when given', async () => {
            const localDirectory = '/local/solo-charts';
            await manager.installChart(namespace, localDirectory, chartVersion, context);
            const repoName = chartManagerStub.install.firstCall.args[3];
            expect(repoName).to.equal(localDirectory);
        });
        it('always passes postgresql.enabled and redis.enabled in values', async () => {
            manager.enablePostgres();
            manager.enableRedis();
            await manager.installChart(namespace, '', chartVersion, context);
            const valuesArgument = chartManagerStub.install.firstCall.args[5];
            expect(valuesArgument).to.include('--set postgresql.enabled=true');
            expect(valuesArgument).to.include('--set redis.enabled=true');
        });
        it('reflects postgres disabled and redis enabled correctly in values', async () => {
            manager.enableRedis();
            await manager.installChart(namespace, '', chartVersion, context);
            const valuesArgument = chartManagerStub.install.firstCall.args[5];
            expect(valuesArgument).to.include('--set postgresql.enabled=false');
            expect(valuesArgument).to.include('--set redis.enabled=true');
        });
        it('merges extra valuesArgumentsMap into the helm --set arguments', async () => {
            const extraValues = {
                'redis.image.registry': constants.REDIS_IMAGE_REGISTRY,
                'redis.sentinel.masterSet': constants.REDIS_SENTINEL_MASTER_SET,
            };
            await manager.installChart(namespace, '', chartVersion, context, extraValues);
            const valuesArgument = chartManagerStub.install.firstCall.args[5];
            expect(valuesArgument).to.include('--set redis.image.registry=gcr.io');
            expect(valuesArgument).to.include('--set redis.sentinel.masterSet=mirror');
        });
        it('installs chart with the correct release name and chart name', async () => {
            await manager.installChart(namespace, '', chartVersion, context);
            const [, releaseName, chartName] = chartManagerStub.install.firstCall.args;
            expect(releaseName).to.equal(constants.SOLO_SHARED_RESOURCES_CHART);
            expect(chartName).to.equal(constants.SOLO_SHARED_RESOURCES_CHART);
        });
        it('passes the correct namespace and context to chartManager.install', async () => {
            await manager.installChart(namespace, '', chartVersion, context);
            const installedNamespace = chartManagerStub.install.firstCall.args[0];
            const installedContext = chartManagerStub.install.firstCall.args[6];
            expect(installedNamespace).to.equal(namespace);
            expect(installedContext).to.equal(context);
        });
    });
    describe('uninstallChart()', () => {
        it('skips uninstallation when chart is not installed', async () => {
            chartManagerStub.isChartInstalled.resolves(false);
            await manager.uninstallChart(namespace, context);
            expect(chartManagerStub.uninstall).to.not.have.been.called;
        });
        it('uninstalls chart when it is installed', async () => {
            chartManagerStub.isChartInstalled.resolves(true);
            await manager.uninstallChart(namespace, context);
            expect(chartManagerStub.uninstall).to.have.been.calledOnceWith(namespace, constants.SOLO_SHARED_RESOURCES_CHART, context);
        });
    });
    describe('enablePostgres() / enableRedis()', () => {
        it('defaults postgres and redis to disabled', async () => {
            await manager.installChart(namespace, '', chartVersion, context);
            const valuesArgument = chartManagerStub.install.firstCall.args[5];
            expect(valuesArgument).to.include('--set postgresql.enabled=false');
            expect(valuesArgument).to.include('--set redis.enabled=false');
        });
        it('enables postgres after calling enablePostgres()', async () => {
            manager.enablePostgres();
            await manager.installChart(namespace, '', chartVersion, context);
            const valuesArgument = chartManagerStub.install.firstCall.args[5];
            expect(valuesArgument).to.include('--set postgresql.enabled=true');
            expect(valuesArgument).to.include('--set redis.enabled=false');
        });
        it('enables redis after calling enableRedis()', async () => {
            manager.enableRedis();
            await manager.installChart(namespace, '', chartVersion, context);
            const valuesArgument = chartManagerStub.install.firstCall.args[5];
            expect(valuesArgument).to.include('--set postgresql.enabled=false');
            expect(valuesArgument).to.include('--set redis.enabled=true');
        });
    });
});
//# sourceMappingURL=shared-resource-manager.test.js.map