import { describe, expect, it } from "vitest"; import { createToolcraftBoundedWorkerCache, createToolcraftWorkerCacheCoordinator, } from "./bounded-worker-cache"; describe("aggregate worker cache budget", () => { it("evicts globally across decode, analysis, and repair caches", () => { const coordinator = createToolcraftWorkerCacheCoordinator({ maxAggregateBytes: 10, }); const cache = () => createToolcraftBoundedWorkerCache({ coordinator, maxEntries: 4, sizeOf: (value) => value.length, }); const decode = cache(); const analysis = cache(); const repair = cache(); decode.set("decode", "123456"); expect(coordinator.retainedBytes()).toBe(6); analysis.set("analysis", "123456"); expect(coordinator.retainedBytes()).toBe(6); expect(decode.get("decode")).toBeUndefined(); expect(analysis.get("analysis")).toBe("123456"); repair.set("repair", "123456"); expect(coordinator.retainedBytes()).toBe(6); expect(analysis.get("analysis")).toBeUndefined(); expect(repair.get("repair")).toBe("123456"); expect(coordinator.retainedBytes()).toBe(6); expect(coordinator.retainedBytes()).toBeLessThanOrEqual( coordinator.maxAggregateBytes, ); }); it.each([Infinity, Number.NaN, 0, -1, Number.MAX_SAFE_INTEGER + 1])( "rejects invalid aggregate byte budget %s", (maxAggregateBytes) => { expect(() => createToolcraftWorkerCacheCoordinator({ maxAggregateBytes, })).toThrow(/finite positive safe integer/u); }, ); });