"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MobileTestRunner = void 0;
const execution_1 = require("@mablhq-internal/runtime/execution");
const mobileRunner_1 = require("./runners/mobileRunner");
const promises_1 = require("fs/promises");
const webViewUtil_1 = require("./mobile/util/webViewUtil");
class MobileTestRunner extends execution_1.MablTestRunner {
    constructor(mablTestRunnerConfig) {
        super(mablTestRunnerConfig);
    }
    async runTest(proxyOptions) {
        const mobileConfig = this.testRunConfig.mobileConfig;
        if (!mobileConfig) {
            throw new Error('Missing mobile configuration for a mobile test.');
        }
        const { alternativeFork, androidConfiguration, appiumAddress, appiumHome, appiumPort, mobileAppFileId, recordVideoPath, ...appConfig } = mobileConfig;
        if (mobileAppFileId && !appConfig.path) {
            const mobileAppFile = await this.mobileAppFileCache.getMobileAppFile(mobileAppFileId);
            appConfig.appName = mobileAppFile.name;
            appConfig.path = mobileAppFile.path;
            appConfig.appVersion = mobileAppFile.version;
        }
        if (appConfig.path && !this.testRunConfig.isCloudRun) {
            try {
                await (0, promises_1.stat)(appConfig.path);
            }
            catch (error) {
                throw new Error(`Specified mobile app path not found: [${appConfig.path}] (${error})`);
            }
        }
        const platformName = appConfig.platformName;
        if (!platformName) {
            throw new Error('Must specify mobile platform to run on');
        }
        const runConfig = {
            androidConfiguration,
            branchName: this.branchName,
            interactionSpeed: this.testRunConfig.interactionSpeed,
            headless: this.testRunConfig.headless,
            observerConfig: this.observerConfig,
            proxy: proxyOptions,
            appConfig: { platformName, ...appConfig },
            recordVideoPath,
            server: {
                address: appiumAddress,
                alternativeFork,
                appiumHome,
                port: appiumPort,
                startAppiumServer: !this.testRunConfig.isCloudRun,
                chromedriverExecutableDir: this.testRunConfig.isCloudRun
                    ? webViewUtil_1.ANDROID_CLOUD_IMAGE_CHROMEDRIVER_EXECUTABLE_DIR
                    : undefined,
            },
        };
        const mobileRunConfig = {
            testContext: this.testContext,
            apiClient: this.apiClient,
            runConfig,
            findModels: [],
            findOverrides: [],
            findSummaries: [],
            testExecutionProxy: this.testExecutionProxy,
            isCliCreated: this.testRunConfig._cliCreated,
            allowLocalTestBilling: this.testRunConfig.allowLocalTestBilling,
        };
        return new mobileRunner_1.MobileRunner(mobileRunConfig).runTest();
    }
}
exports.MobileTestRunner = MobileTestRunner;
