1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/client-api/metrics.test.js
Christopher Kolstad c17a1980a2
Add service layer
This simplifies stores to just be storage interaction, they no longer react to events.

Controllers now call services and awaits the result from the call.

When the service calls are returned the database is updated.
This simplifies testing dramatically, cause you know that your state is
updated when returned from a call, rather than hoping the store has
picked up the event (which really was a command) and reacted to it.

Events are still emitted from eventStore, so other parts of the app can
react to events as they're being sent out.

As part of the move to services, we now also emit an application-created
event when we see a new client application.

Fixes: #685
Fixes: #595
2021-01-21 10:59:19 +01:00

187 lines
4.9 KiB
JavaScript

'use strict';
const test = require('ava');
const supertest = require('supertest');
const { EventEmitter } = require('events');
const store = require('../../../test/fixtures/store');
const getLogger = require('../../../test/fixtures/no-logger');
const getApp = require('../../app');
const {
clientMetricsSchema,
} = require('../../services/client-metrics/client-metrics-schema');
const { createServices } = require('../../services');
const eventBus = new EventEmitter();
function getSetup() {
const stores = store.createStores();
const config = {
baseUriPath: '',
stores,
eventBus,
getLogger,
};
const services = createServices(stores, config);
const app = getApp(config, services);
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);
});
test('should accept empty client metrics', 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: {},
},
})
.expect(202);
});
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);
});
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 => {
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,
},
},
},
})
.expect(202);
});
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',
},
};
const { error, value } = clientMetricsSchema.validate(data);
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',
},
};
const { error, value } = clientMetricsSchema.validate(data);
t.falsy(error);
t.is(value.bucket.toggles.Demo2.yes, 12);
t.is(value.bucket.toggles.Demo2.no, 256);
});
test('should set lastSeen on toggle', async t => {
t.plan(1);
const { request, stores } = getSetup();
stores.featureToggleStore.createFeature({ name: 'toggleLastSeen' });
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);
});