generated from nholtware/service-template-express
feat: Unleash feature flags via src/lib/flags.ts, hello-banner on GET /
Ported from service-template-express 9b63993.
This commit is contained in:
parent
28ec495a98
commit
ec4b769be2
12 changed files with 675 additions and 17 deletions
86
src/lib/flags.test.ts
Normal file
86
src/lib/flags.test.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import { EventEmitter } from "node:events";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import pino from "pino";
|
||||
|
||||
// Mock the Unleash SDK: no network, full control over flag state.
|
||||
const sdk = vi.hoisted(() => ({
|
||||
initialize: vi.fn(),
|
||||
enabled: new Set<string>(),
|
||||
}));
|
||||
vi.mock("unleash-client", () => ({ initialize: sdk.initialize }));
|
||||
|
||||
import { createFlags } from "./flags.js";
|
||||
|
||||
function fakeUnleash() {
|
||||
const emitter = new EventEmitter();
|
||||
return Object.assign(emitter, {
|
||||
isEnabled: vi.fn((name: string) => sdk.enabled.has(name)),
|
||||
getVariant: vi.fn((name: string, _ctx: unknown, fallback: unknown) =>
|
||||
sdk.enabled.has(name)
|
||||
? { name: "blue", enabled: true, feature_enabled: true }
|
||||
: fallback,
|
||||
),
|
||||
destroy: vi.fn(),
|
||||
});
|
||||
}
|
||||
|
||||
const base = {
|
||||
SERVICE_NAME: "test-svc",
|
||||
UNLEASH_REFRESH_MS: 5000,
|
||||
} as const;
|
||||
|
||||
describe("createFlags", () => {
|
||||
beforeEach(() => {
|
||||
sdk.initialize.mockReset();
|
||||
sdk.enabled.clear();
|
||||
});
|
||||
|
||||
it("without UNLEASH_URL: all flags off, logs once, never touches the SDK", () => {
|
||||
const lines: { msg: string }[] = [];
|
||||
const logger = pino(
|
||||
{},
|
||||
{ write: (l: string) => lines.push(JSON.parse(l)) },
|
||||
);
|
||||
const flags = createFlags(base, logger);
|
||||
expect(flags.isEnabled("hello-banner")).toBe(false);
|
||||
expect(flags.isEnabled("hello-banner")).toBe(false);
|
||||
expect(flags.getVariant("hello-banner").enabled).toBe(false);
|
||||
expect(sdk.initialize).not.toHaveBeenCalled();
|
||||
expect(lines.filter((l) => /flags disabled/.test(l.msg))).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("with UNLEASH_URL: initializes the SDK and delegates", async () => {
|
||||
const unleash = fakeUnleash();
|
||||
sdk.initialize.mockReturnValue(unleash);
|
||||
const flags = createFlags(
|
||||
{
|
||||
...base,
|
||||
UNLEASH_URL: "http://unleash.test/api",
|
||||
UNLEASH_API_TOKEN: "*:development.abc",
|
||||
},
|
||||
pino({ level: "silent" }),
|
||||
);
|
||||
expect(sdk.initialize).toHaveBeenCalledWith({
|
||||
url: "http://unleash.test/api",
|
||||
appName: "test-svc",
|
||||
customHeaders: { Authorization: "*:development.abc" },
|
||||
refreshInterval: 5000,
|
||||
});
|
||||
|
||||
expect(flags.isEnabled("hello-banner")).toBe(false);
|
||||
sdk.enabled.add("hello-banner");
|
||||
expect(flags.isEnabled("hello-banner", { userId: "u1" })).toBe(true);
|
||||
expect(unleash.isEnabled).toHaveBeenLastCalledWith(
|
||||
"hello-banner",
|
||||
{ userId: "u1" },
|
||||
false,
|
||||
);
|
||||
expect(flags.getVariant("hello-banner").name).toBe("blue");
|
||||
|
||||
// SDK errors are logged, not thrown (no unhandled 'error' event).
|
||||
expect(() => unleash.emit("error", new Error("down"))).not.toThrow();
|
||||
|
||||
await flags.close();
|
||||
expect(unleash.destroy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue