Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default feature flags as an object #5810

Merged
merged 10 commits into from
Nov 22, 2024
26 changes: 22 additions & 4 deletions frontend/src/lib/constants/environment.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,36 @@ export interface FeatureFlags<T> {
TEST_FLAG_EDITABLE: T;
TEST_FLAG_NOT_EDITABLE: T;
}
export const defaultFeatureFlagValues: FeatureFlags<boolean> = {
ENABLE_CKBTC: true,
yhabib marked this conversation as resolved.
Show resolved Hide resolved
ENABLE_CKTESTBTC: false,
DISABLE_IMPORT_TOKEN_VALIDATION_FOR_TESTING: false,
ENABLE_PERIODIC_FOLLOWING_CONFIRMATION: false,
ENABLE_EXPORT_NEURONS_REPORT: false,
TEST_FLAG_EDITABLE: false,
TEST_FLAG_NOT_EDITABLE: false,
};

export type FeatureKey = keyof FeatureFlags<boolean>;

const getFeatureFlagsFromEnv = (): FeatureFlags<boolean> => {
let featureFlags = {};
try {
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
featureFlags = JSON.parse(envVars?.featureFlags);
} finally {
yhabib marked this conversation as resolved.
Show resolved Hide resolved
// do nothing
}
// Complement the default flags with the ones from the environment to avoid missing flags.
return { ...defaultFeatureFlagValues, ...featureFlags };
};

/**
* DO NOT USE DIRECTLY
*
* @see feature-flags.store.ts to use feature flags
*/
export const FEATURE_FLAG_ENVIRONMENT: FeatureFlags<boolean> = JSON.parse(
envVars?.featureFlags ??
'{"ENABLE_CKBTC": true, "ENABLE_CKTESTBTC": false, "ENABLE_SNS_TYPES_FILTER": false, "ENABLE_EXPORT_NEURONS_REPORT": false}'
);
export const FEATURE_FLAG_ENVIRONMENT: FeatureFlags<boolean> =
getFeatureFlagsFromEnv();

export const IS_TESTNET: boolean =
DFX_NETWORK !== "mainnet" &&
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/tests/lib/constants/environment.constants.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { defaultFeatureFlagValues } from "$lib/constants/environment.constants";
import * as envVarsUtils from "$lib/utils/env-vars.utils";

describe("FEATURE_FLAG_ENVIRONMENT", () => {
const environmentVars = envVarsUtils.getEnvVars();

beforeEach(() => {
vi.resetModules();
mstrasinskis marked this conversation as resolved.
Show resolved Hide resolved
});

it("should equal the environment values", async () => {
const { FEATURE_FLAG_ENVIRONMENT } = await import(
"$lib/constants/environment.constants"
);
const expectedFlags = JSON.parse(environmentVars.featureFlags);
expect(FEATURE_FLAG_ENVIRONMENT).toEqual(expectedFlags);
});

it("should contain missing entries substituted with default values", async () => {
vi.spyOn(envVarsUtils, "getEnvVars").mockReturnValue({
...environmentVars,
featureFlags: JSON.stringify({}),
});

const { FEATURE_FLAG_ENVIRONMENT } = await import(
"$lib/constants/environment.constants"
);
expect(FEATURE_FLAG_ENVIRONMENT).toEqual(defaultFeatureFlagValues);
});
});