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-11-12 13:15:51 +01:00
|
|
|
import FeatureToggleService from '../../../lib/services/feature-toggle-service';
|
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';
|
2022-06-08 15:41:02 +02:00
|
|
|
import { SegmentService } from '../../../lib/services/segment-service';
|
2022-07-21 16:23:56 +02:00
|
|
|
import { GroupService } from '../../../lib/services/group-service';
|
2022-11-30 12:41:53 +01:00
|
|
|
import { FavoritesService } from '../../../lib/services';
|
2023-03-24 14:31:43 +01:00
|
|
|
import { ChangeRequestAccessReadModel } from '../../../lib/features/change-request-access-service/sql-change-request-access-read-model';
|
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;
|
2022-07-21 16:23:56 +02:00
|
|
|
let groupService;
|
2021-07-07 10:46:50 +02:00
|
|
|
let accessService;
|
|
|
|
let projectHealthService;
|
2021-08-19 13:25:36 +02:00
|
|
|
let featureToggleService;
|
2022-11-30 12:41:53 +01:00
|
|
|
let favoritesService;
|
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',
|
|
|
|
});
|
2022-07-21 16:23:56 +02:00
|
|
|
groupService = new GroupService(stores, config);
|
|
|
|
accessService = new AccessService(stores, config, groupService);
|
2023-03-24 14:31:43 +01:00
|
|
|
const changeRequestAccessReadModel = new ChangeRequestAccessReadModel(
|
|
|
|
db.rawDatabase,
|
|
|
|
accessService,
|
|
|
|
);
|
2022-06-08 15:41:02 +02:00
|
|
|
featureToggleService = new FeatureToggleService(
|
|
|
|
stores,
|
|
|
|
config,
|
|
|
|
new SegmentService(stores, config),
|
2022-10-05 23:33:36 +02:00
|
|
|
accessService,
|
2023-03-24 14:31:43 +01:00
|
|
|
changeRequestAccessReadModel,
|
2022-06-08 15:41:02 +02:00
|
|
|
);
|
2023-01-18 13:22:58 +01:00
|
|
|
favoritesService = new FavoritesService(stores, config);
|
|
|
|
|
2021-08-19 13:25:36 +02:00
|
|
|
projectService = new ProjectService(
|
|
|
|
stores,
|
|
|
|
config,
|
|
|
|
accessService,
|
|
|
|
featureToggleService,
|
2022-07-21 16:23:56 +02:00
|
|
|
groupService,
|
2023-01-18 13:22:58 +01:00
|
|
|
favoritesService,
|
2021-08-19 13:25:36 +02:00
|
|
|
);
|
2021-09-13 10:23:57 +02:00
|
|
|
projectHealthService = new ProjectHealthService(
|
|
|
|
stores,
|
|
|
|
config,
|
2023-01-18 13:22:58 +01:00
|
|
|
projectService,
|
2021-09-13 10:23:57 +02:00
|
|
|
);
|
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);
|
|
|
|
});
|