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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397/** 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 { blobUrl, createWorker, errorReporterSource, getMessageData, getResult, isNodeRuntime, makeAsynchronous, makeAsynchronousIterable, makeCallWorkerBody, makeIterableWorkerBody, nodeWorkerPreambleSource, workerConstructorFrom, workerThreadsSpecifier,} from '@monochromatic-dev/module-make-asynchronous-fork/ts';
//region Fixtures
/** Expected doubled value of the fixed call argument. */const DOUBLED_VALUE = 42;
/** Fixed call argument doubling to the expected value. */const DOUBLE_ARGUMENT = 21;
/** Reply id carried by the message-shape check. */const MESSAGE_REPLY_ID = 6;
/** Expected doubled value of the string `baseUrl` call. */const BASED_VALUE = 4;
/** Call argument doubling to the string-`baseUrl` value. */const BASED_ARGUMENT = 2;
/** Expected doubled value of the `URL` `baseUrl` call. */const BASED_URL_VALUE = 6;
/** Call argument doubling to the `URL`-`baseUrl` value. */const BASED_URL_ARGUMENT = 3;
/** Doubles one fixed value inside the driver worker. @param value - Value to double. @returns Doubled value. */function double(value: number,): number { return value * 2;}
/** Generator yielding two fixed values. @returns Yielded values before returning. */function * pair(): Generator<number> { yield 1; yield 2;}
/** Throwing function driving the failure paths. @throws Error on every call. */function fail(): never { throw new Error('coverage driver failed',);}
/** Falsy-throwing function driving the falsy-rejection path. The reply protocol keys on `error`, not the value, so falsy rejections must stay rejections. @throws 0 on every call. */function throwZero(): never { // oxlint-disable-next-line eslint/no-throw-literal, typescript/only-throw-error -- falsy worker rejections are the behavior under coverage; the reply protocol keys on `error`, not the value throw 0;}
/** Generator throwing after one value, driving the iterable failure path. @returns Yielded values before throwing. */function * failAfter(): Generator<number> { yield 1; throw new Error('coverage driver iterable failed',);}
//endregion Fixtures
//region Helpers
/** Runs a thunk that is expected to throw, swallowing every outcome so the driver keeps exercising remaining paths. Worker replies key on `error`, so falsy and non-`Error` rejections are expected driver traffic, not defects. @param thunk - Operation expected to throw. @example ```ts swallow(function bad() { getResult({ error: 1, id: 0, }); }); ``` */function swallow(thunk: () => void,): void { try { thunk(); } catch (swallowed) { void swallowed; }}
/** Awaits a thunk that is expected to reject, swallowing every outcome so the driver keeps exercising remaining paths. Worker replies key on `error`, so falsy and non-`Error` rejections are expected driver traffic, not defects. @param thunk - Async operation expected to reject. @example ```ts await swallowAsync(async function bad(): Promise<void> { await Promise.reject(new Error('boom',),); }); ``` */async function swallowAsync(thunk: () => Promise<void>,): Promise<void> { try { await thunk(); } catch (swallowed) { void swallowed; }}
//endregion Helpers
//region Exercise
/** Exercises host detection, worker sources, reply settlement, single calls (success, failure, falsy values, abort, unclonable arguments, baseUrl), iterations (success, failure, abort), and direct worker creation. @example ```ts await exercise(); ``` */async function exercise(): Promise<void> { //region Detection and sources
if (!isNodeRuntime()) throw new Error('coverage driver expects the Node.js runtime',); if (workerThreadsSpecifier() !== 'node:worker_threads') throw new Error('coverage driver specifier diverged; driver inputs are stale',); if (errorReporterSource.length === 0) throw new Error('coverage driver reporter diverged; driver inputs are stale',); if (!nodeWorkerPreambleSource.includes('node:worker_threads',)) throw new Error('coverage driver preamble diverged; driver inputs are stale',); if (!makeCallWorkerBody('function probe() {}',) .includes('function probe() {}',)) throw new Error('coverage driver call body diverged; driver inputs are stale',); if (!makeIterableWorkerBody('function * probe() {}',) .includes('function * probe() {}',)) throw new Error('coverage driver iterable body diverged; driver inputs are stale',); if (workerConstructorFrom({ Worker: globalThis.Worker, },) !== globalThis.Worker) throw new Error('coverage driver constructor diverged; driver inputs are stale',); /** Blob URL revoked after the check. */ const url = blobUrl('globalThis.postMessage({});',); URL.revokeObjectURL(url,);
//endregion Detection and sources
//region Settlement
/* oxlint-disable typescript/no-unsafe-type-assertion -- the driver asserts restored values against fixed inputs */ if ((getResult({ id: 1, output: DOUBLED_VALUE, },) as number) !== DOUBLED_VALUE) throw new Error('coverage driver result diverged; driver inputs are stale',); if ((getResult({ id: 2, output: 0, },) as number) !== 0) throw new Error('coverage driver falsy result diverged; driver inputs are stale',); /* oxlint-enable typescript/no-unsafe-type-assertion */ swallow(function falsyFailure() { getResult({ error: 0, id: 3, },); },); swallow(function namedFailure() { getResult({ error: new Error('boom',), errorName: 'UnicornError', id: 4, },); },); swallow(function propertiedFailure() { getResult({ error: new Error('boom',), errorProperties: { code: 'ENOENT', }, id: 5, },); },); /** Message payload read off the Node.js event shape. */ const message = getMessageData({ value: { id: MESSAGE_REPLY_ID, output: 1, }, },); if (message.id !== MESSAGE_REPLY_ID) throw new Error('coverage driver message diverged; driver inputs are stale',);
//endregion Settlement
//region Single calls
/** Doubled value through the wrapped function. */ const doubled = await makeAsynchronous({ fn: double, },)({ args: [DOUBLE_ARGUMENT], },); if (doubled !== DOUBLED_VALUE) throw new Error('coverage driver call diverged; driver inputs are stale',);
await swallowAsync(async function throwingCall(): Promise<void> { await makeAsynchronous({ fn: fail, },)({ args: [], },); },);
await swallowAsync(async function falsyCall(): Promise<void> { await makeAsynchronous({ fn: throwZero, },)({ args: [], },); },);
/** Falsy value resolving through the wrapped function. */ const zero = await makeAsynchronous({ // oxlint-disable-next-line unicorn/consistent-function-scoping -- distinct serialized fixtures per driver section; hoisting would share one source across sections fn: function identity(value: number,): number { return value; }, },)({ args: [0], },); if (zero !== 0) throw new Error('coverage driver falsy call diverged; driver inputs are stale',);
await swallowAsync(async function preAbortedCall(): Promise<void> { /** Controller aborted before the call starts. */ const controller = new AbortController(); controller.abort(new Error('Aborted',),); await makeAsynchronous({ fn: double, },) .withSignal(controller.signal,)({ args: [1], },); },);
await swallowAsync(async function unclonableCall(): Promise<void> { await makeAsynchronous({ // oxlint-disable-next-line unicorn/consistent-function-scoping -- distinct serialized fixtures per driver section; hoisting would share one source across sections fn: function identity(value: unknown,): unknown { return value; }, },)({ // oxlint-disable-next-line unicorn/consistent-function-scoping -- distinct serialized unclonable marker; hoisting would share it across driver sections args: [function unclonable(): void { throw new Error('unclonable marker',); },], },); },);
/** Call through the string `baseUrl` shape. */ const based = await makeAsynchronous({ fn: double, options: { baseUrl: 'file:///fixture.js', }, },)({ args: [BASED_ARGUMENT], },); if (based !== BASED_VALUE) throw new Error('coverage driver baseUrl call diverged; driver inputs are stale',);
/** Call through the `URL` `baseUrl` shape. */ const basedUrl = await makeAsynchronous({ fn: double, options: { baseUrl: new URL('file:///fixture.js',), }, },)({ args: [BASED_URL_ARGUMENT], },); if (basedUrl !== BASED_URL_VALUE) throw new Error('coverage driver URL call diverged; driver inputs are stale',);
//endregion Single calls
//region Iterations
/** Values drained through the wrapped generator. */ const values: unknown[] = []; for await (const value of makeAsynchronousIterable({ fn: pair, },)({ args: [], })) values.push(value,); if (values.length !== 2) throw new Error('coverage driver iteration diverged; driver inputs are stale',);
await swallowAsync(async function failingIteration(): Promise<void> { for await (const ignored of makeAsynchronousIterable({ fn: failAfter, },)({ args: [], })) void ignored; },);
await swallowAsync(async function preAbortedIteration(): Promise<void> { /** Controller aborted before iteration starts. */ const controller = new AbortController(); controller.abort(new Error('Aborted',),); for await (const ignored of makeAsynchronousIterable({ fn: pair, },) .withSignal(controller.signal,)({ args: [], })) void ignored; },);
//endregion Iterations
//region Direct worker
await swallowAsync(async function preAbortedWorker(): Promise<void> { /** Controller aborted before creation starts. */ const controller = new AbortController(); controller.abort(new Error('Aborted',),); await createWorker({ content: 'globalThis.postMessage({});', nodePreamble: nodeWorkerPreambleSource, signal: controller.signal, },); },);
/** Live worker answering one request, then terminated. */ const worker = await createWorker({ content: 'globalThis.onmessage = async ({data: {id}}) => { globalThis.postMessage({id, output: 1}); };', nodePreamble: nodeWorkerPreambleSource, },); /** Reply of the single request. */ const reply = await worker.request([],); if (reply.id !== 1) throw new Error('coverage driver worker diverged; driver inputs are stale',); worker.cleanup();
//endregion Direct worker}
//endregion Exercise
await exercise();