From 895063fd5652bd391534be6eb5b24acdd1b1f27b Mon Sep 17 00:00:00 2001 From: MarcosPierasNL <pieras.marcos@gmail.com> Date: Tue, 18 Mar 2025 17:56:50 +0100 Subject: [PATCH] test: adds tests on valid insight --- src/readers/insightProcessor.ts | 21 ++------ src/tests/insights/validateInsight.test.ts | 63 ++++++++++++++++++++++ src/utils/insights/validateInsight.ts | 21 ++++++++ 3 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 src/tests/insights/validateInsight.test.ts create mode 100644 src/utils/insights/validateInsight.ts diff --git a/src/readers/insightProcessor.ts b/src/readers/insightProcessor.ts index 71014ea..986a028 100644 --- a/src/readers/insightProcessor.ts +++ b/src/readers/insightProcessor.ts @@ -12,6 +12,7 @@ import { diffCheck } from './diffCheck'; import { VariableNode } from '../utils/lexical'; import { populateTemplate } from '../utils/insights'; import { RabbitMqBroker } from 'ts-common/rabbitMq'; +import { validateInsight } from '../utils/insights/validateInsight'; const dom = new JSDOM(); function setUpDom() { @@ -37,7 +38,7 @@ function setUpDom() { export const insightProcessor = async () => { if (mail == null) { log.warn('Mail is not configured. Insight processor will be disabled'); - return; + //return; } log.info('Starting insight processor'); @@ -52,18 +53,8 @@ export const insightProcessor = async () => { await insightProcessorConsumer.startConsuming<{ insight: InsightModel; force: boolean }>('query-service', async (message, headers) => { 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) { - log.debug('No recipients found in the insight, skipping'); + if (!validateInsight(insight, message.force)) { return; } @@ -81,8 +72,7 @@ export const insightProcessor = async () => { editor.setEditorState(state); }); - if (insight.userId == null) return; - const ss = await ums.getUserSaveState(insight.userId, insight.saveStateId); + const ss = await ums.getUserSaveState(insight.userId as number, insight.saveStateId); const queries = ss.queryStates.openQueryArray; const visualizations = ss.visualizations.openVisualizationArray; @@ -106,8 +96,7 @@ export const insightProcessor = async () => { insight = statCheck(insight, result); } - if (insight.userId == null) return; // fixes ts but never is the case - await ums.updateInsight(insight.userId, insight.id, insight); + await ums.updateInsight(insight.userId as number, insight.id, insight); if (insight.status || message.force) { if (insight.status) log.debug('Insight passed the check'); diff --git a/src/tests/insights/validateInsight.test.ts b/src/tests/insights/validateInsight.test.ts new file mode 100644 index 0000000..15a06c8 --- /dev/null +++ b/src/tests/insights/validateInsight.test.ts @@ -0,0 +1,63 @@ +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); + }); +}); diff --git a/src/utils/insights/validateInsight.ts b/src/utils/insights/validateInsight.ts new file mode 100644 index 0000000..6d04fa7 --- /dev/null +++ b/src/utils/insights/validateInsight.ts @@ -0,0 +1,21 @@ +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; +} -- GitLab