-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature-flags.test.ts
More file actions
97 lines (84 loc) · 3.84 KB
/
Copy pathfeature-flags.test.ts
File metadata and controls
97 lines (84 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
describe("feature-flags", () => {
const originalEnv = process.env;
beforeEach(() => {
vi.resetModules();
process.env = { ...originalEnv };
// Strip all feature-flag and maintenance-related env vars
for (const key of Object.keys(process.env)) {
if (
key.startsWith("NEXT_PUBLIC_FEATURE_") ||
key === "NEXT_PUBLIC_MAINTENANCE_BANNER"
) {
delete process.env[key];
}
}
});
afterEach(() => {
process.env = originalEnv;
});
describe("isFeatureEnabled", () => {
it("returns the code default (true) when no env override is set", async () => {
const { isFeatureEnabled } = await import("@/lib/feature-flags");
expect(isFeatureEnabled("csv_import")).toBe(true);
expect(isFeatureEnabled("pipeline")).toBe(true);
expect(isFeatureEnabled("bulk_actions")).toBe(true);
});
it("returns the code default (false) when a flag is explicitly off in code", async () => {
process.env.NEXT_PUBLIC_FEATURE_BULK_ACTIONS = "false";
const { isFeatureEnabled } = await import("@/lib/feature-flags");
expect(isFeatureEnabled("bulk_actions")).toBe(false);
});
it("treats any non-'false' env value as the code default (fail-open)", async () => {
process.env.NEXT_PUBLIC_FEATURE_PIPELINE = "true";
process.env.NEXT_PUBLIC_FEATURE_CSV_IMPORT = "1";
const { isFeatureEnabled } = await import("@/lib/feature-flags");
expect(isFeatureEnabled("pipeline")).toBe(true);
expect(isFeatureEnabled("csv_import")).toBe(true);
});
it("only the literal string 'false' kills a feature", async () => {
process.env.NEXT_PUBLIC_FEATURE_PIPELINE = "FALSE";
process.env.NEXT_PUBLIC_FEATURE_PIPELINE = "False";
process.env.NEXT_PUBLIC_FEATURE_PIPELINE = "no";
process.env.NEXT_PUBLIC_FEATURE_PIPELINE = "0";
const { isFeatureEnabled } = await import("@/lib/feature-flags");
// All of the above should NOT kill the flag (fail-open for safety)
expect(isFeatureEnabled("pipeline")).toBe(true);
});
});
describe("isMaintenanceBannerVisible", () => {
it("returns false by default when env var is not set", async () => {
const { isMaintenanceBannerVisible } = await import("@/lib/feature-flags");
expect(isMaintenanceBannerVisible()).toBe(false);
});
it("returns true when NEXT_PUBLIC_MAINTENANCE_BANNER is 'true'", async () => {
process.env.NEXT_PUBLIC_MAINTENANCE_BANNER = "true";
const { isMaintenanceBannerVisible } = await import("@/lib/feature-flags");
expect(isMaintenanceBannerVisible()).toBe(true);
});
it("returns false when NEXT_PUBLIC_MAINTENANCE_BANNER is 'false'", async () => {
process.env.NEXT_PUBLIC_MAINTENANCE_BANNER = "false";
const { isMaintenanceBannerVisible } = await import("@/lib/feature-flags");
expect(isMaintenanceBannerVisible()).toBe(false);
});
it("returns false for any value other than the literal 'true'", async () => {
process.env.NEXT_PUBLIC_MAINTENANCE_BANNER = "TRUE";
const { isMaintenanceBannerVisible } = await import("@/lib/feature-flags");
expect(isMaintenanceBannerVisible()).toBe(false);
});
});
describe("FEATURE_FLAGS metadata", () => {
it("exposes a human-readable label for each flag", async () => {
const { FEATURE_FLAGS } = await import("@/lib/feature-flags");
expect(FEATURE_FLAGS.csv_import.label).toBeTruthy();
expect(FEATURE_FLAGS.pipeline.label).toBeTruthy();
expect(FEATURE_FLAGS.bulk_actions.label).toBeTruthy();
});
it("every flag has a default boolean", async () => {
const { FEATURE_FLAGS } = await import("@/lib/feature-flags");
for (const [, value] of Object.entries(FEATURE_FLAGS)) {
expect(typeof value.default).toBe("boolean");
}
});
});
});