11cb0ef41Sopenharmony_ciimport * as common from '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport { setTimeout } from 'timers/promises';
31cb0ef41Sopenharmony_ciimport { Readable } from 'stream';
41cb0ef41Sopenharmony_ciimport assert from 'assert';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifunction oneTo5() {
81cb0ef41Sopenharmony_ci  return Readable.from([1, 2, 3, 4, 5]);
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction oneTo5Async() {
121cb0ef41Sopenharmony_ci  return oneTo5().map(async (x) => {
131cb0ef41Sopenharmony_ci    await Promise.resolve();
141cb0ef41Sopenharmony_ci    return x;
151cb0ef41Sopenharmony_ci  });
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci{
181cb0ef41Sopenharmony_ci  // Some, find, and every work with a synchronous stream and predicate
191cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().some((x) => x > 3), true);
201cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().every((x) => x > 3), false);
211cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().find((x) => x > 3), 4);
221cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().some((x) => x > 6), false);
231cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().every((x) => x < 6), true);
241cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().find((x) => x > 6), undefined);
251cb0ef41Sopenharmony_ci  assert.strictEqual(await Readable.from([]).some(() => true), false);
261cb0ef41Sopenharmony_ci  assert.strictEqual(await Readable.from([]).every(() => true), true);
271cb0ef41Sopenharmony_ci  assert.strictEqual(await Readable.from([]).find(() => true), undefined);
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci{
311cb0ef41Sopenharmony_ci  // Some, find, and every work with an asynchronous stream and synchronous predicate
321cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().some((x) => x > 3), true);
331cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().every((x) => x > 3), false);
341cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().find((x) => x > 3), 4);
351cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().some((x) => x > 6), false);
361cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().every((x) => x < 6), true);
371cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().find((x) => x > 6), undefined);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  // Some, find, and every work on synchronous streams with an asynchronous predicate
421cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().some(async (x) => x > 3), true);
431cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().every(async (x) => x > 3), false);
441cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().find(async (x) => x > 3), 4);
451cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().some(async (x) => x > 6), false);
461cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().every(async (x) => x < 6), true);
471cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5().find(async (x) => x > 6), undefined);
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  // Some, find, and every work on asynchronous streams with an asynchronous predicate
521cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().some(async (x) => x > 3), true);
531cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().every(async (x) => x > 3), false);
541cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().find(async (x) => x > 3), 4);
551cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().some(async (x) => x > 6), false);
561cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().every(async (x) => x < 6), true);
571cb0ef41Sopenharmony_ci  assert.strictEqual(await oneTo5Async().find(async (x) => x > 6), undefined);
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci{
611cb0ef41Sopenharmony_ci  async function checkDestroyed(stream) {
621cb0ef41Sopenharmony_ci    await setTimeout();
631cb0ef41Sopenharmony_ci    assert.strictEqual(stream.destroyed, true);
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  {
671cb0ef41Sopenharmony_ci    // Some, find, and every short circuit
681cb0ef41Sopenharmony_ci    const someStream = oneTo5();
691cb0ef41Sopenharmony_ci    await someStream.some(common.mustCall((x) => x > 2, 3));
701cb0ef41Sopenharmony_ci    await checkDestroyed(someStream);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    const everyStream = oneTo5();
731cb0ef41Sopenharmony_ci    await everyStream.every(common.mustCall((x) => x < 3, 3));
741cb0ef41Sopenharmony_ci    await checkDestroyed(everyStream);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    const findStream = oneTo5();
771cb0ef41Sopenharmony_ci    await findStream.find(common.mustCall((x) => x > 1, 2));
781cb0ef41Sopenharmony_ci    await checkDestroyed(findStream);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    // When short circuit isn't possible the whole stream is iterated
811cb0ef41Sopenharmony_ci    await oneTo5().some(common.mustCall(() => false, 5));
821cb0ef41Sopenharmony_ci    await oneTo5().every(common.mustCall(() => true, 5));
831cb0ef41Sopenharmony_ci    await oneTo5().find(common.mustCall(() => false, 5));
841cb0ef41Sopenharmony_ci  }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  {
871cb0ef41Sopenharmony_ci    // Some, find, and every short circuit async stream/predicate
881cb0ef41Sopenharmony_ci    const someStream = oneTo5Async();
891cb0ef41Sopenharmony_ci    await someStream.some(common.mustCall(async (x) => x > 2, 3));
901cb0ef41Sopenharmony_ci    await checkDestroyed(someStream);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci    const everyStream = oneTo5Async();
931cb0ef41Sopenharmony_ci    await everyStream.every(common.mustCall(async (x) => x < 3, 3));
941cb0ef41Sopenharmony_ci    await checkDestroyed(everyStream);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    const findStream = oneTo5Async();
971cb0ef41Sopenharmony_ci    await findStream.find(common.mustCall(async (x) => x > 1, 2));
981cb0ef41Sopenharmony_ci    await checkDestroyed(findStream);
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci    // When short circuit isn't possible the whole stream is iterated
1011cb0ef41Sopenharmony_ci    await oneTo5Async().some(common.mustCall(async () => false, 5));
1021cb0ef41Sopenharmony_ci    await oneTo5Async().every(common.mustCall(async () => true, 5));
1031cb0ef41Sopenharmony_ci    await oneTo5Async().find(common.mustCall(async () => false, 5));
1041cb0ef41Sopenharmony_ci  }
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci{
1081cb0ef41Sopenharmony_ci  // Concurrency doesn't affect which value is found.
1091cb0ef41Sopenharmony_ci  const found = await Readable.from([1, 2]).find(async (val) => {
1101cb0ef41Sopenharmony_ci    if (val === 1) {
1111cb0ef41Sopenharmony_ci      await setTimeout(100);
1121cb0ef41Sopenharmony_ci    }
1131cb0ef41Sopenharmony_ci    return true;
1141cb0ef41Sopenharmony_ci  }, { concurrency: 2 });
1151cb0ef41Sopenharmony_ci  assert.strictEqual(found, 1);
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci{
1191cb0ef41Sopenharmony_ci  // Support for AbortSignal
1201cb0ef41Sopenharmony_ci  for (const op of ['some', 'every', 'find']) {
1211cb0ef41Sopenharmony_ci    {
1221cb0ef41Sopenharmony_ci      const ac = new AbortController();
1231cb0ef41Sopenharmony_ci      assert.rejects(Readable.from([1, 2, 3])[op](
1241cb0ef41Sopenharmony_ci        () => new Promise(() => { }),
1251cb0ef41Sopenharmony_ci        { signal: ac.signal }
1261cb0ef41Sopenharmony_ci      ), {
1271cb0ef41Sopenharmony_ci        name: 'AbortError',
1281cb0ef41Sopenharmony_ci      }, `${op} should abort correctly with sync abort`).then(common.mustCall());
1291cb0ef41Sopenharmony_ci      ac.abort();
1301cb0ef41Sopenharmony_ci    }
1311cb0ef41Sopenharmony_ci    {
1321cb0ef41Sopenharmony_ci      // Support for pre-aborted AbortSignal
1331cb0ef41Sopenharmony_ci      assert.rejects(Readable.from([1, 2, 3])[op](
1341cb0ef41Sopenharmony_ci        () => new Promise(() => { }),
1351cb0ef41Sopenharmony_ci        { signal: AbortSignal.abort() }
1361cb0ef41Sopenharmony_ci      ), {
1371cb0ef41Sopenharmony_ci        name: 'AbortError',
1381cb0ef41Sopenharmony_ci      }, `${op} should abort with pre-aborted abort controller`).then(common.mustCall());
1391cb0ef41Sopenharmony_ci    }
1401cb0ef41Sopenharmony_ci  }
1411cb0ef41Sopenharmony_ci}
1421cb0ef41Sopenharmony_ci{
1431cb0ef41Sopenharmony_ci  // Error cases
1441cb0ef41Sopenharmony_ci  for (const op of ['some', 'every', 'find']) {
1451cb0ef41Sopenharmony_ci    assert.rejects(async () => {
1461cb0ef41Sopenharmony_ci      await Readable.from([1])[op](1);
1471cb0ef41Sopenharmony_ci    }, /ERR_INVALID_ARG_TYPE/, `${op} should throw for invalid function`).then(common.mustCall());
1481cb0ef41Sopenharmony_ci    assert.rejects(async () => {
1491cb0ef41Sopenharmony_ci      await Readable.from([1])[op]((x) => x, {
1501cb0ef41Sopenharmony_ci        concurrency: 'Foo'
1511cb0ef41Sopenharmony_ci      });
1521cb0ef41Sopenharmony_ci    }, /ERR_OUT_OF_RANGE/, `${op} should throw for invalid concurrency`).then(common.mustCall());
1531cb0ef41Sopenharmony_ci    assert.rejects(async () => {
1541cb0ef41Sopenharmony_ci      await Readable.from([1])[op]((x) => x, 1);
1551cb0ef41Sopenharmony_ci    }, /ERR_INVALID_ARG_TYPE/, `${op} should throw for invalid concurrency`).then(common.mustCall());
1561cb0ef41Sopenharmony_ci    assert.rejects(async () => {
1571cb0ef41Sopenharmony_ci      await Readable.from([1])[op]((x) => x, {
1581cb0ef41Sopenharmony_ci        signal: true
1591cb0ef41Sopenharmony_ci      });
1601cb0ef41Sopenharmony_ci    }, /ERR_INVALID_ARG_TYPE/, `${op} should throw for invalid signal`).then(common.mustCall());
1611cb0ef41Sopenharmony_ci  }
1621cb0ef41Sopenharmony_ci}
1631cb0ef41Sopenharmony_ci{
1641cb0ef41Sopenharmony_ci  for (const op of ['some', 'every', 'find']) {
1651cb0ef41Sopenharmony_ci    const stream = oneTo5();
1661cb0ef41Sopenharmony_ci    Object.defineProperty(stream, 'map', {
1671cb0ef41Sopenharmony_ci      value: common.mustNotCall(),
1681cb0ef41Sopenharmony_ci    });
1691cb0ef41Sopenharmony_ci    // Check that map isn't getting called.
1701cb0ef41Sopenharmony_ci    stream[op](() => {});
1711cb0ef41Sopenharmony_ci  }
1721cb0ef41Sopenharmony_ci}
173