Initial Commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import type { CacheSetOptions, CacheStats } from "@suaveplan/cache";
|
||||
import { AbstractCache } from "@suaveplan/cache";
|
||||
|
||||
/**
|
||||
* In-memory `AbstractCache<Uint8Array>` used by every middleware/handle/plugin
|
||||
* unit test. Tracks every call shape (key, value, signal) so tests can assert
|
||||
* the exact arguments the middleware forwarded into the cache surface.
|
||||
*
|
||||
* This is a real class, not a mock — no `vi.mock`/`jest.mock` involved.
|
||||
*/
|
||||
export class FakeCache extends AbstractCache<Uint8Array> {
|
||||
public store = new Map<string, Uint8Array>();
|
||||
public getCalls: { key: string; signal?: AbortSignal }[] = [];
|
||||
public setCalls: { key: string; value: Uint8Array }[] = [];
|
||||
public deleteCalls: string[] = [];
|
||||
public clearCalls = 0;
|
||||
public throwOnGet: Error | undefined;
|
||||
public throwOnSet: Error | undefined;
|
||||
public throwOnDelete: Error | undefined;
|
||||
|
||||
public async get(
|
||||
key: string,
|
||||
signal?: AbortSignal
|
||||
): Promise<Uint8Array | undefined> {
|
||||
this.getCalls.push({ key, signal });
|
||||
if (this.throwOnGet !== undefined) throw this.throwOnGet;
|
||||
return this.store.get(key);
|
||||
}
|
||||
|
||||
public async set(
|
||||
key: string,
|
||||
value: Uint8Array,
|
||||
_options?: CacheSetOptions
|
||||
): Promise<void> {
|
||||
this.setCalls.push({ key, value });
|
||||
if (this.throwOnSet !== undefined) throw this.throwOnSet;
|
||||
this.store.set(key, value);
|
||||
}
|
||||
|
||||
public async delete(key: string): Promise<boolean> {
|
||||
this.deleteCalls.push(key);
|
||||
if (this.throwOnDelete !== undefined) throw this.throwOnDelete;
|
||||
return this.store.delete(key);
|
||||
}
|
||||
|
||||
public async clear(): Promise<void> {
|
||||
this.clearCalls += 1;
|
||||
this.store.clear();
|
||||
}
|
||||
|
||||
public async has(key: string): Promise<boolean> {
|
||||
return this.store.has(key);
|
||||
}
|
||||
|
||||
public getStats(): CacheStats {
|
||||
return {
|
||||
hits: 0,
|
||||
misses: 0,
|
||||
size: this.store.size,
|
||||
hitRate: 0,
|
||||
evictions: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user