"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.testEndHook = exports.testSystemInfoHook = exports.errorInStepHook = void 0;
const utils_1 = require("./utils");
const os_1 = require("os");
const child_process_1 = require("child_process");
const util_1 = require("util");
async function errorInStepHook(testContext, fnName, proxyContext, rootPage, args, instrumentedFunctionResult) {
    if (!instrumentedFunctionResult.success) {
        testContext.addStepMetadata({
            stepError: unknownErrorToOurRepresentation(instrumentedFunctionResult.error),
        });
    }
}
exports.errorInStepHook = errorInStepHook;
async function getBranchInfo() {
    var _a, _b, _c, _d;
    function getEnvironmentGitBranch() {
        var _a, _b, _c;
        return ((_c = (_b = (_a = process.env.GIT_BRANCH) !== null && _a !== void 0 ? _a : process.env.CIRCLE_BRANCH) !== null && _b !== void 0 ? _b : process.env.TRAVIS_BRANCH) !== null && _c !== void 0 ? _c : process.env.CI_BRANCH);
    }
    async function getCodeGitBranch() {
        let command;
        if (os_1.platform() === 'win32') {
            command = `pushd ${process.cwd()} & git branch | findstr \\*`;
        }
        else {
            command = `(cd ${process.cwd()} ; git branch | grep \\*)`;
        }
        try {
            const result = await util_1.promisify(child_process_1.exec)(command);
            return result.stdout.slice(2, result.stdout.length);
        }
        catch (e) {
            return '';
        }
    }
    function getEnvironmentGitCommit() {
        var _a, _b;
        return (_b = (_a = process.env.GIT_COMMIT) !== null && _a !== void 0 ? _a : process.env.CIRCLE_SHA1) !== null && _b !== void 0 ? _b : process.env.TRAVIS_COMMIT;
    }
    async function getGitCommitExec() {
        try {
            return (await util_1.promisify(child_process_1.exec)('git rev-parse HEAD')).stdout;
        }
        catch (e) {
            return '';
        }
    }
    const branchName = (_b = ((_a = getEnvironmentGitBranch()) !== null && _a !== void 0 ? _a : (await getCodeGitBranch()))) === null || _b === void 0 ? void 0 : _b.trim();
    const commitHash = (_d = ((_c = getEnvironmentGitCommit()) !== null && _c !== void 0 ? _c : (await getGitCommitExec()))) === null || _d === void 0 ? void 0 : _d.trim();
    return { branchName, commitHash };
}
async function testSystemInfoHook(testContext, proxyContext, rootPage) {
    const [systemInfo, branchInfo] = await Promise.all([
        utils_1.getSystemInfoForPage(rootPage),
        getBranchInfo(),
    ]);
    testContext.addTestMetadata({
        systemInfo,
        branchInfo,
    });
}
exports.testSystemInfoHook = testSystemInfoHook;
function unknownErrorToOurRepresentation(error) {
    var _a, _b, _c;
    if (error instanceof Error) {
        return {
            name: error.name,
            message: error.message,
            stack: error.stack,
        };
    }
    if (typeof error === 'object' && error !== null && 'message' in error) {
        const crossRealmError = error;
        return {
            name: (_a = crossRealmError === null || crossRealmError === void 0 ? void 0 : crossRealmError.name) !== null && _a !== void 0 ? _a : '',
            message: (_b = crossRealmError === null || crossRealmError === void 0 ? void 0 : crossRealmError.message) !== null && _b !== void 0 ? _b : '',
            stack: (_c = crossRealmError === null || crossRealmError === void 0 ? void 0 : crossRealmError.stack) !== null && _c !== void 0 ? _c : '',
        };
    }
    return {
        name: 'non serializable error?',
        message: 'non serializabsle error',
    };
}
async function testEndHook(testContext, testEndStatus) {
    if (testEndStatus.success) {
        testContext.addTestMetadata({
            testEndStatus: {
                success: true,
            },
        });
    }
    else {
        const error = unknownErrorToOurRepresentation(testEndStatus.error);
        let codeLocationDetails;
        try {
            codeLocationDetails = utils_1.extractCodeLocationDetailsSync(testContext.testFilePath, process.cwd());
        }
        catch (e) {
        }
        testContext.addTestMetadata({
            testEndStatus: {
                success: testEndStatus.success,
                error,
                codeLocationDetails,
            },
        });
    }
}
exports.testEndHook = testEndHook;
