11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst { Readable } = require('stream');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci{
71cb0ef41Sopenharmony_ci  // Call .setEncoding() while there are bytes already in the buffer.
81cb0ef41Sopenharmony_ci  const r = new Readable({ read() {} });
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  r.push(Buffer.from('a'));
111cb0ef41Sopenharmony_ci  r.push(Buffer.from('b'));
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  r.setEncoding('utf8');
141cb0ef41Sopenharmony_ci  const chunks = [];
151cb0ef41Sopenharmony_ci  r.on('data', (chunk) => chunks.push(chunk));
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  process.nextTick(() => {
181cb0ef41Sopenharmony_ci    assert.deepStrictEqual(chunks, ['ab']);
191cb0ef41Sopenharmony_ci  });
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci{
231cb0ef41Sopenharmony_ci  // Call .setEncoding() while the buffer contains a complete,
241cb0ef41Sopenharmony_ci  // but chunked character.
251cb0ef41Sopenharmony_ci  const r = new Readable({ read() {} });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  r.push(Buffer.from([0xf0]));
281cb0ef41Sopenharmony_ci  r.push(Buffer.from([0x9f]));
291cb0ef41Sopenharmony_ci  r.push(Buffer.from([0x8e]));
301cb0ef41Sopenharmony_ci  r.push(Buffer.from([0x89]));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  r.setEncoding('utf8');
331cb0ef41Sopenharmony_ci  const chunks = [];
341cb0ef41Sopenharmony_ci  r.on('data', (chunk) => chunks.push(chunk));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  process.nextTick(() => {
371cb0ef41Sopenharmony_ci    assert.deepStrictEqual(chunks, ['�']);
381cb0ef41Sopenharmony_ci  });
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci{
421cb0ef41Sopenharmony_ci  // Call .setEncoding() while the buffer contains an incomplete character,
431cb0ef41Sopenharmony_ci  // and finish the character later.
441cb0ef41Sopenharmony_ci  const r = new Readable({ read() {} });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  r.push(Buffer.from([0xf0]));
471cb0ef41Sopenharmony_ci  r.push(Buffer.from([0x9f]));
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  r.setEncoding('utf8');
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  r.push(Buffer.from([0x8e]));
521cb0ef41Sopenharmony_ci  r.push(Buffer.from([0x89]));
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  const chunks = [];
551cb0ef41Sopenharmony_ci  r.on('data', (chunk) => chunks.push(chunk));
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  process.nextTick(() => {
581cb0ef41Sopenharmony_ci    assert.deepStrictEqual(chunks, ['�']);
591cb0ef41Sopenharmony_ci  });
601cb0ef41Sopenharmony_ci}
61