// SPDX-License-Identifier: Apache-2.0
import { after, describe } from 'mocha';
import { expect } from 'chai';
import each from 'mocha-each';
import { Flags as flags } from '../../../src/commands/flags.js';
import { endToEndTestSuite, getTestCluster, HEDERA_PLATFORM_VERSION_TAG, hederaPlatformSupportsNonZeroRealms, } from '../../test-utility.js';
import * as version from '../../../version.js';
import { sleep } from '../../../src/core/helpers.js';
import { RelayCommand } from '../../../src/commands/relay.js';
import { Duration } from '../../../src/core/time/duration.js';
import { NamespaceName } from '../../../src/types/namespace/namespace-name.js';
import { container } from 'tsyringe-neo';
import { InjectTokens } from '../../../src/core/dependency-injection/inject-tokens.js';
import { Argv } from '../../helpers/argv-wrapper.js';
const testName = 'relay-cmd-e2e';
const namespace = NamespaceName.of(testName);
const argv = Argv.getDefaultArgv(namespace);
argv.setArg(flags.namespace, namespace.name);
argv.setArg(flags.releaseTag, HEDERA_PLATFORM_VERSION_TAG);
argv.setArg(flags.nodeAliasesUnparsed, 'node1,node2');
argv.setArg(flags.generateGossipKeys, true);
argv.setArg(flags.generateTlsKeys, true);
argv.setArg(flags.clusterRef, getTestCluster());
argv.setArg(flags.soloChartVersion, version.SOLO_CHART_VERSION);
argv.setArg(flags.force, true);
argv.setArg(flags.relayReleaseTag, flags.relayReleaseTag.definition.defaultValue);
argv.setArg(flags.realm, hederaPlatformSupportsNonZeroRealms() ? 1 : 0);
argv.setArg(flags.shard, 0);
endToEndTestSuite(testName, argv, {}, (bootstrapResp) => {
    const { opts: { k8Factory, logger, commandInvoker }, } = bootstrapResp;
    describe('RelayCommand', async () => {
        let relayCommand;
        const testLogger = container.resolve(InjectTokens.SoloLogger);
        before(() => {
            relayCommand = container.resolve(InjectTokens.RelayCommand);
        });
        afterEach(async () => {
            // wait for k8s to finish destroying containers from relay destroy
            await sleep(Duration.ofMillis(5));
        });
        after(async function () {
            this.timeout(Duration.ofMinutes(5).toMillis());
            await container.resolve(InjectTokens.NetworkNodes).getLogs(namespace);
            await k8Factory.default().namespaces().delete(namespace);
        });
        each(['node1', 'node1,node2']).describe('relay and deploy and destroy for each', async (relayNodes) => {
            it(`relay deploy and destroy should work with ${relayNodes}`, async function () {
                testLogger.info(`#### Running relay deploy for: ${relayNodes} ####`);
                this.timeout(Duration.ofMinutes(5).toMillis());
                argv.setArg(flags.nodeAliasesUnparsed, relayNodes);
                try {
                    await commandInvoker.invoke({
                        argv: argv,
                        command: RelayCommand.COMMAND_NAME,
                        subcommand: 'deploy',
                        // @ts-expect-error to access private property
                        callback: async (argv) => relayCommand.deploy(argv),
                    });
                }
                catch (error) {
                    logger.showUserError(error);
                    expect.fail();
                }
                await sleep(Duration.ofMillis(500));
                testLogger.info(`#### Running relay destroy for: ${relayNodes} ####`);
                try {
                    await commandInvoker.invoke({
                        argv: argv,
                        command: RelayCommand.COMMAND_NAME,
                        subcommand: 'destroy',
                        // @ts-expect-error to access private modifier
                        callback: async (argv) => relayCommand.destroy(argv),
                    });
                }
                catch (error) {
                    logger.showUserError(error);
                    expect.fail();
                }
                testLogger.info(`#### Finished relay deploy and destroy for: ${relayNodes} ####`);
            });
        });
    });
});
//# sourceMappingURL=relay.test.js.map