-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers_test.ts
242 lines (221 loc) · 9.26 KB
/
helpers_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import moment, { Moment } from 'moment-timezone';
import { expect } from 'chai';
import sinon, { SinonSandbox, SinonFakeTimers } from 'sinon';
import { computeNextRun, computeNextRuns, isHealthy, taskRunner } from '../src/helpers';
import { PublishableTask } from '../src/index'
describe('helpers', () => {
let sandbox: SinonSandbox;
let clock: SinonFakeTimers;
beforeEach(() => {
process.env.TZ = 'Australia/Sydney';
sandbox = sinon.createSandbox();
});
afterEach(() => {
process.env.TZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (clock) clock.restore();
sandbox.restore();
});
describe('computeNextRun', () => {
it('Calculates the next date correctly', () => {
const every3Hours = 'FREQ=HOURLY;INTERVAL=4;BYMINUTE=0';
const nextDate = moment(computeNextRun(every3Hours));
expect(nextDate.diff(moment().tz('utc').minute(0), 'hours')).to.equal(3);
});
it('Calculates the next date correctly with DTSTART rule', () => {
const start = moment().tz('UTC').second(0).format('YYYYMMDDTHHmmss')
const every3Hours =
`DTSTART;TZID=UTC:${start}\nRRULE:FREQ=HOURLY;BYMINUTE=0;INTERVAL=3`;
const nextRun = moment(computeNextRun(every3Hours));
const nextDate = moment().tz('Australia/Sydney').add(3, 'hours').minute(0).second(0).millisecond(0);
expect(nextRun.toISOString()).to.equal(nextDate.toISOString());
});
it('Calculates the next date correctly with DTSTART at the end', () => {
const start = moment().tz('UTC').second(0).format('YYYYMMDDTHHmmss')
const daily =
`FREQ=DAILY;INTERVAL=1;BYMINUTE=0;TZID=UTC;DTSTART=${start}`;
const nextRun = moment(computeNextRun(daily));
const nextDate = moment(start).tz('UTC').add(1, 'day').minute(0).second(0).millisecond(0);
expect(nextRun.toISOString()).to.equal(nextDate.toISOString());
});
// List of recurrence rules to test
// comparator that returns boolean
(
[
[
'FREQ=HOURLY;INTERVAL=1',
'UTC',
(m: Moment) => moment().tz('utc').diff(m, 'minutes') === -59,
],
[
'FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;BYHOUR=16;BYMINUTE=40',
'Australia/Sydney',
(m: Moment) =>
m.day() === 1 &&
m.hour() === 16 &&
m.minute() === 40 &&
['+1100', '+1000'].some(x => x === m.format('ZZ')),
],
[
'FREQ=WEEKLY;INTERVAL=1;BYDAY=WE;BYHOUR=17;BYMINUTE=40;BYSECOND=0',
'Australia/Sydney',
(m: Moment) =>
m.day() === 3 &&
m.hour() === 17 &&
m.minute() === 40 &&
m.second() === 0 &&
['+1100', '+1000'].some(x => x === m.format('ZZ')),
],
] as [string, string, (m: Moment) => boolean][]
).forEach(([rule, timezone, comparator]) => {
it(`Calculates the next date for rule ${rule} correctly`, () => {
expect(comparator(moment(computeNextRun(rule, { timezone })))).to.be
.true;
});
});
it('Can handle fortnightly rrule with a set day', () => {
// set the current date to a thursday
// At AEDT this will be 2024-01-25T03:00:00+11:00
clock = sinon.useFakeTimers(new Date('2024-01-24T16:00:00Z').getTime());
const rule =
'FREQ=WEEKLY;INTERVAL=2;BYDAY=WE;BYHOUR=12;BYMINUTE=0;BYSECOND=0';
const nextDate = moment(
computeNextRun(rule, { timezone: 'Australia/Sydney' })
);
expect([nextDate.date(), nextDate.month(), nextDate.year()]).to.eqls([
7, 1, 2024,
]);
});
it('Can handle DST switchover with a rrule', () => {
// set the current date to 6th April 2024
// At AEDT this will be 2024-04-06T03:00:00+11:00
clock = sinon.useFakeTimers(new Date('2024-04-05T16:00:00Z').getTime());
const rule =
'FREQ=WEEKLY;INTERVAL=2;BYDAY=WE;BYHOUR=12;BYMINUTE=0;BYSECOND=0';
const nextDate = computeNextRun(rule, { timezone: 'Australia/Sydney' });
expect(nextDate).to.eqls('2024-04-17T02:00:00.000Z');
const parsed = moment(nextDate);
expect([
parsed.date(),
parsed.month(),
parsed.year(),
parsed.hours(),
parsed.minutes(),
parsed.format('ZZ'),
]).to.eqls([17, 3, 2024, 12, 0, '+1000']);
});
it('Can handle fortnightly rrule with a dtstart', () => {
const rule =
'DTSTART;TZID=Australia/Sydney:20230126T030000\nRRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=WE;BYHOUR=12;BYMINUTE=0;BYSECOND=0';
// set the current date to a thursday
// At AEDT this will be 2024-01-25T03:00:00+11:00
clock = sinon.useFakeTimers(new Date('2023-01-25T16:00:00Z').getTime());
let nextDate = moment(
computeNextRun(rule, { timezone: 'Australia/Sydney' })
);
expect([nextDate.date(), nextDate.month(), nextDate.year()]).to.eqls([
8, 1, 2023,
]);
clock.restore();
// After 16/02/2023, starting from 26/01/2023, the next run should be 22/02/2023
clock = sinon.useFakeTimers(new Date('2023-02-16T16:00:00Z').getTime());
nextDate = moment(computeNextRun(rule, { timezone: 'Australia/Sydney' }));
expect([nextDate.date(), nextDate.month(), nextDate.year()]).to.eqls([
22, 1, 2023,
]);
});
it('Can handle DST switchover with a rrule with DTSTART', () => {
// set the current date to 6th April 2024
// At AEDT this will be 2024-04-06T03:00:00+11:00
clock = sinon.useFakeTimers(new Date('2024-04-05T16:00:00Z').getTime());
const rule =
'DTSTART;TZID=Australia/Sydney:20240406T030000\nRRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=WE;BYHOUR=12;BYMINUTE=0;BYSECOND=0';
const nextDate = computeNextRun(rule, { timezone: 'Australia/Sydney' });
expect(nextDate).to.eqls('2024-04-17T02:00:00.000Z');
const parsed = moment(nextDate);
expect([
parsed.date(),
parsed.month(),
parsed.year(),
parsed.hours(),
parsed.minutes(),
parsed.format('ZZ'),
]).to.eqls([17, 3, 2024, 12, 0, '+1000']);
});
});
describe('computeNextRuns', () => {
it('Calculates the next dates correctly', () => {
const every3Hours = 'FREQ=HOURLY;INTERVAL=4;BYMINUTE=0';
const [nextDates] = computeNextRuns(every3Hours);
const nextDate = moment(nextDates);
expect(nextDate.diff(moment().tz('utc').minute(0), 'hours')).to.equal(3);
});
it('Calculates the next dates correctly with DTSTART rule', () => {
const start = moment().tz('UTC').second(0).format('YYYYMMDDTHHmmss')
const every3Hours =
`DTSTART;TZID=UTC:${start}\nRRULE:FREQ=HOURLY;BYMINUTE=0;INTERVAL=3`;
const [nextRuns] = computeNextRuns(every3Hours);
const nextRun = moment(nextRuns);
const nextDate = moment().tz('Australia/Sydney').add(3, 'hours').minute(0).second(0).millisecond(0);
expect(nextRun.toISOString()).to.equal(nextDate.toISOString());
});
it('Calculates the next dates correctly with DTSTART at the end', () => {
const start = moment().tz('UTC').second(0).format('YYYYMMDDTHHmmss')
const daily =
`FREQ=DAILY;INTERVAL=1;BYMINUTE=0;TZID=UTC;DTSTART=${start}`;
const [nextRuns] = computeNextRuns(daily);
const nextRun = moment(nextRuns);
const nextDate = moment(start).tz('UTC').add(1, 'day').minute(0).second(0).millisecond(0);
expect(nextRun.toISOString()).to.equal(nextDate.toISOString());
});
it('Returns correct number of next runs', () => {
const every3Hours = 'FREQ=HOURLY;INTERVAL=4;BYMINUTE=0';
const nextDates = computeNextRuns(every3Hours, { count: 10 });
expect(nextDates).to.have.length(10);
});
it('Returns correct number of next runs with DTSTART rule', () => {
const every3Hours =
'DTSTART;TZID=Australia/Sydney:20240120T040000\nRRULE:FREQ=HOURLY;BYMINUTE=0;INTERVAL=4';
const nextDates = computeNextRuns(every3Hours, { count: 10 });
expect(nextDates).to.have.length(10);
});
it('Lunartick rule and DTSTART rule should match', () => {
const lunartickRecurrence = 'FREQ=DAILY;INTERVAL=1;BYMINUTE=0;BYSECOND=0;';
const start = moment().tz('UTC').millisecond(0).format('YYYYMMDDTHHmmss')
const rruleRecurrence =
`DTSTART;TZID=UTC:${start}\nRRULE:FREQ=DAILY;BYMINUTE=0;BYSECOND=0;INTERVAL=1`;
const lunartickDates = computeNextRuns(lunartickRecurrence, {
count: 10,
});
const rruleDates = computeNextRuns(rruleRecurrence, {
count: 10,
});
expect(lunartickDates).to.deep.equal(rruleDates);
});
});
describe('healthy', () => {
it('succeeds', () => {
expect(isHealthy(new Date().getTime(), 50000)).to.be.true;
});
it('fails', () => {
expect(isHealthy(moment().subtract(5, 'hours').unix(), 60 * 1000 * 5)).to
.be.false;
});
});
describe('taskRunner Helper', () => {
it('should not merge context with data before publishing message to queue', () => {
const publishStub = sandbox.stub();
const fakeTask: PublishableTask = {
publish: publishStub
};
const wrappedTask = taskRunner(fakeTask);
const payload: any = {
fake: 'payload',
}
const context: any = {
job: {}
}
wrappedTask(payload, context)
sinon.assert.calledWith(publishStub, payload, context);
});
});
});