From 2b50b712bb878831f4cfee7b667ce1024fc40b4b Mon Sep 17 00:00:00 2001 From: manushak Date: Fri, 29 Mar 2024 15:38:27 +0400 Subject: [PATCH 1/2] test(builtins): add missing test coverage of time-sync plugin --- src/__tests__/unit/builtins/time-sync.test.ts | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/src/__tests__/unit/builtins/time-sync.test.ts b/src/__tests__/unit/builtins/time-sync.test.ts index 4b0507efa..d4b21a13d 100644 --- a/src/__tests__/unit/builtins/time-sync.test.ts +++ b/src/__tests__/unit/builtins/time-sync.test.ts @@ -7,6 +7,35 @@ const {InputValidationError} = ERRORS; const {INVALID_OBSERVATION_OVERLAP, INVALID_TIME_NORMALIZATION} = STRINGS; +jest.mock('luxon', () => { + const originalModule = jest.requireActual('luxon'); + return { + ...originalModule, + Interval: { + ...originalModule.Interval, + fromDateTimes: jest.fn((start, end) => ({ + start, + end, + splitBy: jest.fn(_options => { + const intervals = []; + let current = start; + + while (current < end) { + intervals.push({ + start: process.env.MOCK_INTERVAL === 'true' ? null : current, + end: current.plus({seconds: 1}), + }); + + current = current.plus({seconds: 1}); + } + + return intervals; + }), + })), + }, + }; +}); + describe('builtins/time-sync:', () => { describe('time-sync: ', () => { const basicConfig = { @@ -234,6 +263,71 @@ describe('execute(): ', () => { } }); + it('throws error if `timestamp` is missing.', async () => { + const basicConfig = { + 'start-time': '2023-12-12T00:00:00.000Z', + 'end-time': '2023-12-12T00:01:00.000Z', + interval: 5, + 'allow-padding': true, + }; + + const timeModel = TimeSync(basicConfig); + + try { + await timeModel.execute([ + { + duration: 15, + 'cpu/utilization': 10, + }, + { + timestamp: '2023-12-12T00:00:10.000Z', + duration: 30, + 'cpu/utilization': 20, + }, + ]); + } catch (error) { + expect(error).toBeInstanceOf(InputValidationError); + expect(error).toStrictEqual( + new InputValidationError( + '"timestamp" parameter is invalid input. Error code: invalid_union.' + ) + ); + } + }); + + it('throws error if the seconds `timestamp` is above 60.', async () => { + const basicConfig = { + 'start-time': '2023-12-12T00:00:00.000Z', + 'end-time': '2023-12-12T00:01:00.000Z', + interval: 5, + 'allow-padding': true, + }; + + const timeModel = TimeSync(basicConfig); + + try { + await timeModel.execute([ + { + timestamp: '2023-12-12T00:00:90.000Z', + duration: 15, + 'cpu/utilization': 10, + }, + { + timestamp: '2023-12-12T00:00:10.000Z', + duration: 30, + 'cpu/utilization': 20, + }, + ]); + } catch (error) { + expect(error).toBeInstanceOf(InputValidationError); + expect(error).toStrictEqual( + new InputValidationError( + '"timestamp" parameter is invalid input. Error code: invalid_union.' + ) + ); + } + }); + it('throws an error if the `timestamp` is not valid date.', async () => { const basicConfig = { 'start-time': '2023-12-12T00:00:00.000Z', @@ -489,7 +583,37 @@ describe('execute(): ', () => { expect(result).toStrictEqual(expectedResult); }); + it('throws an error when `start-time` is wrong.', async () => { + process.env.MOCK_INTERVAL = 'true'; + const basicConfig = { + 'start-time': '2023-12-12T00:00:90.000Z', + 'end-time': '2023-12-12T00:01:09.000Z', + interval: 5, + 'allow-padding': true, + }; + + const timeModel = TimeSync(basicConfig); + + try { + await timeModel.execute([ + { + timestamp: '2023-12-12T00:00:00.000Z', + duration: 30, + 'cpu/utilization': 20, + }, + ]); + } catch (error) { + expect(error).toBeInstanceOf(InputValidationError); + expect(error).toStrictEqual( + new InputValidationError( + '"timestamp" parameter is invalid datetime in input[1]. Error code: invalid_string.' + ) + ); + } + }); + it('returns a result when the first timestamp in the input has time padding.', async () => { + process.env.MOCK_INTERVAL = 'false'; const basicConfig = { 'start-time': '2023-12-12T00:00:00.000Z', 'end-time': '2023-12-12T00:00:09.000Z', @@ -619,6 +743,7 @@ describe('execute(): ', () => { }); it('checks that timestamps in return object are ISO 8061 and timezone UTC.', async () => { + process.env.MOCK_INTERVAL = 'false'; const basicConfig = { 'start-time': '2023-12-12T00:00:00.000Z', 'end-time': '2023-12-12T00:00:03.000Z', From 21749bee6d9a7b8d162e51d6a611acdf1592b768 Mon Sep 17 00:00:00 2001 From: manushak Date: Fri, 29 Mar 2024 15:50:22 +0400 Subject: [PATCH 2/2] test(builtins): remove mock function's unused argument --- src/__tests__/unit/builtins/time-sync.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/unit/builtins/time-sync.test.ts b/src/__tests__/unit/builtins/time-sync.test.ts index d4b21a13d..2a6c904e4 100644 --- a/src/__tests__/unit/builtins/time-sync.test.ts +++ b/src/__tests__/unit/builtins/time-sync.test.ts @@ -16,7 +16,7 @@ jest.mock('luxon', () => { fromDateTimes: jest.fn((start, end) => ({ start, end, - splitBy: jest.fn(_options => { + splitBy: jest.fn(() => { const intervals = []; let current = start;