11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Flags: --expose-internals
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst stream = require('stream');
71cb0ef41Sopenharmony_ciconst REPL = require('internal/repl');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst fs = require('fs');
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst { inspect } = require('util');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cicommon.skipIfDumbTerminal();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
161cb0ef41Sopenharmony_citmpdir.refresh();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciprocess.throwDeprecation = true;
191cb0ef41Sopenharmony_ciprocess.on('warning', common.mustNotCall());
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst defaultHistoryPath = path.join(tmpdir.path, '.node_repl_history');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Create an input stream specialized for testing an array of actions
241cb0ef41Sopenharmony_ciclass ActionStream extends stream.Stream {
251cb0ef41Sopenharmony_ci  run(data) {
261cb0ef41Sopenharmony_ci    const _iter = data[Symbol.iterator]();
271cb0ef41Sopenharmony_ci    const doAction = () => {
281cb0ef41Sopenharmony_ci      const next = _iter.next();
291cb0ef41Sopenharmony_ci      if (next.done) {
301cb0ef41Sopenharmony_ci        // Close the repl. Note that it must have a clean prompt to do so.
311cb0ef41Sopenharmony_ci        this.emit('keypress', '', { ctrl: true, name: 'd' });
321cb0ef41Sopenharmony_ci        return;
331cb0ef41Sopenharmony_ci      }
341cb0ef41Sopenharmony_ci      const action = next.value;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci      if (typeof action === 'object') {
371cb0ef41Sopenharmony_ci        this.emit('keypress', '', action);
381cb0ef41Sopenharmony_ci      } else {
391cb0ef41Sopenharmony_ci        this.emit('data', `${action}`);
401cb0ef41Sopenharmony_ci      }
411cb0ef41Sopenharmony_ci      setImmediate(doAction);
421cb0ef41Sopenharmony_ci    };
431cb0ef41Sopenharmony_ci    doAction();
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci  resume() {}
461cb0ef41Sopenharmony_ci  pause() {}
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ciActionStream.prototype.readable = true;
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci// Mock keys
511cb0ef41Sopenharmony_ciconst ENTER = { name: 'enter' };
521cb0ef41Sopenharmony_ciconst UP = { name: 'up' };
531cb0ef41Sopenharmony_ciconst DOWN = { name: 'down' };
541cb0ef41Sopenharmony_ciconst LEFT = { name: 'left' };
551cb0ef41Sopenharmony_ciconst RIGHT = { name: 'right' };
561cb0ef41Sopenharmony_ciconst DELETE = { name: 'delete' };
571cb0ef41Sopenharmony_ciconst BACKSPACE = { name: 'backspace' };
581cb0ef41Sopenharmony_ciconst WORD_LEFT = { name: 'left', ctrl: true };
591cb0ef41Sopenharmony_ciconst WORD_RIGHT = { name: 'right', ctrl: true };
601cb0ef41Sopenharmony_ciconst GO_TO_END = { name: 'end' };
611cb0ef41Sopenharmony_ciconst DELETE_WORD_LEFT = { name: 'backspace', ctrl: true };
621cb0ef41Sopenharmony_ciconst SIGINT = { name: 'c', ctrl: true };
631cb0ef41Sopenharmony_ciconst ESCAPE = { name: 'escape', meta: true };
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciconst prompt = '> ';
661cb0ef41Sopenharmony_ciconst WAIT = '€';
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciconst prev = process.features.inspector;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_cilet completions = 0;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciconst tests = [
731cb0ef41Sopenharmony_ci  { // Creates few history to navigate for
741cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
751cb0ef41Sopenharmony_ci    test: [ 'let ab = 45', ENTER,
761cb0ef41Sopenharmony_ci            '555 + 909', ENTER,
771cb0ef41Sopenharmony_ci            'let autocompleteMe = 123', ENTER,
781cb0ef41Sopenharmony_ci            '{key : {key2 :[] }}', ENTER,
791cb0ef41Sopenharmony_ci            'Array(100).fill(1).map((e, i) => i ** i)', LEFT, LEFT, DELETE,
801cb0ef41Sopenharmony_ci            '2', ENTER],
811cb0ef41Sopenharmony_ci    expected: [],
821cb0ef41Sopenharmony_ci    clean: false
831cb0ef41Sopenharmony_ci  },
841cb0ef41Sopenharmony_ci  {
851cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
861cb0ef41Sopenharmony_ci    test: [UP, UP, UP, UP, UP, UP, DOWN, DOWN, DOWN, DOWN, DOWN, DOWN],
871cb0ef41Sopenharmony_ci    expected: [prompt,
881cb0ef41Sopenharmony_ci               `${prompt}Array(100).fill(1).map((e, i) => i ** 2)`,
891cb0ef41Sopenharmony_ci               prev && '\n// [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, ' +
901cb0ef41Sopenharmony_ci                 '144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529,' +
911cb0ef41Sopenharmony_ci                 ' 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, ' +
921cb0ef41Sopenharmony_ci                 '1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936,' +
931cb0ef41Sopenharmony_ci                 ' 2025, 2116, 2209,...',
941cb0ef41Sopenharmony_ci               `${prompt}{key : {key2 :[] }}`,
951cb0ef41Sopenharmony_ci               prev && '\n// { key: { key2: [] } }',
961cb0ef41Sopenharmony_ci               `${prompt}let autocompleteMe = 123`,
971cb0ef41Sopenharmony_ci               `${prompt}555 + 909`,
981cb0ef41Sopenharmony_ci               prev && '\n// 1464',
991cb0ef41Sopenharmony_ci               `${prompt}let ab = 45`,
1001cb0ef41Sopenharmony_ci               prompt,
1011cb0ef41Sopenharmony_ci               `${prompt}let ab = 45`,
1021cb0ef41Sopenharmony_ci               `${prompt}555 + 909`,
1031cb0ef41Sopenharmony_ci               prev && '\n// 1464',
1041cb0ef41Sopenharmony_ci               `${prompt}let autocompleteMe = 123`,
1051cb0ef41Sopenharmony_ci               `${prompt}{key : {key2 :[] }}`,
1061cb0ef41Sopenharmony_ci               prev && '\n// { key: { key2: [] } }',
1071cb0ef41Sopenharmony_ci               `${prompt}Array(100).fill(1).map((e, i) => i ** 2)`,
1081cb0ef41Sopenharmony_ci               prev && '\n// [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, ' +
1091cb0ef41Sopenharmony_ci                 '144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529,' +
1101cb0ef41Sopenharmony_ci                 ' 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, ' +
1111cb0ef41Sopenharmony_ci                 '1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936,' +
1121cb0ef41Sopenharmony_ci                 ' 2025, 2116, 2209,...',
1131cb0ef41Sopenharmony_ci               prompt].filter((e) => typeof e === 'string'),
1141cb0ef41Sopenharmony_ci    clean: false
1151cb0ef41Sopenharmony_ci  },
1161cb0ef41Sopenharmony_ci  { // Creates more history entries to navigate through.
1171cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
1181cb0ef41Sopenharmony_ci    test: [
1191cb0ef41Sopenharmony_ci      '555 + 909', ENTER, // Add a duplicate to the history set.
1201cb0ef41Sopenharmony_ci      'const foo = true', ENTER,
1211cb0ef41Sopenharmony_ci      '555n + 111n', ENTER,
1221cb0ef41Sopenharmony_ci      '5 + 5', ENTER,
1231cb0ef41Sopenharmony_ci      '55 - 13 === 42', ENTER,
1241cb0ef41Sopenharmony_ci    ],
1251cb0ef41Sopenharmony_ci    expected: [],
1261cb0ef41Sopenharmony_ci    clean: false
1271cb0ef41Sopenharmony_ci  },
1281cb0ef41Sopenharmony_ci  {
1291cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
1301cb0ef41Sopenharmony_ci    checkTotal: true,
1311cb0ef41Sopenharmony_ci    preview: false,
1321cb0ef41Sopenharmony_ci    showEscapeCodes: true,
1331cb0ef41Sopenharmony_ci    test: [
1341cb0ef41Sopenharmony_ci      '55', UP, UP, UP, UP, UP, UP, UP, ENTER,
1351cb0ef41Sopenharmony_ci    ],
1361cb0ef41Sopenharmony_ci    expected: [
1371cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J', prompt, '\x1B[3G',
1381cb0ef41Sopenharmony_ci      // '55'
1391cb0ef41Sopenharmony_ci      '5', '5',
1401cb0ef41Sopenharmony_ci      // UP
1411cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
1421cb0ef41Sopenharmony_ci      '> 55 - 13 === 42', '\x1B[17G',
1431cb0ef41Sopenharmony_ci      // UP - skipping 5 + 5
1441cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
1451cb0ef41Sopenharmony_ci      '> 555n + 111n', '\x1B[14G',
1461cb0ef41Sopenharmony_ci      // UP - skipping const foo = true
1471cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
1481cb0ef41Sopenharmony_ci      '> 555 + 909', '\x1B[12G',
1491cb0ef41Sopenharmony_ci      // UP, UP
1501cb0ef41Sopenharmony_ci      // UPs at the end of the history reset the line to the original input.
1511cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
1521cb0ef41Sopenharmony_ci      '> 55', '\x1B[5G',
1531cb0ef41Sopenharmony_ci      // ENTER
1541cb0ef41Sopenharmony_ci      '\r\n', '55\n',
1551cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
1561cb0ef41Sopenharmony_ci      '> ', '\x1B[3G',
1571cb0ef41Sopenharmony_ci      '\r\n',
1581cb0ef41Sopenharmony_ci    ],
1591cb0ef41Sopenharmony_ci    clean: true
1601cb0ef41Sopenharmony_ci  },
1611cb0ef41Sopenharmony_ci  {
1621cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
1631cb0ef41Sopenharmony_ci    skip: !process.features.inspector,
1641cb0ef41Sopenharmony_ci    test: [
1651cb0ef41Sopenharmony_ci      // あ is a full width character with a length of one.
1661cb0ef41Sopenharmony_ci      // � is a full width character with a length of two.
1671cb0ef41Sopenharmony_ci      // � is a half width character with the length of two.
1681cb0ef41Sopenharmony_ci      // '\u0301', '0x200D', '\u200E' are zero width characters.
1691cb0ef41Sopenharmony_ci      `const x1 = '${''.repeat(124)}'`, ENTER, // Fully visible
1701cb0ef41Sopenharmony_ci      ENTER,
1711cb0ef41Sopenharmony_ci      `const y1 = '${''.repeat(125)}'`, ENTER, // Cut off
1721cb0ef41Sopenharmony_ci      ENTER,
1731cb0ef41Sopenharmony_ci      `const x2 = '${''.repeat(124)}'`, ENTER, // Fully visible
1741cb0ef41Sopenharmony_ci      ENTER,
1751cb0ef41Sopenharmony_ci      `const y2 = '${''.repeat(125)}'`, ENTER, // Cut off
1761cb0ef41Sopenharmony_ci      ENTER,
1771cb0ef41Sopenharmony_ci      `const x3 = '${''.repeat(248)}'`, ENTER, // Fully visible
1781cb0ef41Sopenharmony_ci      ENTER,
1791cb0ef41Sopenharmony_ci      `const y3 = '${''.repeat(249)}'`, ENTER, // Cut off
1801cb0ef41Sopenharmony_ci      ENTER,
1811cb0ef41Sopenharmony_ci      `const x4 = 'a${'\u0301'.repeat(1000)}'`, ENTER, // á
1821cb0ef41Sopenharmony_ci      ENTER,
1831cb0ef41Sopenharmony_ci      `const ${'veryLongName'.repeat(30)} = 'I should be previewed'`,
1841cb0ef41Sopenharmony_ci      ENTER,
1851cb0ef41Sopenharmony_ci      'const e = new RangeError("visible\\ninvisible")',
1861cb0ef41Sopenharmony_ci      ENTER,
1871cb0ef41Sopenharmony_ci      'e',
1881cb0ef41Sopenharmony_ci      ENTER,
1891cb0ef41Sopenharmony_ci      'veryLongName'.repeat(30),
1901cb0ef41Sopenharmony_ci      ENTER,
1911cb0ef41Sopenharmony_ci      `${'\x1B[90m \x1B[39m'.repeat(229)} aut`,
1921cb0ef41Sopenharmony_ci      ESCAPE,
1931cb0ef41Sopenharmony_ci      ENTER,
1941cb0ef41Sopenharmony_ci      `${' '.repeat(230)} aut`,
1951cb0ef41Sopenharmony_ci      ESCAPE,
1961cb0ef41Sopenharmony_ci      ENTER,
1971cb0ef41Sopenharmony_ci    ],
1981cb0ef41Sopenharmony_ci    expected: [],
1991cb0ef41Sopenharmony_ci    clean: false
2001cb0ef41Sopenharmony_ci  },
2011cb0ef41Sopenharmony_ci  {
2021cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
2031cb0ef41Sopenharmony_ci    columns: 250,
2041cb0ef41Sopenharmony_ci    checkTotal: true,
2051cb0ef41Sopenharmony_ci    showEscapeCodes: true,
2061cb0ef41Sopenharmony_ci    skip: !process.features.inspector,
2071cb0ef41Sopenharmony_ci    test: [
2081cb0ef41Sopenharmony_ci      UP,
2091cb0ef41Sopenharmony_ci      UP,
2101cb0ef41Sopenharmony_ci      UP,
2111cb0ef41Sopenharmony_ci      WORD_LEFT,
2121cb0ef41Sopenharmony_ci      UP,
2131cb0ef41Sopenharmony_ci      BACKSPACE,
2141cb0ef41Sopenharmony_ci      'x1',
2151cb0ef41Sopenharmony_ci      BACKSPACE,
2161cb0ef41Sopenharmony_ci      '2',
2171cb0ef41Sopenharmony_ci      BACKSPACE,
2181cb0ef41Sopenharmony_ci      '3',
2191cb0ef41Sopenharmony_ci      BACKSPACE,
2201cb0ef41Sopenharmony_ci      '4',
2211cb0ef41Sopenharmony_ci      DELETE_WORD_LEFT,
2221cb0ef41Sopenharmony_ci      'y1',
2231cb0ef41Sopenharmony_ci      BACKSPACE,
2241cb0ef41Sopenharmony_ci      '2',
2251cb0ef41Sopenharmony_ci      BACKSPACE,
2261cb0ef41Sopenharmony_ci      '3',
2271cb0ef41Sopenharmony_ci      SIGINT,
2281cb0ef41Sopenharmony_ci    ],
2291cb0ef41Sopenharmony_ci    // A = Cursor n up
2301cb0ef41Sopenharmony_ci    // B = Cursor n down
2311cb0ef41Sopenharmony_ci    // C = Cursor n forward
2321cb0ef41Sopenharmony_ci    // D = Cursor n back
2331cb0ef41Sopenharmony_ci    // G = Cursor to column n
2341cb0ef41Sopenharmony_ci    // J = Erase in screen; 0 = right; 1 = left; 2 = total
2351cb0ef41Sopenharmony_ci    // K = Erase in line; 0 = right; 1 = left; 2 = total
2361cb0ef41Sopenharmony_ci    expected: [
2371cb0ef41Sopenharmony_ci      // 0. Start
2381cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2391cb0ef41Sopenharmony_ci      prompt, '\x1B[3G',
2401cb0ef41Sopenharmony_ci      // 1. UP
2411cb0ef41Sopenharmony_ci      // This exceeds the maximum columns (250):
2421cb0ef41Sopenharmony_ci      // Whitespace + prompt + ' // '.length + 'autocompleteMe'.length
2431cb0ef41Sopenharmony_ci      // 230 + 2 + 4 + 14
2441cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2451cb0ef41Sopenharmony_ci      `${prompt}${' '.repeat(230)} aut`, '\x1B[237G',
2461cb0ef41Sopenharmony_ci      ' // ocompleteMe', '\x1B[237G',
2471cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[237G',
2481cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
2491cb0ef41Sopenharmony_ci      '\x1B[0K',
2501cb0ef41Sopenharmony_ci      // 2. UP
2511cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2521cb0ef41Sopenharmony_ci      `${prompt}${' '.repeat(229)} aut`, '\x1B[236G',
2531cb0ef41Sopenharmony_ci      ' // ocompleteMe', '\x1B[236G',
2541cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[236G',
2551cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
2561cb0ef41Sopenharmony_ci      // Preview cleanup
2571cb0ef41Sopenharmony_ci      '\x1B[0K',
2581cb0ef41Sopenharmony_ci      // 3. UP
2591cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2601cb0ef41Sopenharmony_ci      // 'veryLongName'.repeat(30).length === 360
2611cb0ef41Sopenharmony_ci      // prompt.length === 2
2621cb0ef41Sopenharmony_ci      // 360 % 250 + 2 === 112 (+1)
2631cb0ef41Sopenharmony_ci      `${prompt}${'veryLongName'.repeat(30)}`, '\x1B[113G',
2641cb0ef41Sopenharmony_ci      // "// 'I should be previewed'".length + 86 === 112 (+1)
2651cb0ef41Sopenharmony_ci      "\n// 'I should be previewed'", '\x1B[113G', '\x1B[1A',
2661cb0ef41Sopenharmony_ci      // Preview cleanup
2671cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
2681cb0ef41Sopenharmony_ci      // 4. WORD LEFT
2691cb0ef41Sopenharmony_ci      // Almost identical as above. Just one extra line.
2701cb0ef41Sopenharmony_ci      // Math.floor(360 / 250) === 1
2711cb0ef41Sopenharmony_ci      '\x1B[1A',
2721cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2731cb0ef41Sopenharmony_ci      `${prompt}${'veryLongName'.repeat(30)}`, '\x1B[3G', '\x1B[1A',
2741cb0ef41Sopenharmony_ci      '\x1B[1B', "\n// 'I should be previewed'", '\x1B[3G', '\x1B[2A',
2751cb0ef41Sopenharmony_ci      // Preview cleanup
2761cb0ef41Sopenharmony_ci      '\x1B[2B', '\x1B[2K', '\x1B[2A',
2771cb0ef41Sopenharmony_ci      // 5. UP
2781cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2791cb0ef41Sopenharmony_ci      `${prompt}e`, '\x1B[4G',
2801cb0ef41Sopenharmony_ci      // '// RangeError: visible'.length - 19 === 3 (+1)
2811cb0ef41Sopenharmony_ci      '\n// RangeError: visible', '\x1B[4G', '\x1B[1A',
2821cb0ef41Sopenharmony_ci      // Preview cleanup
2831cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
2841cb0ef41Sopenharmony_ci      // 6. Backspace
2851cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2861cb0ef41Sopenharmony_ci      '> ', '\x1B[3G', 'x', '1',
2871cb0ef41Sopenharmony_ci      `\n// '${'あ'.repeat(124)}'`,
2881cb0ef41Sopenharmony_ci      '\x1B[5G', '\x1B[1A',
2891cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
2901cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2911cb0ef41Sopenharmony_ci      '> x', '\x1B[4G', '2',
2921cb0ef41Sopenharmony_ci      `\n// '${'�'.repeat(124)}'`,
2931cb0ef41Sopenharmony_ci      '\x1B[5G', '\x1B[1A',
2941cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
2951cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
2961cb0ef41Sopenharmony_ci      '> x', '\x1B[4G', '3',
2971cb0ef41Sopenharmony_ci      `\n// '${'�'.repeat(248)}'`,
2981cb0ef41Sopenharmony_ci      '\x1B[5G', '\x1B[1A',
2991cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
3001cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
3011cb0ef41Sopenharmony_ci      '> x', '\x1B[4G', '4',
3021cb0ef41Sopenharmony_ci      `\n// 'a${'\u0301'.repeat(1000)}'`,
3031cb0ef41Sopenharmony_ci      '\x1B[5G', '\x1B[1A',
3041cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
3051cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
3061cb0ef41Sopenharmony_ci      '> ', '\x1B[3G', 'y', '1',
3071cb0ef41Sopenharmony_ci      `\n// '${'あ'.repeat(121)}...`,
3081cb0ef41Sopenharmony_ci      '\x1B[5G', '\x1B[1A',
3091cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
3101cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
3111cb0ef41Sopenharmony_ci      '> y', '\x1B[4G', '2',
3121cb0ef41Sopenharmony_ci      `\n// '${'�'.repeat(121)}...`,
3131cb0ef41Sopenharmony_ci      '\x1B[5G', '\x1B[1A',
3141cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
3151cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
3161cb0ef41Sopenharmony_ci      '> y', '\x1B[4G', '3',
3171cb0ef41Sopenharmony_ci      `\n// '${'�'.repeat(242)}...`,
3181cb0ef41Sopenharmony_ci      '\x1B[5G', '\x1B[1A',
3191cb0ef41Sopenharmony_ci      '\x1B[1B', '\x1B[2K', '\x1B[1A',
3201cb0ef41Sopenharmony_ci      '\r\n',
3211cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
3221cb0ef41Sopenharmony_ci      '> ', '\x1B[3G',
3231cb0ef41Sopenharmony_ci      '\r\n',
3241cb0ef41Sopenharmony_ci    ],
3251cb0ef41Sopenharmony_ci    clean: true
3261cb0ef41Sopenharmony_ci  },
3271cb0ef41Sopenharmony_ci  {
3281cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
3291cb0ef41Sopenharmony_ci    showEscapeCodes: true,
3301cb0ef41Sopenharmony_ci    skip: !process.features.inspector,
3311cb0ef41Sopenharmony_ci    checkTotal: true,
3321cb0ef41Sopenharmony_ci    test: [
3331cb0ef41Sopenharmony_ci      'au',
3341cb0ef41Sopenharmony_ci      't',
3351cb0ef41Sopenharmony_ci      RIGHT,
3361cb0ef41Sopenharmony_ci      BACKSPACE,
3371cb0ef41Sopenharmony_ci      LEFT,
3381cb0ef41Sopenharmony_ci      LEFT,
3391cb0ef41Sopenharmony_ci      'A',
3401cb0ef41Sopenharmony_ci      BACKSPACE,
3411cb0ef41Sopenharmony_ci      GO_TO_END,
3421cb0ef41Sopenharmony_ci      BACKSPACE,
3431cb0ef41Sopenharmony_ci      WORD_LEFT,
3441cb0ef41Sopenharmony_ci      WORD_RIGHT,
3451cb0ef41Sopenharmony_ci      ESCAPE,
3461cb0ef41Sopenharmony_ci      ENTER,
3471cb0ef41Sopenharmony_ci      UP,
3481cb0ef41Sopenharmony_ci      LEFT,
3491cb0ef41Sopenharmony_ci      ENTER,
3501cb0ef41Sopenharmony_ci      UP,
3511cb0ef41Sopenharmony_ci      ENTER,
3521cb0ef41Sopenharmony_ci    ],
3531cb0ef41Sopenharmony_ci    // C = Cursor n forward
3541cb0ef41Sopenharmony_ci    // D = Cursor n back
3551cb0ef41Sopenharmony_ci    // G = Cursor to column n
3561cb0ef41Sopenharmony_ci    // J = Erase in screen; 0 = right; 1 = left; 2 = total
3571cb0ef41Sopenharmony_ci    // K = Erase in line; 0 = right; 1 = left; 2 = total
3581cb0ef41Sopenharmony_ci    expected: [
3591cb0ef41Sopenharmony_ci      // 0.
3601cb0ef41Sopenharmony_ci      // 'a'
3611cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J', prompt, '\x1B[3G', 'a',
3621cb0ef41Sopenharmony_ci      // 'u'
3631cb0ef41Sopenharmony_ci      'u', ' // tocompleteMe', '\x1B[5G',
3641cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[5G',
3651cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
3661cb0ef41Sopenharmony_ci      // 't' - Cleanup
3671cb0ef41Sopenharmony_ci      '\x1B[0K',
3681cb0ef41Sopenharmony_ci      't', ' // ocompleteMe', '\x1B[6G',
3691cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[6G',
3701cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
3711cb0ef41Sopenharmony_ci      // 1. Right. Cleanup
3721cb0ef41Sopenharmony_ci      '\x1B[0K',
3731cb0ef41Sopenharmony_ci      'ocompleteMe',
3741cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[17G',
3751cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
3761cb0ef41Sopenharmony_ci      // 2. Backspace. Refresh
3771cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J', `${prompt}autocompleteM`, '\x1B[16G',
3781cb0ef41Sopenharmony_ci      // Autocomplete and refresh?
3791cb0ef41Sopenharmony_ci      ' // e', '\x1B[16G',
3801cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[16G',
3811cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
3821cb0ef41Sopenharmony_ci      // 3. Left. Cleanup
3831cb0ef41Sopenharmony_ci      '\x1B[0K',
3841cb0ef41Sopenharmony_ci      '\x1B[1D', '\x1B[16G', ' // e', '\x1B[15G',
3851cb0ef41Sopenharmony_ci      // 4. Left. Cleanup
3861cb0ef41Sopenharmony_ci      '\x1B[16G', '\x1B[0K', '\x1B[15G',
3871cb0ef41Sopenharmony_ci      '\x1B[1D', '\x1B[16G', ' // e', '\x1B[14G',
3881cb0ef41Sopenharmony_ci      // 5. 'A' - Cleanup
3891cb0ef41Sopenharmony_ci      '\x1B[16G', '\x1B[0K', '\x1B[14G',
3901cb0ef41Sopenharmony_ci      // Refresh
3911cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J', `${prompt}autocompletAeM`, '\x1B[15G',
3921cb0ef41Sopenharmony_ci      // 6. Backspace. Refresh
3931cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J', `${prompt}autocompleteM`,
3941cb0ef41Sopenharmony_ci      '\x1B[14G', '\x1B[16G', ' // e',
3951cb0ef41Sopenharmony_ci      '\x1B[14G', '\x1B[16G', ' // e',
3961cb0ef41Sopenharmony_ci      '\x1B[14G', '\x1B[16G',
3971cb0ef41Sopenharmony_ci      // 7. Go to end. Cleanup
3981cb0ef41Sopenharmony_ci      '\x1B[0K', '\x1B[14G', '\x1B[2C',
3991cb0ef41Sopenharmony_ci      'e',
4001cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[17G',
4011cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
4021cb0ef41Sopenharmony_ci      // 8. Backspace. Refresh
4031cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J', `${prompt}autocompleteM`, '\x1B[16G',
4041cb0ef41Sopenharmony_ci      // Autocomplete
4051cb0ef41Sopenharmony_ci      ' // e', '\x1B[16G',
4061cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[16G',
4071cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
4081cb0ef41Sopenharmony_ci      // 9. Word left. Cleanup
4091cb0ef41Sopenharmony_ci      '\x1B[0K', '\x1B[13D', '\x1B[16G', ' // e', '\x1B[3G', '\x1B[16G',
4101cb0ef41Sopenharmony_ci      // 10. Word right. Cleanup
4111cb0ef41Sopenharmony_ci      '\x1B[0K', '\x1B[3G', '\x1B[13C', ' // e', '\x1B[16G',
4121cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[16G',
4131cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
4141cb0ef41Sopenharmony_ci      // 11. ESCAPE
4151cb0ef41Sopenharmony_ci      '\x1B[0K',
4161cb0ef41Sopenharmony_ci      // 12. ENTER
4171cb0ef41Sopenharmony_ci      '\r\n',
4181cb0ef41Sopenharmony_ci      'Uncaught ReferenceError: autocompleteM is not defined\n',
4191cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
4201cb0ef41Sopenharmony_ci      // 13. UP
4211cb0ef41Sopenharmony_ci      prompt, '\x1B[3G', '\x1B[1G', '\x1B[0J',
4221cb0ef41Sopenharmony_ci      `${prompt}autocompleteM`, '\x1B[16G',
4231cb0ef41Sopenharmony_ci      ' // e', '\x1B[16G',
4241cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[16G',
4251cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
4261cb0ef41Sopenharmony_ci      // 14. LEFT
4271cb0ef41Sopenharmony_ci      '\x1B[0K', '\x1B[1D', '\x1B[16G',
4281cb0ef41Sopenharmony_ci      ' // e', '\x1B[15G', '\x1B[16G',
4291cb0ef41Sopenharmony_ci      // 15. ENTER
4301cb0ef41Sopenharmony_ci      '\x1B[0K', '\x1B[15G', '\x1B[1C',
4311cb0ef41Sopenharmony_ci      '\r\n',
4321cb0ef41Sopenharmony_ci      'Uncaught ReferenceError: autocompleteM is not defined\n',
4331cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
4341cb0ef41Sopenharmony_ci      prompt, '\x1B[3G',
4351cb0ef41Sopenharmony_ci      // 16. UP
4361cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
4371cb0ef41Sopenharmony_ci      `${prompt}autocompleteM`, '\x1B[16G',
4381cb0ef41Sopenharmony_ci      ' // e', '\x1B[16G',
4391cb0ef41Sopenharmony_ci      '\n// 123', '\x1B[16G',
4401cb0ef41Sopenharmony_ci      '\x1B[1A', '\x1B[1B', '\x1B[2K', '\x1B[1A',
4411cb0ef41Sopenharmony_ci      '\x1B[0K',
4421cb0ef41Sopenharmony_ci      // 17. ENTER
4431cb0ef41Sopenharmony_ci      'e', '\r\n',
4441cb0ef41Sopenharmony_ci      '123\n',
4451cb0ef41Sopenharmony_ci      '\x1B[1G', '\x1B[0J',
4461cb0ef41Sopenharmony_ci      prompt, '\x1B[3G',
4471cb0ef41Sopenharmony_ci      '\r\n',
4481cb0ef41Sopenharmony_ci    ],
4491cb0ef41Sopenharmony_ci    clean: true
4501cb0ef41Sopenharmony_ci  },
4511cb0ef41Sopenharmony_ci  {
4521cb0ef41Sopenharmony_ci    // Check changed inspection defaults.
4531cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
4541cb0ef41Sopenharmony_ci    skip: !process.features.inspector,
4551cb0ef41Sopenharmony_ci    test: [
4561cb0ef41Sopenharmony_ci      'util.inspect.replDefaults.showHidden',
4571cb0ef41Sopenharmony_ci      ENTER,
4581cb0ef41Sopenharmony_ci    ],
4591cb0ef41Sopenharmony_ci    expected: [],
4601cb0ef41Sopenharmony_ci    clean: false
4611cb0ef41Sopenharmony_ci  },
4621cb0ef41Sopenharmony_ci  {
4631cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
4641cb0ef41Sopenharmony_ci    skip: !process.features.inspector,
4651cb0ef41Sopenharmony_ci    checkTotal: true,
4661cb0ef41Sopenharmony_ci    test: [
4671cb0ef41Sopenharmony_ci      '[ ]',
4681cb0ef41Sopenharmony_ci      WORD_LEFT,
4691cb0ef41Sopenharmony_ci      WORD_LEFT,
4701cb0ef41Sopenharmony_ci      UP,
4711cb0ef41Sopenharmony_ci      ' = true',
4721cb0ef41Sopenharmony_ci      ENTER,
4731cb0ef41Sopenharmony_ci      '[ ]',
4741cb0ef41Sopenharmony_ci      ENTER,
4751cb0ef41Sopenharmony_ci    ],
4761cb0ef41Sopenharmony_ci    expected: [
4771cb0ef41Sopenharmony_ci      prompt,
4781cb0ef41Sopenharmony_ci      '[', ' ', ']',
4791cb0ef41Sopenharmony_ci      '\n// []', '\n// []', '\n// []',
4801cb0ef41Sopenharmony_ci      '> util.inspect.replDefaults.showHidden',
4811cb0ef41Sopenharmony_ci      '\n// false',
4821cb0ef41Sopenharmony_ci      ' ', '=', ' ', 't', 'r', 'u', 'e',
4831cb0ef41Sopenharmony_ci      'true\n',
4841cb0ef41Sopenharmony_ci      '> ', '[', ' ', ']',
4851cb0ef41Sopenharmony_ci      '\n// [ [length]: 0 ]',
4861cb0ef41Sopenharmony_ci      '[ [length]: 0 ]\n',
4871cb0ef41Sopenharmony_ci      '> ',
4881cb0ef41Sopenharmony_ci    ],
4891cb0ef41Sopenharmony_ci    clean: true
4901cb0ef41Sopenharmony_ci  },
4911cb0ef41Sopenharmony_ci  {
4921cb0ef41Sopenharmony_ci    // Check that the completer ignores completions that are outdated.
4931cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
4941cb0ef41Sopenharmony_ci    completer(line, callback) {
4951cb0ef41Sopenharmony_ci      if (line.endsWith(WAIT)) {
4961cb0ef41Sopenharmony_ci        if (completions++ === 0) {
4971cb0ef41Sopenharmony_ci          callback(null, [[`${WAIT}WOW`], line]);
4981cb0ef41Sopenharmony_ci        } else {
4991cb0ef41Sopenharmony_ci          setTimeout(callback, 1000, null, [[`${WAIT}WOW`], line]).unref();
5001cb0ef41Sopenharmony_ci        }
5011cb0ef41Sopenharmony_ci      } else {
5021cb0ef41Sopenharmony_ci        callback(null, [[' Always visible'], line]);
5031cb0ef41Sopenharmony_ci      }
5041cb0ef41Sopenharmony_ci    },
5051cb0ef41Sopenharmony_ci    skip: !process.features.inspector,
5061cb0ef41Sopenharmony_ci    test: [
5071cb0ef41Sopenharmony_ci      WAIT, // The first call is awaited before new input is triggered!
5081cb0ef41Sopenharmony_ci      BACKSPACE,
5091cb0ef41Sopenharmony_ci      's',
5101cb0ef41Sopenharmony_ci      BACKSPACE,
5111cb0ef41Sopenharmony_ci      WAIT, // The second call is not awaited. It won't trigger the preview.
5121cb0ef41Sopenharmony_ci      BACKSPACE,
5131cb0ef41Sopenharmony_ci      's',
5141cb0ef41Sopenharmony_ci      BACKSPACE,
5151cb0ef41Sopenharmony_ci    ],
5161cb0ef41Sopenharmony_ci    expected: [
5171cb0ef41Sopenharmony_ci      prompt,
5181cb0ef41Sopenharmony_ci      WAIT,
5191cb0ef41Sopenharmony_ci      ' // WOW',
5201cb0ef41Sopenharmony_ci      prompt,
5211cb0ef41Sopenharmony_ci      's',
5221cb0ef41Sopenharmony_ci      ' // Always visible',
5231cb0ef41Sopenharmony_ci      prompt,
5241cb0ef41Sopenharmony_ci      WAIT,
5251cb0ef41Sopenharmony_ci      prompt,
5261cb0ef41Sopenharmony_ci      's',
5271cb0ef41Sopenharmony_ci      ' // Always visible',
5281cb0ef41Sopenharmony_ci      prompt,
5291cb0ef41Sopenharmony_ci    ],
5301cb0ef41Sopenharmony_ci    clean: true
5311cb0ef41Sopenharmony_ci  },
5321cb0ef41Sopenharmony_ci  {
5331cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
5341cb0ef41Sopenharmony_ci    test: (function*() {
5351cb0ef41Sopenharmony_ci      // Deleting Array iterator should not break history feature.
5361cb0ef41Sopenharmony_ci      //
5371cb0ef41Sopenharmony_ci      // Using a generator function instead of an object to allow the test to
5381cb0ef41Sopenharmony_ci      // keep iterating even when Array.prototype[Symbol.iterator] has been
5391cb0ef41Sopenharmony_ci      // deleted.
5401cb0ef41Sopenharmony_ci      yield 'const ArrayIteratorPrototype =';
5411cb0ef41Sopenharmony_ci      yield '  Object.getPrototypeOf(Array.prototype[Symbol.iterator]());';
5421cb0ef41Sopenharmony_ci      yield ENTER;
5431cb0ef41Sopenharmony_ci      yield 'const {next} = ArrayIteratorPrototype;';
5441cb0ef41Sopenharmony_ci      yield ENTER;
5451cb0ef41Sopenharmony_ci      yield 'const realArrayIterator = Array.prototype[Symbol.iterator];';
5461cb0ef41Sopenharmony_ci      yield ENTER;
5471cb0ef41Sopenharmony_ci      yield 'delete Array.prototype[Symbol.iterator];';
5481cb0ef41Sopenharmony_ci      yield ENTER;
5491cb0ef41Sopenharmony_ci      yield 'delete ArrayIteratorPrototype.next;';
5501cb0ef41Sopenharmony_ci      yield ENTER;
5511cb0ef41Sopenharmony_ci      yield UP;
5521cb0ef41Sopenharmony_ci      yield UP;
5531cb0ef41Sopenharmony_ci      yield DOWN;
5541cb0ef41Sopenharmony_ci      yield DOWN;
5551cb0ef41Sopenharmony_ci      yield 'fu';
5561cb0ef41Sopenharmony_ci      yield 'n';
5571cb0ef41Sopenharmony_ci      yield RIGHT;
5581cb0ef41Sopenharmony_ci      yield BACKSPACE;
5591cb0ef41Sopenharmony_ci      yield LEFT;
5601cb0ef41Sopenharmony_ci      yield LEFT;
5611cb0ef41Sopenharmony_ci      yield 'A';
5621cb0ef41Sopenharmony_ci      yield BACKSPACE;
5631cb0ef41Sopenharmony_ci      yield GO_TO_END;
5641cb0ef41Sopenharmony_ci      yield BACKSPACE;
5651cb0ef41Sopenharmony_ci      yield WORD_LEFT;
5661cb0ef41Sopenharmony_ci      yield WORD_RIGHT;
5671cb0ef41Sopenharmony_ci      yield ESCAPE;
5681cb0ef41Sopenharmony_ci      yield ENTER;
5691cb0ef41Sopenharmony_ci      yield 'Array.proto';
5701cb0ef41Sopenharmony_ci      yield RIGHT;
5711cb0ef41Sopenharmony_ci      yield '.pu';
5721cb0ef41Sopenharmony_ci      yield ENTER;
5731cb0ef41Sopenharmony_ci      yield 'ArrayIteratorPrototype.next = next;';
5741cb0ef41Sopenharmony_ci      yield ENTER;
5751cb0ef41Sopenharmony_ci      yield 'Array.prototype[Symbol.iterator] = realArrayIterator;';
5761cb0ef41Sopenharmony_ci      yield ENTER;
5771cb0ef41Sopenharmony_ci    })(),
5781cb0ef41Sopenharmony_ci    expected: [],
5791cb0ef41Sopenharmony_ci    clean: false
5801cb0ef41Sopenharmony_ci  },
5811cb0ef41Sopenharmony_ci  {
5821cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
5831cb0ef41Sopenharmony_ci    test: ['const util = {}', ENTER,
5841cb0ef41Sopenharmony_ci           'ut', RIGHT, ENTER],
5851cb0ef41Sopenharmony_ci    expected: [
5861cb0ef41Sopenharmony_ci      prompt, ...'const util = {}',
5871cb0ef41Sopenharmony_ci      'undefined\n',
5881cb0ef41Sopenharmony_ci      prompt, ...'ut', ...(prev ? [' // il', '\n// {}',
5891cb0ef41Sopenharmony_ci                                   'il', '\n// {}'] : [' // il', 'il']),
5901cb0ef41Sopenharmony_ci      '{}\n',
5911cb0ef41Sopenharmony_ci      prompt,
5921cb0ef41Sopenharmony_ci    ],
5931cb0ef41Sopenharmony_ci    clean: false
5941cb0ef41Sopenharmony_ci  },
5951cb0ef41Sopenharmony_ci  {
5961cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
5971cb0ef41Sopenharmony_ci    test: [
5981cb0ef41Sopenharmony_ci      'const utilDesc = Reflect.getOwnPropertyDescriptor(globalThis, "util")',
5991cb0ef41Sopenharmony_ci      ENTER,
6001cb0ef41Sopenharmony_ci      'globalThis.util = {}', ENTER,
6011cb0ef41Sopenharmony_ci      'ut', RIGHT, ENTER,
6021cb0ef41Sopenharmony_ci      'Reflect.defineProperty(globalThis, "util", utilDesc)', ENTER],
6031cb0ef41Sopenharmony_ci    expected: [
6041cb0ef41Sopenharmony_ci      prompt, ...'const utilDesc = ' +
6051cb0ef41Sopenharmony_ci      'Reflect.getOwnPropertyDescriptor(globalThis, "util")',
6061cb0ef41Sopenharmony_ci      'undefined\n',
6071cb0ef41Sopenharmony_ci      prompt, ...'globalThis.util = {}',
6081cb0ef41Sopenharmony_ci      '{}\n',
6091cb0ef41Sopenharmony_ci      prompt, ...'ut', ' // il', 'il',
6101cb0ef41Sopenharmony_ci      '{}\n',
6111cb0ef41Sopenharmony_ci      prompt, ...'Reflect.defineProperty(globalThis, "util", utilDesc)',
6121cb0ef41Sopenharmony_ci      'true\n',
6131cb0ef41Sopenharmony_ci      prompt,
6141cb0ef41Sopenharmony_ci    ],
6151cb0ef41Sopenharmony_ci    clean: false
6161cb0ef41Sopenharmony_ci  },
6171cb0ef41Sopenharmony_ci  {
6181cb0ef41Sopenharmony_ci    // Test that preview should not be removed when pressing ESCAPE key
6191cb0ef41Sopenharmony_ci    env: { NODE_REPL_HISTORY: defaultHistoryPath },
6201cb0ef41Sopenharmony_ci    skip: !process.features.inspector,
6211cb0ef41Sopenharmony_ci    test: [
6221cb0ef41Sopenharmony_ci      '1+1',
6231cb0ef41Sopenharmony_ci      ESCAPE,
6241cb0ef41Sopenharmony_ci      ENTER,
6251cb0ef41Sopenharmony_ci    ],
6261cb0ef41Sopenharmony_ci    expected: [
6271cb0ef41Sopenharmony_ci      prompt, ...'1+1',
6281cb0ef41Sopenharmony_ci      '\n// 2',
6291cb0ef41Sopenharmony_ci      '\n// 2',
6301cb0ef41Sopenharmony_ci      '2\n',
6311cb0ef41Sopenharmony_ci      prompt,
6321cb0ef41Sopenharmony_ci    ],
6331cb0ef41Sopenharmony_ci    clean: false
6341cb0ef41Sopenharmony_ci  },
6351cb0ef41Sopenharmony_ci];
6361cb0ef41Sopenharmony_ciconst numtests = tests.length;
6371cb0ef41Sopenharmony_ci
6381cb0ef41Sopenharmony_ciconst runTestWrap = common.mustCall(runTest, numtests);
6391cb0ef41Sopenharmony_ci
6401cb0ef41Sopenharmony_cifunction cleanupTmpFile() {
6411cb0ef41Sopenharmony_ci  try {
6421cb0ef41Sopenharmony_ci    // Write over the file, clearing any history
6431cb0ef41Sopenharmony_ci    fs.writeFileSync(defaultHistoryPath, '');
6441cb0ef41Sopenharmony_ci  } catch (err) {
6451cb0ef41Sopenharmony_ci    if (err.code === 'ENOENT') return true;
6461cb0ef41Sopenharmony_ci    throw err;
6471cb0ef41Sopenharmony_ci  }
6481cb0ef41Sopenharmony_ci  return true;
6491cb0ef41Sopenharmony_ci}
6501cb0ef41Sopenharmony_ci
6511cb0ef41Sopenharmony_cifunction runTest() {
6521cb0ef41Sopenharmony_ci  const opts = tests.shift();
6531cb0ef41Sopenharmony_ci  if (!opts) return; // All done
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_ci  const { expected, skip } = opts;
6561cb0ef41Sopenharmony_ci
6571cb0ef41Sopenharmony_ci  // Test unsupported on platform.
6581cb0ef41Sopenharmony_ci  if (skip) {
6591cb0ef41Sopenharmony_ci    setImmediate(runTestWrap, true);
6601cb0ef41Sopenharmony_ci    return;
6611cb0ef41Sopenharmony_ci  }
6621cb0ef41Sopenharmony_ci  const lastChunks = [];
6631cb0ef41Sopenharmony_ci  let i = 0;
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ci  REPL.createInternalRepl(opts.env, {
6661cb0ef41Sopenharmony_ci    input: new ActionStream(),
6671cb0ef41Sopenharmony_ci    output: new stream.Writable({
6681cb0ef41Sopenharmony_ci      write(chunk, _, next) {
6691cb0ef41Sopenharmony_ci        const output = chunk.toString();
6701cb0ef41Sopenharmony_ci
6711cb0ef41Sopenharmony_ci        if (!opts.showEscapeCodes &&
6721cb0ef41Sopenharmony_ci            (output[0] === '\x1B' || /^[\r\n]+$/.test(output))) {
6731cb0ef41Sopenharmony_ci          return next();
6741cb0ef41Sopenharmony_ci        }
6751cb0ef41Sopenharmony_ci
6761cb0ef41Sopenharmony_ci        lastChunks.push(output);
6771cb0ef41Sopenharmony_ci
6781cb0ef41Sopenharmony_ci        if (expected.length && !opts.checkTotal) {
6791cb0ef41Sopenharmony_ci          try {
6801cb0ef41Sopenharmony_ci            assert.strictEqual(output, expected[i]);
6811cb0ef41Sopenharmony_ci          } catch (e) {
6821cb0ef41Sopenharmony_ci            console.error(`Failed test # ${numtests - tests.length}`);
6831cb0ef41Sopenharmony_ci            console.error('Last outputs: ' + inspect(lastChunks, {
6841cb0ef41Sopenharmony_ci              breakLength: 5, colors: true
6851cb0ef41Sopenharmony_ci            }));
6861cb0ef41Sopenharmony_ci            throw e;
6871cb0ef41Sopenharmony_ci          }
6881cb0ef41Sopenharmony_ci          // TODO(BridgeAR): Auto close on last chunk!
6891cb0ef41Sopenharmony_ci          i++;
6901cb0ef41Sopenharmony_ci        }
6911cb0ef41Sopenharmony_ci
6921cb0ef41Sopenharmony_ci        next();
6931cb0ef41Sopenharmony_ci      }
6941cb0ef41Sopenharmony_ci    }),
6951cb0ef41Sopenharmony_ci    completer: opts.completer,
6961cb0ef41Sopenharmony_ci    prompt,
6971cb0ef41Sopenharmony_ci    useColors: false,
6981cb0ef41Sopenharmony_ci    preview: opts.preview,
6991cb0ef41Sopenharmony_ci    terminal: true
7001cb0ef41Sopenharmony_ci  }, function(err, repl) {
7011cb0ef41Sopenharmony_ci    if (err) {
7021cb0ef41Sopenharmony_ci      console.error(`Failed test # ${numtests - tests.length}`);
7031cb0ef41Sopenharmony_ci      throw err;
7041cb0ef41Sopenharmony_ci    }
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ci    repl.once('close', () => {
7071cb0ef41Sopenharmony_ci      if (opts.clean)
7081cb0ef41Sopenharmony_ci        cleanupTmpFile();
7091cb0ef41Sopenharmony_ci
7101cb0ef41Sopenharmony_ci      if (opts.checkTotal) {
7111cb0ef41Sopenharmony_ci        assert.deepStrictEqual(lastChunks, expected);
7121cb0ef41Sopenharmony_ci      } else if (expected.length !== i) {
7131cb0ef41Sopenharmony_ci        console.error(tests[numtests - tests.length - 1]);
7141cb0ef41Sopenharmony_ci        throw new Error(`Failed test # ${numtests - tests.length}`);
7151cb0ef41Sopenharmony_ci      }
7161cb0ef41Sopenharmony_ci
7171cb0ef41Sopenharmony_ci      setImmediate(runTestWrap, true);
7181cb0ef41Sopenharmony_ci    });
7191cb0ef41Sopenharmony_ci
7201cb0ef41Sopenharmony_ci    if (opts.columns) {
7211cb0ef41Sopenharmony_ci      Object.defineProperty(repl, 'columns', {
7221cb0ef41Sopenharmony_ci        value: opts.columns,
7231cb0ef41Sopenharmony_ci        enumerable: true
7241cb0ef41Sopenharmony_ci      });
7251cb0ef41Sopenharmony_ci    }
7261cb0ef41Sopenharmony_ci    repl.input.run(opts.test);
7271cb0ef41Sopenharmony_ci  });
7281cb0ef41Sopenharmony_ci}
7291cb0ef41Sopenharmony_ci
7301cb0ef41Sopenharmony_ci// run the tests
7311cb0ef41Sopenharmony_cirunTest();
732