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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450/** Tests for source iterator selection and shutdown, exercised through the public `pMap` and `pMapIterable` entry points (iterator handling is package-internal). @module */
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { InvalidInputError, pMap, pMapIterable,} from '../dist/final/neutral/index.mjs';
import { createGate, yieldTurn,} from './test-support.ts';
//region Fixtures
/** Telemetry recorded by the tracked iterable fixtures. */type TrackingState = { /** Whether the source's `return` method ran. */ returnCalled: boolean;};
/** Builds a synchronous iterable of `1..count` whose `return` method records its invocation into the supplied telemetry. @param bounds - How many values to yield and where to record the close. @returns Iterable whose shutdown is observable. @example ```ts const iterable = countingIterable({ count: 2, tracking: { returnCalled: false, }, }); ``` */function countingIterable( { count, tracking, }: { /** Number of values yielded before `done`. */ readonly count: number; /** Telemetry the close request writes to. */ readonly tracking: TrackingState; },): Iterable<number> { return { [Symbol.iterator]: function openCountingIterator(): Iterator<number> { /** Pull cursor shared by this iterator's `next` calls. */ const cursor = { position: 0, }; return { next: function countingNext(): IteratorResult<number> { if (cursor.position >= count) return { done: true, value: undefined, }; cursor.position += 1; return { done: false, value: cursor.position, }; }, return: function countingReturn(): IteratorResult<number> { tracking.returnCalled = true; return { done: true, value: undefined, }; }, }; }, };}
/** Builds a source carrying both iterator slots with distinguishable values, so selection precedence is observable. @returns Input whose async slot yields `2` and sync slot yields `1`. @example ```ts const results = await pMap({ iterable: bothSlotsIterable(), mapper, }); ``` */function bothSlotsIterable(): { readonly [Symbol.iterator]: () => Iterator<number>; readonly [Symbol.asyncIterator]: () => AsyncIterator<number>;} { return { [Symbol.iterator]: function openSyncSlot(): Iterator<number> { const cursor = { position: 0, }; return { next: function syncNext(): IteratorResult<number> { if (cursor.position >= 1) return { done: true, value: undefined, }; cursor.position += 1; return { done: false, value: 1, }; }, }; }, [Symbol.asyncIterator]: function openAsyncSlot(): AsyncIterator<number> { const cursor = { position: 0, }; return { next: async function asyncNext(): Promise<IteratorResult<number>> { if (cursor.position >= 1) return { done: true, value: undefined, }; cursor.position += 1; return { done: false, value: 2, }; }, }; }, };}
//endregion Fixtures
/** Builds a synchronous iterable of one value whose `return` method records its invocation and then throws, exercising the swallowed close failure. @param tracking - Telemetry the close request writes to. @returns Iterable whose shutdown fails loudly but harmlessly. @example ```ts const iterable = closeFailingIterable({ returnCalled: false, }); ``` */function closeFailingIterable(tracking: TrackingState,): Iterable<number> { return { [Symbol.iterator]: function openCloseFailingIterator(): Iterator<number> { const cursor = { position: 0, }; return { next: function closeFailingNext(): IteratorResult<number> { if (cursor.position >= 1) return { done: true, value: undefined, }; cursor.position += 1; return { done: false, value: 1, }; }, return: function closeFailingReturn(): IteratorResult<number> { tracking.returnCalled = true; throw new Error('close failed',); }, }; }, };}
await describe({ name: 'input and iterator handling', children: [ describe({ name: 'input validation', children: [ it({ name: 'accepts a synchronous iterable', fn: async () => { const results = await pMap({ iterable: [ 4, 5, ], mapper: function identity(value: number,): number { return value; }, },); expect(results,).toEqual([ 4, 5, ],); }, },),
it({ name: 'accepts an asynchronous iterable', fn: async () => { /** Asynchronous source yielding two values. */ const asyncIterable: AsyncIterable<number> = { [Symbol.asyncIterator]: function openAsyncIterable(): AsyncIterator<number> { const cursor = { position: 0, }; return { next: async function asyncNext(): Promise<IteratorResult<number>> { if (cursor.position >= 2) return { done: true, value: undefined, }; cursor.position += 1; return { done: false, value: cursor.position, }; }, }; }, }; const results = await pMap({ iterable: asyncIterable, mapper: function identity(value: number,): number { return value; }, },); expect(results,).toEqual([ 1, 2, ],); }, },),
it({ name: 'rejects a non-iterable input with upstream p-map message text', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: 5 as never, mapper: function identity(value: number,): number { return value; }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(InvalidInputError,); expect((caught as Error).message,).toBe('Expected `input` to be either an `Iterable` or `AsyncIterable`, got (number)',); }, },),
it({ name: 'rejects a null input from the probe itself, like upstream property access', fn: async () => { /** Failure observed from the run's promise. */ let caught: unknown; try { await pMap({ iterable: null as never, mapper: function identity(value: number,): number { return value; }, },); } catch (error) { caught = error; } expect(caught,).toBeInstanceOf(TypeError,); expect((caught as Error).message,).toContain('is not iterable',); }, },), ], },),
describe({ name: 'iterator selection', children: [ it({ name: 'prefers the asynchronous slot when the input carries both', fn: async () => { const results = await pMap({ iterable: bothSlotsIterable(), mapper: function identity(value: number,): number { return value; }, },); expect(results,).toEqual([2],); }, },), ], },),
describe({ name: 'iterator shutdown', children: [ it({ name: 'closes the source when the run rejects before the source is done', fn: async () => { /** Close telemetry for this source. */ const tracking: TrackingState = { returnCalled: false, }; /** Gate holding the single mapper call until the test releases it. */ const gate = createGate(); /** Run promise observed for its rejection. */ const running = pMap({ iterable: countingIterable({ count: 2, tracking, },), mapper: async function gatedFailure(value: number,): Promise<never> { await gate.open; throw new Error(`mapper failed ${String(value,)}`,); }, options: { concurrency: 1, stopOnError: true, }, },); 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 1',); await yieldTurn(); expect(tracking.returnCalled,).toBe(true,); }, },),
it({ name: 'leaves an exhausted source open rather than closing it twice', fn: async () => { /** Close telemetry for this source. */ const tracking: TrackingState = { returnCalled: false, }; await pMap({ iterable: countingIterable({ count: 2, tracking, },), mapper: function identity(value: number,): number { return value; }, },); await yieldTurn(); expect(tracking.returnCalled,).toBe(false,); }, },),
it({ name: 'swallows a source close failure so the run rejection is untouched', fn: async () => { /** Close telemetry for this source; its `return` throws. */ const tracking: TrackingState = { returnCalled: false, }; /** Gate holding the single mapper call until the test releases it. */ const gate = createGate(); /** Run promise observed for its rejection. */ const running = pMap({ iterable: closeFailingIterable(tracking,), mapper: async function gatedFailure(): Promise<never> { await gate.open; throw new Error('mapper failed',); }, options: { concurrency: 1, }, },); 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(tracking.returnCalled,).toBe(true,); }, },), ], },), ],},);