1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-13 13:48:59 +02:00

Fix tests

This commit is contained in:
Gastón Fournier 2025-07-30 15:19:25 +02:00
parent 6eb6307940
commit e83b9aff2f
No known key found for this signature in database
GPG Key ID: AF45428626E17A8E
2 changed files with 33 additions and 54 deletions

View File

@ -117,7 +117,6 @@ import { sortStrategies } from '../../util/sortStrategies.js';
import type { ResourceLimitsSchema } from '../../openapi/index.js';
import type FeatureLinkService from '../feature-links/feature-link-service.js';
import type { IFeatureLink } from '../feature-links/feature-links-read-model-type.js';
import merge from 'deepmerge';
interface IFeatureContext {
featureName: string;
projectId: string;
@ -126,10 +125,6 @@ interface IFeatureContext {
interface IFeatureStrategyContext extends IFeatureContext {
environment: string;
}
function mergeAll<T>(objects: Partial<T>[]): T {
return merge.all<T>(objects.filter((i) => i));
}
export interface IGetFeatureParams {
featureName: string;
archived?: boolean;
@ -673,46 +668,32 @@ export class FeatureToggleService {
);
}
private async defaultParameters(
projectId: string,
strategyName: string,
featureName: string,
params: IFeatureStrategy['parameters'] | undefined,
) {
if (strategyName === 'flexibleRollout') {
if (params?.stickiness === '') {
// If stickiness is an empty string, we remove it to use the default stickiness.
delete params?.stickiness;
}
return {
rollout: params?.rollout ?? '100',
stickiness:
params?.stickiness ??
(await this.featureStrategiesStore.getDefaultStickiness(
projectId,
)),
groupId: params?.groupId ?? featureName,
};
} else {
/// We don't really have good defaults for the other kinds of known strategies, so return an empty map.
return {};
}
}
private async parametersWithDefaults(
projectId: string,
featureName: string,
strategyName: string,
params: IFeatureStrategy['parameters'] | undefined,
) {
return mergeAll([
await this.defaultParameters(
if (strategyName === 'flexibleRollout') {
const stickiness =
params?.stickiness === undefined || params?.stickiness === ''
? await this.featureStrategiesStore.getDefaultStickiness(
projectId,
strategyName,
featureName,
params,
),
params ?? {},
]);
)
: params?.stickiness;
console.log(
`stickiness: ${stickiness} from params: ${JSON.stringify(params)}`,
);
return {
...params,
rollout: params?.rollout ?? '100',
stickiness,
groupId: params?.groupId ?? featureName,
};
} else {
// We don't really have good defaults for the other kinds of known strategies, so return an empty map.
return params ?? {};
}
}
private async standardizeStrategyConfig(
projectId: string,

View File

@ -770,6 +770,7 @@ test.each([
],
['different rollout', { rollout: '25' }],
['empty parameters', {}],
['extra parameters are preserved', { extra: 'value', rollout: '100' }],
])(
'Should use default parameters when creating a flexibleRollout strategy with %s',
async (description, parameters: { [key: string]: any }) => {
@ -787,15 +788,12 @@ test.each([
parameters?.stickiness === ''
? defaultStickiness
: (parameters?.stickiness ?? defaultStickiness);
const expectedStrategies = [
{
parameters: {
const expectedParameters = {
...parameters, // expect extra parameters to be preserved
groupId: parameters?.groupId ?? feature.name,
stickiness: expectedStickiness,
rollout: parameters?.rollout ?? '100', // default rollout
},
},
];
};
await stores.projectStore.update({
id: projectId,
name: 'stickiness-project-test',
@ -818,9 +816,9 @@ test.each([
featureName: feature.name,
});
expect(featureDB.environments[0]).toMatchObject({
strategies: expectedStrategies,
});
expect(
featureDB.environments[0].strategies[0].parameters,
).toStrictEqual(expectedParameters);
// Verify that updating the strategy with same data is idempotent
await service.updateStrategy(
@ -833,9 +831,9 @@ test.each([
featureName: feature.name,
});
expect(featureDBAfterUpdate.environments[0]).toMatchObject({
strategies: expectedStrategies,
});
expect(
featureDBAfterUpdate.environments[0].strategies[0].parameters,
).toStrictEqual(expectedParameters);
},
);