Files
2026-05-27 23:47:53 +01:00

71 lines
2.4 KiB
TypeScript

import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { IdentityCodec, StorageCache } from "@suaveplan/cache-storage";
import { Drive } from "@suaveplan/drive";
import { createNodeFsStorage } from "@suaveplan/storage-file";
import {
afterEach,
beforeEach,
describe,
expect,
it,
} from "@suaveplan/testing/runner";
import { driveCachePlugin } from "../../src/plugin/plugin.js";
describe("drive-plugin-cache integration: StorageCache over storage-file", () => {
let driveRoot: string;
let cacheRoot: string;
beforeEach(() => {
driveRoot = mkdtempSync(join(tmpdir(), "drive-plugin-cache-drive-"));
cacheRoot = mkdtempSync(join(tmpdir(), "drive-plugin-cache-disk-"));
});
afterEach(() => {
rmSync(driveRoot, { recursive: true, force: true });
rmSync(cacheRoot, { recursive: true, force: true });
});
it("disk-backed cache survives plugin reinstall", {
timeout: 30000,
}, async () => {
const fileStorage = createNodeFsStorage({
rootDir: driveRoot,
mkdirp: true,
});
const storage = Object.assign(fileStorage, { id: "default" });
const drive = new Drive(storage);
await drive.initialize();
const cacheStorage = createNodeFsStorage({
rootDir: cacheRoot,
mkdirp: true,
});
const cache1 = new StorageCache<Uint8Array>({
storage: cacheStorage,
codec: new IdentityCodec(),
});
const handle1 = await drive.usePlugin(
driveCachePlugin({ cache: cache1 })
);
const payload = new Uint8Array([10, 20, 30, 40]);
await drive.write("clip.bin", payload);
await drive.read("clip.bin"); // miss + populate
expect(handle1.getStats().populations).toBe(1);
// Build a fresh plugin/cache instance pointing at the same on-disk
// cache directory. The previously-populated entry must rehydrate.
const cache2 = new StorageCache<Uint8Array>({
storage: cacheStorage,
codec: new IdentityCodec(),
});
const fresh = await cache2.get("drive::clip.bin");
expect(fresh).toBeDefined();
expect(Array.from(fresh ?? new Uint8Array())).toEqual([10, 20, 30, 40]);
await drive.dispose();
});
});