"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Fs = require("fs");
const util_1 = require("util");
const Helpers = require("@unblocked-web/plugins-testing/helpers");
const agent_1 = require("@unblocked-web/agent");
const Location_1 = require("@unblocked-web/specifications/agent/browser/Location");
const plugins_testing_1 = require("@unblocked-web/plugins-testing");
const index_1 = require("../index");
const pluginsChrome = require("./plugins-Chrome.json");
const DomOverridesBuilder_1 = require("../lib/DomOverridesBuilder");
const parseNavigatorPlugins_1 = require("../lib/utils/parseNavigatorPlugins");
const DomExtractor = require("./DomExtractor");
const paths_1 = require("../paths");
const logger = plugins_testing_1.TestLogger.forTest(module);
let navigatorConfig;
let browser;
beforeEach(Helpers.beforeEach);
beforeAll(async () => {
    const selectBrowserMeta = index_1.default.selectBrowserMeta('~ mac = 10.15');
    const { browserVersion, operatingSystemVersion } = selectBrowserMeta.userAgentOption;
    const asOsDataDir = `${paths_1.emulatorDataDir}/as-chrome-${browserVersion.major}-0/as-mac-os-${operatingSystemVersion.major}-${operatingSystemVersion.minor}`;
    const navigatorJsonPath = `${asOsDataDir}/window-navigator.json`;
    ({ navigator: navigatorConfig } = JSON.parse(Fs.readFileSync(navigatorJsonPath, '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 override plugins in a browser window', async () => {
    const httpServer = await Helpers.runHttpServer();
    const context = await browser.newContext({ logger });
    Helpers.onClose(() => context.close());
    const page = await context.newPage();
    page.on('page-error', console.log);
    if (debug) {
        page.on('console', console.log);
    }
    const pluginsData = (0, parseNavigatorPlugins_1.default)(navigatorConfig);
    if (debug)
        console.log(pluginsData);
    await page.addNewDocumentScript((0, DomOverridesBuilder_1.getOverrideScript)('navigator.plugins', pluginsData).script, false);
    await page.goto(httpServer.baseUrl);
    await page.waitForLoad(Location_1.LoadStatus.DomContentLoaded);
    const hasPlugins = await page.mainFrame.evaluate(`'plugins' in navigator && 'mimeTypes' in navigator`, false);
    expect(hasPlugins).toBe(true);
    const pluginCount = await page.mainFrame.evaluate(`navigator.plugins.length`, false);
    expect(pluginCount).toBe(pluginsData.plugins.length);
    const plugin1Mimes = await page.mainFrame.evaluate(`(() => {
  let mimes = [];
  for(const mime of navigator.plugins[0]) {
    mimes.push(mime.type);
  }
  return mimes;
})()`, false);
    expect(plugin1Mimes).toStrictEqual(pluginsData.mimeTypes.map(x => x.type));
    const mimecount = await page.mainFrame.evaluate(`navigator.mimeTypes.length`, false);
    expect(mimecount).toBe(pluginsData.mimeTypes.length);
    const structure = JSON.parse((await page.mainFrame.evaluate(`new (${DomExtractor.toString()})('window').run(window, 'window',  ['Plugin', 'PluginArray', 'MimeType', 'MimeTypeArray','navigator'])`, false))).window;
    for (const proto of ['Plugin', 'PluginArray', 'MimeType', 'MimeTypeArray']) {
        if (debug)
            console.log(proto, (0, util_1.inspect)(structure[proto], false, null, true));
        expect(structure[proto]).toStrictEqual(pluginsChrome[proto]);
    }
    const navigatorPageStructure = structure.navigator;
    if (debug) {
        console.log('Installed', (0, util_1.inspect)(navigatorPageStructure.mimeTypes, false, null, true));
        console.log('Expected', (0, util_1.inspect)(navigatorConfig.mimeTypes, false, null, true));
    }
    expect(navigatorPageStructure.mimeTypes).toStrictEqual(navigatorConfig.mimeTypes);
    if (debug)
        console.log((0, util_1.inspect)(navigatorPageStructure.plugins, false, null, true));
    expect(navigatorPageStructure.plugins).toStrictEqual(navigatorConfig.plugins);
}, 60e3);
test('it should override userAgentData in a browser window', async () => {
    const httpServer = await Helpers.runHttpsServer((req, res) => {
        res.end('<html><head></head><body>Hi</body></html>');
    });
    const brands = [];
    for (let i = 0; i < 3; i += 1) {
        const { brand, version } = navigatorConfig.userAgentData.brands[i];
        brands.push({ brand: brand._$value, version: version._$value });
    }
    const context = await browser.newContext({
        logger,
        hooks: {
            onNewPage(page) {
                return page.addNewDocumentScript((0, DomOverridesBuilder_1.getOverrideScript)('navigator', {
                    userAgentData: {
                        brands,
                        platform: 'macOS',
                        mobile: false,
                    },
                }).script, false);
            },
        },
    });
    Helpers.onClose(() => context.close());
    const page = await context.newPage();
    page.on('page-error', console.log);
    if (debug) {
        page.on('console', console.log);
    }
    const pluginsData = {};
    if (debug)
        console.log(pluginsData);
    await page.goto(httpServer.url);
    await page.waitForLoad(Location_1.LoadStatus.DomContentLoaded);
    const structure = JSON.parse((await page.mainFrame.evaluate(`new (${DomExtractor.toString()})('window').run(window, 'window',  ['NavigatorUAData','navigator'])`, false))).window;
    for (const proto of ['NavigatorUAData']) {
        if (debug)
            console.log(proto, (0, util_1.inspect)(structure[proto], false, null, true));
        expect(structure[proto]).toStrictEqual(pluginsChrome[proto]);
    }
    const navigatorPageStructure = structure.navigator;
    if (debug) {
        console.log('Installed', (0, util_1.inspect)(navigatorPageStructure, false, null, true));
        console.log('Expected', (0, util_1.inspect)(navigatorConfig, false, null, true));
    }
    expect(navigatorPageStructure.userAgentData).toStrictEqual(navigatorConfig.userAgentData);
}, 60e3);
//# sourceMappingURL=navigator.test.js.map