Skip to content
Snippets Groups Projects
Commit 895063fd authored by Marcos Pieras's avatar Marcos Pieras
Browse files

test: adds tests on valid insight

parent c669d9d0
No related branches found
Tags v1.18.0
1 merge request!42test: adds return message to handle e2e tests
Pipeline #147103 passed
...@@ -12,6 +12,7 @@ import { diffCheck } from './diffCheck'; ...@@ -12,6 +12,7 @@ import { diffCheck } from './diffCheck';
import { VariableNode } from '../utils/lexical'; import { VariableNode } from '../utils/lexical';
import { populateTemplate } from '../utils/insights'; import { populateTemplate } from '../utils/insights';
import { RabbitMqBroker } from 'ts-common/rabbitMq'; import { RabbitMqBroker } from 'ts-common/rabbitMq';
import { validateInsight } from '../utils/insights/validateInsight';
const dom = new JSDOM(); const dom = new JSDOM();
function setUpDom() { function setUpDom() {
...@@ -37,7 +38,7 @@ function setUpDom() { ...@@ -37,7 +38,7 @@ function setUpDom() {
export const insightProcessor = async () => { export const insightProcessor = async () => {
if (mail == null) { if (mail == null) {
log.warn('Mail is not configured. Insight processor will be disabled'); log.warn('Mail is not configured. Insight processor will be disabled');
return; //return;
} }
log.info('Starting insight processor'); log.info('Starting insight processor');
...@@ -52,18 +53,8 @@ export const insightProcessor = async () => { ...@@ -52,18 +53,8 @@ export const insightProcessor = async () => {
await insightProcessorConsumer.startConsuming<{ insight: InsightModel; force: boolean }>('query-service', async (message, headers) => { await insightProcessorConsumer.startConsuming<{ insight: InsightModel; force: boolean }>('query-service', async (message, headers) => {
let insight = message.insight; let insight = message.insight;
if (insight == null || insight.template == null || insight.userId == null || insight.saveStateId == null) {
log.error('Invalid Insight received in insightProcessorConsumer:', insight);
return;
}
if (insight.alarmMode === 'disabled' && !message.force) {
log.debug('Alarm mode is disabled', insight.id);
return;
}
if (insight.recipients == null || insight.recipients.length === 0) { if (!validateInsight(insight, message.force)) {
log.debug('No recipients found in the insight, skipping');
return; return;
} }
...@@ -81,8 +72,7 @@ export const insightProcessor = async () => { ...@@ -81,8 +72,7 @@ export const insightProcessor = async () => {
editor.setEditorState(state); editor.setEditorState(state);
}); });
if (insight.userId == null) return; const ss = await ums.getUserSaveState(insight.userId as number, insight.saveStateId);
const ss = await ums.getUserSaveState(insight.userId, insight.saveStateId);
const queries = ss.queryStates.openQueryArray; const queries = ss.queryStates.openQueryArray;
const visualizations = ss.visualizations.openVisualizationArray; const visualizations = ss.visualizations.openVisualizationArray;
...@@ -106,8 +96,7 @@ export const insightProcessor = async () => { ...@@ -106,8 +96,7 @@ export const insightProcessor = async () => {
insight = statCheck(insight, result); insight = statCheck(insight, result);
} }
if (insight.userId == null) return; // fixes ts but never is the case await ums.updateInsight(insight.userId as number, insight.id, insight);
await ums.updateInsight(insight.userId, insight.id, insight);
if (insight.status || message.force) { if (insight.status || message.force) {
if (insight.status) log.debug('Insight passed the check'); if (insight.status) log.debug('Insight passed the check');
......
import { describe, it, expect, vi } from 'vitest';
import { validateInsight } from '../../utils/insights/validateInsight';
import type { InsightModel, InsightRequest } from 'ts-common';
import { log } from '../../logger';
import { Logger } from 'ts-common';
Logger.excludedOwners.push('ts-common');
Logger.excludedOwners.push('query-service');
describe('validateInsight', () => {
const baseInsight: InsightModel = {
id: 1,
name: 'Test Insight',
description: 'A test insight',
recipients: ['user@example.com'],
frequency: 'daily',
saveStateId: '1c9351d5-0dc5-4c08-979f-829f9f83bb29',
userId: 42,
status: true,
type: 'report',
template: 'this is an email',
alarmMode: 'conditional',
createdAt: '2023-12-12',
updatedAt: '2023-12-12',
conditionsCheck: [{ nodeLabel: 'Movie', statistic: 'Count', operator: '>', value: 50 }],
};
it('should return true for a valid insight', () => {
expect(validateInsight(baseInsight, true)).toBe(true);
});
it('should return false if insight is null', () => {
expect(validateInsight(null as unknown as InsightModel, true)).toBe(false);
});
it('should return false if required fields are missing', () => {
const invalidInsight = { ...baseInsight, template: null };
expect(validateInsight(invalidInsight as unknown as InsightModel, true)).toBe(false);
});
it('should return false if alarm mode is disabled and force is false', () => {
const disabledInsight: InsightModel = {
...baseInsight,
alarmMode: 'disabled',
};
expect(validateInsight(disabledInsight, false)).toBe(false);
});
it('should return true if alarm mode is disabled but force is true', () => {
const disabledInsight: InsightModel = { ...baseInsight, alarmMode: 'disabled' };
expect(validateInsight(disabledInsight, true)).toBe(true);
});
it('should return false if no recipients are provided', () => {
const noRecipientsInsight = { ...baseInsight, recipients: [] };
expect(validateInsight(noRecipientsInsight, true)).toBe(false);
});
it('should return false if userId is null', () => {
const noUserInsight: InsightModel = { ...baseInsight, userId: null as unknown as number };
expect(validateInsight(noUserInsight, true)).toBe(false);
});
});
import { type InsightModel } from 'ts-common';
import { log } from '../../logger';
export function validateInsight(insight: InsightModel, force: boolean) {
if (!insight || insight.userId == null || !insight.template || !insight.userId || !insight.saveStateId) {
log.error('Invalid Insight received in insightProcessorConsumer:', insight);
return false;
}
if (insight.alarmMode === 'disabled' && !force) {
log.debug('Alarm mode is disabled', insight.id);
return false;
}
if (!insight.recipients || insight.recipients.length === 0) {
log.debug('No recipients found in the insight, skipping');
return false;
}
return true;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment