// SPDX-License-Identifier: Apache-2.0
import { describe, it } from 'mocha';
import { expect } from 'chai';
import { Flags as flags } from '../../../src/commands/flags.js';
import { getTemporaryDirectory, HEDERA_PLATFORM_VERSION_TAG } from '../../test-utility.js';
import { Duration } from '../../../src/core/time/duration.js';
import { HEDERA_HAPI_PATH, ROOT_CONTAINER } from '../../../src/core/constants.js';
import fs from 'node:fs';
import { Zippy } from '../../../src/core/zippy.js';
import { ContainerReference } from '../../../src/integration/kube/resources/container/container-reference.js';
import { ConsensusCommandDefinition } from '../../../src/commands/command-definitions/consensus-command-definition.js';
import { TEST_UPGRADE_VERSION } from '../../../version-test.js';
export function testSeparateNodeUpgrade(argv, bootstrapResp, namespace) {
    argv.setArg(flags.nodeAliasesUnparsed, 'node1,node2');
    argv.setArg(flags.releaseTag, HEDERA_PLATFORM_VERSION_TAG);
    const zipFile = 'upgrade.zip';
    const { opts: { k8Factory, logger, commandInvoker }, cmd: { nodeCmd }, } = bootstrapResp;
    describe('Node upgrade', async () => {
        it('should succeed with separate upgrade command', async () => {
            // Create file version.txt at tmp directory
            const temporaryDirectory = getTemporaryDirectory();
            fs.writeFileSync(`${temporaryDirectory}/version.txt`, TEST_UPGRADE_VERSION);
            // Create upgrade.zip file from tmp directory using zippy.ts
            const zipper = new Zippy(logger);
            await zipper.zip(temporaryDirectory, zipFile);
            const temporaryDirectory2 = 'contextDir';
            const argvPrepare = argv.clone();
            argvPrepare.setArg(flags.upgradeZipFile, zipFile);
            argvPrepare.setArg(flags.outputDir, temporaryDirectory2);
            const argvExecute = argv.clone();
            argvExecute.setArg(flags.inputDir, temporaryDirectory2);
            await commandInvoker.invoke({
                argv: argvPrepare,
                command: ConsensusCommandDefinition.COMMAND_NAME,
                subcommand: ConsensusCommandDefinition.DEV_NODE_UPGRADE_SUBCOMMAND_NAME,
                action: ConsensusCommandDefinition.DEV_NODE_PREPARE,
                callback: async (argv) => nodeCmd.handlers.upgradePrepare(argv),
            });
            await commandInvoker.invoke({
                argv: argvExecute,
                command: ConsensusCommandDefinition.COMMAND_NAME,
                subcommand: ConsensusCommandDefinition.DEV_NODE_UPGRADE_SUBCOMMAND_NAME,
                action: ConsensusCommandDefinition.DEV_NODE_SUBMIT_TRANSACTION,
                callback: async (argv) => nodeCmd.handlers.upgradeSubmitTransactions(argv),
            });
            await commandInvoker.invoke({
                argv: argvExecute,
                command: ConsensusCommandDefinition.COMMAND_NAME,
                subcommand: ConsensusCommandDefinition.DEV_NODE_UPGRADE_SUBCOMMAND_NAME,
                action: ConsensusCommandDefinition.DEV_NODE_EXECUTE,
                callback: async (argv) => nodeCmd.handlers.upgradeExecute(argv),
            });
        }).timeout(Duration.ofMinutes(5).toMillis());
        it('network nodes version file was upgraded', async () => {
            // Copy the version.txt file from the pod data/upgrade/current directory
            const temporaryDirectory = getTemporaryDirectory();
            const pods = await k8Factory.default().pods().list(namespace, ['solo.hedera.com/type=network-node']);
            const podReference = pods[0].podReference;
            const containerReference = ContainerReference.of(podReference, ROOT_CONTAINER);
            await k8Factory
                .default()
                .containers()
                .readByRef(containerReference)
                .copyFrom(`${HEDERA_HAPI_PATH}/data/upgrade/current/version.txt`, temporaryDirectory);
            // Compare the version.txt
            const version = fs.readFileSync(`${temporaryDirectory}/version.txt`, 'utf8');
            expect(version).to.equal(TEST_UPGRADE_VERSION);
        }).timeout(Duration.ofMinutes(5).toMillis());
    });
}
//# sourceMappingURL=separate-node-upgrade.test.js.map