/** * Edge-path coverage for `evaluateFlag`: * - variant selection fallbacks (even distribution, invalid weights, * tenant/anonymous bucketing, malformed variant entries), * - defensive `parseRule` handling of malformed stored rules, * - parent time-window gating and explicit `maxDepth`. * * These assert the engine fails *closed* on dependency problems and * fails *open* (flag stays on) when the stored rules JSON is junk — * matching the documented contract in evaluate.ts. */ import { describe, expect, it } from "vitest"; import { evaluateFlag, getFeatureFlagVariant, type StoredFeatureFlag } from "./evaluate.js"; function mkFlag(overrides: Partial = {}): StoredFeatureFlag { return { id: overrides.id ?? "flag_1", name: overrides.name ?? "test-flag", enabled: overrides.enabled ?? true, rules: overrides.rules ?? null, variants: overrides.variants ?? [], enabledFrom: overrides.enabledFrom ?? null, enabledUntil: overrides.enabledUntil ?? null, parentFlagId: overrides.parentFlagId ?? null, }; } function mkLoader(flags: StoredFeatureFlag[]) { const byKey = new Map(); for (const f of flags) { byKey.set(f.name, f); byKey.set(f.id, f); } return async (key: string) => byKey.get(key) ?? null; } describe("evaluateFlag — variant weighting fallbacks", () => { it("all-zero weights → even distribution, still sticky per user", async () => { const variants = [ { key: "A", weight: 0 }, { key: "B", weight: 0 }, { key: "C", weight: 0 }, ]; const flag = mkFlag({ name: "even", variants }); const r1 = await evaluateFlag("even", { flagName: "even", userId: "u_77" }, { load: mkLoader([flag]) }); const r2 = await evaluateFlag("even", { flagName: "even", userId: "u_77" }, { load: mkLoader([flag]) }); expect(r1.variant?.key).toMatch(/^[ABC]$/); expect(r1.variant?.key).toBe(r2.variant?.key); }); it("negative weights are coerced to zero so that variant is never picked", async () => { const variants = [ { key: "loser", weight: -5 }, { key: "winner", weight: 3 }, ]; const flag = mkFlag({ name: "neg", variants }); // With one weight clamped to 0, the whole probability mass sits on // "winner" — deterministically chosen regardless of bucket key. for (const userId of ["a", "b", "c", "zzz", "user_999"]) { const r = await evaluateFlag("neg", { flagName: "neg", userId }, { load: mkLoader([flag]) }); expect(r.variant?.key).toBe("winner"); } }); it("non-finite weights (NaN / Infinity) are coerced to zero", async () => { const variants = [ { key: "nan", weight: Number.NaN }, { key: "inf", weight: Number.POSITIVE_INFINITY }, { key: "real", weight: 4 }, ]; const flag = mkFlag({ name: "nonfinite", variants }); const r = await evaluateFlag("nonfinite", { flagName: "nonfinite", userId: "x" }, { load: mkLoader([flag]) }); expect(r.variant?.key).toBe("real"); }); it("buckets by tenantId when no userId is present", async () => { const variants = [ { key: "A", weight: 1 }, { key: "B", weight: 1 }, ]; const flag = mkFlag({ name: "tenant-bucket", variants }); const r1 = await evaluateFlag("tenant-bucket", { flagName: "tenant-bucket", tenantId: "t_5" }, { load: mkLoader([flag]) }); const r2 = await evaluateFlag("tenant-bucket", { flagName: "tenant-bucket", tenantId: "t_5" }, { load: mkLoader([flag]) }); expect(r1.variant?.key).toMatch(/^[AB]$/); expect(r1.variant?.key).toBe(r2.variant?.key); }); it("buckets by flag name for anonymous traffic (no userId, no tenantId)", async () => { const variants = [ { key: "A", weight: 1 }, { key: "B", weight: 1 }, ]; const flag = mkFlag({ name: "anon", variants }); const r1 = await evaluateFlag("anon", { flagName: "anon" }, { load: mkLoader([flag]) }); const r2 = await evaluateFlag("anon", { flagName: "anon" }, { load: mkLoader([flag]) }); expect(r1.variant?.key).toMatch(/^[AB]$/); // Anonymous traffic must collapse to a single deterministic variant. expect(r1.variant?.key).toBe(r2.variant?.key); }); it("skips malformed variant entries; none valid → enabled with no variant", async () => { const variants = [null, 42, "nope", { weight: 1 }, { value: "x" }]; const flag = mkFlag({ name: "junk-variants", variants }); const r = await evaluateFlag("junk-variants", { flagName: "junk-variants", userId: "u" }, { load: mkLoader([flag]) }); expect(r).toEqual({ enabled: true, reason: "matched" }); }); it("filters junk entries but keeps the single valid variant", async () => { const variants = [null, { key: "only", value: { n: 1 } }, 99]; const flag = mkFlag({ name: "one-valid", variants }); const r = await evaluateFlag("one-valid", { flagName: "one-valid", userId: "u" }, { load: mkLoader([flag]) }); expect(r.variant?.key).toBe("only"); expect(r.variant?.value).toEqual({ n: 1 }); }); }); describe("evaluateFlag — malformed stored rules fail open", () => { it("treats a non-object rules value as no-rules (flag stays on)", async () => { const r = await evaluateFlag( "f", { flagName: "f" }, { load: mkLoader([mkFlag({ name: "f", rules: "garbage-string" })]) }, ); expect(r).toEqual({ enabled: true, reason: "matched" }); }); it("treats an object without a `type` field as no-rules", async () => { const r = await evaluateFlag( "f", { flagName: "f" }, { load: mkLoader([mkFlag({ name: "f", rules: { foo: "bar" } })]) }, ); expect(r).toEqual({ enabled: true, reason: "matched" }); }); it("treats an explicit undefined rules value as no-rules", async () => { const r = await evaluateFlag( "f", { flagName: "f" }, { load: mkLoader([mkFlag({ name: "f", rules: undefined })]) }, ); expect(r).toEqual({ enabled: true, reason: "matched" }); }); }); describe("evaluateFlag — parent time-window gating", () => { it("parent enabledFrom in the future → child parent-disabled", async () => { const parent = mkFlag({ id: "p", name: "p", enabledFrom: new Date("2030-01-01T00:00:00Z") }); const child = mkFlag({ id: "c", name: "c", parentFlagId: "p" }); const r = await evaluateFlag( "c", { flagName: "c", now: new Date("2026-01-01T00:00:00Z") }, { load: mkLoader([parent, child]) }, ); expect(r.reason).toBe("parent-disabled"); expect(r.enabled).toBe(false); }); it("parent enabledUntil in the past → child parent-disabled", async () => { const parent = mkFlag({ id: "p", name: "p", enabledUntil: new Date("2020-01-01T00:00:00Z") }); const child = mkFlag({ id: "c", name: "c", parentFlagId: "p" }); const r = await evaluateFlag( "c", { flagName: "c", now: new Date("2026-01-01T00:00:00Z") }, { load: mkLoader([parent, child]) }, ); expect(r.reason).toBe("parent-disabled"); }); }); describe("evaluateFlag — explicit maxDepth guard", () => { it("a chain deeper than maxDepth fails closed as parent-disabled", async () => { const a = mkFlag({ id: "a", name: "a", parentFlagId: "b" }); const b = mkFlag({ id: "b", name: "b", parentFlagId: "c" }); const c = mkFlag({ id: "c", name: "c" }); const r = await evaluateFlag( "a", { flagName: "a" }, { load: mkLoader([a, b, c]), maxDepth: 1 }, ); expect(r.reason).toBe("parent-disabled"); expect(r.enabled).toBe(false); }); }); describe("getFeatureFlagVariant — no-variant matched flag", () => { it("returns null when the flag is enabled but has no variants", async () => { const v = await getFeatureFlagVariant( "novar", { flagName: "novar", userId: "u" }, { load: mkLoader([mkFlag({ name: "novar", variants: [] })]) }, ); expect(v).toBeNull(); }); });