mirror of
https://github.com/Unleash/unleash.git
synced 2025-08-13 13:48:59 +02:00
Fix tests
This commit is contained in:
parent
6eb6307940
commit
e83b9aff2f
@ -117,7 +117,6 @@ import { sortStrategies } from '../../util/sortStrategies.js';
|
|||||||
import type { ResourceLimitsSchema } from '../../openapi/index.js';
|
import type { ResourceLimitsSchema } from '../../openapi/index.js';
|
||||||
import type FeatureLinkService from '../feature-links/feature-link-service.js';
|
import type FeatureLinkService from '../feature-links/feature-link-service.js';
|
||||||
import type { IFeatureLink } from '../feature-links/feature-links-read-model-type.js';
|
import type { IFeatureLink } from '../feature-links/feature-links-read-model-type.js';
|
||||||
import merge from 'deepmerge';
|
|
||||||
interface IFeatureContext {
|
interface IFeatureContext {
|
||||||
featureName: string;
|
featureName: string;
|
||||||
projectId: string;
|
projectId: string;
|
||||||
@ -126,10 +125,6 @@ interface IFeatureContext {
|
|||||||
interface IFeatureStrategyContext extends IFeatureContext {
|
interface IFeatureStrategyContext extends IFeatureContext {
|
||||||
environment: string;
|
environment: string;
|
||||||
}
|
}
|
||||||
function mergeAll<T>(objects: Partial<T>[]): T {
|
|
||||||
return merge.all<T>(objects.filter((i) => i));
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IGetFeatureParams {
|
export interface IGetFeatureParams {
|
||||||
featureName: string;
|
featureName: string;
|
||||||
archived?: boolean;
|
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(
|
private async parametersWithDefaults(
|
||||||
projectId: string,
|
projectId: string,
|
||||||
featureName: string,
|
featureName: string,
|
||||||
strategyName: string,
|
strategyName: string,
|
||||||
params: IFeatureStrategy['parameters'] | undefined,
|
params: IFeatureStrategy['parameters'] | undefined,
|
||||||
) {
|
) {
|
||||||
return mergeAll([
|
if (strategyName === 'flexibleRollout') {
|
||||||
await this.defaultParameters(
|
const stickiness =
|
||||||
|
params?.stickiness === undefined || params?.stickiness === ''
|
||||||
|
? await this.featureStrategiesStore.getDefaultStickiness(
|
||||||
projectId,
|
projectId,
|
||||||
strategyName,
|
)
|
||||||
featureName,
|
: params?.stickiness;
|
||||||
params,
|
console.log(
|
||||||
),
|
`stickiness: ${stickiness} from params: ${JSON.stringify(params)}`,
|
||||||
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(
|
private async standardizeStrategyConfig(
|
||||||
projectId: string,
|
projectId: string,
|
||||||
|
@ -770,6 +770,7 @@ test.each([
|
|||||||
],
|
],
|
||||||
['different rollout', { rollout: '25' }],
|
['different rollout', { rollout: '25' }],
|
||||||
['empty parameters', {}],
|
['empty parameters', {}],
|
||||||
|
['extra parameters are preserved', { extra: 'value', rollout: '100' }],
|
||||||
])(
|
])(
|
||||||
'Should use default parameters when creating a flexibleRollout strategy with %s',
|
'Should use default parameters when creating a flexibleRollout strategy with %s',
|
||||||
async (description, parameters: { [key: string]: any }) => {
|
async (description, parameters: { [key: string]: any }) => {
|
||||||
@ -787,15 +788,12 @@ test.each([
|
|||||||
parameters?.stickiness === ''
|
parameters?.stickiness === ''
|
||||||
? defaultStickiness
|
? defaultStickiness
|
||||||
: (parameters?.stickiness ?? defaultStickiness);
|
: (parameters?.stickiness ?? defaultStickiness);
|
||||||
const expectedStrategies = [
|
const expectedParameters = {
|
||||||
{
|
...parameters, // expect extra parameters to be preserved
|
||||||
parameters: {
|
|
||||||
groupId: parameters?.groupId ?? feature.name,
|
groupId: parameters?.groupId ?? feature.name,
|
||||||
stickiness: expectedStickiness,
|
stickiness: expectedStickiness,
|
||||||
rollout: parameters?.rollout ?? '100', // default rollout
|
rollout: parameters?.rollout ?? '100', // default rollout
|
||||||
},
|
};
|
||||||
},
|
|
||||||
];
|
|
||||||
await stores.projectStore.update({
|
await stores.projectStore.update({
|
||||||
id: projectId,
|
id: projectId,
|
||||||
name: 'stickiness-project-test',
|
name: 'stickiness-project-test',
|
||||||
@ -818,9 +816,9 @@ test.each([
|
|||||||
featureName: feature.name,
|
featureName: feature.name,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(featureDB.environments[0]).toMatchObject({
|
expect(
|
||||||
strategies: expectedStrategies,
|
featureDB.environments[0].strategies[0].parameters,
|
||||||
});
|
).toStrictEqual(expectedParameters);
|
||||||
|
|
||||||
// Verify that updating the strategy with same data is idempotent
|
// Verify that updating the strategy with same data is idempotent
|
||||||
await service.updateStrategy(
|
await service.updateStrategy(
|
||||||
@ -833,9 +831,9 @@ test.each([
|
|||||||
featureName: feature.name,
|
featureName: feature.name,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(featureDBAfterUpdate.environments[0]).toMatchObject({
|
expect(
|
||||||
strategies: expectedStrategies,
|
featureDBAfterUpdate.environments[0].strategies[0].parameters,
|
||||||
});
|
).toStrictEqual(expectedParameters);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user