11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst { mustCall, skipIfInspectorDisabled } = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciskipIfInspectorDisabled();
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cifunction test(arg, port = '') {
101cb0ef41Sopenharmony_ci  const args = [arg, '-p', 'process.debugPort'];
111cb0ef41Sopenharmony_ci  const proc = spawn(process.execPath, args);
121cb0ef41Sopenharmony_ci  proc.stdout.setEncoding('utf8');
131cb0ef41Sopenharmony_ci  proc.stderr.setEncoding('utf8');
141cb0ef41Sopenharmony_ci  let stdout = '';
151cb0ef41Sopenharmony_ci  let stderr = '';
161cb0ef41Sopenharmony_ci  proc.stdout.on('data', (data) => stdout += data);
171cb0ef41Sopenharmony_ci  proc.stderr.on('data', (data) => stderr += data);
181cb0ef41Sopenharmony_ci  proc.stdout.on('close', (hadErr) => assert(!hadErr));
191cb0ef41Sopenharmony_ci  proc.stderr.on('close', (hadErr) => assert(!hadErr));
201cb0ef41Sopenharmony_ci  proc.stderr.on('data', () => {
211cb0ef41Sopenharmony_ci    if (!stderr.includes('\n')) return;
221cb0ef41Sopenharmony_ci    assert.match(stderr, /Debugger listening on (.+)/);
231cb0ef41Sopenharmony_ci    port = new URL(RegExp.$1).port;
241cb0ef41Sopenharmony_ci    assert(+port > 0);
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci  if (/inspect-brk/.test(arg)) {
271cb0ef41Sopenharmony_ci    proc.stderr.on('data', () => {
281cb0ef41Sopenharmony_ci      if (stderr.includes('\n') && !proc.killed) proc.kill();
291cb0ef41Sopenharmony_ci    });
301cb0ef41Sopenharmony_ci  } else {
311cb0ef41Sopenharmony_ci    let onclose = () => {
321cb0ef41Sopenharmony_ci      onclose = () => assert.strictEqual(port, stdout.trim());
331cb0ef41Sopenharmony_ci    };
341cb0ef41Sopenharmony_ci    proc.stdout.on('close', mustCall(() => onclose()));
351cb0ef41Sopenharmony_ci    proc.stderr.on('close', mustCall(() => onclose()));
361cb0ef41Sopenharmony_ci    proc.on('exit', mustCall((exitCode, signal) => assert.strictEqual(
371cb0ef41Sopenharmony_ci      exitCode,
381cb0ef41Sopenharmony_ci      0,
391cb0ef41Sopenharmony_ci      `exitCode: ${exitCode}, signal: ${signal}`)));
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_citest('--inspect=0');
441cb0ef41Sopenharmony_citest('--inspect=127.0.0.1:0');
451cb0ef41Sopenharmony_citest('--inspect=localhost:0');
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_citest('--inspect-brk=0');
481cb0ef41Sopenharmony_citest('--inspect-brk=127.0.0.1:0');
491cb0ef41Sopenharmony_citest('--inspect-brk=localhost:0');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci// In these cases, the inspector doesn't listen, so an ephemeral port is not
521cb0ef41Sopenharmony_ci// allocated and the expected value of `process.debugPort` is `0`.
531cb0ef41Sopenharmony_citest('--inspect-port=0', '0');
541cb0ef41Sopenharmony_citest('--inspect-port=127.0.0.1:0', '0');
551cb0ef41Sopenharmony_citest('--inspect-port=localhost:0', '0');
56