11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/13557
41cb0ef41Sopenharmony_ci// Tests that multiple subsequent readline instances can re-use an input stream.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst readline = require('readline');
91cb0ef41Sopenharmony_ciconst { PassThrough } = require('stream');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst input = new PassThrough();
121cb0ef41Sopenharmony_ciconst output = new PassThrough();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst rl1 = readline.createInterface({
151cb0ef41Sopenharmony_ci  input,
161cb0ef41Sopenharmony_ci  output,
171cb0ef41Sopenharmony_ci  terminal: true
181cb0ef41Sopenharmony_ci});
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cirl1.on('line', common.mustCall(rl1OnLine));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Write a line plus the first byte of a UTF-8 multibyte character to make sure
231cb0ef41Sopenharmony_ci// that it doesn’t get lost when closing the readline instance.
241cb0ef41Sopenharmony_ciinput.write(Buffer.concat([
251cb0ef41Sopenharmony_ci  Buffer.from('foo\n'),
261cb0ef41Sopenharmony_ci  Buffer.from([ 0xe2 ]),  // Exactly one third of a ☃ snowman.
271cb0ef41Sopenharmony_ci]));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cifunction rl1OnLine(line) {
301cb0ef41Sopenharmony_ci  assert.strictEqual(line, 'foo');
311cb0ef41Sopenharmony_ci  rl1.close();
321cb0ef41Sopenharmony_ci  const rl2 = readline.createInterface({
331cb0ef41Sopenharmony_ci    input,
341cb0ef41Sopenharmony_ci    output,
351cb0ef41Sopenharmony_ci    terminal: true
361cb0ef41Sopenharmony_ci  });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  rl2.on('line', common.mustCall((line) => {
391cb0ef41Sopenharmony_ci    assert.strictEqual(line, '☃bar');
401cb0ef41Sopenharmony_ci    rl2.close();
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci  input.write(Buffer.from([0x98, 0x83])); // The rest of the ☃ snowman.
431cb0ef41Sopenharmony_ci  input.write('bar\n');
441cb0ef41Sopenharmony_ci}
45