Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaanus committed Dec 13, 2024
1 parent 7ed4fcb commit fba22c3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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',
},
],
},
];

Expand All @@ -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',
},
],
});
});

Expand Down Expand Up @@ -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: [],
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, RevisionCache>;
Expand All @@ -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) {
Expand All @@ -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 };
};
Expand All @@ -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 {
Expand Down

0 comments on commit fba22c3

Please sign in to comment.