"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanJestMockFunctionCalls = exports.setupPageInstrumentorForTesting = exports.runPageInstrumentorTest = void 0;
const PuppeteerPageHooker_1 = require("../PuppeteerPageHooker");
const TestContextMemory_1 = require("../TestContextMemory");
const utils_1 = require("../utils");
async function runPageInstrumentorTest(testName, fullTestFileName, page, testCode) {
    const { pageInstrumentor, wrappedPage, hooks } = await setupPageInstrumentorForTesting(testName, fullTestFileName, page);
    pageInstrumentor.start();
    await testCode(wrappedPage);
    await pageInstrumentor.end({
        success: true,
    });
    return hooks;
}
exports.runPageInstrumentorTest = runPageInstrumentorTest;
async function setupPageInstrumentorForTesting(testName, fullTestFileName, page) {
    const testContext = new TestContextMemory_1.TestContextMemory('/tmp', testName, `full ${testName}`, fullTestFileName, {
        screenshots: false,
        console: false,
        networkLogs: false,
        jestAssertions: false,
        html: false,
    }, utils_1.createDateMocker());
    const pageInstrumentor = new PuppeteerPageHooker_1.PuppeteerPageHooker(testContext, page);
    const wrappedPage = pageInstrumentor.wrapWithProxy(page);
    const beforeAllHook = jest.fn();
    const afterAllHook = jest.fn();
    const beforeEachHook = jest.fn();
    const afterEachHook = jest.fn();
    pageInstrumentor.registerBeforeAllHook(beforeAllHook);
    pageInstrumentor.registerAfterAllHook(afterAllHook);
    pageInstrumentor.registerBeforeHook(beforeEachHook);
    pageInstrumentor.registerAfterHook(afterEachHook);
    return {
        wrappedPage,
        pageInstrumentor,
        hooks: {
            beforeAllHook,
            afterAllHook,
            beforeEachHook,
            afterEachHook,
        },
    };
}
exports.setupPageInstrumentorForTesting = setupPageInstrumentorForTesting;
const objPropToClean = [
    'testContext',
    'proxyContext',
    'rootPage',
    'instrumentedFunctionResult',
    'stepResult',
];
function cleanJestMockFunctionCalls(calls) {
    return calls.map((callArg) => {
        const cloned = { ...callArg };
        for (const prop of objPropToClean) {
            if (prop in cloned) {
                cloned[prop] = 'cleaned';
            }
        }
        return cloned;
    });
}
exports.cleanJestMockFunctionCalls = cleanJestMockFunctionCalls;
