Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598/** Tests for the concurrent map's scheduling, result collection, and failure handling. @module */
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { pMap, } from '../dist/final/neutral/index.mjs';
import { createGate, type Gate, yieldTurn,} from './test-support.ts';
await describe({ name: pMap.name, children: [ //region Results
it({ name: 'collects mapper results in input order', fn: async () => { const results = await pMap({ iterable: [ 10, 20, 30, ], mapper: function scaled(value: number, index: number,): number { return value + index; }, },); expect(results,).toEqual([ 10, 21, 32, ],); }, },),
it({ name: 'collects in input order even when later inputs finish first', fn: async () => { /** Gate held by the first input's mapper call only. */ const gate = createGate(); /** Run promise observed once the slow input's gate releases. */ const running = pMap({ iterable: [ 'slow', 'fast', ], mapper: async function gatedEcho(value: string,): Promise<string> { if (value === 'slow') await gate.open; return value; }, },); await yieldTurn(); gate.release(); expect(await running,).toEqual([ 'slow', 'fast', ],); }, },),
it({ name: 'resolves an empty input to an empty result', fn: async () => { const results = await pMap({ iterable: [], mapper: function unusedMapper(value: unknown,): unknown { return value; }, },); expect(results,).toEqual([],); }, },),
it({ name: 'awaits promise elements before invoking the mapper', fn: async () => { /** Element order observed by the mapper. */ const seen: number[] = []; const results = await pMap({ iterable: [ Promise.resolve(1,), Promise.resolve(2,), ], mapper: function recordingMapper(value: number,): number { seen.push(value,); return value; }, },); expect(results,).toEqual([ 1, 2, ],); expect(seen,).toEqual([ 1, 2, ],); }, },),
it({ name: 'awaits thenable mapper results before collecting them', fn: async () => { const results = await pMap({ iterable: ['done',], mapper: async function delayedEcho(value: string,): Promise<string> { return value; }, },); expect(results,).toEqual(['done',],); }, },),
//endregion Results
//region Scheduling
it({ name: 'starts mapper calls in input order and at most concurrency at once', fn: async () => { /** Overlap counters shared by every mapper call. */ const counts = { running: 0, maxRunning: 0, }; /** Gates for the four mapper calls, released in creation order. */ const gates: Gate[] = Array.from( { length: 4, }, function makeGate(): Gate { return createGate(); }, ); /** Start order recorded as mapper calls run. */ const started: number[] = []; /** Run promise observed once every gate releases. */ const running = pMap({ iterable: [ 1, 2, 3, 4, ], mapper: async function trackedMapper(value: number,): Promise<number> { started.push(value,); counts.running += 1; counts.maxRunning = Math.max( counts.maxRunning, counts.running, ); await gates.at(value - 1,) ?.open; counts.running -= 1; return value; }, options: { concurrency: 2, }, },);
await yieldTurn(); expect(started,).toEqual([ 1, 2, ],); expect(counts.running,).toBe(2,);
for (const gate of gates) gate.release(); await running; expect(counts.maxRunning,).toBe(2,); expect(started,).toEqual([ 1, 2, 3, 4, ],); }, },),
it({ name: 'starts the next mapper call after one settles', fn: async () => { /** Gate held by the first mapper call only. */ const gate = createGate(); /** Start order recorded as mapper calls run. */ const started: number[] = []; /** Run promise observed once the first mapper's gate releases. */ const running = pMap({ iterable: [ 1, 2, ], mapper: async function chainedMapper(value: number,): Promise<number> { started.push(value,); if (value === 1) await gate.open; return value; }, options: { concurrency: 1, }, },);
await yieldTurn(); expect(started,).toEqual([1],);
gate.release(); await running; expect(started,).toEqual([ 1, 2, ],); }, },),
it({ name: 'never invokes the mapper during the pMap call itself', fn: async () => { /** Invocation flag set the moment the mapper runs. */ const state = { invoked: false, }; const running = pMap({ iterable: [1], mapper: function markInvoked(value: number,): number { state.invoked = true; return value; }, },); expect(state.invoked,).toBe(false,); await running; expect(state.invoked,).toBe(true,); }, },),
//endregion Scheduling
//region Failures
it({ name: 'rejects with the synchronous mapper throw, like upstream', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: [1], mapper: function throwingMapper(): never { throw new Error('sync throw',); }, },); } catch (error) { caught = error; } expect((caught as Error).message,).toBe('sync throw',); }, },),
it({ name: 'rejects with the first mapper rejection when stopOnError is true', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: [ 1, 2, 3, ], mapper: function failingOnTwo(value: number,): number { if (value === 2) throw new Error('first failure',); return value; }, options: { concurrency: 1, stopOnError: true, }, },); } catch (error) { caught = error; } expect((caught as Error).message,).toBe('first failure',); }, },),
it({ name: 'rejects with an AggregateError of every failure when stopOnError is false', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: [ 1, 2, 3, ], mapper: function failingOnEvens(value: number,): number { if ((value % 2) === 0) throw new Error(`failure ${String(value,)}`,); return value; }, options: { concurrency: 1, stopOnError: false, }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(AggregateError,); /** Aggregated failures as upstream collects them. */ const aggregated = (caught as AggregateError).errors; expect(aggregated,).toHaveLength(1,); expect((aggregated[0] as Error).message,).toBe('failure 2',); }, },),
it({ name: 'resolves normally when stopOnError is false and nothing fails', fn: async () => { const results = await pMap({ iterable: [ 1, 2, ], mapper: function double(value: number,): number { return value * 2; }, options: { concurrency: 1, stopOnError: false, }, },); expect(results,).toEqual([ 2, 4, ],); }, },),
it({ name: 'rejects with the iterable failure mid-iteration, even when stopOnError is false', fn: async () => { /** Source yielding one value and then throwing. */ function* oneThenThrow(): Generator<number> { yield 1; throw new Error('iterable failed',); }
/** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: oneThenThrow(), mapper: function identity(value: number,): number { return value; }, options: { concurrency: 1, stopOnError: false, }, },); } catch (error) { caught = error; } expect((caught as Error).message,).toBe('iterable failed',); }, },),
it({ name: 'rejects with the configuration failure instead of hanging', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: [1], mapper: function identity(value: number,): number { return value; }, options: { concurrency: 0, }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(TypeError,); }, },),
//endregion Failures
//region Shutdown
it({ name: 'closes the source exactly once when two gated mapper calls fail', fn: async () => { /** Close and pull counters for this source. */ const telemetry = { pulls: 0, closes: 0, }; /** Source counting its pulls and closes. */ const countedIterable: Iterable<number> = { [Symbol.iterator]: function openCountedIterator(): Iterator<number> { return { next: function countedNext(): IteratorResult<number> { telemetry.pulls += 1; return { done: false, value: telemetry.pulls, }; }, return: function countedReturn(): IteratorResult<number> { telemetry.closes += 1; return { done: true, value: undefined, }; }, }; }, }; /** Gate holding both mapper calls past the settled check, so both failures reach the run's rejection handling. */ const gate = createGate(); /** Run promise observed for its rejection. */ const running = pMap({ iterable: countedIterable, mapper: async function gatedFailure(): Promise<never> { await gate.open; throw new Error('first failure',); }, options: { concurrency: 2, stopOnError: true, }, },); await yieldTurn(); gate.release(); /** Failure observed from the run's promise. */ let caught: unknown; try { await running; } catch (error) { caught = error; } expect((caught as Error).message,).toBe('first failure',); await yieldTurn(); expect(telemetry.closes,).toBe(1,); }, },),
it({ name: 'leaves an exhausted source open when a mapper fails after the source is done', fn: async () => { /** Close counter for this source. */ const telemetry = { closes: 0, }; /** Source yielding one value and then reporting `done` on the second pull, recording its close. */ const oneValueIterable: Iterable<number> = { [Symbol.iterator]: function openOneValueIterator(): Iterator<number> { const cursor = { position: 0, }; return { next: function oneValueNext(): IteratorResult<number> { if (cursor.position >= 1) return { done: true, value: undefined, }; cursor.position += 1; return { done: false, value: 1, }; }, return: function oneValueReturn(): IteratorResult<number> { telemetry.closes += 1; return { done: true, value: undefined, }; }, }; }, }; /** Gate holding the single mapper call until the source is done. */ const gate = createGate(); /** Run promise observed for its rejection. */ const running = pMap({ iterable: oneValueIterable, mapper: async function gatedFailure(): Promise<never> { await gate.open; throw new Error('mapper failed',); }, options: { concurrency: 2, stopOnError: true, }, },); await yieldTurn(); gate.release(); /** Failure observed from the run's promise. */ let caught: unknown; try { await running; } catch (error) { caught = error; } expect((caught as Error).message,).toBe('mapper failed',); await yieldTurn(); expect(telemetry.closes,).toBe(0,); }, },),
//endregion Shutdown ],},);