2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2019-05-06 10:50:50 +02:00
|
|
|
const supertest = require('supertest');
|
2020-04-14 22:29:11 +02:00
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
const store = require('../../../test/fixtures/store');
|
2019-04-30 21:14:23 +02:00
|
|
|
const getLogger = require('../../../test/fixtures/no-logger');
|
2017-06-28 10:20:22 +02:00
|
|
|
const getApp = require('../../app');
|
2021-01-18 12:32:19 +01:00
|
|
|
const {
|
|
|
|
clientMetricsSchema,
|
|
|
|
} = require('../../services/client-metrics/client-metrics-schema');
|
2020-12-17 19:22:30 +01:00
|
|
|
const { createServices } = require('../../services');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
|
|
|
|
function getSetup() {
|
|
|
|
const stores = store.createStores();
|
2020-12-17 19:22:30 +01:00
|
|
|
const config = {
|
2017-06-28 10:20:22 +02:00
|
|
|
baseUriPath: '',
|
|
|
|
stores,
|
|
|
|
eventBus,
|
2019-04-30 21:14:23 +02:00
|
|
|
getLogger,
|
2020-12-17 19:22:30 +01:00
|
|
|
};
|
|
|
|
const services = createServices(stores, config);
|
|
|
|
const app = getApp(config, services);
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
request: supertest(app),
|
|
|
|
stores,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test('should validate client metrics', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const { request } = getSetup();
|
|
|
|
return request
|
|
|
|
.post('/api/client/metrics')
|
|
|
|
.send({ random: 'blush' })
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2017-08-04 11:24:58 +02:00
|
|
|
test('should accept empty client metrics', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
|
|
|
const { request } = getSetup();
|
|
|
|
return request
|
|
|
|
.post('/api/client/metrics')
|
|
|
|
.send({
|
|
|
|
appName: 'demo',
|
|
|
|
instanceId: '1',
|
|
|
|
bucket: {
|
|
|
|
start: Date.now(),
|
|
|
|
stop: Date.now(),
|
|
|
|
toggles: {},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.expect(202);
|
|
|
|
});
|
2017-08-04 11:24:58 +02:00
|
|
|
|
|
|
|
test('should accept client metrics with yes/no', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const { request } = getSetup();
|
|
|
|
return request
|
|
|
|
.post('/api/client/metrics')
|
|
|
|
.send({
|
|
|
|
appName: 'demo',
|
|
|
|
instanceId: '1',
|
|
|
|
bucket: {
|
|
|
|
start: Date.now(),
|
|
|
|
stop: Date.now(),
|
|
|
|
toggles: {
|
|
|
|
toggleA: {
|
|
|
|
yes: 200,
|
|
|
|
no: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.expect(202);
|
|
|
|
});
|
|
|
|
|
2019-01-25 13:05:25 +01:00
|
|
|
test('should accept client metrics with variants', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const { request } = getSetup();
|
|
|
|
return request
|
|
|
|
.post('/api/client/metrics')
|
|
|
|
.send({
|
|
|
|
appName: 'demo',
|
|
|
|
instanceId: '1',
|
|
|
|
bucket: {
|
|
|
|
start: Date.now(),
|
|
|
|
stop: Date.now(),
|
|
|
|
toggles: {
|
|
|
|
toggleA: {
|
|
|
|
yes: 200,
|
|
|
|
no: 0,
|
|
|
|
variants: {
|
|
|
|
variant1: 1,
|
|
|
|
variant2: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.expect(202);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should accept client metrics without yes/no', t => {
|
2017-08-04 11:24:58 +02:00
|
|
|
t.plan(0);
|
|
|
|
const { request } = getSetup();
|
|
|
|
return request
|
|
|
|
.post('/api/client/metrics')
|
|
|
|
.send({
|
|
|
|
appName: 'demo',
|
|
|
|
instanceId: '1',
|
|
|
|
bucket: {
|
|
|
|
start: Date.now(),
|
|
|
|
stop: Date.now(),
|
|
|
|
toggles: {
|
|
|
|
toggleA: {
|
|
|
|
blue: 200,
|
|
|
|
green: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-01-25 13:05:25 +01:00
|
|
|
.expect(202);
|
2017-08-04 11:24:58 +02:00
|
|
|
});
|
2019-05-06 10:50:50 +02:00
|
|
|
|
|
|
|
test('shema allow empty strings', t => {
|
|
|
|
const data = {
|
|
|
|
appName: 'java-test',
|
|
|
|
instanceId: 'instance y',
|
|
|
|
bucket: {
|
|
|
|
toggles: { Demo2: { yes: '', no: '', variants: {} } },
|
|
|
|
start: '2019-05-06T08:30:40.514Z',
|
|
|
|
stop: '2019-05-06T09:30:50.515Z',
|
|
|
|
},
|
|
|
|
};
|
2020-01-02 19:23:52 +01:00
|
|
|
const { error, value } = clientMetricsSchema.validate(data);
|
2019-05-06 10:50:50 +02:00
|
|
|
t.falsy(error);
|
|
|
|
t.is(value.bucket.toggles.Demo2.yes, 0);
|
|
|
|
t.is(value.bucket.toggles.Demo2.no, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('shema allow yes=<string nbr>', t => {
|
|
|
|
const data = {
|
|
|
|
appName: 'java-test',
|
|
|
|
instanceId: 'instance y',
|
|
|
|
bucket: {
|
|
|
|
toggles: { Demo2: { yes: '12', no: 256, variants: {} } },
|
|
|
|
start: '2019-05-06T08:30:40.514Z',
|
|
|
|
stop: '2019-05-06T09:30:50.515Z',
|
|
|
|
},
|
|
|
|
};
|
2020-01-02 19:23:52 +01:00
|
|
|
const { error, value } = clientMetricsSchema.validate(data);
|
2019-05-06 10:50:50 +02:00
|
|
|
t.falsy(error);
|
|
|
|
t.is(value.bucket.toggles.Demo2.yes, 12);
|
|
|
|
t.is(value.bucket.toggles.Demo2.no, 256);
|
|
|
|
});
|
2020-12-22 10:49:17 +01:00
|
|
|
|
|
|
|
test('should set lastSeen on toggle', async t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request, stores } = getSetup();
|
2021-01-18 12:32:19 +01:00
|
|
|
stores.featureToggleStore.createFeature({ name: 'toggleLastSeen' });
|
2020-12-22 10:49:17 +01:00
|
|
|
await request
|
|
|
|
.post('/api/client/metrics')
|
|
|
|
.send({
|
|
|
|
appName: 'demo',
|
|
|
|
instanceId: '1',
|
|
|
|
bucket: {
|
|
|
|
start: Date.now(),
|
|
|
|
stop: Date.now(),
|
|
|
|
toggles: {
|
|
|
|
toggleLastSeen: {
|
|
|
|
yes: 200,
|
|
|
|
no: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.expect(202);
|
|
|
|
|
|
|
|
const toggle = await stores.featureToggleStore.getFeature('toggleLastSeen');
|
|
|
|
|
|
|
|
t.truthy(toggle.lastSeenAt);
|
|
|
|
});
|