"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Fs = require("fs");
const Path = require("path");
const Helpers = require("@unblocked-web/plugins-testing/helpers");
const util_1 = require("util");
const agent_1 = require("@unblocked-web/agent");
const plugins_testing_1 = require("@unblocked-web/plugins-testing");
const index_1 = require("../index");
const DomOverridesBuilder_1 = require("../lib/DomOverridesBuilder");
const DomExtractor = require("./DomExtractor");
const paths_1 = require("../paths");
let chrome;
let prevProperty;
let browser;
beforeEach(Helpers.beforeEach);
beforeAll(async () => {
    const selectBrowserMeta = index_1.default.selectBrowserMeta('~ mac = 10.14');
    const { browserVersion, operatingSystemVersion } = selectBrowserMeta.userAgentOption;
    const windowChromePath = Path.resolve(paths_1.emulatorDataDir, `as-chrome-${browserVersion.major}-0/as-mac-os-${operatingSystemVersion.major}-${operatingSystemVersion.minor}/window-chrome.json`);
    ({ chrome, prevProperty } = JSON.parse(Fs.readFileSync(windowChromePath, 'utf8')));
    browser = new agent_1.Browser(selectBrowserMeta.browserEngine);
    Helpers.onClose(() => browser.close(), true);
    await browser.launch();
});
afterAll(Helpers.afterAll);
afterEach(Helpers.afterEach);
const debug = process.env.DEBUG || false;
test('it should mimic a chrome object', async () => {
    const httpServer = await Helpers.runHttpServer();
    const page = await createPage();
    const script = (0, DomOverridesBuilder_1.getOverrideScript)('window.chrome', {
        polyfill: {
            property: chrome,
            prevProperty,
        },
    }).script;
    await page.addNewDocumentScript(script, false);
    await Promise.all([
        page.navigate(httpServer.baseUrl),
        page.mainFrame.waitOn('frame-lifecycle', ev => ev.name === 'DOMContentLoaded'),
    ]);
    const structure = JSON.parse((await page.mainFrame.evaluate(`new (${DomExtractor.toString()})('window').run(window, 'window', ['chrome'])`, false))).window;
    if (debug)
        console.log((0, util_1.inspect)(structure.chrome, false, null, true));
    // these values are getting out of order in recreation, but that doesn't impact anything
    expect(structure.chrome._$type).toBe(chrome._$type);
    expect(structure.chrome._$flags).toBe(chrome._$flags);
    delete structure.chrome._$type;
    delete structure.chrome._$flags;
    delete chrome._$type;
    delete chrome._$flags;
    const structureJson = JSON.stringify(structure.chrome, (key, value) => {
        if (key === '_$value' || key === '_$invocation')
            return undefined;
        return value;
    });
    const chromeJson = JSON.stringify(chrome, (key, value) => {
        if (key === '_$value' || key === '_$invocation')
            return undefined;
        return value;
    });
    // must delete csi's invocation since it's different on each run
    expect(structureJson).toBe(chromeJson);
}, 60e3);
test('it should update loadtimes and csi values', async () => {
    const httpServer = await Helpers.runHttpServer();
    const page = await createPage();
    await page.addNewDocumentScript((0, DomOverridesBuilder_1.getOverrideScript)('window.chrome', {
        updateLoadTimes: true,
        polyfill: {
            property: chrome,
            prevProperty,
        },
    }).script, false);
    await Promise.all([
        page.navigate(httpServer.baseUrl),
        page.mainFrame.waitOn('frame-lifecycle', ev => ev.name === 'DOMContentLoaded'),
    ]);
    const loadTimes = JSON.parse((await page.mainFrame.evaluate(`JSON.stringify(chrome.loadTimes())`, false)));
    if (debug)
        console.log((0, util_1.inspect)(loadTimes, false, null, true));
    expect(loadTimes.requestTime).not.toBe(chrome.loadTimes['new()'].requestTime._$value);
    const csi = JSON.parse((await page.mainFrame.evaluate(`JSON.stringify(chrome.csi())`, false)));
    if (debug)
        console.log((0, util_1.inspect)(csi, false, null, true));
    expect(csi.pageT).not.toBe(chrome.csi['new()'].pageT._$value);
    expect(csi.onloadT).not.toBe(chrome.csi['new()'].onloadT._$value);
    expect(String(csi.onloadT).length).toBe(String(chrome.csi['new()'].onloadT._$value).length);
    expect(Object.keys(csi)).toHaveLength(4);
}, 60e3);
async function createPage() {
    const context = await browser.newContext({ logger: plugins_testing_1.TestLogger.forTest(module) });
    Helpers.onClose(() => context.close());
    const page = await context.newPage();
    page.on('page-error', console.log);
    if (debug) {
        page.on('console', console.log);
    }
    return page;
}
//# sourceMappingURL=chrome.test.js.map