文件
hyb-oa/server/tests/unit/featureFlags.evaluate.test.js
wangsli 1f8d5b658a 完善借款、编号、付款与预算并发及验收(09-06-release-gate-integrity)
汇总已落盘的借款本金与还款、兼容历史编号的周期化编号、付款责任与回单核验、报销进度和排序、PWA/采购事务、错误原因与日期校验及ERP退役。
统一共享预算池锁序和锁后判断,恢复差旅原预算选择,补原因码固定夹具及反向完整性检查。
纳入配套迁移、测试、受管QA、分层验收合同、规约及任务记录;原始证据留在ignored artifacts。

关联任务:09-17-company-loan-principal-repayment、09-17-erp-exclusive-retirement、09-18-reimbursement-periodic-numbering、09-08-reimbursement-payment-closeout、09-18-local-concurrency-product-fixes、09-19-budget-pool-concurrency、09-05-error-explainability-payment-readiness。
预算补审由Claude定向验证2文件928项通过,原4446/28及r6结果仍仅对应各自历史输入;本轮未重跑测试。
完整新候选门禁、R1真实入口接入与12项公网义务仍开放。生产编号未启用、差旅仍冻结;不推送、不部署,不宣称发布资格。
2026-09-19 13:11:04 +08:00

223 行
7.3 KiB
JavaScript

const {
FEATURE_FLAG_KEYS,
evaluateFeatureFlag,
listFeatureFlags,
} = require("../../src/services/featureFlags");
function buildRecord({
flagKey,
overrideMode = "force_on",
rolloutType = "global",
departmentIds = [],
}) {
const now = new Date("2026-02-12T00:00:00.000Z");
return {
id: `flag-${flagKey}`,
flag_key: flagKey,
override_mode: overrideMode,
rollout_type: rolloutType,
department_ids: departmentIds,
note: "unit-test",
version: 1,
created_at: now,
created_by: null,
updated_at: now,
updated_by: null,
};
}
function buildPrisma(recordMap = {}) {
return {
featureFlag: {
findUnique: async ({ where }) => recordMap[where.flag_key] ?? null,
findMany: async () => Object.values(recordMap),
},
};
}
describe("evaluateFeatureFlag", () => {
it("returns env_hard_off when env gate is closed", async () => {
const result = await evaluateFeatureFlag({
prisma: buildPrisma(),
config: { paymentBatchEnabled: false, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
});
expect(result.enabled).toBe(false);
expect(result.reason).toBe("env_hard_off");
});
it("returns missing_flag_record when record does not exist", async () => {
const result = await evaluateFeatureFlag({
prisma: buildPrisma(),
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
});
expect(result.enabled).toBe(false);
expect(result.reason).toBe("missing_flag_record");
});
it("returns flag_force_off when override mode is force_off", async () => {
const prisma = buildPrisma({
[FEATURE_FLAG_KEYS.paymentBatch]: buildRecord({
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
overrideMode: "force_off",
}),
});
const result = await evaluateFeatureFlag({
prisma,
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
});
expect(result.enabled).toBe(false);
expect(result.reason).toBe("flag_force_off");
});
it("returns global enabled when inherit + global", async () => {
const prisma = buildPrisma({
[FEATURE_FLAG_KEYS.paymentBatch]: buildRecord({
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
overrideMode: "inherit",
rolloutType: "global",
}),
});
const result = await evaluateFeatureFlag({
prisma,
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
});
expect(result.enabled).toBe(true);
expect(result.reason).toBe("env_inherit_global_on");
});
it("returns allowlist guards for department rollout", async () => {
const prisma = buildPrisma({
[FEATURE_FLAG_KEYS.paymentBatch]: buildRecord({
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
rolloutType: "department_allowlist",
departmentIds: ["dept-a"],
}),
});
const missingDepartment = await evaluateFeatureFlag({
prisma,
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
strictDepartment: true,
});
const blockedDepartment = await evaluateFeatureFlag({
prisma,
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
departmentId: "dept-b",
strictDepartment: true,
});
const allowedDepartment = await evaluateFeatureFlag({
prisma,
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
departmentId: "dept-a",
strictDepartment: true,
});
expect(missingDepartment.reason).toBe("department_missing");
expect(blockedDepartment.reason).toBe("department_not_allowed");
expect(allowedDepartment.reason).toBe("flag_force_on_department_allowed");
expect(allowedDepartment.enabled).toBe(true);
});
it("returns department_deferred when strictDepartment is false", async () => {
const prisma = buildPrisma({
[FEATURE_FLAG_KEYS.invoiceVerification]: buildRecord({
flagKey: FEATURE_FLAG_KEYS.invoiceVerification,
rolloutType: "department_allowlist",
departmentIds: ["dept-a"],
}),
});
const result = await evaluateFeatureFlag({
prisma,
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.invoiceVerification,
strictDepartment: false,
});
expect(result.enabled).toBe(true);
expect(result.reason).toBe("department_deferred");
});
it("returns department_allowlist_empty when allowlist has no items", async () => {
const prisma = buildPrisma({
[FEATURE_FLAG_KEYS.invoiceVerification]: buildRecord({
flagKey: FEATURE_FLAG_KEYS.invoiceVerification,
rolloutType: "department_allowlist",
departmentIds: [],
}),
});
const result = await evaluateFeatureFlag({
prisma,
config: { paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.invoiceVerification,
strictDepartment: true,
});
expect(result.enabled).toBe(false);
expect(result.reason).toBe("department_allowlist_empty");
});
});
describe("feature flag env gates", () => {
it("uses the dedicated leave holiday calendar env gate", async () => {
const result = await evaluateFeatureFlag({
prisma: buildPrisma(),
config: { aiAssistEnabled: true, paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: false },
flagKey: FEATURE_FLAG_KEYS.leaveHolidayCalendar,
});
expect(result.enabled).toBe(false);
expect(result.reason).toBe("env_hard_off");
});
it("uses the dedicated ai assist env gate", async () => {
const result = await evaluateFeatureFlag({
prisma: buildPrisma(),
config: { aiAssistEnabled: false, paymentBatchEnabled: true, invoiceVerificationEnabled: true, leaveHolidayCalendarEnabled: true },
flagKey: FEATURE_FLAG_KEYS.aiAssist,
});
expect(result.enabled).toBe(false);
expect(result.reason).toBe("env_hard_off");
});
});
describe("listFeatureFlags", () => {
it("returns all known keys even if part of records are missing", async () => {
const prisma = buildPrisma({
[FEATURE_FLAG_KEYS.paymentBatch]: buildRecord({
flagKey: FEATURE_FLAG_KEYS.paymentBatch,
overrideMode: "inherit",
}),
});
const items = await listFeatureFlags(prisma, {
paymentBatchEnabled: true,
invoiceVerificationEnabled: true,
});
expect(items).toHaveLength(Object.keys(FEATURE_FLAG_KEYS).length);
expect(items.map((item) => item.flagKey).sort()).toEqual(Object.values(FEATURE_FLAG_KEYS).sort());
const missingItem = items.find((item) => item.flagKey === FEATURE_FLAG_KEYS.aiAssist);
expect(missingItem.id).toBeNull();
expect(missingItem.effectiveEnabled).toBe(false);
});
});