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

chore(runway): cherry-pick feat: customize fetchInterval for remoteFeatureFlagController to 15min #13342

Merged
merged 1 commit into from
Feb 5, 2025
Merged
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
1 change: 1 addition & 0 deletions app/core/AppConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,6 @@ export default {
FEATURE_FLAGS_API: {
BASE_URL: 'https://client-config.api.cx.metamask.io',
VERSION: 'v1',
DEFAULT_FETCH_INTERVAL: 15 * 60 * 1000, // 15 minutes
},
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export interface RemoteFeatureFlagInitParamTypes {
messenger: RemoteFeatureFlagControllerMessenger;
disabled: boolean;
getMetaMetricsId: () => string;
fetchInterval?: number;
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import {
import { createRemoteFeatureFlagController } from './utils';
import { v4 as uuidv4 } from 'uuid';

const mockUpdateRemoteFeatureFlags = jest.fn().mockResolvedValue(undefined);

jest.mock('@metamask/remote-feature-flag-controller', () => {
const originalModule = jest.requireActual('@metamask/remote-feature-flag-controller');
return {
...originalModule,
RemoteFeatureFlagController: jest.fn().mockImplementation((params) => ({
updateRemoteFeatureFlags: mockUpdateRemoteFeatureFlags, // Ensures it returns a resolved promise
...params, // Ensure that fetchInterval and other params are stored
})),
};
});

describe('RemoteFeatureFlagController utils', () => {
let messenger: RemoteFeatureFlagControllerMessenger;

Expand All @@ -16,91 +29,45 @@ describe('RemoteFeatureFlagController utils', () => {
});

describe('createRemoteFeatureFlagController', () => {
it('creates controller with initial undefined state', () => {
const controller = createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
getMetaMetricsId: () => uuidv4(),
});

expect(controller).toBeDefined();

// Initializing with am empty object should return an empty obj?
expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
remoteFeatureFlags: {},
});
});

it('internal state matches initial state', () => {
const initialState = {
remoteFeatureFlags: {
testFlag: true,
},
cacheTimestamp: 123,
};

const controller = createRemoteFeatureFlagController({
state: initialState,
messenger,
disabled: false,
getMetaMetricsId: () => uuidv4(),
});

expect(controller.state).toStrictEqual(initialState);
});

it('calls updateRemoteFeatureFlags when enabled', () => {
const spy = jest.spyOn(
RemoteFeatureFlagController.prototype,
'updateRemoteFeatureFlags',
);

createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
getMetaMetricsId: () => uuidv4(),
});

expect(spy).toHaveBeenCalled();
expect(mockUpdateRemoteFeatureFlags).toHaveBeenCalled();
});

it('does not call updateRemoteFeatureFlagscontroller when controller is disabled', () => {
const spy = jest.spyOn(
RemoteFeatureFlagController.prototype,
'updateRemoteFeatureFlags',
);

createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: true,
getMetaMetricsId: () => uuidv4(),
});

expect(spy).not.toHaveBeenCalled();
expect(mockUpdateRemoteFeatureFlags).not.toHaveBeenCalled();
});

it('controller keeps initial extra data into its state', () => {
const initialState = {
extraData: true,
};
it('passes fetchInterval to RemoteFeatureFlagController', async () => {
const fetchInterval = 6000;

const controller = createRemoteFeatureFlagController({
// @ts-expect-error giving a wrong initial state
state: initialState,
createRemoteFeatureFlagController({
state: undefined,
messenger,
disabled: false,
getMetaMetricsId: () => uuidv4(),
fetchInterval,
});

expect(controller.state).toStrictEqual({
cacheTimestamp: 0,
extraData: true,
remoteFeatureFlags: {},
});
// Ensure the constructor was called with fetchInterval
expect(RemoteFeatureFlagController).toHaveBeenCalledWith(
expect.objectContaining({ fetchInterval })
);
});

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import Logger from '../../../../util/Logger';

import { RemoteFeatureFlagInitParamTypes } from './types';
import AppConstants from '../../../AppConstants';

const getFeatureFlagAppEnvironment = () => {
const env = process.env.METAMASK_ENVIRONMENT;
Expand All @@ -34,6 +35,7 @@ export const createRemoteFeatureFlagController = ({
messenger,
disabled,
getMetaMetricsId,
fetchInterval = AppConstants.FEATURE_FLAGS_API.DEFAULT_FETCH_INTERVAL,
}: RemoteFeatureFlagInitParamTypes) => {
const remoteFeatureFlagController = new RemoteFeatureFlagController({
messenger,
Expand All @@ -48,6 +50,7 @@ export const createRemoteFeatureFlagController = ({
distribution: getFeatureFlagAppDistribution(),
},
}),
fetchInterval,
});

if (disabled) {
Expand Down
Loading