// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {assert} from 'chai';
import {describe, it} from 'mocha';

import {click, getBrowserAndPages, pasteText, step} from '../../shared/helper.js';
import {CONSOLE_TAB_SELECTOR, focusConsolePrompt} from '../helpers/console-helpers.js';
import {getCurrentConsoleMessages} from '../helpers/console-helpers.js';

describe('The Console Tab', async () => {
  it('doesn’t break when global `eval` is overwritten', async () => {
    const {frontend} = getBrowserAndPages();
    let messages: string[];

    await step('navigate to the Console tab', async () => {
      await click(CONSOLE_TAB_SELECTOR);
      await focusConsolePrompt();
    });

    await step('enter code that overwrites eval', async () => {
      await pasteText(`
        const foo = 'fooValue';
        globalThis.eval = 'non-function';
      `);
      await frontend.keyboard.press('Enter');

      // Wait for the console to be usable again.
      await frontend.waitForFunction(() => {
        return document.querySelectorAll('.console-user-command-result').length === 1;
      });
    });

    await step('enter a code snippet', async () => {
      await pasteText('foo;');
      await frontend.keyboard.press('Enter');
    });

    await step('retrieve the console log', async () => {
      messages = await getCurrentConsoleMessages();
    });

    await step('check that the expected output is logged', async () => {
      assert.deepEqual(messages, [
        '"non-function"',
        '"fooValue"',
      ]);
    });
  });
});

Add e2e test for fake eval in Console
Add e2e test for fake eval in Console

This patch ports the following layout test:
https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/web_tests/http/tests/devtools/console/console-eval-fake.js;drc=78e2bc9c2def358a0745878ffc66ce85ee5221d8

CL removing the upstream test:
https://chromium-review.googlesource.com/c/chromium/src/+/2270177

Bug: chromium:1099603
Change-Id: Id33fa12254d4bf10f4797aa1f54487a0b3acd4cb
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2270157
Reviewed-by: Peter Marshall <6d0eecbeb7fb9d172f49b4b32481d2466fd89429@chromium.org>
Commit-Queue: Mathias Bynens <17a2cbc4e33156874a9862b0f530521117e66a3d@chromium.org>
