biz-a-cli 2.3.53 → 2.3.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,6 @@
1
1
  import { checkSchedule } from "./datalib.js"
2
2
  import { getWatchers, getHistories } from "./watcherController.js";
3
3
  import { isItTime, loopTimer } from "./watcherlib.js"
4
- import { setInterval } from 'timers/promises';
5
4
  import { getCompanyObjectId, getSelectedConfig } from "../scheduler/configController.js";
6
5
  import { historyRecordToJson, watcherRecordToJson, setUtcDate } from "./converter.js";
7
6
 
@@ -46,31 +45,44 @@ export async function runHistory(histories, config, watchers, now) {
46
45
  }
47
46
  }
48
47
 
48
+ export async function runRecentSchedule(intervalTime, config) {
49
+ const result = await getWatchers(config);
50
+ const watchers = watcherRecordToJson(result);
51
+
52
+ for (const watcher of watchers) {
53
+ loopTimer(watcher.timer, intervalTime, config, true);
54
+ }
55
+
56
+ // reload == true ?
57
+ }
58
+
59
+ export async function* setConstantInterval(interval, start) { //SCY BZ 4169
60
+ let nextTime = start;
61
+ while (true) {
62
+ await new Promise(resolve => setTimeout(resolve, nextTime - Date.now()));
63
+ yield nextTime;
64
+ nextTime += interval;
65
+ }
66
+ }
49
67
 
50
68
  export const runScheduler = async (companyName) => {
51
69
  const company = await getCompanyObjectId(companyName);
52
70
  const config = await getSelectedConfig(company._id);
53
71
 
54
- Promise.all([getHistories(config), getWatchers(config)])
55
- .then(([historyResult, watcherResult]) => {
56
- const histories = historyRecordToJson(historyResult);
57
- const watchers = watcherRecordToJson(watcherResult);
58
- runHistory(histories, config, watchers, new Date());
59
- });
72
+ Promise.all([getHistories(config), getWatchers(config)]).then(([historyResult, watcherResult]) => {
73
+ const histories = historyRecordToJson(historyResult);
74
+ const historyWatchers = watcherRecordToJson(watcherResult);
75
+ runHistory(histories, config, historyWatchers, new Date());
76
+ });
77
+
60
78
 
61
79
  let intervalStartTime = '';
62
- for await (const startTime of setInterval(SCHEDULE_INTERVAL, Date.now())) {
80
+ for await (const intervalTime of setConstantInterval(SCHEDULE_INTERVAL, Date.now())) {
63
81
  if (!intervalStartTime) {
64
- console.log('Interval Time starts at :', new Date(startTime));
65
- intervalStartTime = new Date(startTime);
66
- }
67
-
68
- const result = await getWatchers(config);
69
- const watchers = watcherRecordToJson(result);
70
- for (const watcher of watchers) {
71
- loopTimer(watcher.timer, Date.now(), config, true);
82
+ console.log('Interval Time starts at :', new Date(intervalTime));
83
+ intervalStartTime = new Date(intervalTime);
72
84
  }
73
85
 
74
- // // reload == true ?
86
+ await runRecentSchedule(intervalTime, config);
75
87
  }
76
88
  }
@@ -46,6 +46,8 @@ describe('data test', () => {
46
46
  "}";
47
47
 
48
48
  test('check schedule subscription no history', async () => {
49
+ const logSpy = jest.spyOn(console, 'log').mockImplementation();
50
+
49
51
  const config = {
50
52
  _id: 'ffffffff2ae49fab9ea654e1',
51
53
  url: 'http://localhost:212',
@@ -62,9 +64,13 @@ describe('data test', () => {
62
64
 
63
65
  await scheduleSubscription(config, data, trigger, false, true);
64
66
  expect(mockInsertHistory).toBeCalledTimes(0);
67
+ expect(logSpy).toHaveBeenCalledWith('Run Schedule : New Watcher 1');
68
+
69
+ logSpy.mockRestore();
65
70
  });
66
71
 
67
72
  test('check schedule subscription with history', async () => {
73
+ const logSpy = jest.spyOn(console, 'log').mockImplementation();
68
74
  const config = {
69
75
  _id: 'ffffffff2ae49fab9ea654e1',
70
76
  url: 'http://localhost:212',
@@ -82,6 +88,9 @@ describe('data test', () => {
82
88
 
83
89
  await scheduleSubscription(config, data, trigger, true, true);
84
90
  expect(mockInsertHistory).toBeCalledTimes(1);
91
+ expect(logSpy).toHaveBeenCalledWith('Run Recent Schedule : New Watcher 1');
92
+
93
+ logSpy.mockRestore();
85
94
  });
86
95
 
87
96
  test('get input data', () => {
@@ -339,6 +348,8 @@ describe('data test', () => {
339
348
  });
340
349
 
341
350
  test('should return undefined when data has error', async () => {
351
+ const logSpy = jest.spyOn(console, 'log').mockImplementation();
352
+
342
353
  const config = {};
343
354
  const data = { error: 'error' };
344
355
 
@@ -346,6 +357,9 @@ describe('data test', () => {
346
357
  const expected = undefined;
347
358
 
348
359
  expect(actual).toStrictEqual(expected);
360
+ expect(logSpy).toHaveBeenCalledWith('error', 'error');
361
+
362
+ logSpy.mockRestore();
349
363
  });
350
364
 
351
365
  test('should return undefined when no data', async () => {
package/tests/hub.test.js CHANGED
@@ -58,6 +58,7 @@ describe('cli req test', () => {
58
58
  })
59
59
 
60
60
  test('request to cli', async () => {
61
+ const logSpy = jest.spyOn(console, 'log').mockImplementation();
61
62
  let mockedRequest = jest.spyOn(axios, 'request').mockReturnValue({ data: 'OK' });
62
63
 
63
64
  let socket;
@@ -103,6 +104,10 @@ describe('cli req test', () => {
103
104
  });
104
105
 
105
106
  socket.disconnect();
107
+
108
+ expect(logSpy).toHaveBeenCalledWith('error', 'error');
109
+
110
+ logSpy.mockRestore();
106
111
  } catch (error) {
107
112
  console.log(error);
108
113
  }
@@ -17,6 +17,8 @@ describe('Mail Controller', () => {
17
17
  let req;
18
18
 
19
19
  test('transporter.sendMailCliScript is called', async () => {
20
+ const logSpy = jest.spyOn(console, 'log').mockImplementation();
21
+
20
22
  req = {
21
23
  body: {
22
24
  companyname: 'abc',
@@ -35,5 +37,8 @@ describe('Mail Controller', () => {
35
37
  expect(await sendMailCliScript(req)).toEqual('error');
36
38
 
37
39
  expect(mockSendMail).toBeCalledTimes(2);
40
+ expect(logSpy).toHaveBeenCalledWith('Error: error');
41
+
42
+ logSpy.mockRestore();
38
43
  })
39
44
  })
@@ -1,4 +1,31 @@
1
1
  import { jest } from '@jest/globals'
2
+ const { ObjectId } = await import('mongodb');
3
+
4
+ const CONFIGS = [{
5
+ companyObjectId: new ObjectId('62948492f7d559fba6f32196')
6
+ }]
7
+
8
+ const RAW_WATCHERS = [{
9
+ watcher_id: 1,
10
+ company_id: '62948492f7d559fba6f32196',
11
+ timer_id: 1,
12
+ timer_watcher_id: 1,
13
+ name: 'New Watcher 1',
14
+ active: true,
15
+ timezone: 'Asia/Jakarta',
16
+ templateName: 'cekUser.js',
17
+ seq: 0,
18
+ daily_days: '0,1,2,3,4,5,6',
19
+ weekly_ordinal: '',
20
+ weekly_days: '',
21
+ monthly_days: '',
22
+ minutely_everymin: 1,
23
+ minutely_time_from: '08:00',
24
+ minutely_time_to: '22:00',
25
+ hourly_hours: '',
26
+ scriptid: 1,
27
+ cli_script: 'abc'
28
+ }];
2
29
 
3
30
  const mockCheckSchedule = jest.fn();
4
31
  jest.unstable_mockModule("../scheduler/datalib.js", () => ({
@@ -9,13 +36,29 @@ jest.unstable_mockModule("../scheduler/datalib.js", () => ({
9
36
  delay: jest.fn().mockResolvedValue('OK')
10
37
  }))
11
38
 
39
+ jest.unstable_mockModule("../scheduler/watcherController.js", () => ({
40
+ getWatchers: jest.fn(async () => {
41
+ await new Promise((resolve) => setTimeout(resolve, 10));
42
+ return RAW_WATCHERS;
43
+ }),
44
+ getHistories: jest.fn().mockResolvedValue('OK')
45
+ }))
46
+
47
+ const mockLoopTimer = jest.fn();
48
+ jest.unstable_mockModule("../scheduler/watcherLib.js", () => ({
49
+ loopTimer: mockLoopTimer.mockResolvedValue('OK'),
50
+ isItTime: jest.fn().mockResolvedValue('OK'),
51
+ }))
52
+
12
53
  const {
13
- SCHEDULE_INTERVAL,
54
+ // SCHEDULE_INTERVAL,
14
55
  getSelectedData,
15
56
  increaseInterval,
16
- runHistory
57
+ runHistory,
58
+ runRecentSchedule,
59
+ setConstantInterval
17
60
  } = await import('../scheduler/timer.js');
18
- const { ObjectId } = await import('mongodb');
61
+
19
62
 
20
63
  describe('timer and mitigation test', () => {
21
64
  test('increase interval', async () => {
@@ -60,9 +103,7 @@ describe('timer and mitigation test', () => {
60
103
  // latestRun: new Date(decreaseInterval(NOW)).toISOString()
61
104
  }
62
105
  ]
63
- const CONFIGS = [{
64
- companyObjectId: new ObjectId('62948492f7d559fba6f32196')
65
- }]
106
+
66
107
  const WATCHERS = [
67
108
  {
68
109
  _id: new ObjectId('65c34bd674ae2595b5efb5a7'),
@@ -89,4 +130,58 @@ describe('timer and mitigation test', () => {
89
130
  await runHistory(HISTORIES, CONFIGS, WATCHERS, new Date('2024-02-29T07:00:00.000Z'));
90
131
  expect(mockCheckSchedule).toBeCalledTimes(3);
91
132
  });
133
+
134
+ test('run recent schedule with delay functions', async () => {
135
+ const startTime = Date.now();
136
+
137
+ const WATCHER_CONVERTED = [{
138
+ "_id": 1,
139
+ "watcherObjectId": 1,
140
+ "name": "New Watcher 1",
141
+ "active": true,
142
+ "timezone": "Asia/Jakarta",
143
+ "templateName": "cekUser.js",
144
+ "seq": 0,
145
+ "scriptid": 1,
146
+ "script": "\"abc\"",
147
+ "daily": [0, 1, 2, 3, 4, 5, 6],
148
+ "minutely": {
149
+ "everyMin": 1,
150
+ "from": "08:00",
151
+ "to": "22:00"
152
+ }
153
+ }];
154
+
155
+ await runRecentSchedule(startTime, CONFIGS[0]);
156
+ expect(mockLoopTimer).toHaveBeenCalledWith(WATCHER_CONVERTED, startTime, CONFIGS[0], true);
157
+ });
158
+
159
+ test('constant interval', async () => {
160
+ jest.useFakeTimers();
161
+ jest.spyOn(global, 'setTimeout');
162
+
163
+ const interval = 1000;
164
+ const startTime = Date.now();
165
+ const generator = setConstantInterval(interval, startTime);
166
+
167
+ let results = [];
168
+
169
+ const fetchNextValue = async () => {
170
+ const next = generator.next();
171
+ jest.advanceTimersByTime(interval);
172
+
173
+ return (await next).value;
174
+ };
175
+
176
+ results.push(await fetchNextValue());
177
+ results.push(await fetchNextValue());
178
+ results.push(await fetchNextValue());
179
+
180
+ expect(results[0]).toBe(startTime);
181
+ expect(results[1]).toBe(startTime + interval);
182
+ expect(results[2]).toBe(startTime + 2 * interval);
183
+ expect(results[3]).toBe(undefined);
184
+
185
+ jest.useRealTimers();
186
+ });
92
187
  })
@@ -97,8 +97,10 @@ describe('isItTime', () => {
97
97
  to: '17:00'
98
98
  }
99
99
  }
100
+ expect(isItTime(dataWatcher, '2023-11-7 9:00:00')).toBe(true)
100
101
  expect(isItTime(dataWatcher, '2023-11-7 9:00:01')).toBe(true)
101
102
  expect(isItTime(dataWatcher, '2023-11-7 9:05:01')).toBe(true)
103
+ expect(isItTime(dataWatcher, '2023-11-7 17:00:00')).toBe(true)
102
104
  expect(isItTime(dataWatcher, '2023-11-7 17:00:01')).toBe(true)
103
105
  expect(isItTime(dataWatcher, '2023-11-7 16:55:59')).toBe(true)
104
106
  expect(isItTime(dataWatcher, '2023-11-7 8:59:59')).toBe(false)