11cb0ef41Sopenharmony_ciimport '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport { Readable } from 'stream';
31cb0ef41Sopenharmony_ciimport { deepStrictEqual, rejects, throws } from 'assert';
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci{
61cb0ef41Sopenharmony_ci  // asIndexedPairs with a synchronous stream
71cb0ef41Sopenharmony_ci  const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray();
81cb0ef41Sopenharmony_ci  deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
91cb0ef41Sopenharmony_ci  const empty = await Readable.from([]).asIndexedPairs().toArray();
101cb0ef41Sopenharmony_ci  deepStrictEqual(empty, []);
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  // asIndexedPairs works an asynchronous streams
151cb0ef41Sopenharmony_ci  const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x);
161cb0ef41Sopenharmony_ci  const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray();
171cb0ef41Sopenharmony_ci  deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
181cb0ef41Sopenharmony_ci  const empty = await asyncFrom([]).asIndexedPairs().toArray();
191cb0ef41Sopenharmony_ci  deepStrictEqual(empty, []);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci{
231cb0ef41Sopenharmony_ci  // Does not enumerate an infinite stream
241cb0ef41Sopenharmony_ci  const infinite = () => Readable.from(async function* () {
251cb0ef41Sopenharmony_ci    while (true) yield 1;
261cb0ef41Sopenharmony_ci  }());
271cb0ef41Sopenharmony_ci  const pairs = await infinite().asIndexedPairs().take(3).toArray();
281cb0ef41Sopenharmony_ci  deepStrictEqual(pairs, [[0, 1], [1, 1], [2, 1]]);
291cb0ef41Sopenharmony_ci  const empty = await infinite().asIndexedPairs().take(0).toArray();
301cb0ef41Sopenharmony_ci  deepStrictEqual(empty, []);
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  // AbortSignal
351cb0ef41Sopenharmony_ci  await rejects(async () => {
361cb0ef41Sopenharmony_ci    const ac = new AbortController();
371cb0ef41Sopenharmony_ci    const { signal } = ac;
381cb0ef41Sopenharmony_ci    const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
391cb0ef41Sopenharmony_ci    ac.abort();
401cb0ef41Sopenharmony_ci    await p;
411cb0ef41Sopenharmony_ci  }, { name: 'AbortError' });
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  await rejects(async () => {
441cb0ef41Sopenharmony_ci    const signal = AbortSignal.abort();
451cb0ef41Sopenharmony_ci    await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
461cb0ef41Sopenharmony_ci  }, /AbortError/);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  // Error cases
511cb0ef41Sopenharmony_ci  throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/);
521cb0ef41Sopenharmony_ci  throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/);
531cb0ef41Sopenharmony_ci}
54