From fba22c353113087283a1d7592a6044cbbade27c4 Mon Sep 17 00:00:00 2001 From: sjaanus Date: Fri, 13 Dec 2024 14:33:02 +0200 Subject: [PATCH] Fix --- .../cache/client-feature-toggle-cache.test.ts | 53 +++++++++++++++++-- .../cache/client-feature-toggle-cache.ts | 26 ++++++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.test.ts b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.test.ts index 6573adff0ccd..5fd8302ad68c 100644 --- a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.test.ts +++ b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.test.ts @@ -51,7 +51,12 @@ test('revision that adds, removes then adds again does not end up with the remov { revisionId: 2, updated: [], - removed: ['some-toggle'], + removed: [ + { + name: 'some-toggle', + project: 'default', + }, + ], }, { revisionId: 3, @@ -76,7 +81,12 @@ test('revision that removes, adds then removes again does not end up with the re { revisionId: 1, updated: [], - removed: ['some-toggle'], + removed: [ + { + name: 'some-toggle', + project: 'default', + }, + ], }, { revisionId: 2, @@ -86,7 +96,12 @@ test('revision that removes, adds then removes again does not end up with the re { revisionId: 3, updated: [], - removed: ['some-toggle'], + removed: [ + { + name: 'some-toggle', + project: 'default', + }, + ], }, ]; @@ -97,7 +112,12 @@ test('revision that removes, adds then removes again does not end up with the re expect(revisions).toEqual({ revisionId: 3, updated: [], - removed: ['some-toggle'], + removed: [ + { + name: 'some-toggle', + project: 'default', + }, + ], }); }); @@ -134,3 +154,28 @@ test('revision equal to the base case returns only later revisions ', () => { removed: [], }); }); + +test('project filter removes features not in project', () => { + const revisionList = [ + { + revisionId: 1, + updated: [mockAdd({ name: 'feature1', project: 'project1' })], + removed: [], + }, + { + revisionId: 2, + updated: [mockAdd({ name: 'feature2', project: 'project2' })], + removed: [], + }, + ]; + + const revisions = calculateRequiredClientRevision(revisionList, 0, [ + 'project1', + ]); + + expect(revisions).toEqual({ + revisionId: 2, + updated: [mockAdd({ name: 'feature1', project: 'project1' })], + removed: [], + }); +}); diff --git a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts index a3db4c5d24ff..15de4d1f53b0 100644 --- a/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts +++ b/src/lib/features/client-feature-toggles/cache/client-feature-toggle-cache.ts @@ -12,16 +12,21 @@ import type { IClientFeatureToggleCacheReadModel, } from './client-feature-toggle-cache-read-model-type'; +type DeletedFeature = { + name: string; + project: string; +}; + export type RevisionCacheEntry = { updated: FeatureConfigurationCacheClient[]; revisionId: number; - removed: string[]; + removed: DeletedFeature[]; }; export type Revision = { revisionId: number; updated: any[]; - removed: string[]; + removed: DeletedFeature[]; }; type Revisions = Record; @@ -33,10 +38,15 @@ const applyRevision = (first: Revision, last: Revision): Revision => { feature, ]), ); - const removedMap = new Set([...first.removed, ...last.removed]); + const removedMap = new Map( + [...first.removed, ...last.removed].map((feature) => [ + feature.name, + feature, + ]), + ); for (const feature of last.removed) { - updatedMap.delete(feature); + updatedMap.delete(feature.name); } for (const feature of last.updated) { @@ -59,7 +69,8 @@ const filterRevisionByProject = ( projects.includes('*') || projects.includes(feature.project), ); const removed = revision.removed.filter( - (feature) => projects.includes('*') || projects.includes(feature), + (feature) => + projects.includes('*') || projects.includes(feature.project), ); return { ...revision, updated, removed }; }; @@ -73,8 +84,11 @@ export const calculateRequiredClientRevision = ( (revision) => revision.revisionId > requiredRevisionId, ); console.log('targeted revisions', targetedRevisions); + const projectFeatureRevisions = targetedRevisions.map((revision) => + filterRevisionByProject(revision, projects), + ); - return targetedRevisions.reduce(applyRevision); + return projectFeatureRevisions.reduce(applyRevision); }; export class ClientFeatureToggleCache {