"use strict";
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const Mocha = __importStar(require("mocha"));
// @ts-expect-error
const commonInterface = __importStar(require("mocha/lib/interfaces/common.js"));
const Path = __importStar(require("node:path"));
const mocha_interface_helpers_js_1 = require("./mocha-interface-helpers.js");
const platform_js_1 = require("./platform.js");
const test_config_js_1 = require("./test_config.js");
function devtoolsTestInterface(suite) {
    const suites = [suite];
    suite.on(Mocha.Suite.constants.EVENT_FILE_PRE_REQUIRE, function (context, file, mocha) {
        const common = 
        // Different module outputs between tsc and esbuild.
        ('default' in commonInterface ? commonInterface.default : commonInterface)(suites, context, mocha);
        // @ts-expect-error Custom interface.
        context['before'] = function before(fn) {
            return common.before((0, mocha_interface_helpers_js_1.makeInstrumentedTestFunction)(fn, 'before'));
        };
        // @ts-expect-error Custom interface.
        context['after'] = function after(fn) {
            return common.after((0, mocha_interface_helpers_js_1.makeInstrumentedTestFunction)(fn, 'after'));
        };
        // @ts-expect-error Custom interface.
        context['beforeEach'] = function beforeEach(fn) {
            return common.beforeEach((0, mocha_interface_helpers_js_1.makeInstrumentedTestFunction)(fn, 'beforeEach'));
        };
        // @ts-expect-error Custom interface.
        context['afterEach'] = function afterEach(fn) {
            return common.afterEach((0, mocha_interface_helpers_js_1.makeInstrumentedTestFunction)(fn, 'afterEach'));
        };
        if (mocha.options.delay) {
            context['run'] = common.runWithSuite(suite);
        }
        function describeTitle(title) {
            const parsedPath = Path.parse(file);
            const directories = parsedPath.dir.split(Path.sep);
            const index = directories.lastIndexOf('e2e');
            let prefix = parsedPath.name;
            if (index >= 0) {
                prefix = [...directories.slice(index + 1), `${parsedPath.name}.ts`].join('/');
            }
            if (title.includes(prefix)) {
                return title;
            }
            return `${prefix}: ${title}`;
        }
        function describe(title, fn) {
            return common.suite.create({
                title: describeTitle(title),
                file,
                fn,
            });
        }
        describe.only = function (title, fn) {
            return common.suite.only({
                title: describeTitle(title),
                file,
                fn,
            });
        };
        describe.skip = function (title, fn) {
            return common.suite.skip({
                title: describeTitle(title),
                file,
                fn,
            });
        };
        // @ts-expect-error Custom interface.
        context['describe'] = describe;
        function iterationSuffix(iteration) {
            if (iteration === 0) {
                return '';
            }
            return ` (#${iteration})`;
        }
        function createTest(title, fn) {
            const suite = suites[0];
            const test = new Mocha.Test(title, suite.isPending() || !fn ? undefined : (0, mocha_interface_helpers_js_1.makeInstrumentedTestFunction)(fn, 'test'));
            test.file = file;
            suite.addTest(test);
            return test;
        }
        // Regular mocha it returns the test instance.
        // It is not possible with TestConfig.repetitions.
        const it = function (title, fn) {
            for (let i = 0; i < test_config_js_1.TestConfig.repetitions; i++) {
                const iterationTitle = title + iterationSuffix(i);
                createTest(iterationTitle, fn);
            }
        };
        // @ts-expect-error Custom interface.
        context.it = it;
        it.skip = function (title, _fn) {
            // no fn to skip.
            return createTest(title);
        };
        it.only = function (title, fn) {
            for (let i = 0; i < test_config_js_1.TestConfig.repetitions; i++) {
                const iterationTitle = title + iterationSuffix(i);
                common.test.only(mocha, createTest(iterationTitle, fn));
            }
        };
        it.skipOnPlatforms = function (platforms, title, fn) {
            const shouldSkip = platforms.includes(platform_js_1.platform);
            if (shouldSkip) {
                return context.it.skip(title);
            }
            return context.it(title, fn);
        };
    });
}
devtoolsTestInterface.description = 'DevTools test interface';
module.exports = devtoolsTestInterface;
//# sourceMappingURL=mocha-interface.js.map