From 1d526e707bcb1692cd1d2dd4ff9c4aa673fb741c Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Tue, 12 Mar 2024 12:22:27 +0100 Subject: [PATCH] test: compare frontend api implementations (#6515) --- .../frontend-api-service.e2e.test.ts | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/lib/features/frontend-api/frontend-api-service.e2e.test.ts diff --git a/src/lib/features/frontend-api/frontend-api-service.e2e.test.ts b/src/lib/features/frontend-api/frontend-api-service.e2e.test.ts new file mode 100644 index 0000000000..eb81dbf211 --- /dev/null +++ b/src/lib/features/frontend-api/frontend-api-service.e2e.test.ts @@ -0,0 +1,151 @@ +import { IApiUser, IUnleashConfig, IUnleashStores } from '../../types'; +import dbInit, { ITestDb } from '../../../test/e2e/helpers/database-init'; +import { FrontendApiService } from './frontend-api-service'; +import { createFrontendApiService } from './createFrontendApiService'; +import { createLastSeenService } from '../metrics/last-seen/createLastSeenService'; +import ClientMetricsServiceV2 from '../metrics/client-metrics/metrics-service-v2'; +import ConfigurationRevisionService from '../feature-toggle/configuration-revision-service'; +import getLogger from '../../../test/fixtures/no-logger'; +import { createTestConfig } from '../../../test/config/test-config'; +import { ApiTokenType } from '../../types/models/api-token'; +import FeatureToggleService from '../feature-toggle/feature-toggle-service'; +import { createFeatureToggleService } from '../feature-toggle/createFeatureToggleService'; +import { + FRONTEND_API_REPOSITORY_CREATED, + PROXY_REPOSITORY_CREATED, +} from '../../metric-events'; + +let stores: IUnleashStores; +let db: ITestDb; +let frontendApiService: FrontendApiService; +let featureToggleService: FeatureToggleService; +let configurationRevisionService: ConfigurationRevisionService; +let config: IUnleashConfig; + +beforeAll(async () => { + db = await dbInit('frontend_api_service', getLogger); + stores = db.stores; + config = createTestConfig({ + experimental: { + flags: { + globalFrontendApiCache: true, + }, + }, + }); + + const lastSeenService = createLastSeenService(db.rawDatabase, config); + const clientMetricsServiceV2 = new ClientMetricsServiceV2( + stores, + config, + lastSeenService, + ); + configurationRevisionService = ConfigurationRevisionService.getInstance( + stores, + config, + ); + frontendApiService = createFrontendApiService( + db.rawDatabase, + config, + clientMetricsServiceV2, + configurationRevisionService, + ); + featureToggleService = createFeatureToggleService(db.rawDatabase, config); +}); + +afterAll(async () => { + await db.destroy(); +}); + +const createProject = async (project: string) => { + await stores.projectStore.create({ + name: project, + description: '', + id: project, + }); + await stores.projectStore.addEnvironmentToProject(project, 'development'); + await stores.projectStore.addEnvironmentToProject(project, 'production'); +}; + +const createEnvironment = async (environment: string) => { + await stores.environmentStore.create({ name: environment, type: 'test' }); +}; + +const createFeature = async (project: string, featureName: string) => { + await featureToggleService.createFeatureToggle( + project, + { name: featureName, description: '' }, + 'user@example.com', + 1, + ); +}; + +const enableFeature = async ( + project: string, + featureName: string, + environment: string, +) => { + await featureToggleService.unprotectedUpdateEnabled( + project, + featureName, + environment, + true, + 'user@example.com', + ); +}; + +test('Compare Frontend API implementations', async () => { + const projectA = 'projectA'; + const projectB = 'projectB'; + + await createEnvironment('development'); + await createEnvironment('production'); + + await createProject(projectA); + await createProject(projectB); + + await createFeature(projectA, 'featureA'); // include + await createFeature(projectA, 'featureB'); // another env + await createFeature(projectA, 'featureC'); // not enabled + await createFeature(projectA, 'featureD'); // include + await enableFeature(projectA, 'featureA', 'development'); + await enableFeature(projectA, 'featureD', 'development'); + await enableFeature(projectA, 'featureB', 'production'); + + await createFeature(projectB, 'featureE'); // another project + await enableFeature(projectB, 'featureE', 'development'); + + await configurationRevisionService.updateMaxRevisionId(); + + let proxyRepositoriesCount = 0; + config.eventBus.on(PROXY_REPOSITORY_CREATED, () => { + proxyRepositoriesCount++; + }); + + let frontendRepositoriesCount = 0; + config.eventBus.on(FRONTEND_API_REPOSITORY_CREATED, () => { + frontendRepositoriesCount++; + }); + + const oldFeatures = await frontendApiService.getFrontendApiFeatures( + { + projects: [projectA], + environment: 'development', + type: ApiTokenType.FRONTEND, + } as IApiUser, + { sessionId: '1234' }, + ); + + const newFeatures = await frontendApiService.getNewFrontendApiFeatures( + { + projects: [projectA], + environment: 'development', + type: ApiTokenType.FRONTEND, + } as IApiUser, + { sessionId: '1234' }, + ); + + expect(proxyRepositoriesCount).toBe(1); + expect(frontendRepositoriesCount).toBe(1); + expect(oldFeatures).toEqual(newFeatures); + expect(newFeatures.length).toBe(2); +});