"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 platform_js_1 = require("../../conductor/platform.js");
const test_config_js_1 = require("../../conductor/test_config.js");
const mocha_interface_helpers_js_1 = require("./mocha-interface-helpers.js");
const state_provider_js_1 = require("./state-provider.js");
function devtoolsTestInterface(rootSuite) {
    let defaultImplementation;
    let mochaGlobals;
    let mochaRoot;
    rootSuite.on(Mocha.Suite.constants.EVENT_FILE_PRE_REQUIRE, (context, file, mocha) => {
        mochaGlobals = context;
        mochaRoot = mocha;
        // Different module outputs between tsc and esbuild.
        const defaultFactory = ('default' in commonInterface ? commonInterface.default : commonInterface);
        defaultImplementation = defaultFactory([rootSuite], context, mocha);
        if (mocha.options.delay) {
            context.run = defaultImplementation.runWithSuite(rootSuite);
        }
        // @ts-expect-error Custom interface.
        context.describe = customDescribe(defaultImplementation.suite, file);
    });
    function customDescribe(suiteImplementation, file) {
        function withAugmentedTitle(suiteFn) {
            return function (title, describeBodyFn) {
                const suite = suiteFn({
                    title: describeTitle(file, title),
                    file,
                    fn: function () {
                        const thisSuite = this;
                        const parentDefinitions = { describe: mochaGlobals.describe, setup: mochaGlobals.setup, it: mochaGlobals.it };
                        // @ts-expect-error Custom interface.
                        mochaGlobals.describe = customDescribe(defaultImplementation.suite, '', thisSuite);
                        // @ts-expect-error Custom interface.
                        mochaGlobals.setup = function (suiteSettings) {
                            state_provider_js_1.StateProvider.instance.registerSuiteSettings(thisSuite, suiteSettings);
                        };
                        // @ts-expect-error Custom interface.
                        mochaGlobals.it = customIt(defaultImplementation.test, thisSuite, thisSuite.file || '', mochaRoot);
                        if (describeBodyFn) {
                            describeBodyFn.call(thisSuite);
                        }
                        // Restore definitions so when we come back from a nested describe
                        // we have the same definitions available as for the current block,
                        // therefore correctly handling the next describe block that is at
                        // the same level with this one.
                        mochaGlobals.describe = parentDefinitions.describe;
                        mochaGlobals.setup = parentDefinitions.setup;
                        mochaGlobals.it = parentDefinitions.it;
                    },
                });
                if (!suite.isPending()) {
                    suite.beforeEach(async function () {
                        this.timeout(0);
                        await state_provider_js_1.StateProvider.instance.resolveBrowser(suite);
                    });
                }
                return suite;
            };
        }
        const describe = withAugmentedTitle(suiteImplementation.create.bind(suiteImplementation));
        // @ts-expect-error Custom interface.
        describe.only = withAugmentedTitle(suiteImplementation.only.bind(suiteImplementation));
        // @ts-expect-error Custom interface.
        describe.skip = withAugmentedTitle(suiteImplementation.skip.bind(suiteImplementation));
        return describe;
    }
}
function describeTitle(file, 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 customIt(testImplementation, suite, file, mocha) {
    function createTest(title, itBodyFn) {
        const test = new Mocha.Test(title, suite.isPending() || !itBodyFn ? undefined : mocha_interface_helpers_js_1.InstrumentedTestFunction.instrument(itBodyFn, 'test', suite));
        test.file = file;
        // Creates a proxy that changes the duration to return
        // our own timing.
        const proxyTest = new Proxy(test, {
            get(target, property, receiver) {
                if (property === 'duration' && target.realDuration) {
                    return Reflect.get(target, 'realDuration', receiver) ?? Reflect.get(target, property, receiver);
                }
                return Reflect.get(target, property, receiver);
            },
        });
        suite.addTest(proxyTest);
        return proxyTest;
    }
    // Regular mocha it returns the test instance.
    // It is not possible with TestConfig.repetitions.
    const localIt = function (title, fn) {
        for (let i = 0; i < test_config_js_1.TestConfig.repetitions; i++) {
            createTest(title, fn);
        }
    };
    localIt.skip = function (title, _fn) {
        // no fn to skip.
        return createTest(title);
    };
    localIt.only = function (title, fn) {
        for (let i = 0; i < test_config_js_1.TestConfig.repetitions; i++) {
            testImplementation.only(mocha, createTest(title, fn));
        }
    };
    localIt.skipOnPlatforms = function (platforms, title, fn) {
        const shouldSkip = platforms.includes(platform_js_1.platform);
        if (shouldSkip) {
            return localIt.skip(title, fn);
        }
        return localIt(title, fn);
    };
    return localIt;
}
devtoolsTestInterface.description = 'DevTools test interface';
module.exports = devtoolsTestInterface;
//# sourceMappingURL=mocha-interface.js.map