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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312/** fast-check arbitraries generating cache workloads: constructor bounds, mixed operations over a small key universe, per-item lifetime quirks, and controlled clock advances. Also generates the junk constructor inputs the totality and validation properties feed. @module */
import { type Arbitrary, array, boolean, constant, constantFrom, integer, oneof, record,} from 'fast-check';
import type { CacheOp, CacheWorkload,} from './cache-workload.ts';
//region Atoms
/** Negative per-item lifetime: stamps expiry in the past so items expire on first touch. */const NEGATIVE_LIFETIME_MS = -5;
/** Fractional per-item lifetime exercising non-integer expiry stamps. */const FRACTIONAL_LIFETIME_MS = 1.5;
/** Short lifetime in milliseconds, smaller than the generated clock advances so expiry actually fires. */const SHORT_LIFETIME_MS = 500;
/** Long global lifetime in milliseconds, larger than the generated clock advances. */const LONG_LIFETIME_MS = 1_000;
/** Fractional eviction count exercising upstream's `Math.trunc` rounding. */const FRACTIONAL_EVICT_COUNT = 1.9;
/** Negative eviction count exercising the rejected-count gate. */const NEGATIVE_EVICT_COUNT = -1;
/** Key universe: four keys keep duplicate-key and both-map states reachable while staying enumerable in traces. */const KEY_POOL: readonly string[] = [ 'a', 'b', 'c', 'd',];
/** Value universe for stored items. */const VALUE_POOL: readonly string[] = [ 'v0', 'v1', 'v2',];
/** Per-item lifetimes, quirks included: negative and zero stamps, plain durations, an infinite one, and `NaN` whose stamp upstream treats as never-expires. */const ITEM_MAX_AGE_POOL: readonly number[] = [ NEGATIVE_LIFETIME_MS, 0, FRACTIONAL_LIFETIME_MS, 100, SHORT_LIFETIME_MS, Number.POSITIVE_INFINITY, Number.NaN,];
/** Global lifetimes, quirks included; `Number.POSITIVE_INFINITY` stands in for upstream's absent `maxAge`. */const GLOBAL_MAX_AGE_POOL: readonly number[] = [ 100, LONG_LIFETIME_MS, Number.POSITIVE_INFINITY, Number.NaN,];
/** Eviction counts, quirks included: zero, negative, fractional, `NaN`, and counts larger than the cache. */const EVICT_COUNT_POOL: readonly number[] = [ 0, 1, 2, 10, FRACTIONAL_EVICT_COUNT, NEGATIVE_EVICT_COUNT, Number.NaN, 100,];
/** Random key from {@link KEY_POOL}. */const keyArb: Arbitrary<string> = constantFrom(...KEY_POOL);
/** Random value from {@link VALUE_POOL}. */const valueArb: Arbitrary<string> = constantFrom(...VALUE_POOL);
/** Random operation over the key universe. */const opArb: Arbitrary<CacheOp> = oneof( record({ key: keyArb, value: valueArb, }) .map(function toSet(fields: { readonly key: string; readonly value: string; },): CacheOp { return { kind: 'set', key: fields.key, value: fields.value, }; },), record({ key: keyArb, value: valueArb, maxAge: constantFrom(...ITEM_MAX_AGE_POOL), }) .map(function toSetWithMaxAge(fields: { readonly key: string; readonly value: string; readonly maxAge: number; },): CacheOp { return { kind: 'setWithMaxAge', key: fields.key, value: fields.value, maxAge: fields.maxAge, }; },), keyArb.map(function toGet(key: string,): CacheOp { return { kind: 'get', key, }; },), keyArb.map(function toPeek(key: string,): CacheOp { return { kind: 'peek', key, }; },), keyArb.map(function toHas(key: string,): CacheOp { return { kind: 'has', key, }; },), keyArb.map(function toDelete(key: string,): CacheOp { return { kind: 'delete', key, }; },), keyArb.map(function toExpiresIn(key: string,): CacheOp { return { kind: 'expiresIn', key, }; },), constant({ kind: 'clear', } satisfies CacheOp), oneof( integer({ min: 1, max: 6, }), constantFrom( 0, -1, Number.NaN, ), ) .map(function toResize(maxSize: number,): CacheOp { return { kind: 'resize', maxSize, }; },), constantFrom(...EVICT_COUNT_POOL) .map(function toEvict(count: number,): CacheOp { return { kind: 'evict', count, }; },), integer({ min: 0, max: 2_000, }) .map(function toAdvanceClock(milliseconds: number,): CacheOp { return { kind: 'advanceClock', milliseconds, }; },),);
//endregion Atoms
//region Workload
/** Generated workload: one to twelve operations over four keys, a target maximum of one to four items, a global lifetime from the quirk pool, and an eviction-callback toggle. */export const workloadArb: Arbitrary<CacheWorkload> = record({ maxSize: integer({ min: 1, max: 4, }), maxAge: constantFrom(...GLOBAL_MAX_AGE_POOL), onEviction: boolean(), ops: array( opArb, { minLength: 1, maxLength: 12, }, ),}) .map(function toWorkload(fields: { readonly maxSize: number; readonly maxAge: number; readonly onEviction: boolean; readonly ops: readonly CacheOp[]; },): CacheWorkload { return fields; },);
//endregion Workload
//region Junk inputs
/** Junk constructor values: nullish, numeric, string, boolean, array, and object shapes that must each land on exactly one upstream failure mode. */const JUNK_POOL: readonly unknown[] = [ undefined, null, 0, -1, Number.NaN, Number.POSITIVE_INFINITY, 1, 'x', '', true, false, [], {},];
/** Random junk constructor input, either a bare junk value or an options object with junk `maxSize` and `maxAge` fields. */export const junkInputArb: Arbitrary<unknown> = oneof( constantFrom(...JUNK_POOL), record({ maxSize: constantFrom(...JUNK_POOL), maxAge: constantFrom(...JUNK_POOL), }) .map(function toJunkOptions(fields: { readonly maxSize: unknown; readonly maxAge: unknown; },): unknown { return fields; },),);
//endregion Junk inputs