2018-12-12 10:09:38 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2018-12-12 10:09:38 +01:00
|
|
|
const { featureShema } = require('./feature-schema');
|
|
|
|
|
|
|
|
test('should require URL firendly name', t => {
|
|
|
|
const toggle = {
|
|
|
|
name: 'io`dasd',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
};
|
|
|
|
|
2020-01-02 19:23:52 +01:00
|
|
|
const { error } = featureShema.validate(toggle);
|
2018-12-12 10:09:38 +01:00
|
|
|
t.deepEqual(error.details[0].message, '"name" must be URL friendly');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be valid toggle name', t => {
|
|
|
|
const toggle = {
|
|
|
|
name: 'app.name',
|
2020-08-06 11:18:52 +02:00
|
|
|
type: 'release',
|
2018-12-12 10:09:38 +01:00
|
|
|
enabled: false,
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: false,
|
2018-12-12 10:09:38 +01:00
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
};
|
|
|
|
|
2020-01-02 19:23:52 +01:00
|
|
|
const { value } = featureShema.validate(toggle);
|
2018-12-12 10:09:38 +01:00
|
|
|
t.deepEqual(value, toggle);
|
|
|
|
});
|
2019-01-29 09:21:54 +01:00
|
|
|
|
|
|
|
test('should strip extra variant fields', t => {
|
|
|
|
const toggle = {
|
|
|
|
name: 'app.name',
|
2020-08-06 11:18:52 +02:00
|
|
|
type: 'release',
|
2019-01-29 09:21:54 +01:00
|
|
|
enabled: false,
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: false,
|
2019-01-29 09:21:54 +01:00
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
name: 'variant-a',
|
|
|
|
weight: 1,
|
|
|
|
unkown: 'not-allowed',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-01-02 19:23:52 +01:00
|
|
|
const { value } = featureShema.validate(toggle);
|
2019-01-29 09:21:54 +01:00
|
|
|
t.notDeepEqual(value, toggle);
|
|
|
|
t.falsy(value.variants[0].unkown);
|
|
|
|
});
|
|
|
|
|
2020-08-03 13:24:51 +02:00
|
|
|
test('should allow weightType=fix', t => {
|
|
|
|
const toggle = {
|
|
|
|
name: 'app.name',
|
2020-08-06 11:18:52 +02:00
|
|
|
type: 'release',
|
2020-08-03 13:24:51 +02:00
|
|
|
enabled: false,
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: false,
|
2020-08-03 13:24:51 +02:00
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
name: 'variant-a',
|
|
|
|
weight: 1,
|
|
|
|
weightType: 'fix',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const { value } = featureShema.validate(toggle);
|
|
|
|
t.deepEqual(value, toggle);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should disallow weightType=unknown', t => {
|
|
|
|
const toggle = {
|
|
|
|
name: 'app.name',
|
2020-08-06 11:18:52 +02:00
|
|
|
type: 'release',
|
2020-08-03 13:24:51 +02:00
|
|
|
enabled: false,
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: false,
|
2020-08-03 13:24:51 +02:00
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
name: 'variant-a',
|
|
|
|
weight: 1,
|
|
|
|
weightType: 'unknown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const { error } = featureShema.validate(toggle);
|
|
|
|
t.deepEqual(
|
|
|
|
error.details[0].message,
|
|
|
|
'"variants[0].weightType" must be one of [variable, fix]',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-01-29 09:21:54 +01:00
|
|
|
test('should be possible to define variant overrides', t => {
|
|
|
|
const toggle = {
|
|
|
|
name: 'app.name',
|
2020-08-06 11:18:52 +02:00
|
|
|
type: 'release',
|
2019-01-29 09:21:54 +01:00
|
|
|
enabled: false,
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: false,
|
2019-01-29 09:21:54 +01:00
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
name: 'variant-a',
|
|
|
|
weight: 1,
|
2020-08-03 13:24:51 +02:00
|
|
|
weightType: 'variable',
|
2019-01-29 09:21:54 +01:00
|
|
|
overrides: [
|
|
|
|
{
|
2019-02-04 14:04:58 +01:00
|
|
|
contextName: 'userId',
|
2019-01-29 09:21:54 +01:00
|
|
|
values: ['123'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-01-02 19:23:52 +01:00
|
|
|
const { value, error } = featureShema.validate(toggle);
|
2019-01-29 09:21:54 +01:00
|
|
|
t.deepEqual(value, toggle);
|
|
|
|
t.falsy(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('variant overrides must have corect shape', async t => {
|
|
|
|
t.plan(1);
|
|
|
|
const toggle = {
|
|
|
|
name: 'app.name',
|
2020-08-06 11:18:52 +02:00
|
|
|
type: 'release',
|
2019-01-29 09:21:54 +01:00
|
|
|
enabled: false,
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: false,
|
2019-01-29 09:21:54 +01:00
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
variants: [
|
|
|
|
{
|
|
|
|
name: 'variant-a',
|
|
|
|
weight: 1,
|
|
|
|
overrides: {
|
|
|
|
userId: ['not-alloed'],
|
|
|
|
sessionId: ['not-alloed'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2020-01-02 19:23:52 +01:00
|
|
|
await featureShema.validateAsync(toggle);
|
2019-01-29 09:21:54 +01:00
|
|
|
} catch (error) {
|
2020-01-02 19:23:52 +01:00
|
|
|
t.is(
|
|
|
|
error.details[0].message,
|
2020-04-14 22:29:11 +02:00
|
|
|
'"variants[0].overrides" must be an array',
|
2020-01-02 19:23:52 +01:00
|
|
|
);
|
2019-01-29 09:21:54 +01:00
|
|
|
}
|
|
|
|
});
|
2019-11-20 22:11:30 +01:00
|
|
|
|
|
|
|
test('should keep constraints', t => {
|
|
|
|
const toggle = {
|
|
|
|
name: 'app.constraints',
|
2020-08-06 11:18:52 +02:00
|
|
|
type: 'release',
|
2019-11-20 22:11:30 +01:00
|
|
|
enabled: false,
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: false,
|
2019-11-20 22:11:30 +01:00
|
|
|
strategies: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
constraints: [
|
|
|
|
{
|
|
|
|
contextName: 'environment',
|
|
|
|
operator: 'IN',
|
|
|
|
values: ['asd'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-01-02 19:23:52 +01:00
|
|
|
const { value, error } = featureShema.validate(toggle);
|
2019-11-20 22:11:30 +01:00
|
|
|
t.deepEqual(value, toggle);
|
|
|
|
t.falsy(error);
|
|
|
|
});
|