11cb0ef41Sopenharmony_ciimport { mustCall } from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport { ReadableStream } from 'stream/web';
31cb0ef41Sopenharmony_ciimport assert from 'assert';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci{
61cb0ef41Sopenharmony_ci  // Test tee() with close in the nextTick after enqueue
71cb0ef41Sopenharmony_ci  async function read(stream) {
81cb0ef41Sopenharmony_ci    const chunks = [];
91cb0ef41Sopenharmony_ci    for await (const chunk of stream)
101cb0ef41Sopenharmony_ci      chunks.push(chunk);
111cb0ef41Sopenharmony_ci    return Buffer.concat(chunks).toString();
121cb0ef41Sopenharmony_ci  }
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  const [r1, r2] = new ReadableStream({
151cb0ef41Sopenharmony_ci    start(controller) {
161cb0ef41Sopenharmony_ci      process.nextTick(() => {
171cb0ef41Sopenharmony_ci        controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci        process.nextTick(() => {
201cb0ef41Sopenharmony_ci          controller.close();
211cb0ef41Sopenharmony_ci        });
221cb0ef41Sopenharmony_ci      });
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci  }).tee();
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  (async () => {
271cb0ef41Sopenharmony_ci    const [dataReader1, dataReader2] = await Promise.all([
281cb0ef41Sopenharmony_ci      read(r1),
291cb0ef41Sopenharmony_ci      read(r2),
301cb0ef41Sopenharmony_ci    ]);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    assert.strictEqual(dataReader1, dataReader2);
331cb0ef41Sopenharmony_ci    assert.strictEqual(dataReader1, 'foobar');
341cb0ef41Sopenharmony_ci    assert.strictEqual(dataReader2, 'foobar');
351cb0ef41Sopenharmony_ci  })().then(mustCall());
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  // Test ReadableByteStream.tee() with close in the nextTick after enqueue
401cb0ef41Sopenharmony_ci  async function read(stream) {
411cb0ef41Sopenharmony_ci    const chunks = [];
421cb0ef41Sopenharmony_ci    for await (const chunk of stream)
431cb0ef41Sopenharmony_ci      chunks.push(chunk);
441cb0ef41Sopenharmony_ci    return Buffer.concat(chunks).toString();
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  const [r1, r2] = new ReadableStream({
481cb0ef41Sopenharmony_ci    type: 'bytes',
491cb0ef41Sopenharmony_ci    start(controller) {
501cb0ef41Sopenharmony_ci      process.nextTick(() => {
511cb0ef41Sopenharmony_ci        controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci        process.nextTick(() => {
541cb0ef41Sopenharmony_ci          controller.close();
551cb0ef41Sopenharmony_ci        });
561cb0ef41Sopenharmony_ci      });
571cb0ef41Sopenharmony_ci    }
581cb0ef41Sopenharmony_ci  }).tee();
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  (async () => {
611cb0ef41Sopenharmony_ci    const [dataReader1, dataReader2] = await Promise.all([
621cb0ef41Sopenharmony_ci      read(r1),
631cb0ef41Sopenharmony_ci      read(r2),
641cb0ef41Sopenharmony_ci    ]);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    assert.strictEqual(dataReader1, dataReader2);
671cb0ef41Sopenharmony_ci    assert.strictEqual(dataReader1, 'foobar');
681cb0ef41Sopenharmony_ci    assert.strictEqual(dataReader2, 'foobar');
691cb0ef41Sopenharmony_ci  })().then(mustCall());
701cb0ef41Sopenharmony_ci}
71