Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/remote-feature-flag-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Bump `@metamask/controller-utils` from `^12.0.0` to `^12.1.0` ([#8774](https://github.com/MetaMask/core/pull/8774))

### Fixed

- Support `thresholdVersion: 2` threshold feature flag entries that return the selected `value` directly while preserving the existing threshold wrapper shape for unversioned entries ([#8908](https://github.com/MetaMask/core/pull/8908))

## [4.2.1]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export type FeatureFlagScope = {

export type FeatureFlagScopeValue = {
name: string;
thresholdName?: string;
thresholdVersion?: number;
scope: FeatureFlagScope;
value: Json;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,31 @@ describe('RemoteFeatureFlagController', () => {
});
});

describe('feature flag value normalization', () => {
it('preserves direct feature flag config objects without value metadata', async () => {
const directConfig = {
enabled: true,
minimumVersion: '13.10.0',
};
const clientConfigApiService = buildClientConfigApiService({
remoteFeatureFlags: {
directConfig,
},
});
const { controller, messenger } = createController({
clientConfigApiService,
});

await messenger.call(
'RemoteFeatureFlagController:updateRemoteFeatureFlags',
);

expect(controller.state.remoteFeatureFlags.directConfig).toStrictEqual(
directConfig,
);
});
});

describe('threshold feature flags', () => {
it('processes threshold feature flags based on provided metaMetricsId', async () => {
const clientConfigApiService = buildClientConfigApiService({
Expand All @@ -448,6 +473,74 @@ describe('RemoteFeatureFlagController', () => {
});
});

it('preserves selected legacy threshold object value wrappers', async () => {
const thresholdFlagValue = {
enabled: true,
minimumVersion: '13.10.0',
attemptsMax: 5,
};
const mockFlags = {
thresholdObjectFlag: [
{
name: 'enabled',
scope: { type: 'threshold', value: 1.0 },
value: thresholdFlagValue,
},
],
};
const clientConfigApiService = buildClientConfigApiService({
remoteFeatureFlags: mockFlags,
});
const { controller, messenger } = createController({
clientConfigApiService,
getMetaMetricsId: () => MOCK_METRICS_ID,
});

await messenger.call(
'RemoteFeatureFlagController:updateRemoteFeatureFlags',
);

expect(
controller.state.remoteFeatureFlags.thresholdObjectFlag,
).toStrictEqual({
name: 'enabled',
value: thresholdFlagValue,
});
});

it('returns selected threshold version 2 values without wrapper metadata', async () => {
const thresholdFlagValue = {
enabled: true,
minimumVersion: '13.10.0',
attemptsMax: 5,
};
const mockFlags = {
thresholdObjectFlag: [
{
thresholdName: 'enabled',
thresholdVersion: 2,
scope: { type: 'threshold', value: 1.0 },
value: thresholdFlagValue,
},
],
};
const clientConfigApiService = buildClientConfigApiService({
remoteFeatureFlags: mockFlags,
});
const { controller, messenger } = createController({
clientConfigApiService,
getMetaMetricsId: () => MOCK_METRICS_ID,
});

await messenger.call(
'RemoteFeatureFlagController:updateRemoteFeatureFlags',
);

expect(
controller.state.remoteFeatureFlags.thresholdObjectFlag,
).toStrictEqual(thresholdFlagValue);
});

it('preserves non-threshold feature flags unchanged', async () => {
const clientConfigApiService = buildClientConfigApiService({
remoteFeatureFlags: MOCK_FLAGS_WITH_THRESHOLD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import { isVersionFeatureFlag, getVersionData } from './utils/version';
export const controllerName = 'RemoteFeatureFlagController';
export const DEFAULT_CACHE_DURATION = 24 * 60 * 60 * 1000; // 1 day

enum ThresholdVersion {
DirectValue = 2,
}

// === STATE ===

export type RemoteFeatureFlagControllerState = {
Expand Down Expand Up @@ -118,6 +122,17 @@ export function getDefaultRemoteFeatureFlagControllerState(): RemoteFeatureFlagC
};
}

function normalizeThresholdValue(featureFlag: FeatureFlagScopeValue): Json {
if (featureFlag.thresholdVersion === ThresholdVersion.DirectValue) {
return featureFlag.value;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, could still be useful to also include thresholdName property only if an object, for debug in the client?

Not sure how this will impact the new AB testing hooks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good points let's add that if and when it becomes necessary then

}

return {
name: featureFlag.name,
value: featureFlag.value,
};
}

/**
* The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags.
* It fetches feature flags from a remote API, caches them, and provides methods to access
Expand Down Expand Up @@ -371,11 +386,9 @@ export class RemoteFeatureFlagController extends BaseController<
return threshold <= featureFlag.scope.value;
},
);

if (selectedGroup) {
processedValue = {
name: selectedGroup.name,
value: selectedGroup.value,
};
processedValue = normalizeThresholdValue(selectedGroup);
}
}

Expand Down
Loading