1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

Chore: extract variant creation arbitrary.

This commit is contained in:
Thomas Heartman 2022-07-19 12:08:58 +02:00
parent 4332072db4
commit cb56846f7f

View File

@ -2,7 +2,7 @@ import fc, { Arbitrary } from 'fast-check';
import { ALL_OPERATORS } from '../lib/util/constants';
import { ClientFeatureSchema } from '../lib/openapi/spec/client-feature-schema';
import { WeightType } from '../lib/types/model';
import { IVariant, WeightType } from '../lib/types/model';
import { FeatureStrategySchema } from '../lib/openapi/spec/feature-strategy-schema';
import { ConstraintSchema } from 'lib/openapi/spec/constraint-schema';
@ -92,6 +92,43 @@ export const strategies = (): Arbitrary<FeatureStrategySchema[]> =>
),
);
export const variant = (): Arbitrary<IVariant> =>
fc.record({
name: urlFriendlyString(),
weight: fc.nat({ max: 1000 }),
weightType: fc.constant(WeightType.VARIABLE),
stickiness: fc.constant('default'),
payload: fc.option(
fc.oneof(
fc.record({
type: fc.constant('json'),
value: fc.json(),
}),
fc.record({
type: fc.constant('csv'),
value: fc.array(fc.lorem()).map((words) => words.join(',')),
}),
fc.record({
type: fc.constant('string'),
value: fc.string(),
}),
),
),
});
export const variants = (): Arbitrary<IVariant[]> =>
fc
.uniqueArray(variant(), {
maxLength: 1000,
selector: (variantInstance) => variantInstance.name,
})
.map((allVariants) =>
allVariants.map((variantInstance) => ({
...variantInstance,
weight: Math.round(1000 / allVariants.length),
})),
);
export const clientFeature = (name?: string): Arbitrary<ClientFeatureSchema> =>
fc.record(
{
@ -111,32 +148,7 @@ export const clientFeature = (name?: string): Arbitrary<ClientFeatureSchema> =>
stale: fc.boolean(),
impressionData: fc.option(fc.boolean()),
strategies: strategies(),
variants: fc.array(
fc.record({
name: urlFriendlyString(),
weight: fc.nat({ max: 1000 }),
weightType: fc.constant(WeightType.VARIABLE),
stickiness: fc.constant('default'),
payload: fc.option(
fc.oneof(
fc.record({
type: fc.constant('json'),
value: fc.json(),
}),
fc.record({
type: fc.constant('csv'),
value: fc
.array(fc.lorem())
.map((words) => words.join(',')),
}),
fc.record({
type: fc.constant('string'),
value: fc.string(),
}),
),
),
}),
),
variants: variants(),
},
{ requiredKeys: ['name', 'enabled', 'project', 'strategies'] },
);