Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/feature-flags/src/cache.test.ts
T

151 lines
6.3 KiB
TypeScript

import { describe, expect, it, beforeEach } from "vitest";
import { createSharedStore, createTwoTierCache, MemoryAdapter } from "@hivegrid/shared-store";
import { buildCacheKey, createFlagEvalCache, hashContext } from "./cache.js";
import type { EvaluateFlagResult } from "./evaluate.js";
function mkStore() {
const store = createSharedStore({ namespace: "ff-test", adapter: new MemoryAdapter() });
const tier = createTwoTierCache<EvaluateFlagResult>({
store,
namespace: "feature-flags",
ttlSeconds: 60,
pubsubEnabled: false,
});
return { store, tier };
}
describe("hashContext", () => {
it("is stable for same input", () => {
const a = hashContext({ flagName: "x", tenantId: "t", userId: "u" });
const b = hashContext({ flagName: "x", tenantId: "t", userId: "u" });
expect(a).toBe(b);
});
it("differs across distinct contexts", () => {
const a = hashContext({ flagName: "x", tenantId: "t", userId: "u" });
const b = hashContext({ flagName: "x", tenantId: "t", userId: "v" });
expect(a).not.toBe(b);
});
it("ignores flagName (it's already part of the key prefix)", () => {
const a = hashContext({ flagName: "x", tenantId: "t" });
const b = hashContext({ flagName: "y", tenantId: "t" });
expect(a).toBe(b);
});
it("normalizes role order", () => {
const a = hashContext({ flagName: "x", roles: ["a", "b"] });
const b = hashContext({ flagName: "x", roles: ["b", "a"] });
expect(a).toBe(b);
});
});
describe("buildCacheKey", () => {
it("uses flag-name prefix + ctx-hash", () => {
const k = buildCacheKey("my-flag", { flagName: "my-flag", tenantId: "t" });
expect(k.startsWith("ff:my-flag:")).toBe(true);
});
});
describe("createFlagEvalCache", () => {
let cache: ReturnType<typeof createFlagEvalCache>;
let tier: ReturnType<typeof mkStore>["tier"];
beforeEach(() => {
const built = mkStore();
tier = built.tier;
cache = createFlagEvalCache(tier, built.store);
});
it("set then get returns the same value", async () => {
const result: EvaluateFlagResult = { enabled: true, reason: "matched" };
await cache.set("flag1", { flagName: "flag1", userId: "u1" }, result);
const got = await cache.get("flag1", { flagName: "flag1", userId: "u1" });
expect(got).toEqual(result);
});
it("returns null on miss", async () => {
const got = await cache.get("missing", { flagName: "missing" });
expect(got).toBeNull();
});
it("invalidateAll flushes local L1", async () => {
const result: EvaluateFlagResult = { enabled: true, reason: "matched" };
await cache.set("flag1", { flagName: "flag1", userId: "u1" }, result);
expect(await cache.get("flag1", { flagName: "flag1", userId: "u1" })).toEqual(result);
await cache.invalidateAll();
expect(await cache.get("flag1", { flagName: "flag1", userId: "u1" })).toBeNull();
});
it("invalidateFlag makes old L1 and L2 evaluations unreachable", async () => {
const result: EvaluateFlagResult = { enabled: true, reason: "matched" };
await cache.set("flag1", { flagName: "flag1" }, result);
await cache.invalidateFlag("flag1");
expect(await cache.get("flag1", { flagName: "flag1" })).toBeNull();
});
it("invalidates two replica caches without mixing tenant contexts", async () => {
const adapter = new MemoryAdapter();
const storeA = createSharedStore({ namespace: "ff-replicas", adapter });
const storeB = createSharedStore({ namespace: "ff-replicas", adapter });
const tierA = createTwoTierCache<EvaluateFlagResult>({
store: storeA,
namespace: "feature-flags",
ttlSeconds: 60,
});
const tierB = createTwoTierCache<EvaluateFlagResult>({
store: storeB,
namespace: "feature-flags",
ttlSeconds: 60,
});
const replicaA = createFlagEvalCache(tierA, storeA);
const replicaB = createFlagEvalCache(tierB, storeB);
const dispose = await tierB.subscribe((token) => replicaB.handleInvalidation(token));
const tenantA = { flagName: "billing.beta", tenantId: "tenant-a" };
const tenantB = { flagName: "billing.beta", tenantId: "tenant-b" };
await replicaA.set("billing.beta", tenantA, { enabled: true, reason: "matched" });
await replicaA.set("billing.beta", tenantB, { enabled: false, reason: "disabled" });
expect(await replicaB.get("billing.beta", tenantA)).toEqual({ enabled: true, reason: "matched" });
expect(await replicaB.get("billing.beta", tenantB)).toEqual({ enabled: false, reason: "disabled" });
await replicaA.invalidateFlag("billing.beta");
await Promise.resolve();
await Promise.resolve();
expect(await replicaB.get("billing.beta", tenantA)).toBeNull();
expect(await replicaB.get("billing.beta", tenantB)).toBeNull();
await replicaA.set("billing.beta", tenantA, { enabled: false, reason: "no-match" });
expect(await replicaB.get("billing.beta", tenantA)).toEqual({ enabled: false, reason: "no-match" });
await dispose();
await storeA.close();
});
it("self-heals from a missed pub/sub event after the generation TTL", async () => {
let now = 0;
const adapter = new MemoryAdapter({ now: () => now });
const storeA = createSharedStore({ namespace: "ff-missed-event", adapter });
const storeB = createSharedStore({ namespace: "ff-missed-event", adapter });
const tierA = createTwoTierCache<EvaluateFlagResult>({
store: storeA,
namespace: "feature-flags",
ttlSeconds: 60,
l1Now: () => now,
pubsubEnabled: false,
});
const tierB = createTwoTierCache<EvaluateFlagResult>({
store: storeB,
namespace: "feature-flags",
ttlSeconds: 60,
l1Now: () => now,
pubsubEnabled: false,
});
const replicaA = createFlagEvalCache(tierA, storeA, { generationTtlMs: 10, now: () => now });
const replicaB = createFlagEvalCache(tierB, storeB, { generationTtlMs: 10, now: () => now });
const context = { flagName: "billing.beta", tenantId: "tenant-a" };
await replicaA.set("billing.beta", context, { enabled: true, reason: "matched" });
expect(await replicaB.get("billing.beta", context)).toEqual({ enabled: true, reason: "matched" });
await replicaA.invalidateFlag("billing.beta");
expect(await replicaB.get("billing.beta", context)).toEqual({ enabled: true, reason: "matched" });
now = 11;
expect(await replicaB.get("billing.beta", context)).toBeNull();
await storeA.close();
});
});