"use strict";
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const console_helpers_js_1 = require("../helpers/console-helpers.js");
const quick_open_helpers_js_1 = require("../helpers/quick_open-helpers.js");
const sources_helpers_js_1 = require("../helpers/sources-helpers.js");
describe('Snippet creation', () => {
    it('can show newly created snippets show up in command menu', async ({ devToolsPage }) => {
        await (0, sources_helpers_js_1.openSourcesPanel)(devToolsPage);
        await (0, sources_helpers_js_1.openSnippetsSubPane)(devToolsPage);
        await (0, sources_helpers_js_1.createNewSnippet)('New snippet', undefined, devToolsPage);
        await (0, quick_open_helpers_js_1.openCommandMenu)(devToolsPage);
        await (0, quick_open_helpers_js_1.showSnippetsAutocompletion)(devToolsPage);
        chai_1.assert.deepEqual(await (0, quick_open_helpers_js_1.getAvailableSnippets)(devToolsPage), ['New snippet']);
        await devToolsPage.typeText('New ');
        chai_1.assert.deepEqual(await (0, quick_open_helpers_js_1.getAvailableSnippets)(devToolsPage), ['New snippet']);
        await devToolsPage.typeText('w');
        chai_1.assert.deepEqual(await (0, quick_open_helpers_js_1.getAvailableSnippets)(devToolsPage), []);
        await devToolsPage.pressKey('Backspace');
        // TODO: it should actually wait for rendering to finish.
        await devToolsPage.drainTaskQueue();
        chai_1.assert.deepEqual(await (0, quick_open_helpers_js_1.getAvailableSnippets)(devToolsPage), ['New snippet']);
    });
});
describe('Expression evaluation', () => {
    const message = '\'Hello\'';
    async function selectFunctionParameterElement(devToolsPage) {
        const functionParameterElement = await devToolsPage.waitFor('.token-definition');
        const parameterElementPosition = await functionParameterElement.evaluate(elem => {
            const { x, y, right } = elem.getBoundingClientRect();
            return { x, y, right };
        });
        await devToolsPage.page.mouse.move(parameterElementPosition.x, parameterElementPosition.y);
        await devToolsPage.page.mouse.down();
        await devToolsPage.page.mouse.move(parameterElementPosition.right, parameterElementPosition.y);
        await devToolsPage.page.mouse.up();
    }
    async function navigateToSourcesAndRunSnippet(devToolsPage) {
        await (0, sources_helpers_js_1.openSourcesPanel)(devToolsPage);
        await (0, sources_helpers_js_1.openSnippetsSubPane)(devToolsPage);
        await (0, sources_helpers_js_1.createNewSnippet)('New snippet', undefined, devToolsPage);
        await devToolsPage.typeText(`(x => {debugger})(${message});`);
        await (0, sources_helpers_js_1.runSnippet)(devToolsPage);
    }
    it('evaluates a selected expression in the console', async ({ devToolsPage }) => {
        await navigateToSourcesAndRunSnippet(devToolsPage);
        const messages = await devToolsPage.waitForFunction(async () => {
            await selectFunctionParameterElement(devToolsPage);
            await (0, sources_helpers_js_1.evaluateSelectedTextInConsole)(devToolsPage);
            const maybeMessages = await (0, console_helpers_js_1.maybeGetCurrentConsoleMessages)(false, undefined, devToolsPage);
            if (maybeMessages.length) {
                return maybeMessages;
            }
            await (0, sources_helpers_js_1.openSourcesPanel)(devToolsPage);
            await (0, sources_helpers_js_1.openSnippetsSubPane)(devToolsPage);
            return null;
        });
        chai_1.assert.deepEqual(messages, [
            message,
        ]);
        await devToolsPage.reload();
    });
    it('adds an expression to watches', async ({ devToolsPage }) => {
        await navigateToSourcesAndRunSnippet(devToolsPage);
        const watchExpressions = await devToolsPage.waitForFunction(async () => {
            await selectFunctionParameterElement(devToolsPage);
            await (0, sources_helpers_js_1.addSelectedTextToWatches)(devToolsPage);
            return await (0, sources_helpers_js_1.getWatchExpressionsValues)(devToolsPage);
        });
        chai_1.assert.isOk(watchExpressions, 'No watch expressions found');
        const cleanWatchExpressions = watchExpressions.map(expression => expression.replace(/["]+/g, '\''));
        chai_1.assert.deepEqual(cleanWatchExpressions[0], message);
        await devToolsPage.reload();
    });
});
describe('Snippet evaluation', () => {
    it('highlights the correct line when a snippet throws an error', async ({ devToolsPage }) => {
        await (0, sources_helpers_js_1.openSourcesPanel)(devToolsPage);
        await (0, sources_helpers_js_1.openSnippetsSubPane)(devToolsPage);
        await (0, sources_helpers_js_1.createNewSnippet)('throwing', `
      (function foo() {
        throw new Error('kaboom');
      })();`, devToolsPage);
        await (0, sources_helpers_js_1.runSnippet)(devToolsPage);
        const errorLine = await devToolsPage.waitFor('.cm-waveUnderline');
        const text = await errorLine.evaluate(el => el.textContent);
        chai_1.assert.strictEqual(text, 'throw new Error(\'kaboom\');');
    });
});
//# sourceMappingURL=snippets.test.js.map