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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393/** Deterministic coverage driver: exercises every exported function and its error paths with fixed inputs, so the V8 coverage it produces is reproducible. Run under `NODE_V8_COVERAGE` by the `fuzz:coverage` task, then summarized by `coverage-report.ts`. @module */
import { wait, } from '@monochromatic-dev/module-async-time/ts';
import { pMap, pMapIterable, pMapSkip,} from '@monochromatic-dev/module-p-map-fork/ts';
import { bothSlots, type CloseState, closeCounting, droppingMapper, failingMapper, identityMapper, MAP_INPUTS, oneThenThrow, promisedMapper, swallow, swallowAsync, throwingMapper, twoAsyncValues,} from './coverage-fixtures.ts';
//region Exercise
/** Exercises option resolution and validation, scheduling over sync, async, and promise-element sources, skip staging, both failure modes, abort paths, iterator selection and shutdown, and the streaming variant's bounds, skips, failures, and early exit. @example ```ts await exercise(); ``` */async function exercise(): Promise<void> { //region Option resolution
/* oxlint-disable typescript/no-unsafe-type-assertion -- invalid runtime options and inputs mirror a mis-typed caller */
await swallowAsync(async function invalidConcurrencyRejected(): Promise<void> { await pMap({ iterable: [1], mapper: identityMapper, options: { concurrency: 0, }, },); },); await swallowAsync(async function invalidInput(): Promise<void> { await pMap({ iterable: true as never, mapper: identityMapper, },); },); await swallowAsync(async function nullInputProbe(): Promise<void> { await pMap({ iterable: null as never, mapper: identityMapper, },); },); await swallowAsync(async function missingMapper(): Promise<void> { await pMap({ iterable: [1], mapper: undefined as never, },); },); await swallowAsync(async function nonObjectOptions(): Promise<void> { await pMap({ iterable: [1], mapper: identityMapper, options: null as never, },); },); await swallowAsync(async function symbolBound(): Promise<void> { await pMap({ iterable: [1], mapper: identityMapper, options: { concurrency: Symbol('symbolic concurrency bound value',) as never, }, },); },); swallow(function invalidBackpressure() { pMapIterable({ iterable: [1], mapper: identityMapper, options: { concurrency: 2, backpressure: 1, }, },); },); swallow(function invalidStreamingOptions() { pMapIterable({ iterable: [1], mapper: identityMapper, options: null as never, },); },); swallow(function invalidStreamingConcurrency() { pMapIterable({ iterable: [1], mapper: identityMapper, options: { concurrency: 0, }, },); },); swallow(function invalidStreamingInput() { pMapIterable({ iterable: true as never, mapper: identityMapper, },); },); swallow(function invalidStreamingMapper() { pMapIterable({ iterable: [1], mapper: true as never, },); },);
/* oxlint-enable typescript/no-unsafe-type-assertion */
//endregion Option resolution
//region Concurrent map
/** Ordered results over a fixed input list. */ const ordered = await pMap({ iterable: MAP_INPUTS, mapper: identityMapper, options: { concurrency: 2, stopOnError: true, }, },); if ((ordered.length !== MAP_INPUTS.length) || (ordered[0] !== 0)) throw new Error('coverage driver map diverged; driver inputs are stale',);
await pMap({ iterable: MAP_INPUTS, mapper: promisedMapper, },); await pMap({ iterable: twoAsyncValues(), mapper: identityMapper, options: { concurrency: Number.POSITIVE_INFINITY, }, },); await pMap({ iterable: bothSlots(), mapper: identityMapper, },); /** Skip-staged results: the middle input is dropped. */ const skipped = await pMap({ iterable: MAP_INPUTS, mapper: droppingMapper, },); if ((skipped.length !== 2) || (skipped[0] !== 0)) throw new Error('coverage driver skip diverged; driver inputs are stale',); await pMap({ iterable: [1], mapper: droppingMapper, },);
await swallowAsync(async function rejectingMapper(): Promise<void> { await pMap({ iterable: [ 1, 2, ], mapper: failingMapper, },); },); await swallowAsync(async function throwingMapperRun(): Promise<void> { await pMap({ iterable: [1], mapper: throwingMapper, },); },); await swallowAsync(async function aggregatedFailures(): Promise<void> { await pMap({ iterable: [ 1, 2, ], mapper: failingMapper, options: { concurrency: 1, stopOnError: false, }, },); },); await swallowAsync(async function failingIterable(): Promise<void> { await pMap({ iterable: oneThenThrow(), mapper: identityMapper, options: { stopOnError: false, }, },); },);
//endregion Concurrent map
//region Abort signal
/** Controller whose signal aborts before the run starts. */ const earlyAbort = new AbortController(); earlyAbort.abort('stopped early',); await swallowAsync(async function abortedBeforeStart(): Promise<void> { await pMap({ iterable: [ 1, 2, ], mapper: identityMapper, options: { signal: earlyAbort.signal, }, },); },);
/** Controller whose signal aborts while the mapper is gated. */ const midAbort = new AbortController(); /** Deferred holding the mapper until the abort fires. */ const gate = Promise.withResolvers<void>(); /** Close counter for the aborted run's source. */ const abortCloseState: CloseState = { closes: 0, }; /** Run under abort observation. */ const aborting = pMap({ iterable: closeCounting({ closeState: abortCloseState, },), mapper: async function gatedMapper(value: number,): Promise<number> { await gate.promise; return value; }, options: { concurrency: 1, signal: midAbort.signal, }, },); midAbort.abort(); gate.resolve(); await swallowAsync(async function abortedMidRun(): Promise<void> { await aborting; },); // The close runs detached, exactly like upstream `p-map`; give it a turn // before observing the counter. await wait( 0, ); if (abortCloseState.closes < 1) throw new Error('coverage driver abort close diverged; driver inputs are stale',);
//endregion Abort signal
//region Streaming map
/** Streamed values over a fixed input list. */ const streamed: number[] = []; for await (const value of pMapIterable({ iterable: MAP_INPUTS, mapper: identityMapper, options: { concurrency: 2, backpressure: 2, }, })) streamed.push(value,); if (streamed.length !== MAP_INPUTS.length) throw new Error('coverage driver stream diverged; driver inputs are stale',);
/** Streamed values with the middle input dropped. */ const streamedSkips: number[] = []; for await (const value of pMapIterable({ iterable: MAP_INPUTS, mapper: droppingMapper, options: { concurrency: 1, backpressure: 1, }, })) streamedSkips.push(value,); if ((streamedSkips.length !== 2) || (streamedSkips[0] !== 0)) throw new Error('coverage driver stream skips diverged; driver inputs are stale',);
await swallowAsync(async function failingStream(): Promise<void> { for await (const value of pMapIterable({ iterable: [ 1, 2, ], mapper: failingMapper, options: { concurrency: 2, backpressure: 2, }, })) void value; },);
/** Close counter for the early-exit stream's source. */ const breakCloseState: CloseState = { closes: 0, }; for await (const value of pMapIterable({ iterable: closeCounting({ closeState: breakCloseState, },), mapper: identityMapper, options: { concurrency: 1, backpressure: 1, }, })) { void value; break; } // The close runs detached, exactly like upstream `p-map`; give it a turn // before observing the counter. await wait( 0, ); if (breakCloseState.closes < 1) throw new Error('coverage driver stream close diverged; driver inputs are stale',);
/** Stream over an asynchronous source, driven fully. */ const asyncStreamed: number[] = []; for await (const value of pMapIterable({ iterable: twoAsyncValues(), mapper: promisedMapper, options: { concurrency: Number.POSITIVE_INFINITY, }, })) asyncStreamed.push(value,); if (asyncStreamed.length !== 2) throw new Error('coverage driver async stream diverged; driver inputs are stale',);
//endregion Streaming map
/** Sentinel identity check keeping {@link pMapSkip} referenced. */ const skipHeld = pMapSkip; if ((typeof skipHeld) !== 'symbol') throw new Error('coverage driver skip sentinel diverged; driver inputs are stale',);}
//endregion Exercise
await exercise();