Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/utils.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import { expect } from '@std/expect';import { describe, it } from 'node:test';
import * as streams from './mod.ts';
async function* values(...items: number[]): AsyncIterableIterator<number> { for (const item of items) yield item;}
describe('stream adapters and limits', () => { it('batches by item count without changing order', async () => { const result = await Array.fromAsync(streams.batch(values(1, 2, 3, 4, 5), { maximumItems: 2 })); expect(result).toEqual([[1, 2], [3, 4], [5]]); expect(Object.isFrozen(result[0])).toBe(true); });
it('batches before crossing the configured byte estimate', async () => { const result = await Array.fromAsync(streams.batch(['aa', 'bbb', 'c'], { maximumBytes: 4, size: (value) => value.length, })); expect(result).toEqual([['aa'], ['bbb', 'c']]); });
it('rejects one oversized item instead of violating the advertised limit', async () => { await expect(Array.fromAsync(streams.batch(['large'], { maximumBytes: 4, size: (value) => value.length, }))).rejects.toThrow(streams.StreamLimitError); });
it('stops materialization at item and byte limits', async () => { await expect(streams.collect(values(1, 2, 3), { maximumItems: 2 })).rejects.toThrow(streams.StreamLimitError); await expect(streams.collect(['one', 'two'], { maximumBytes: 5, size: (value) => value.length, })).rejects.toThrow(streams.StreamLimitError); });
it('propagates cancellation and closes an async generator on failure', async () => { let closed = false; async function* source() { try { yield 1; yield 2; } finally { closed = true; } } const controller = new AbortController(); const batches = streams.batch(source(), { maximumItems: 1, signal: controller.signal }); expect((await batches.next()).value).toEqual([1]); controller.abort('stop batching'); await expect(batches.next()).rejects.toBe('stop batching'); expect(closed).toBe(true); });
it('uses ReadableStream iteration cancellation unless explicitly prevented', async () => { let cancelled = 0; const source = new ReadableStream<number>({ pull(controller) { controller.enqueue(1); }, cancel() { cancelled += 1; }, }); for await (const _ of streams.iterable(source)) break; expect(cancelled).toBe(1);
let preserved = 0; const retained = new ReadableStream<number>({ pull(controller) { controller.enqueue(1); }, cancel() { preserved += 1; }, }); for await (const _ of streams.iterable(retained, { preventCancel: true })) break; expect(preserved).toBe(0); await retained.cancel(); });
it('decodes UTF-8 lines across byte chunks and normalizes CRLF', async () => { const encoder = new TextEncoder(); const encoded = encoder.encode('alpha\r\nβeta\nfinal'); const source = new ReadableStream<Uint8Array>({ start(controller) { controller.enqueue(encoded.subarray(0, 8)); controller.enqueue(encoded.subarray(8, 10)); controller.enqueue(encoded.subarray(10)); controller.close(); }, }); expect(await Array.fromAsync(streams.lines(source))).toEqual(['alpha', 'βeta', 'final']); });
it('enforces a byte limit for each line instead of the complete stream', async () => { const encoder = new TextEncoder(); const source = new ReadableStream<Uint8Array>({ start(controller) { controller.enqueue(encoder.encode('1234\n12345')); controller.close(); }, }); const iterator = streams.lines(source, { maximumLineBytes: 4 }); expect((await iterator.next()).value).toBe('1234'); await expect(iterator.next()).rejects.toBeInstanceOf(streams.StreamLineLimitError); });
it('cancels the byte source when line iteration ends early', async () => { let cancelled = false; const source = new ReadableStream<Uint8Array>({ pull(controller) { controller.enqueue(new TextEncoder().encode('line\n')); }, cancel() { cancelled = true; }, }); for await (const _ of streams.lines(source)) break; expect(cancelled).toBe(true); });
it('pipes iterable values through native Web Stream pressure handling', async () => { const written: number[] = []; await streams.pipe( values(1, 2, 3), new WritableStream<number>({ write(value) { written.push(value); }, }), ); expect(written).toEqual([1, 2, 3]); });});