11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci// META: script=resources/readable-stream-from-array.js
31cb0ef41Sopenharmony_ci// META: script=resources/readable-stream-to-array.js
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci'use strict';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst inputBytes = [73, 32, 240, 159, 146, 153, 32, 115, 116, 114, 101,
81cb0ef41Sopenharmony_ci                    97, 109, 115];
91cb0ef41Sopenharmony_cifor (const splitPoint of [2, 3, 4, 5]) {
101cb0ef41Sopenharmony_ci  promise_test(async () => {
111cb0ef41Sopenharmony_ci    const input = readableStreamFromArray(
121cb0ef41Sopenharmony_ci        [new Uint8Array(inputBytes.slice(0, splitPoint)),
131cb0ef41Sopenharmony_ci         new Uint8Array(inputBytes.slice(splitPoint))]);
141cb0ef41Sopenharmony_ci    const expectedOutput = ['I ', '\u{1F499} streams'];
151cb0ef41Sopenharmony_ci    const output = input.pipeThrough(new TextDecoderStream());
161cb0ef41Sopenharmony_ci    const array = await readableStreamToArray(output);
171cb0ef41Sopenharmony_ci    assert_array_equals(array, expectedOutput,
181cb0ef41Sopenharmony_ci                        'the split code point should be in the second chunk ' +
191cb0ef41Sopenharmony_ci                        'of the output');
201cb0ef41Sopenharmony_ci  }, 'a code point split between chunks should not be emitted until all ' +
211cb0ef41Sopenharmony_ci      'bytes are available; split point = ' + splitPoint);
221cb0ef41Sopenharmony_ci}
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cipromise_test(async () => {
251cb0ef41Sopenharmony_ci  const splitPoint = 6;
261cb0ef41Sopenharmony_ci  const input = readableStreamFromArray(
271cb0ef41Sopenharmony_ci      [new Uint8Array(inputBytes.slice(0, splitPoint)),
281cb0ef41Sopenharmony_ci       new Uint8Array(inputBytes.slice(splitPoint))]);
291cb0ef41Sopenharmony_ci  const output = input.pipeThrough(new TextDecoderStream());
301cb0ef41Sopenharmony_ci  const array = await readableStreamToArray(output);
311cb0ef41Sopenharmony_ci  assert_array_equals(array, ['I \u{1F499}', ' streams'],
321cb0ef41Sopenharmony_ci                      'the multibyte character should be in the first chunk ' +
331cb0ef41Sopenharmony_ci                      'of the output');
341cb0ef41Sopenharmony_ci}, 'a code point should be emitted as soon as all bytes are available');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cifor (let splitPoint = 1; splitPoint < 7; ++splitPoint) {
371cb0ef41Sopenharmony_ci  promise_test(async () => {
381cb0ef41Sopenharmony_ci    const input = readableStreamFromArray(
391cb0ef41Sopenharmony_ci      [new Uint8Array(inputBytes.slice(0, splitPoint)),
401cb0ef41Sopenharmony_ci       new Uint8Array([]),
411cb0ef41Sopenharmony_ci       new Uint8Array(inputBytes.slice(splitPoint))]);
421cb0ef41Sopenharmony_ci    const concatenatedOutput = 'I \u{1F499} streams';
431cb0ef41Sopenharmony_ci    const output = input.pipeThrough(new TextDecoderStream());
441cb0ef41Sopenharmony_ci    const array = await readableStreamToArray(output);
451cb0ef41Sopenharmony_ci    assert_equals(array.length, 2, 'two chunks should be output');
461cb0ef41Sopenharmony_ci    assert_equals(array[0].concat(array[1]), concatenatedOutput,
471cb0ef41Sopenharmony_ci                  'output should be unchanged by the empty chunk');
481cb0ef41Sopenharmony_ci  }, 'an empty chunk inside a code point split between chunks should not ' +
491cb0ef41Sopenharmony_ci     'change the output; split point = ' + splitPoint);
501cb0ef41Sopenharmony_ci}
51