diff --git a/src/lib/services/feature-service-potentially-stale.test.ts b/src/lib/services/feature-service-potentially-stale.test.ts new file mode 100644 index 0000000000..8009f5e295 --- /dev/null +++ b/src/lib/services/feature-service-potentially-stale.test.ts @@ -0,0 +1,52 @@ +import { IUnleashConfig, IUnleashStores } from '../types'; +import { createTestConfig } from '../../test/config/test-config'; +import FeatureToggleService from './feature-toggle-service'; +import { AccessService } from './access-service'; +import { IChangeRequestAccessReadModel } from 'lib/features/change-request-access-service/change-request-access-read-model'; +import { ISegmentService } from 'lib/segments/segment-service-interface'; + +test('Should store events', async () => { + expect.assertions(4); + const featureUpdates = [ + { name: 'feature1', potentiallyStale: true }, + { name: 'feature2', potentiallyStale: false }, + ]; + + const config = createTestConfig(); + const featureToggleService = new FeatureToggleService( + { + featureToggleStore: { + updatePotentiallyStaleFeatures: () => featureUpdates, + }, + eventStore: { + batchStore: ([event1, event2]) => { + // expect 'feature1' + expect(event1.data).toMatchObject({ + name: 'feature1', + potentiallyStale: true, + }); + expect(event1.preData).toBeUndefined(); + // expect 'feature1' + expect(event2.data).toMatchObject({ + name: 'feature2', + potentiallyStale: false, + }); + expect(event2.preData).toBeUndefined(); + }, + }, + } as unknown as IUnleashStores, + { + ...config, + flagResolver: { isEnabled: () => true }, + experimental: { + ...(config.experimental ?? {}), + emitPotentiallyStaleEvents: true, + }, + } as unknown as IUnleashConfig, + {} as ISegmentService, + {} as AccessService, + {} as IChangeRequestAccessReadModel, + ); + + await featureToggleService.updatePotentiallyStaleFeatures(); +}); diff --git a/src/lib/services/feature-toggle-service.ts b/src/lib/services/feature-toggle-service.ts index f369397d18..925023e453 100644 --- a/src/lib/services/feature-toggle-service.ts +++ b/src/lib/services/feature-toggle-service.ts @@ -39,6 +39,7 @@ import { SKIP_CHANGE_REQUEST, Unsaved, WeightType, + FEATURE_POTENTIALLY_STALE_UPDATED, } from '../types'; import { Logger } from '../logger'; import BadDataError from '../error/bad-data-error'; @@ -1984,7 +1985,24 @@ class FeatureToggleService { } async updatePotentiallyStaleFeatures(): Promise { - await this.featureToggleStore.updatePotentiallyStaleFeatures(); + const potentiallyStaleFeatures = + await this.featureToggleStore.updatePotentiallyStaleFeatures(); + if (this.flagResolver.isEnabled('emitPotentiallyStaleEvents')) { + if (potentiallyStaleFeatures.length > 0) { + return this.eventStore.batchStore( + potentiallyStaleFeatures.map( + ({ name, potentiallyStale }) => ({ + type: FEATURE_POTENTIALLY_STALE_UPDATED, + createdBy: 'unleash-system', + data: { + name, + potentiallyStale, + }, + }), + ), + ); + } + } } }