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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579/** 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 { type CacheStorage, NonMethodDecorationError, NotMemoizedError, pMemoize, pMemoizeClear, pMemoizeDecorator, PrivateMethodDecorationError, UnclearableCacheError,} from '@monochromatic-dev/module-p-memoize-fork/ts';import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';
//region Fixtures
/** Monotonic counter answered on every wrapped invocation. */const counterState = { index: 0,};
/** Records cache method calls in order. */const cacheLog: string[] = [];
/** Backing store behind the recording cache. */const cacheEntries = new Map<string, number>();
/** Initializers registered by the decorator context double. */const initializers: ((this: unknown,) => void)[] = [];
/** Wrapped function ignoring its key and counting invocations. @param _key - Cache key ignored by the fixture. @returns Next invocation ordinal. */function counter(_key?: unknown,): number { return counterState.index++;}
/** Wrapped function throwing before it can return a promise. @param _key - Cache key ignored by the fixture. @throws Error on every call. */function syncThrower(_key: string,): Promise<never> { throw new Error('sync-boom',);}
/** Wrapped function rejecting every call. @param _key - Cache key ignored by the fixture. */function rejecting(_key: string,): Promise<never> { return Promise.reject(new Error('boom',),);}
/** Wrapped function that must never run for the unknown-function clear path. @returns Resolved constant. */function neverMemoized(): Promise<number> { return Promise.resolve(1,);}
/** Stringifies the first argument for the custom-key fixtures. @param args - Argument tuple of one call. @returns String form of the first argument. */function stringifyFirst(args: readonly unknown[],): string { /** First element of the argument tuple. */ const [firstArgument] = args; return String(firstArgument,);}
/** Stringifies the whole argument tuple for the JSON-key fixture. @param args - Argument tuple of one call. @returns JSON form of the argument tuple. */function stringifyAll(args: readonly unknown[],): string { return JSON.stringify(args,);}
/** Identity key for the WeakMap fixture. @param args - Argument tuple of one call. @returns First argument as an object key. */function identityKey(args: readonly unknown[],): object { /** First element of the argument tuple. */ const [firstArgument] = args; // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the WeakMap fixture keys by object identity, so the first argument must be narrowed from `unknown` return firstArgument as object;}
/** Denies every cache write. @returns `false` on every call. */function deny(): boolean { return false;}
/** Throws instead of deciding, exercising the predicate-failure path. @returns Never; every call throws. @throws Error on every call. */function explode(): boolean { throw new Error('predicate-boom',);}
/** Allows every cache write asynchronously. @param _value - Fulfilled value ignored by the predicate. @returns Resolved `true`. */function allow(_value: number,): Promise<boolean> { return Promise.resolve(true,);}
/** Async cache storage recording `has`, `get`, `set`, `delete`, and `clear`. */const recordingCache: CacheStorage<string, number> & { readonly clear: () => unknown;} = { has: function has(key: string,): Promise<boolean> { cacheLog.push(`has ${key}`,); return Promise.resolve(cacheEntries.has(key,),); }, // oxlint-disable-next-line no-restricted-syntax/no-nullish-union -- mirrors `CacheStorage.get` documented cache-miss `undefined` value get: function get(key: string,): Promise<number | undefined> { cacheLog.push(`get ${key}`,); return Promise.resolve(cacheEntries.get(key,),); }, set: function set( key: string, value: number, ): Promise<void> { cacheLog.push(`set ${key}`,); cacheEntries.set( key, value, ); return Promise.resolve(); }, delete: function remove(key: string,): boolean { cacheLog.push(`delete ${key}`,); return cacheEntries.delete(key,); }, clear: function clear(): void { cacheLog.push('clear',); cacheEntries.clear(); },};
/** Cache reporting every key present while reading `undefined`, exercising the has-hit get-miss path. */const alwaysHitCache: CacheStorage<string, number> = { has: function has(_key: string,): boolean { return true; }, get: function get(_key: string,): undefined { return undefined; }, set: function set( _key: string, _value: number, ): void { cacheLog.push('set unexpected',); }, delete: function remove(_key: string,): boolean { return false; },};
/** Synthetic method context the decorator receives. */const methodContext = { kind: 'method', private: false, name: 'counter', addInitializer: function addInitializer(initializer: (this: unknown,) => void,): void { initializers.push(initializer,); },};
/** Decorated method body bumping the receiver's counter. The `this` parameter is the receiver the decorator initializer installs the memoized method onto. @returns Counter value before the increment. */function decoratedMethod(this: { index: number;},): number { return this.index++;}
/** Instance shape the decorator initializer installs the memoized method onto. */type DecoratedInstance = { /** Counter incremented through the memoized method. */ index: number; /** Memoized method slot installed by the decorator initializer. */ counter: (call: { args: readonly unknown[]; },) => Promise<number>;};
/** Placeholder memoized method the decorator initializer replaces. @returns Resolved `-1` sentinel. */function uncinstalled(): Promise<number> { return Promise.resolve(-1,);}
/** Wrapped function for the WeakMap fixture. @param key - Identity key ignored by the fixture. @returns Resolved constant. */function keyed(key: object,): Promise<number> { void key; return Promise.resolve(1,);}
//endregion Fixtures
//region Driver
/** Settles one promise without letting its outcome propagate, for fixtures whose failures are the behavior under test. @param promise - Promise to settle silently. */async function settleSilently(promise: Promise<unknown>,): Promise<void> { await Promise.allSettled([ promise, ],);}
/** Runs one throwing fixture and records its failure text, for the error paths the driver exercises deliberately. @param action - Fixture expected to throw. */function captureThrow(action: () => void,): void { try { action(); } catch (error) { cacheLog.push(`threw ${caughtValueText(error,)}`,); }}
/** Runs every fixture sequence once; called at module load so the coverage report sees a single deterministic pass. */async function runDriver(): Promise<void> { /** Memoized function reading and writing the recording cache. */ const cached = pMemoize({ fn: counter, options: { cache: recordingCache, }, },); await cached({ args: ['k'], },); await cached({ args: ['k'], },);
/** Memoized function with a custom key and a denying predicate. */ const selective = pMemoize({ fn: counter, options: { cache: recordingCache, cacheKey: stringifyFirst, shouldCache: deny, }, },); await selective({ args: [1], },);
/** Memoized function whose predicate throws on every write. */ const poisoned = pMemoize({ fn: counter, options: { cache: recordingCache, shouldCache: explode, }, },); await settleSilently(poisoned({ args: ['p'], },),);
/** Memoized function whose predicate allows writes asynchronously. */ const awaited = pMemoize({ fn: counter, options: { cache: recordingCache, shouldCache: allow, }, },); await awaited({ args: ['w'], },);
/** Memoized function whose cache reports hits but reads `undefined`. */ const alwaysHit = pMemoize({ fn: counter, options: { cache: alwaysHitCache, }, },); await alwaysHit({ args: ['h'], },);
/** Argument pairs the JSON-keyed fixture's wrapped function observed. */ const recordedArguments: unknown[] = []; /** Memoized function keying by the whole argument tuple; its wrapped function is the memoizer's callback, so its two-parameter shape is dictated by the call protocol, and it records into the local list above. */ const jsonKeyed = pMemoize({ fn: function counterPair( _first: unknown, _second: unknown, ): number { recordedArguments.push( _first, _second, ); return counterState.index++; }, options: { cacheKey: stringifyAll, }, },); await jsonKeyed({ args: [ 1, 2, ], },); await jsonKeyed({ args: [ 1, 2, ], },);
/** Memoized function whose wrapped call rejects every time. */ const flaky = pMemoize({ fn: rejecting, },); await settleSilently(flaky({ args: ['r'], },),); await settleSilently(flaky({ args: ['r'], },),);
/** Memoized function with caching disabled: concurrent de-duplication. */ const uncached = pMemoize({ fn: counter, options: { cache: false, }, },); await Promise.all([ uncached({ args: ['u'], },), uncached({ args: ['u'], },), ],);
/** Memoized function with caching disabled whose wrapped call throws synchronously, exercising the retained in-flight replay. */ const uncachedThrower = pMemoize({ fn: syncThrower, options: { cache: false, }, },); /** First call's promise; the second call must join its in-flight entry. */ const replayed = uncachedThrower({ args: ['t'], },); await settleSilently(uncachedThrower({ args: ['t'], },),); await settleSilently(replayed,);
/** Memoized function over a WeakMap cache, cleared through `pMemoizeClear`. */ const weak = pMemoize({ fn: keyed, options: { cache: new WeakMap<object, number>(), cacheKey: identityKey, }, },); await weak({ args: [{}], },); captureThrow(function clearWeak(): void { pMemoizeClear(weak,); },);
// pMemoizeClear paths: success, disabled cache, unknown function. pMemoizeClear(cached,); captureThrow(function clearDisabled(): void { pMemoizeClear(pMemoize({ fn: counter, options: { cache: false, }, },),); },); captureThrow(function clearUnknown(): void { pMemoizeClear(neverMemoized,); },);
// Decorator: per-instance initialization. /** Decorator under test, applied through the synthetic context below. */ const decorate = pMemoizeDecorator(); /* oxlint-disable typescript/no-unsafe-type-assertion -- the synthetic decorator context is a test double for the runtime-supplied `ClassMethodDecoratorContext` */ /** Synthetic method context asserted into the decorator protocol slot. */ const methodContextDouble = methodContext as never; /* oxlint-enable typescript/no-unsafe-type-assertion */ decorate( decoratedMethod, methodContextDouble, ); /** Instance whose counter the memoized method bumps. */ const instance: DecoratedInstance = { index: 0, counter: uncinstalled, }; for (const initialize of initializers) initialize.call(instance,); await instance.counter({ args: [], },);
// Decorator validation failures. /* oxlint-disable typescript/no-unsafe-type-assertion -- non-method decorator context double for the `NonMethodDecorationError` path */ /** Non-method context double for the `NonMethodDecorationError` path. */ const getterContextDouble = { kind: 'getter', } as never; /* oxlint-enable typescript/no-unsafe-type-assertion */ captureThrow(function decorateGetter(): void { decorate( decoratedMethod, getterContextDouble, ); },); /* oxlint-disable typescript/no-unsafe-type-assertion -- private-method decorator context double for the `PrivateMethodDecorationError` path */ /** Private-method context double for the `PrivateMethodDecorationError` path. */ const privateContextDouble = { kind: 'method', private: true, } as never; /* oxlint-enable typescript/no-unsafe-type-assertion */ captureThrow(function decoratePrivate(): void { decorate( decoratedMethod, privateContextDouble, ); },);
// Error class construction, one per class. void new NotMemoizedError(); void new PrivateMethodDecorationError(); void new NonMethodDecorationError(); void new UnclearableCacheError();}
await runDriver();
//endregion Driver