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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412/** Tests for mapper option destructuring and validation, exercised through the public `pMap` and `pMapIterable` entry points (options handling is package-internal). @module */
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { InvalidBackpressureError, InvalidConcurrencyError, MapperRequiredError, pMap, pMapIterable,} from '../dist/final/neutral/index.mjs';
await describe({ name: 'map option handling', children: [ describe({ name: 'defaults', children: [ it({ name: 'maps every input uncapped when options are omitted', fn: async () => { const results = await pMap({ iterable: [ 1, 2, 3, 4, ], mapper: function double(value: number,): number { return value * 2; }, },); expect(results,).toEqual([ 2, 4, 6, 8, ],); }, },),
it({ name: 'stops on the first mapper failure when stopOnError is omitted', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: [ 1, 2, 3, ], mapper: function failingMapper(): never { throw new Error('first failure',); }, },); } catch (error) { caught = error; } expect((caught as Error).message,).toBe('first failure',); }, },),
it({ name: 'defaults backpressure to concurrency for the streaming map', fn: async () => { /** Streamed values proving the run completes under the defaulted backpressure bound. */ const streamed: number[] = []; for await (const mapped of pMapIterable({ iterable: [1], mapper: function identity(value: number,): number { return value; }, options: { concurrency: 1, }, })) streamed.push(mapped,); expect(streamed,).toEqual([1],); }, },), ], },),
describe({ name: 'destructuring failures', children: [ it({ name: 'rejects like upstream when options is null instead of an object', 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: null as never, },); } catch (error) { caught = error; } // The text comes from the engine's destructuring failure, so the // live parity check is the fuzz sidecar's differential oracle; // here the destructured field name must surface. expect(caught,).toBeInstanceOf(TypeError,); expect((caught as Error).message,).toContain('concurrency',); }, },),
it({ name: 'throws synchronously like upstream when streaming options is null', fn: async () => { /** Failure caught at the call site, matching upstream `pMapIterable`'s synchronous destructuring. */ let caught: unknown; try { pMapIterable({ iterable: [1], mapper: function identity(value: number,): number { return value; }, options: null as never, },); } catch (error) { caught = error; } // Engine destructuring text again; the fuzz oracle compares the // exact message against upstream. expect(caught,).toBeInstanceOf(TypeError,); expect((caught as Error).message,).toContain('concurrency',); }, },), ], },),
describe({ name: 'concurrency validation', children: [ it({ name: 'rejects a zero bound with upstream p-map message text', 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(InvalidConcurrencyError,); expect((caught as Error).message,).toBe('Expected `concurrency` to be an integer from 1 and up or `Infinity`, got `0` (number)',); }, },),
it({ name: 'rejects a fractional bound', 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: 1.5, }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(InvalidConcurrencyError,); }, },),
it({ name: 'rejects an unsafe integer bound, keeping upstream isSafeInteger acceptance', 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: Number.MAX_SAFE_INTEGER + 1, }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(InvalidConcurrencyError,); }, },),
it({ name: 'accepts Number.POSITIVE_INFINITY as the uncapped bound', fn: async () => { const results = await pMap({ iterable: [7], mapper: function identity(value: number,): number { return value; }, options: { concurrency: Number.POSITIVE_INFINITY, }, },); expect(results,).toEqual([7],); }, },),
it({ name: 'throws synchronously for the streaming map, like upstream', fn: async () => { /** Failure caught at the call site, matching upstream `pMapIterable`'s synchronous validation. */ let caught: unknown; try { pMapIterable({ iterable: [1], mapper: function identity(value: number,): number { return value; }, options: { concurrency: 0, }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(InvalidConcurrencyError,); }, },), ], },),
describe({ name: 'backpressure validation', children: [ it({ name: 'rejects a backpressure bound below concurrency with upstream message text', fn: async () => { /** Failure caught at the call site, matching upstream `pMapIterable`'s synchronous validation. */ let caught: unknown; try { pMapIterable({ iterable: [1], mapper: function identity(value: number,): number { return value; }, options: { concurrency: 4, backpressure: 2, }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(InvalidBackpressureError,); expect((caught as Error).message,).toBe('Expected `backpressure` to be an integer from `concurrency` (4) and up or `Infinity`, got `2` (number)',); }, },),
it({ name: 'accepts Number.POSITIVE_INFINITY as the uncapped backpressure bound', fn: async () => { /** Streamed values proving the run completes under the uncapped backpressure bound. */ const streamed: number[] = []; for await (const mapped of pMapIterable({ iterable: [3], mapper: function identity(value: number,): number { return value; }, options: { concurrency: 1, backpressure: Number.POSITIVE_INFINITY, }, })) streamed.push(mapped,); expect(streamed,).toEqual([3],); }, },), ], },),
describe({ name: 'signal resolution', children: [ it({ name: 'forwards a present signal to the run so an abort rejects it', fn: async () => { /** AbortController whose signal aborts before the run starts. */ const abortController = new AbortController(); abortController.abort('stopped early',); /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: [1], mapper: function identity(value: number,): number { return value; }, options: { signal: abortController.signal, }, },); } catch (error) { caught = error; } expect(caught,).toBe('stopped early',); }, },), ], },),
describe({ name: 'check ordering', children: [ it({ name: 'reports the input failure before the mapper failure, like upstream', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: 5 as never, mapper: undefined as never, },); } catch (error) { caught = error; } expect((caught as Error).message,).toBe('Expected `input` to be either an `Iterable` or `AsyncIterable`, got (number)',); }, },),
it({ name: 'reports the mapper failure before the concurrency failure, like upstream', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: [1], mapper: undefined as never, options: { concurrency: 0, }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(MapperRequiredError,); }, },), ], },), ],},);