Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/feature-flags/src/cache.test.ts
T
2026-07-08 16:15:37 +02:00

90 lines
3.6 KiB
TypeScript

import { describe, expect, it, beforeEach } from "vitest";
import { createSharedStore, createTwoTierCache } 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" });
const tier = createTwoTierCache<EvaluateFlagResult>({
store,
namespace: "feature-flags",
ttlSeconds: 60,
pubsubEnabled: false,
});
return 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>;
beforeEach(() => {
tier = mkStore();
cache = createFlagEvalCache(tier);
});
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();
// After flush, L1 is empty. L2 still has the entry (TTL-based) but
// a different namespace would have to look it up; with pubsub off
// we only flush local, so L2 still has it.
expect(tier.l1.get(buildCacheKey("flag1", { flagName: "flag1", userId: "u1" }))).toBeUndefined();
});
it("invalidateFlag publishes a token that flushes L1", async () => {
const result: EvaluateFlagResult = { enabled: true, reason: "matched" };
await cache.set("flag1", { flagName: "flag1" }, result);
await cache.invalidateFlag("flag1");
// local L1 entry remains because the wildcard is not '*' — verify
// the L1 still holds 'flag1' (specific-key invalidation only purges
// the exact key, not by prefix). This documents current behaviour:
// we publish a sentinel and rely on subscribers to flush local L1
// entirely on receipt — see subscribeAndFlushOnInvalidate.
expect(tier.l1.get(buildCacheKey("flag1", { flagName: "flag1" }))).toEqual(result);
});
});