11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst net = require('net');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciif (!common.isMainThread)
91cb0ef41Sopenharmony_ci  common.skip('process.chdir is not available in Workers');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciprocess.chdir(fixtures.fixturesDir);
121cb0ef41Sopenharmony_ciconst repl = require('repl');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci{
151cb0ef41Sopenharmony_ci  const server = net.createServer((conn) => {
161cb0ef41Sopenharmony_ci    repl.start('', conn).on('exit', () => {
171cb0ef41Sopenharmony_ci      conn.destroy();
181cb0ef41Sopenharmony_ci      server.close();
191cb0ef41Sopenharmony_ci    });
201cb0ef41Sopenharmony_ci  });
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const host = common.localhostIPv4;
231cb0ef41Sopenharmony_ci  const port = 0;
241cb0ef41Sopenharmony_ci  const options = { host, port };
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  let answer = '';
271cb0ef41Sopenharmony_ci  server.listen(options, function() {
281cb0ef41Sopenharmony_ci    options.port = this.address().port;
291cb0ef41Sopenharmony_ci    const conn = net.connect(options);
301cb0ef41Sopenharmony_ci    conn.setEncoding('utf8');
311cb0ef41Sopenharmony_ci    conn.on('data', (data) => answer += data);
321cb0ef41Sopenharmony_ci    conn.write('require("baz")\nrequire("./baz")\n.exit\n');
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  process.on('exit', function() {
361cb0ef41Sopenharmony_ci    assert.doesNotMatch(answer, /Cannot find module/);
371cb0ef41Sopenharmony_ci    assert.doesNotMatch(answer, /Error/);
381cb0ef41Sopenharmony_ci    assert.strictEqual(answer, '\'eye catcher\'\n\'perhaps I work\'\n');
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Test for https://github.com/nodejs/node/issues/30808
431cb0ef41Sopenharmony_ci// In REPL, we shouldn't look up relative modules from 'node_modules'.
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  const server = net.createServer((conn) => {
461cb0ef41Sopenharmony_ci    repl.start('', conn).on('exit', () => {
471cb0ef41Sopenharmony_ci      conn.destroy();
481cb0ef41Sopenharmony_ci      server.close();
491cb0ef41Sopenharmony_ci    });
501cb0ef41Sopenharmony_ci  });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  const host = common.localhostIPv4;
531cb0ef41Sopenharmony_ci  const port = 0;
541cb0ef41Sopenharmony_ci  const options = { host, port };
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  let answer = '';
571cb0ef41Sopenharmony_ci  server.listen(options, function() {
581cb0ef41Sopenharmony_ci    options.port = this.address().port;
591cb0ef41Sopenharmony_ci    const conn = net.connect(options);
601cb0ef41Sopenharmony_ci    conn.setEncoding('utf8');
611cb0ef41Sopenharmony_ci    conn.on('data', (data) => answer += data);
621cb0ef41Sopenharmony_ci    conn.write('require("./bar")\n.exit\n');
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  process.on('exit', function() {
661cb0ef41Sopenharmony_ci    assert.match(answer, /Uncaught Error: Cannot find module '\.\/bar'/);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    assert.match(answer, /code: 'MODULE_NOT_FOUND'/);
691cb0ef41Sopenharmony_ci    assert.match(answer, /requireStack: \[ '<repl>' \]/);
701cb0ef41Sopenharmony_ci  });
711cb0ef41Sopenharmony_ci}
72