// SPDX-License-Identifier: Apache-2.0
import { Flags } from '../../../../src/commands/flags.js';
import { getTestCacheDirectory } from '../../../test-utility.js';
import { container } from 'tsyringe-neo';
import { InjectTokens } from '../../../../src/core/dependency-injection/inject-tokens.js';
import { DeploymentCommandDefinition } from '../../../../src/commands/command-definitions/deployment-command-definition.js';
import { Argv } from '../../../helpers/argv-wrapper.js';
import { NamespaceName } from '../../../../src/types/namespace/namespace-name.js';
import { getEnvironmentVariable } from '../../../../src/core/constants.js';
import { Duration } from '../../../../src/core/time/duration.js';
export class BaseCommandTest {
    static newArgv() {
        return ['${PATH}/node', '${SOLO_ROOT}/solo.ts'];
    }
    static optionFromFlag(flag) {
        return `--${flag.name}`;
    }
    static argvPushGlobalFlags(argv, testName, shouldSetTestCacheDirectory = false, shouldSetChartDirectory = false) {
        argv.push(BaseCommandTest.optionFromFlag(Flags.devMode), BaseCommandTest.optionFromFlag(Flags.quiet));
        const soloChartsDirectory = getEnvironmentVariable('SOLO_CHARTS_DIR');
        if (shouldSetChartDirectory && soloChartsDirectory && soloChartsDirectory !== '') {
            argv.push(BaseCommandTest.optionFromFlag(Flags.chartDirectory), process.env.SOLO_CHARTS_DIR);
        }
        if (shouldSetTestCacheDirectory) {
            argv.push(BaseCommandTest.optionFromFlag(Flags.cacheDir), getTestCacheDirectory(testName));
        }
        return argv;
    }
    /**
     * Collects diagnostic logs using the deployment diagnostics command.
     * This is a shared helper used by both test patterns.
     */
    static async collectDiagnosticLogs(testName, testLogger, deployment) {
        try {
            testLogger.info(`${testName}: Collecting diagnostic logs...`);
            // Create proper Argv object
            const argv = Argv.getDefaultArgv(NamespaceName.of(testName));
            argv.setArg(Flags.deployment, deployment);
            argv.setCommand(DeploymentCommandDefinition.COMMAND_NAME, DeploymentCommandDefinition.DIAGNOSTIC_SUBCOMMAND_NAME, DeploymentCommandDefinition.DIAGNOSTIC_LOGS);
            const nodeCmd = container.resolve(InjectTokens.NodeCommand);
            await nodeCmd.handlers.logs(argv.build());
            testLogger.info(`${testName}: Diagnostic logs collected successfully`);
        }
        catch (error) {
            testLogger.error(`${testName}: Error collecting diagnostic logs: ${error}`);
            if (error instanceof Error && error.stack) {
                testLogger.error(`${testName}: Stack trace:\n${error.stack}`);
            }
        }
    }
    /**
     * Sets up an after() hook for diagnostic log collection in E2E tests.
     * Call this within your test suite describe block.
     */
    static setupDiagnosticLogCollection(options) {
        const { testName, testLogger, deployment } = options;
        after(async function () {
            this.timeout(Duration.ofMinutes(5).toMillis());
            await BaseCommandTest.collectDiagnosticLogs(testName, testLogger, deployment);
        });
    }
}
//# sourceMappingURL=base-command-test.js.map