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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620/** Deterministic cache workload model and runner shared by this package's property files. A workload constructs a cache and applies generated operations with a controlled clock: storage and reads, bound changes, evictions, clears, and clock advances. Every operation's normalized result, the eviction events it fired, and a full state snapshot are recorded, so one cache fed one workload produces a directly comparable trace. @module */
import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';
import type { FakeClock, } from '@monochromatic-dev/module-quick-lru-fork/ts/test-support.ts';
//region Types
/** One generated cache operation. `set` relies on the cache's global `maxAge`; `setWithMaxAge` overrides the lifetime per item exactly as upstream's `set(key, value, {maxAge})` does. */export type CacheOp = | { /** Store a value under the global lifetime. */ readonly kind: 'set'; /** Key to store under. */ readonly key: string; /** Value to store. */ readonly value: string; } | { /** Store a value under a per-item lifetime. */ readonly kind: 'setWithMaxAge'; /** Key to store under. */ readonly key: string; /** Value to store. */ readonly value: string; /** Per-item lifetime in milliseconds, quirks included. */ readonly maxAge: number; } | { /** Read a value, marking it most recently used. */ readonly kind: 'get'; /** Key to read. */ readonly key: string; } | { /** Read a value without touching recency. */ readonly kind: 'peek'; /** Key to read. */ readonly key: string; } | { /** Check presence without touching recency. */ readonly kind: 'has'; /** Key to check. */ readonly key: string; } | { /** Remove one item without notifications. */ readonly kind: 'delete'; /** Key to remove. */ readonly key: string; } | { /** Read one item's remaining lifetime without removing it. */ readonly kind: 'expiresIn'; /** Key to inspect. */ readonly key: string; } | { /** Remove every item without notifications. */ readonly kind: 'clear'; } | { /** Update the target maximum in place. */ readonly kind: 'resize'; /** New target maximum, quirks included. */ readonly maxSize: number; } | { /** Evict the least recently used items. */ readonly kind: 'evict'; /** Eviction count, quirks included. */ readonly count: number; } | { /** Move the controlled clock forward. */ readonly kind: 'advanceClock'; /** Milliseconds to advance by. */ readonly milliseconds: number; };
/** Fully generated workload: constructor bounds and operations. */export type CacheWorkload = { /** Target maximum number of items. */ readonly maxSize: number; /** Global lifetime in milliseconds; `Number.POSITIVE_INFINITY` stands in for upstream's absent `maxAge`, which resolves to the same bound. */ readonly maxAge: number; /** Whether the cache carries an `onEviction` callback. */ readonly onEviction: boolean; /** Operations to apply, in order. */ readonly ops: readonly CacheOp[];};
/** One cache's observable state, projected to strings so both implementations compare structurally. */export type CacheSnapshot = { /** Reported item count. */ readonly size: string; /** Reported target maximum. */ readonly maxSize: string; /** Reported global lifetime. */ readonly maxAge: string; /** `entriesAscending` pairs, oldest first. */ readonly ascending: readonly string[]; /** `entries` pairs; upstream aliases `entriesAscending` here. */ readonly entries: readonly string[]; /** `entriesDescending` pairs, newest first within each map. */ readonly descending: readonly string[]; /** Default-iterator pairs, recent map before old map. */ readonly iterator: readonly string[]; /** Live keys in default-iterator order. */ readonly keys: readonly string[]; /** Live values in default-iterator order. */ readonly values: readonly string[]; /** Old map's raw key and item projections, in map order. */ readonly oldCache: readonly string[]; /** `toString()` output. */ readonly text: string;};
/** Observable trace of one operation. */export type StepTrace = { /** Operation label identifying its kind and target. */ readonly opLabel: string; /** Normalized result: the operation's value projection or its thrown failure. */ readonly result: string; /** Eviction events fired while the operation ran, in order. */ readonly evictions: readonly string[]; /** Full state snapshot taken right after the operation. */ readonly snapshot: CacheSnapshot;};
/** Observable trace of one workload run. */export type WorkloadTrace = { /** Per-operation traces, in application order. */ readonly steps: readonly StepTrace[];};
/** One cache's workload surface, normalized so the same workload drives this package's fork and upstream `quick-lru`. */export type CacheAdapter = { /** Stores one value, with an optional per-item lifetime. */ readonly set: (options: { /** Key to store under. */ readonly key: string; /** Value to store. */ readonly value: string; /** Per-item lifetime override. */ readonly maxAge?: number; },) => void; /** Reads one value, `absent` when the key is missing or expired. */ readonly get: (key: string,) => string; /** Reads one value without touching recency, `absent` when missing or expired. */ readonly peek: (key: string,) => string; /** Checks one key without touching recency. */ readonly has: (key: string,) => boolean; /** Removes one item without notifications. */ readonly remove: (key: string,) => boolean; /** Reads one item's remaining lifetime as `absent`, `infinite`, or `remaining:<milliseconds>`. */ readonly expiresIn: (key: string,) => string; /** Removes every item without notifications. */ readonly clear: () => void; /** Updates the target maximum in place. */ readonly resize: (maxSize: number,) => void; /** Evicts the least recently used items. */ readonly evict: (count: number,) => void; /** Projects the full observable state. */ readonly snapshot: () => CacheSnapshot; /** Eviction events fired so far, in order, collected by the `onEviction` callback installed at construction. */ readonly evictions: string[];};
//endregion Types
//region Projection
/** Renders one key and value pair the way every ordering projection does. @param key - Pair key. @param value - Pair value. @returns `key=value` text. @example ```ts renderPair('a', 'b'); // => 'a=b' ``` */export function renderPair({ key, value,}: { /** Pair key. */ readonly key: string; /** Pair value. */ readonly value: string;},): string { return `${key}=${value}`;}
/** Renders one cached item for the old-map projection. @param key - Item key. @param value - Item value. @param expiry - Item expiry stamp as reported by the cache internals. @returns `key=value@expiry` text. @example ```ts renderItem('a', 'b', undefined,); // => 'a=b@none' ``` */export function renderItem({ key, value, expiry,}: { /** Item key. */ readonly key: string; /** Item value. */ readonly value: string; /** Item expiry stamp as reported by the cache internals. */ readonly expiry: string;},): string { return `${key}=${value}@${(expiry === 'undefined') ? 'none' : expiry}`;}
/** Renders one thrown failure for comparison across implementations. Failures normalize on message text only: the fork's configuration errors name their own classes while keeping upstream's message text verbatim, so comparing names would flag the documented deviation instead of a real behavior change. Class shape is pinned by the runtime package's unit suites. @param reason - Failure observed by the runner. @returns Message of the failure, normalized for deep comparison. @mutates reason - Rendering a non-Error value runs JavaScript string conversion, which may invoke getters, proxies, `Symbol.toPrimitive`, `toString`, or `valueOf` through `caughtValueText`. @example ```ts describeThrown(new TypeError('bad',),); // => 'bad' ``` */export function describeThrown(reason: unknown,): string { if (Error.isError(reason,)) return reason.message;
return `ThrownValue: ${caughtValueText(reason,)}`;}
/** Labels one operation for trace comparison. @param op - Operation under label. @returns Stable label naming the operation's kind and target. @example ```ts labelFor({ kind: 'get', key: 'a', },); // => 'get a' ``` */export function labelFor(op: CacheOp,): string { if ((op.kind === 'set') || (op.kind === 'setWithMaxAge')) return `set ${op.key}`; if ((op.kind === 'get') || (op.kind === 'peek') || (op.kind === 'has') || (op.kind === 'delete') || (op.kind === 'expiresIn')) return `${op.kind} ${op.key}`; if (op.kind === 'resize') return `resize ${String(op.maxSize,)}`; if (op.kind === 'evict') return `evict ${String(op.count,)}`; if (op.kind === 'advanceClock') return `advance ${String(op.milliseconds,)}`; return op.kind;}
//endregion Projection
//region Runner
/** Applies one operation and projects its normalized result. @param adapter - Cache under test. @param op - Operation to apply. @param clock - Controlled clock the `advanceClock` operation moves. @returns Normalized result or thrown-failure text. @example ```ts applyOp(adapter, { kind: 'clear', }, clock,); // => 'void' ``` */export function applyOp({ adapter, op, clock,}: { /** Cache under test. */ readonly adapter: CacheAdapter; /** Operation to apply. */ readonly op: CacheOp; /** Controlled clock the `advanceClock` operation moves. */ readonly clock: FakeClock;},): string { try { if (op.kind === 'set') { adapter.set({ key: op.key, value: op.value, },); return 'void'; } if (op.kind === 'setWithMaxAge') { adapter.set({ key: op.key, value: op.value, maxAge: op.maxAge, },); return 'void'; } if (op.kind === 'get') return adapter.get(op.key,); if (op.kind === 'peek') return adapter.peek(op.key,); if (op.kind === 'has') return String(adapter.has(op.key,),); if (op.kind === 'delete') return String(adapter.remove(op.key,),); if (op.kind === 'expiresIn') return adapter.expiresIn(op.key,); if (op.kind === 'clear') { adapter.clear(); return 'void'; } if (op.kind === 'resize') { adapter.resize(op.maxSize,); return 'void'; } if (op.kind === 'evict') { adapter.evict(op.count,); return 'void'; } clock.advance(op.milliseconds,); return 'void'; } catch (error) { return `threw ${describeThrown(error,)}`; }}
/** Runs one workload against one cache adapter and records its trace. The clock is rewound to the workload's start instant first, so two adapters fed one workload observe identical time. @param options - Adapter, workload, clock, and starting instant. @returns Per-operation traces in application order. @example ```ts const trace = runWorkload({ adapter, workload, clock, startMilliseconds: 1_000, },); ``` */export function runWorkload(options: { /** Cache under test. */ readonly adapter: CacheAdapter; /** Generated constructor bounds and operations. */ readonly workload: CacheWorkload; /** Controlled clock shared by both adapters. */ readonly clock: FakeClock; /** Instant every workload run starts from. */ readonly startMilliseconds: number;},): WorkloadTrace { options.clock .advance(options.startMilliseconds - options.clock .now,); /** Per-operation traces collected below. */ const steps: StepTrace[] = []; /** How many eviction events the previous steps already recorded. */ const seen = { count: 0, }; for (const op of options.workload .ops) { /** Result of the operation, captured before the snapshot is taken. */ const result = applyOp({ adapter: options.adapter, op, clock: options.clock, },); /** Eviction events fired during this operation, in order. */ const evictions = options.adapter .evictions .slice(seen.count,); seen.count = options.adapter .evictions .length; steps.push({ opLabel: labelFor(op,), result, evictions, snapshot: options.adapter .snapshot(), },); }
return { steps, };}
//endregion Runner