import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const publishMock = vi.fn(); vi.mock("@toolctrl/feature-flags", () => ({ publishFeatureFlagInvalidationOnce: (...args: unknown[]) => publishMock(...args), })); import { broadcastFlagInvalidation } from "./invalidate.js"; describe("broadcastFlagInvalidation", () => { const originalEnv = { ...process.env }; beforeEach(() => { publishMock.mockReset(); publishMock.mockResolvedValue(undefined); }); afterEach(() => { process.env = { ...originalEnv }; }); it("forwards the flag name and defaults the sharedStoreNamespace to 'web'", async () => { delete process.env.FEATURE_FLAGS_PUBLISH_NAMESPACE; await broadcastFlagInvalidation("billing.beta"); expect(publishMock).toHaveBeenCalledTimes(1); expect(publishMock).toHaveBeenCalledWith({ flagName: "billing.beta", sharedStoreNamespace: "web", }); }); it("uses FEATURE_FLAGS_PUBLISH_NAMESPACE when set", async () => { process.env.FEATURE_FLAGS_PUBLISH_NAMESPACE = "platform"; await broadcastFlagInvalidation("billing.beta"); expect(publishMock).toHaveBeenCalledWith({ flagName: "billing.beta", sharedStoreNamespace: "platform", }); }); it("falls back to 'web' when FEATURE_FLAGS_PUBLISH_NAMESPACE is blank", async () => { process.env.FEATURE_FLAGS_PUBLISH_NAMESPACE = " "; await broadcastFlagInvalidation("billing.beta"); expect(publishMock).toHaveBeenCalledWith({ flagName: "billing.beta", sharedStoreNamespace: "web", }); }); it("swallows publisher errors so the calling command still exits cleanly", async () => { publishMock.mockRejectedValueOnce(new Error("redis down")); await expect(broadcastFlagInvalidation("billing.beta")).resolves.toBeUndefined(); expect(publishMock).toHaveBeenCalledTimes(1); }); });