Files
suaveplan/packages/networking/drive/drive-plugin-cache/tests/integration/with-namespaced-caches.test.ts
2026-05-27 23:47:53 +01:00

76 lines
2.7 KiB
TypeScript

import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { MemoryCache } from "@suaveplan/cache";
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: shared cache, distinct namespaces", () => {
let rootA: string;
let rootB: string;
let driveA: Drive;
let driveB: Drive;
beforeEach(async () => {
rootA = mkdtempSync(join(tmpdir(), "drive-plugin-cache-a-"));
rootB = mkdtempSync(join(tmpdir(), "drive-plugin-cache-b-"));
const storageA = Object.assign(
createNodeFsStorage({ rootDir: rootA, mkdirp: true }),
{ id: "default" }
);
const storageB = Object.assign(
createNodeFsStorage({ rootDir: rootB, mkdirp: true }),
{ id: "default" }
);
driveA = new Drive(storageA);
driveB = new Drive(storageB);
await driveA.initialize();
await driveB.initialize();
});
afterEach(async () => {
await driveA.dispose();
await driveB.dispose();
rmSync(rootA, { recursive: true, force: true });
rmSync(rootB, { recursive: true, force: true });
});
it("distinct namespaces isolate entries on a shared cache", {
timeout: 30000,
}, async () => {
const cache = new MemoryCache<Uint8Array>({ maxSize: 100 });
const handleA = await driveA.usePlugin(
driveCachePlugin({ cache, namespace: "a" })
);
const handleB = await driveB.usePlugin(
driveCachePlugin({ cache, namespace: "b" })
);
await driveA.write("clip.mov", new Uint8Array([1]));
await driveB.write("clip.mov", new Uint8Array([2]));
// Populate A's entry under namespace "a"
await driveA.read("clip.mov");
// B's read for the same path under namespace "b" must miss
await driveB.read("clip.mov");
expect(handleA.getStats().populations).toBe(1);
expect(handleB.getStats().populations).toBe(1);
expect(handleA.getStats().hits).toBe(0);
expect(handleB.getStats().hits).toBe(0);
// Verify keys are distinct on the shared cache
expect(await cache.get("a::clip.mov")).toBeDefined();
expect(await cache.get("b::clip.mov")).toBeDefined();
expect(Array.from((await cache.get("a::clip.mov")) ?? [])).toEqual([1]);
expect(Array.from((await cache.get("b::clip.mov")) ?? [])).toEqual([2]);
});
});