/** * Covers the one-shot publisher's close-error swallow path and the * `CACHE_INVALIDATION_ENABLED=0` kill-switch alias. * * The whole `@hivegrid/shared-store` module is mocked here so we can * force `SharedStore.close()` to reject — something the real in-memory * adapter never does — and assert the helper logs-and-continues instead * of letting the rejection escape the calling (often exiting) process. */ import { afterEach, describe, expect, it, vi } from "vitest"; const closeMock = vi.hoisted(() => vi.fn()); const invalidateMock = vi.hoisted(() => vi.fn()); vi.mock("@hivegrid/shared-store", () => ({ createSharedStore: vi.fn(() => ({ close: closeMock })), createTwoTierCache: vi.fn(() => ({ invalidate: invalidateMock })), })); import { publishFeatureFlagInvalidationOnce } from "./publisher.js"; afterEach(() => { vi.clearAllMocks(); delete process.env.CACHE_INVALIDATION_ENABLED; }); describe("publishFeatureFlagInvalidationOnce — close error handling", () => { it("logs a warning (with Error message) when store.close() rejects, without throwing", async () => { delete process.env.CACHE_INVALIDATION_ENABLED; closeMock.mockRejectedValueOnce(new Error("redis quit failed")); const warn = vi.fn(); await expect( publishFeatureFlagInvalidationOnce({ flagName: "billing.beta", sharedStoreNamespace: "ns", redisUrl: undefined, logger: { warn }, }), ).resolves.toBeUndefined(); expect(closeMock).toHaveBeenCalledTimes(1); expect(warn).toHaveBeenCalledTimes(1); expect(warn).toHaveBeenCalledWith( expect.objectContaining({ component: "feature-flags-publisher", err: "redis quit failed" }), expect.stringContaining("close failed"), ); }); it("stringifies a non-Error close rejection", async () => { delete process.env.CACHE_INVALIDATION_ENABLED; closeMock.mockRejectedValueOnce("socket hang up"); const warn = vi.fn(); await publishFeatureFlagInvalidationOnce({ flagName: "billing.beta", sharedStoreNamespace: "ns", redisUrl: undefined, logger: { warn }, }); expect(warn).toHaveBeenCalledWith( expect.objectContaining({ err: "socket hang up" }), expect.any(String), ); }); it("swallows the close error silently when no logger is supplied", async () => { delete process.env.CACHE_INVALIDATION_ENABLED; closeMock.mockRejectedValueOnce(new Error("boom")); await expect( publishFeatureFlagInvalidationOnce({ flagName: "billing.beta", sharedStoreNamespace: "ns", redisUrl: undefined, }), ).resolves.toBeUndefined(); expect(closeMock).toHaveBeenCalledTimes(1); }); it("does not warn when close() succeeds", async () => { delete process.env.CACHE_INVALIDATION_ENABLED; closeMock.mockResolvedValueOnce(undefined); const warn = vi.fn(); await publishFeatureFlagInvalidationOnce({ flagName: "billing.beta", sharedStoreNamespace: "ns", redisUrl: undefined, logger: { warn }, }); expect(closeMock).toHaveBeenCalledTimes(1); expect(warn).not.toHaveBeenCalled(); }); it("treats CACHE_INVALIDATION_ENABLED=0 as kill-switch: never opens a store", async () => { process.env.CACHE_INVALIDATION_ENABLED = "0"; await expect( publishFeatureFlagInvalidationOnce({ flagName: "billing.beta", sharedStoreNamespace: "ns", redisUrl: undefined, }), ).resolves.toBeUndefined(); // Early return before constructing the SharedStore → close untouched. expect(closeMock).not.toHaveBeenCalled(); }); });