11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
41cb0ef41Sopenharmony_ciconst path = require('../common/fixtures').path;
51cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn;
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst fixture = path('debugger-repeat-last.js');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst args = [
101cb0ef41Sopenharmony_ci  'inspect',
111cb0ef41Sopenharmony_ci  '--port=0',
121cb0ef41Sopenharmony_ci  fixture,
131cb0ef41Sopenharmony_ci];
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst proc = spawn(process.execPath, args, { stdio: 'pipe' });
161cb0ef41Sopenharmony_ciproc.stdout.setEncoding('utf8');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cilet stdout = '';
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cilet sentCommand = false;
211cb0ef41Sopenharmony_cilet sentExit = false;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciproc.stdout.on('data', (data) => {
241cb0ef41Sopenharmony_ci  stdout += data;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // Send 'n' as the first step.
271cb0ef41Sopenharmony_ci  if (!sentCommand && stdout.includes('> 1 ')) {
281cb0ef41Sopenharmony_ci    setImmediate(() => { proc.stdin.write('n\n'); });
291cb0ef41Sopenharmony_ci    return sentCommand = true;
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci  // Send empty (repeat last command) until we reach line 5.
321cb0ef41Sopenharmony_ci  if (sentCommand && !stdout.includes('> 5')) {
331cb0ef41Sopenharmony_ci    setImmediate(() => { proc.stdin.write('\n'); });
341cb0ef41Sopenharmony_ci    return true;
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci  if (!sentExit && stdout.includes('> 5')) {
371cb0ef41Sopenharmony_ci    setTimeout(() => { proc.stdin.write('\n\n\n.exit\n\n\n'); }, 1);
381cb0ef41Sopenharmony_ci    return sentExit = true;
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci});
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciprocess.on('exit', (exitCode) => {
431cb0ef41Sopenharmony_ci  assert.strictEqual(exitCode, 0);
441cb0ef41Sopenharmony_ci  console.log(stdout);
451cb0ef41Sopenharmony_ci});
46