// Test-side IO for the sharded session cache: the on-disk form is a directory // (envelope + one shard per provider), so tests read and write it through the // real load/save path instead of touching a single JSON file. import { readFile, readdir } from 'fs/promises' import { join } from 'path' import { clearLoadCacheMemo, loadCache, markCacheDirty, saveCache, sessionCacheDir, type SessionCache, } from '../../src/session-cache.js' /** The cache exactly as it is on disk, bypassing the in-process memo. */ export async function readCacheOnDisk(): Promise { clearLoadCacheMemo() return loadCache() } /** Publish `cache`, rewriting every provider's shard. */ export async function writeCacheOnDisk(cache: SessionCache): Promise { for (const provider of Object.keys(cache.providers)) markCacheDirty(cache, provider) await saveCache(cache) clearLoadCacheMemo() } /** Byte-level snapshot of the whole cache directory (names + contents). */ export async function cacheDirSnapshot(): Promise { const dir = sessionCacheDir() const names = (await readdir(dir)).sort() const parts = await Promise.all(names.map(async name => `${name}:${await readFile(join(dir, name), 'utf-8')}`)) return parts.join('\n') }