// SPDX-License-Identifier: Apache-2.0
import { describe } from 'mocha';
import { resetForTest } from '../../test-container.js';
import { container } from 'tsyringe-neo';
import { InjectTokens } from '../../../src/core/dependency-injection/inject-tokens.js';
import fs from 'node:fs';
import { DEFAULT_LOCAL_CONFIG_FILE } from '../../../src/core/constants.js';
import { Duration } from '../../../src/core/time/duration.js';
import { PathEx } from '../../../src/business/utils/path-ex.js';
import { EndToEndTestSuiteBuilder } from '../end-to-end-test-suite-builder.js';
import { main } from '../../../src/index.js';
import { BaseCommandTest } from './tests/base-command-test.js';
const testName = 'quick-start-single';
const testTitle = 'Quick Start Single E2E Test';
const endToEndTestSuite = new EndToEndTestSuiteBuilder()
    .withTestName(testName)
    .withTestSuiteName(`${testTitle} Suite`)
    .withNamespace(testName)
    .withDeployment(`${testName}-deployment`)
    .withClusterCount(1)
    .withTestSuiteCallback((options) => {
    describe(testTitle, () => {
        const { testCacheDirectory, testLogger, namespace, contexts } = options;
        // TODO the kube config context causes issues if it isn't one of the selected clusters we are deploying to
        before(async () => {
            fs.rmSync(testCacheDirectory, { recursive: true, force: true });
            try {
                fs.rmSync(PathEx.joinWithRealPath(testCacheDirectory, '..', DEFAULT_LOCAL_CONFIG_FILE), {
                    force: true,
                });
            }
            catch {
                // allowed to fail if the file doesn't exist
            }
            if (!fs.existsSync(testCacheDirectory)) {
                fs.mkdirSync(testCacheDirectory, { recursive: true });
            }
            resetForTest(namespace.name, testCacheDirectory, false);
            for (const item of contexts) {
                const k8Client = container.resolve(InjectTokens.K8Factory).getK8(item);
                await k8Client.namespaces().delete(namespace);
            }
            testLogger.info(`${testName}: starting ${testName} e2e test`);
        }).timeout(Duration.ofMinutes(5).toMillis());
        after(async () => {
            testLogger.info(`${testName}: beginning ${testName}: destroy`);
            await main(soloQuickStartDestroy(testName));
            testLogger.info(`${testName}: finished ${testName}: destroy`);
        }).timeout(Duration.ofMinutes(5).toMillis());
        // TODO pass in namespace for cache directory for proper destroy on restart
        it(`${testName}: deploy`, async () => {
            testLogger.info(`${testName}: beginning ${testName}: deploy`);
            await main(soloQuickStartDeploy(testName));
            testLogger.info(`${testName}: finished ${testName}: deploy`);
        }).timeout(Duration.ofMinutes(15).toMillis());
        // TODO add verifications
    });
})
    .build();
endToEndTestSuite.runTestSuite();
export function soloQuickStartDeploy(testName) {
    const { newArgv, argvPushGlobalFlags } = BaseCommandTest;
    const argv = newArgv();
    argv.push('quick-start', 'single', 'deploy');
    argvPushGlobalFlags(argv, testName);
    return argv;
}
export function soloQuickStartDestroy(testName) {
    const { newArgv, argvPushGlobalFlags } = BaseCommandTest;
    const argv = newArgv();
    argv.push('quick-start', 'single', 'destroy');
    argvPushGlobalFlags(argv, testName);
    return argv;
}
//# sourceMappingURL=quick-start-single.test.js.map