import { expect, expectAsync, tap } from '@push.rocks/tapbundle'; import * as webstream from '../ts_web/index.js'; tap.test('WebDuplexStream', async (toolsArg) => { const testDone = toolsArg.defer(); // Create a deferred object to control test completion. const inputUint8Array = new Uint8Array([1, 2, 3, 4, 5]); const stream = webstream.WebDuplexStream.fromUInt8Array(inputUint8Array); const reader = stream.readable.getReader(); let readUint8Array = new Uint8Array(); reader.read().then(function processText({ done, value }) { if (done) { expect(readUint8Array).toEqual(inputUint8Array); testDone.resolve(); // Correctly signal that the test is done. return; } readUint8Array = new Uint8Array([...readUint8Array, ...value]); return reader.read().then(processText); }); return testDone.promise; // Return the promise to properly wait for the test to complete. }); tap.test('should handle transform with a write function', async (toolsArg) => { const testDone = toolsArg.defer(); const input = [1, 2, 3, 4, 5]; const expectedOutput = [2, 4, 6, 8, 10]; const transformStream = new webstream.WebDuplexStream({ writeFunction: (chunk, { push }) => { push(chunk * 2); // Push the doubled number into the stream return Promise.resolve(); // Resolve the promise immediately }, }); const writableStream = transformStream.writable.getWriter(); const readableStream = transformStream.readable.getReader(); const output: number[] = []; // Process the text and resolve the test once done. const processText = async ({ done, value }) => { if (done) { expect(output).toEqual(expectedOutput); testDone.resolve(); // Resolve the deferred test once all values have been read. return; } if (value !== undefined) { output.push(value); } // Continue reading and processing. await readableStream.read().then(processText); }; // Start the read process before writing to the stream. readableStream.read().then(processText); // Sequentially write to the stream and close when done. for (const num of input) { await writableStream.write(num); } await writableStream.close(); return testDone.promise; // This will wait until the testDone is resolved before completing the test. }); tap.start();