<commit_before>'use strict';

const path = require('path');
const Promise = require('bluebird');
const _ = require('lodash');
const fs = require('fs-extra');
const utils = require('../../utils');
const {logger} = require('../../utils');

const NO_STATE = 'NO_STATE';

module.exports = class ReportBuilder {
    static create(toolConfig, pluginConfig, TestAdapter) {
        return new ReportBuilder(toolConfig, pluginConfig, TestAdapter);
    }

    constructor(toolConfig, pluginConfig, TestAdapter) {
        this._tree = {name: 'root'};
        this._skips = [];
        this._toolConfig = toolConfig;
        this._pluginConfig = pluginConfig;
        this._TestAdapter = TestAdapter;
    }

    format(result) {
        return this._TestAdapter.create(result, this._toolConfig);
    }

    addSkipped(result) {
        result = this.format(result);
        const comment = result.suite.skipComment;

        this._skips.push({suite: result.suite.fullName, browser: result.browserId, comment});

        this._addTestResult(result, {
            skipped: true,
            reason: comment
        });
    }

    addSuccess(result) {
        result = this.format(result);
        this._addTestResult(result, {
            success: true,
            actualPath: utils.getCurrentPath(result),
            expectedPath: utils.getReferencePath(result)
        });
    }

    addFail(result) {
        result = this.format(result);
        this._addFailResult(result);
    }

    _addFailResult(result) {
        this._addTestResult(result, {
            fail: true,
            actualPath: utils.getCurrentPath(result),
            expectedPath: utils.getReferencePath(result),
            diffPath: utils.getDiffPath(result)
        });
    }

    addError(result) {
        result = this.format(result);
        this._addErrorResult(result);
    }

    _addErrorResult(result) {
        this._addTestResult(result, {
            actualPath: result.state ? utils.getCurrentPath(result) : '',
            error: true,
            image: !!result.imagePath || !!result.currentPath || !!result.screenshot,
            reason: result.error
        });
    }

    addRetry(result) {
        result = this.format(result);

        if (result.isEqual()) {
            this._addFailResult(result);
        } else {
            this._addErrorResult(result);
        }
    }

    setStats(stats) {
        this._stats = stats;

        return this;
    }

    _addTestResult(result, props) {
        const {browserId, suite, sessionId} = result;
        const {baseHost} = this._pluginConfig;
        const suiteUrl = suite.getUrl({browserId, baseHost});

        const metaInfo = {url: suite.fullUrl, file: suite.file, sessionId};
        const testResult = _.assign({suiteUrl, metaInfo}, props);

        const node = findOrCreate(this._tree, suite.path.concat(result.state ? result.state.name : NO_STATE));
        node.browsers = Array.isArray(node.browsers) ? node.browsers : [];

        const existing = _.findIndex(node.browsers, {name: browserId});

        if (existing === -1) {
            node.browsers.push({name: browserId, result: testResult, retries: []});

            return;
        }

        const stateInBrowser = node.browsers[existing];
        const previousResult = stateInBrowser.result;

        if (!previousResult.skipped) {
            stateInBrowser.retries.push(previousResult);
        }

        stateInBrowser.result = testResult;
    }

    save() {
        const reportDir = this._pluginConfig.path;

        return fs.mkdirsAsync(reportDir)
            .then(() => Promise.all([
                fs.writeFileAsync(path.join(reportDir, 'data.js'), this._prepareData(), 'utf8'),
                this._copyToReportDir(['index.html', 'bundle.min.js', 'bundle.min.css'])<commit_msg>fix: add browser name to skip reason<commit_after>'use strict';

const path = require('path');
const Promise = require('bluebird');
const _ = require('lodash');
const fs = require('fs-extra');
const utils = require('../../utils');
const {logger} = require('../../utils');

const NO_STATE = 'NO_STATE';

module.exports = class ReportBuilder {
    static create(toolConfig, pluginConfig, TestAdapter) {
        return new ReportBuilder(toolConfig, pluginConfig, TestAdapter);
    }

    constructor(toolConfig, pluginConfig, TestAdapter) {
        this._tree = {name: 'root'};
        this._skips = [];
        this._toolConfig = toolConfig;
        this._pluginConfig = pluginConfig;
        this._TestAdapter = TestAdapter;
    }

    format(result) {
        return this._TestAdapter.create(result, this._toolConfig);
    }

    addSkipped(result) {
        result = this.format(result);
        const comment = result.suite.skipComment;

        this._skips.push({suite: result.suite.fullName, browser: result.browserId, comment});

        this._addTestResult(result, {
            skipped: true,
            reason: comment
        });
    }

    addSuccess(result) {
        result = this.format(result);
        this._addTestResult(result, {
            success: true,
            actualPath: utils.getCurrentPath(result),
            expectedPath: utils.getReferencePath(result)
        });
    }

    addFail(result) {
        result = this.format(result);
        this._addFailResult(result);
    }

    _addFailResult(result) {
        this._addTestResult(result, {
            fail: true,
            actualPath: utils.getCurrentPath(result),
            expectedPath: utils.getReferencePath(result),
            diffPath: utils.getDiffPath(result)
        });
    }

    addError(result) {
        result = this.format(result);
        this._addErrorResult(result);
    }

    _addErrorResult(result) {
        this._addTestResult(result, {
            actualPath: result.state ? utils.getCurrentPath(result) : '',
            error: true,
            image: !!result.imagePath || !!result.currentPath || !!result.screenshot,
            reason: result.error
        });
    }

    addRetry(result) {
        result = this.format(result);

        if (result.isEqual()) {
            this._addFailResult(result);
        } else {
            this._addErrorResult(result);
        }
    }

    setStats(stats) {
        this._stats = stats;

        return this;
    }

    _addTestResult(result, props) {
        const {browserId, suite, sessionId} = result;
        const {baseHost} = this._pluginConfig;
        const suiteUrl = suite.getUrl({browserId, baseHost});

        const metaInfo = {url: suite.fullUrl, file: suite.file, sessionId};
        const testResult = _.assign({suiteUrl, name: browserId, metaInfo}, props);

        const node = findOrCreate(this._tree, suite.path.concat(result.state ? result.state.name : NO_STATE));
        node.browsers = Array.isArray(node.browsers) ? node.browsers : [];

        const existing = _.findIndex(node.browsers, {name: browserId});

        if (existing === -1) {
            node.browsers.push({name: browserId, result: testResult, retries: []});

            return;
        }

        const stateInBrowser = node.browsers[existing];
        const previousResult = stateInBrowser.result;

        if (!previousResult.skipped) {
            stateInBrowser.retries.push(previousResult);
        }

        stateInBrowser.result = testResult;
    }

    save() {
        const reportDir = this._pluginConfig.path;

        return fs.mkdirsAsync(reportDir)
            .then(() => Promise.all([
                fs.writeFileAsync(path.join(reportDir, 'data.js'), this._prepareData(), 'utf8'),
                this._copyToReportDir(['index.html', 'bundle.min.js', 'bundle.min.css'])
{'fraction_non_alphanumeric': 0.12715629900679562, 'fraction_numerical': 0.0005227391531625719, 'mean_word_length': 2.3551074090311266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4032619', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2144, 'n_words': 399}