// SPDX-License-Identifier: Apache-2.0
import { BaseCommandTest } from './base-command-test.js';
import { Flags } from '../../../../src/commands/flags.js';
import { main } from '../../../../src/index.js';
import { Duration } from '../../../../src/core/time/duration.js';
import { InjectTokens } from '../../../../src/core/dependency-injection/inject-tokens.js';
import { sleep } from '../../../../src/core/helpers.js';
import http from 'node:http';
import { expect } from 'chai';
import { container } from 'tsyringe-neo';
import { Templates } from '../../../../src/core/templates.js';
import { ExplorerCommandDefinition } from '../../../../src/commands/command-definitions/explorer-command-definition.js';
export class ExplorerTest extends BaseCommandTest {
    static soloExplorerDeployArgv(testName, deployment, clusterReference) {
        const { newArgv, argvPushGlobalFlags, optionFromFlag } = ExplorerTest;
        const argv = newArgv();
        argv.push(ExplorerCommandDefinition.COMMAND_NAME, ExplorerCommandDefinition.NODE_SUBCOMMAND_NAME, ExplorerCommandDefinition.NODE_ADD, optionFromFlag(Flags.deployment), deployment, optionFromFlag(Flags.clusterRef), clusterReference);
        argvPushGlobalFlags(argv, testName, true, true);
        return argv;
    }
    static soloExplorerDestroyArgv(testName, deployment, clusterReference) {
        const { newArgv, argvPushGlobalFlags, optionFromFlag } = ExplorerTest;
        const argv = newArgv();
        argv.push(ExplorerCommandDefinition.COMMAND_NAME, ExplorerCommandDefinition.NODE_SUBCOMMAND_NAME, ExplorerCommandDefinition.NODE_DESTROY, optionFromFlag(Flags.deployment), deployment, optionFromFlag(Flags.clusterRef), clusterReference, optionFromFlag(Flags.force), optionFromFlag(Flags.quiet), optionFromFlag(Flags.devMode));
        argvPushGlobalFlags(argv, testName, false, true);
        return argv;
    }
    static async verifyExplorerDeployWasSuccessful(contexts, namespace, createdAccountIds, testLogger) {
        const k8Factory = container.resolve(InjectTokens.K8Factory);
        const k8 = k8Factory.getK8(contexts[1]);
        const explorerPods = await k8.pods().list(namespace, Templates.renderExplorerLabels(1));
        expect(explorerPods).to.have.lengthOf(1);
        try {
            await sleep(Duration.ofSeconds(2));
            const queryUrl = 'http://127.0.0.1:8080/api/v1/accounts?limit=15&order=desc';
            const packageDownloader = container.resolve(InjectTokens.PackageDownloader);
            expect(await packageDownloader.urlExists(queryUrl), 'the hedera explorer Accounts URL should exist').to.be.true;
            let received = false;
            // wait until the transaction reached consensus and retrievable from the mirror node API
            while (!received) {
                const request = http.request(queryUrl, { method: 'GET', timeout: 100, headers: { Connection: 'close' } }, (response) => {
                    response.setEncoding('utf8');
                    response.on('data', (chunk) => {
                        // convert chunk to json object
                        const object = JSON.parse(chunk);
                        expect(object.accounts?.length, "expect there to be more than one account in the hedera explorer's call to mirror node").to.be.greaterThan(1);
                        for (const accountId of createdAccountIds) {
                            expect(object.accounts.some((account) => account.account === accountId), `expect ${accountId} to be in the response`).to.be.true;
                        }
                        received = true;
                    });
                });
                request.on('error', (error) => {
                    testLogger.debug(`problem with request: ${error.message}`, error);
                });
                request.end(); // make the request
                await sleep(Duration.ofSeconds(2));
            }
        }
        catch (error) {
            testLogger.debug(`problem with request: ${error.message}`, error);
        }
    }
    static add(options) {
        const { testName, deployment, namespace, contexts, clusterReferenceNameArray, testLogger, createdAccountIds } = options;
        const { soloExplorerDeployArgv, verifyExplorerDeployWasSuccessful } = ExplorerTest;
        it(`${testName}: explorer node add`, async () => {
            await main(soloExplorerDeployArgv(testName, deployment, clusterReferenceNameArray[1]));
        }).timeout(Duration.ofMinutes(5).toMillis());
    }
    static destroy(options) {
        const { testName, deployment, clusterReferenceNameArray } = options;
        const { soloExplorerDestroyArgv } = ExplorerTest;
        it(`${testName}: explorer node destroy`, async () => {
            await main(soloExplorerDestroyArgv(testName, deployment, clusterReferenceNameArray[1]));
        }).timeout(Duration.ofMinutes(5).toMillis());
    }
}
//# sourceMappingURL=explorer-test.js.map