100 行
3.9 KiB
JavaScript
100 行
3.9 KiB
JavaScript
|
|
const featureFlags = require("../../../src/services/featureFlags");
|
|
|
|
describe("FeatureFlags Service", () => {
|
|
describe("normalizeOverrideMode", () => {
|
|
it("should default to force_off", () => {
|
|
expect(featureFlags.OVERRIDE_MODE_LIST.includes("force_off")).toBe(true);
|
|
// internal function tested indirectly or we can just mock config
|
|
});
|
|
});
|
|
|
|
describe("evaluateFeatureFlag", () => {
|
|
it("returns env_hard_off when config disables it globally", async () => {
|
|
const result = await featureFlags.evaluateFeatureFlag({
|
|
prisma: {},
|
|
config: { invoiceVerificationEnabled: false, leaveHolidayCalendarEnabled: true },
|
|
flagKey: featureFlags.FEATURE_FLAG_KEYS.invoiceVerification,
|
|
});
|
|
expect(result.enabled).toBe(false);
|
|
expect(result.reason).toBe("env_hard_off");
|
|
});
|
|
|
|
it("evaluates using db record when config enabled", async () => {
|
|
const mockPrisma = {
|
|
featureFlag: {
|
|
findUnique: vi.fn().mockResolvedValue({
|
|
flag_key: "invoice_verification",
|
|
override_mode: "inherit",
|
|
rollout_type: "global",
|
|
department_ids: [],
|
|
}),
|
|
},
|
|
};
|
|
|
|
const result = await featureFlags.evaluateFeatureFlag({
|
|
prisma: mockPrisma,
|
|
config: { invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
|
|
flagKey: featureFlags.FEATURE_FLAG_KEYS.invoiceVerification,
|
|
});
|
|
|
|
expect(result.enabled).toBe(true);
|
|
expect(result.reason).toBe("env_inherit_global_on");
|
|
});
|
|
|
|
it("blocks if department is not in allowlist", async () => {
|
|
const mockPrisma = {
|
|
featureFlag: {
|
|
findUnique: vi.fn().mockResolvedValue({
|
|
flag_key: "invoice_verification",
|
|
override_mode: "inherit",
|
|
rollout_type: "department_allowlist",
|
|
department_ids: ["dep_A"],
|
|
}),
|
|
},
|
|
};
|
|
|
|
const result = await featureFlags.evaluateFeatureFlag({
|
|
prisma: mockPrisma,
|
|
config: { invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
|
|
flagKey: featureFlags.FEATURE_FLAG_KEYS.invoiceVerification,
|
|
departmentId: "dep_B",
|
|
strictDepartment: true
|
|
});
|
|
|
|
expect(result.enabled).toBe(false);
|
|
expect(result.reason).toBe("department_not_allowed");
|
|
});
|
|
|
|
it("allows if department is in allowlist", async () => {
|
|
const mockPrisma = {
|
|
featureFlag: {
|
|
findUnique: vi.fn().mockResolvedValue({
|
|
flag_key: "invoice_verification",
|
|
override_mode: "inherit",
|
|
rollout_type: "department_allowlist",
|
|
department_ids: ["dep_A", "dep_B"],
|
|
}),
|
|
},
|
|
};
|
|
|
|
const result = await featureFlags.evaluateFeatureFlag({
|
|
prisma: mockPrisma,
|
|
config: { invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
|
|
flagKey: featureFlags.FEATURE_FLAG_KEYS.invoiceVerification,
|
|
departmentId: "dep_B",
|
|
});
|
|
|
|
expect(result.enabled).toBe(true);
|
|
expect(result.reason).toBe("env_inherit_department_allowed");
|
|
});
|
|
});
|
|
|
|
describe("updateFeatureFlag", () => {
|
|
it("returns not_found for unknown flag", async () => {
|
|
const result = await featureFlags.updateFeatureFlag({}, { flagKey: "unknown" });
|
|
expect(result.status).toBe("not_found");
|
|
});
|
|
});
|
|
});
|