Files
suaveplan/packages/networking/drive/drive-plugin-cache/tests/setup/fake-cache.ts
2026-05-27 23:47:53 +01:00

65 lines
2.0 KiB
TypeScript

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,
};
}
}