2021-09-13 10:23:57 +02:00
|
|
|
import dbInit, { ITestDb } from '../helpers/database-init';
|
2021-07-07 10:46:50 +02:00
|
|
|
import getLogger from '../../fixtures/no-logger';
|
2021-08-19 13:25:36 +02:00
|
|
|
import FeatureToggleServiceV2 from '../../../lib/services/feature-toggle-service-v2';
|
2021-07-07 10:46:50 +02:00
|
|
|
import { AccessService } from '../../../lib/services/access-service';
|
|
|
|
import ProjectService from '../../../lib/services/project-service';
|
|
|
|
import ProjectHealthService from '../../../lib/services/project-health-service';
|
|
|
|
import { createTestConfig } from '../../config/test-config';
|
2021-09-13 10:23:57 +02:00
|
|
|
import { IUnleashStores } from '../../../lib/types';
|
|
|
|
import { IUser } from '../../../lib/server-impl';
|
2021-07-07 10:46:50 +02:00
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
let stores: IUnleashStores;
|
|
|
|
let db: ITestDb;
|
2021-07-07 10:46:50 +02:00
|
|
|
let projectService;
|
|
|
|
let accessService;
|
|
|
|
let projectHealthService;
|
2021-08-19 13:25:36 +02:00
|
|
|
let featureToggleService;
|
2021-09-13 10:23:57 +02:00
|
|
|
let user: IUser;
|
2021-07-07 10:46:50 +02:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const config = createTestConfig();
|
|
|
|
db = await dbInit('project_health_service_serial', getLogger);
|
|
|
|
stores = db.stores;
|
|
|
|
user = await stores.userStore.insert({
|
|
|
|
name: 'Some Name',
|
|
|
|
email: 'test@getunleash.io',
|
|
|
|
});
|
|
|
|
accessService = new AccessService(stores, config);
|
2021-08-19 13:25:36 +02:00
|
|
|
featureToggleService = new FeatureToggleServiceV2(stores, config);
|
|
|
|
projectService = new ProjectService(
|
|
|
|
stores,
|
|
|
|
config,
|
|
|
|
accessService,
|
|
|
|
featureToggleService,
|
|
|
|
);
|
2021-09-13 10:23:57 +02:00
|
|
|
projectHealthService = new ProjectHealthService(
|
|
|
|
stores,
|
|
|
|
config,
|
|
|
|
featureToggleService,
|
|
|
|
);
|
2021-07-07 10:46:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await db.destroy();
|
|
|
|
});
|
|
|
|
test('Project with no stale toggles should have 100% health rating', async () => {
|
|
|
|
const project = {
|
|
|
|
id: 'health-rating',
|
|
|
|
name: 'Health rating',
|
|
|
|
description: 'Fancy',
|
|
|
|
};
|
|
|
|
const savedProject = await projectService.createProject(project, user);
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-not-stale',
|
|
|
|
description: 'new',
|
|
|
|
stale: false,
|
|
|
|
});
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-not-stale-2',
|
|
|
|
description: 'new too',
|
|
|
|
stale: false,
|
|
|
|
});
|
|
|
|
const rating = await projectHealthService.calculateHealthRating(
|
|
|
|
savedProject,
|
|
|
|
);
|
|
|
|
expect(rating).toBe(100);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Project with two stale toggles and two non stale should have 50% health rating', async () => {
|
|
|
|
const project = {
|
|
|
|
id: 'health-rating-2',
|
|
|
|
name: 'Health rating',
|
|
|
|
description: 'Fancy',
|
|
|
|
};
|
|
|
|
const savedProject = await projectService.createProject(project, user);
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating-2', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-2-not-stale',
|
|
|
|
description: 'new',
|
|
|
|
stale: false,
|
|
|
|
});
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating-2', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-2-not-stale-2',
|
|
|
|
description: 'new too',
|
|
|
|
stale: false,
|
|
|
|
});
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating-2', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-2-stale-1',
|
|
|
|
description: 'stale',
|
|
|
|
stale: true,
|
|
|
|
});
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating-2', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-2-stale-2',
|
|
|
|
description: 'stale too',
|
|
|
|
stale: true,
|
|
|
|
});
|
|
|
|
const rating = await projectHealthService.calculateHealthRating(
|
|
|
|
savedProject,
|
|
|
|
);
|
|
|
|
expect(rating).toBe(50);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Project with one non-stale, one potentially stale and one stale should have 33% health rating', async () => {
|
|
|
|
const project = {
|
|
|
|
id: 'health-rating-3',
|
|
|
|
name: 'Health rating',
|
|
|
|
description: 'Fancy',
|
|
|
|
};
|
|
|
|
const savedProject = await projectService.createProject(project, user);
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating-3', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-3-not-stale',
|
|
|
|
description: 'new',
|
|
|
|
stale: false,
|
|
|
|
});
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating-3', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-3-potentially-stale',
|
|
|
|
description: 'new too',
|
|
|
|
type: 'release',
|
|
|
|
stale: false,
|
|
|
|
createdAt: new Date(Date.UTC(2020, 1, 1)),
|
|
|
|
});
|
2021-09-13 10:23:57 +02:00
|
|
|
await stores.featureToggleStore.create('health-rating-3', {
|
2021-07-07 10:46:50 +02:00
|
|
|
name: 'health-rating-3-stale',
|
|
|
|
description: 'stale',
|
|
|
|
stale: true,
|
|
|
|
});
|
|
|
|
const rating = await projectHealthService.calculateHealthRating(
|
|
|
|
savedProject,
|
|
|
|
);
|
|
|
|
expect(rating).toBe(33);
|
|
|
|
});
|