"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestContext = void 0;
const StepResultWithName_1 = require("./StepResultWithName");
const fs_extra_1 = __importDefault(require("fs-extra"));
const consts_1 = require("./consts");
const path_1 = __importDefault(require("path"));
const debug_1 = __importDefault(require("debug"));
const loggerError = debug_1.default('root-cause:error');
class TestContext {
    constructor(testArtifactsFolder, testName, testFullName, testFilePath, featuresSettings, dateConstructor = Date) {
        this.testArtifactsFolder = testArtifactsFolder;
        this.testName = testName;
        this.testFullName = testFullName;
        this.testFilePath = testFilePath;
        this.featuresSettings = featuresSettings;
        this.dateConstructor = dateConstructor;
        this.stepIndex = 0;
        this.stepResults = [];
        this.testMetadata = {
            fileName: this.testFilePath,
            testName: this.testName,
            testFullName: this.testFullName,
            timestamp: 0,
            endedTimestamp: 0,
        };
        this._currentStep = undefined;
        this.consoleEntries = [];
        this.unhandledExceptions = [];
        this.testMetadata.timestamp = dateConstructor.now();
    }
    get currentStep() {
        return this._currentStep;
    }
    stepStarted() {
        this.stepIndex++;
        this._currentStep = new StepResultWithName_1.StepResultWithName(this.stepIndex, this.dateConstructor);
    }
    getStepIndex() {
        return this.stepIndex;
    }
    async stepEnded() {
        if (this._currentStep) {
            this._currentStep.endTimestamp = this.dateConstructor.now();
            this.stepResults.push(this._currentStep);
            this._currentStep = undefined;
        }
        await this.persistResults();
    }
    async testEnded() {
        this.testMetadata.endedTimestamp = this.dateConstructor.now();
        await this.persistResults();
    }
    addTestMetadata(metadata) {
        Object.assign(this.testMetadata, metadata);
    }
    addStepMetadata(metadata) {
        Object.assign(this._currentStep, metadata);
    }
    addAssertionStep(partialStep) {
        if (this._currentStep) {
            throw new Error("invariant: Can't add assertion in a middle of step");
        }
        this.stepIndex++;
        const stepWithIndex = {
            index: this.stepIndex,
            startTimestamp: this.dateConstructor.now(),
            ...partialStep,
        };
        this.stepResults.push(stepWithIndex);
    }
    async persistResults() {
        try {
            await fs_extra_1.default.writeFile(path_1.default.resolve(this.testArtifactsFolder, consts_1.TEST_RESULTS_FILE_NAME), JSON.stringify(this.getResultsForPersistency(), null, 2));
        }
        catch (error) {
            loggerError('persistResults error');
            loggerError(error);
        }
    }
    getResultsForPersistency() {
        return {
            metadata: this.testMetadata,
            steps: this.stepResults,
        };
    }
}
exports.TestContext = TestContext;
