/
test-crontime.js
235 lines (193 loc) · 6.8 KB
/
test-crontime.js
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
var chai = require('chai'),
expect = chai.expect,
cron = require('../lib/cron');
describe('crontime', function () {
it('should test stars (* * * * * *)', function () {
expect(function () {
new cron.CronTime('* * * * * *');
}).to.not.throw(Error);
});
it('should test digit (0 * * * * *)', function () {
expect(function () {
new cron.CronTime('0 * * * * *');
}).to.not.throw(Error);
});
it('should test multi digits (08 * * * * *)', function () {
expect(function () {
new cron.CronTime('08 * * * * *');
}).to.not.throw(Error);
});
it('should test all digits (08 8 8 8 8 5)', function () {
expect(function () {
new cron.CronTime('08 * * * * *');
}).to.not.throw(Error);
});
it('should test too many digits (08 8 8 8 8 5)', function () {
expect(function () {
new cron.CronTime('08 * * * * *');
}).to.not.throw(Error);
});
it('should test standard cron format (* * * * *)', function () {
expect(function () {
new cron.CronTime('* * * * *');
}).to.not.throw(Error);
});
it('should test standard cron format (8 8 8 8 5)', function () {
var standard = new cron.CronTime('8 8 8 8 5');
var extended = new cron.CronTime('0 8 8 8 8 5');
expect(standard.dayOfWeek).to.deep.eql(extended.dayOfWeek);
expect(standard.month).to.deep.eql(extended.month);
expect(standard.dayOfMonth).to.deep.eql(extended.dayOfMonth);
expect(standard.hour).to.deep.eql(extended.hour);
expect(standard.minute).to.deep.eql(extended.minute);
expect(standard.second).to.deep.eql(extended.second);
});
it('should test hyphen (0-10 * * * * *)', function () {
expect(function () {
new cron.CronTime('0-10 * * * * *');
}).to.not.throw(Error);
});
it('should test multi hyphens (0-10 0-10 * * * *)', function () {
expect(function () {
new cron.CronTime('0-10 0-10 * * * *');
}).to.not.throw(Error);
});
it('should test all hyphens (0-10 0-10 0-10 0-10 0-10 0-1)', function () {
expect(function () {
new cron.CronTime('0-10 0-10 0-10 0-10 0-10 0-1');
}).to.not.throw(Error);
});
it('should test comma (0,10 * * * * *)', function () {
expect(function () {
new cron.CronTime('0,10 * * * * *');
}).to.not.throw(Error);
});
it('should test multi commas (0,10 0,10 * * * *)', function () {
expect(function () {
new cron.CronTime('0,10 0,10 * * * *');
}).to.not.throw(Error);
});
it('should test all commas (0,10 0,10 0,10 0,10 0,10 0,1)', function () {
expect(function () {
new cron.CronTime('0,10 0,10 0,10 0,10 0,10 0,1');
}).to.not.throw(Error);
});
it('should test alias (* * * * jan *)', function () {
expect(function () {
new cron.CronTime('* * * * jan *');
}).to.not.throw(Error);
});
it('should test multi aliases (* * * * jan,feb *)', function () {
expect(function () {
new cron.CronTime('* * * * jan,feb *');
}).to.not.throw(Error);
});
it('should test all aliases (* * * * jan,feb mon,tue)', function () {
expect(function () {
new cron.CronTime('* * * * jan,feb mon,tue');
}).to.not.throw(Error);
});
it('should test every second monday (* * * * * mon/2)');
it('should test unknown alias (* * * * jar *)', function () {
expect(function () {
new cron.CronTime('* * * * jar *');
}).to.throw(Error);
});
it('should test unknown alias - short (* * * * j *)', function () {
expect(function () {
new cron.CronTime('* * * * j *');
}).to.throw(Error);
});
it('should test Date', function () {
var d = new Date();
var ct = new cron.CronTime(d);
expect(ct.source.isSame(d.getTime())).to.be.true
});
it('should test day roll-over', function () {
var numHours = 24;
var ct = new cron.CronTime('0 0 17 * * *');
for (var hr = 0; hr < numHours; hr++) {
var start = new Date(2012, 3, 16, hr, 30, 30);
var next = ct._getNextDateFrom(start);
expect(next - start).to.be.lt(24 * 60 * 60 * 1000);
expect(next._d).to.be.gt(start);
}
});
it('should test illegal repetition syntax', function () {
expect(function () {
new cron.CronTime('* * /4 * * *');
}).to.throw(Error);
});
it('should test next date', function () {
var ct = new cron.CronTime('0 0 */4 * * *');
var nextDate = new Date();
nextDate.setHours(23);
var nextdt = ct._getNextDateFrom(nextDate);
expect(nextdt._d).to.be.gt(nextDate);
expect(nextdt.hours() % 4).to.eql(0);
});
it('should test next date from invalid date', function () {
var ct = new cron.CronTime('0 0 * * * *');
var nextDate = new Date('My invalid date string');
var nextdt = ct._getNextDateFrom(nextDate);
expect(nextdt.toString()).to.eql('Invalid date');
});
it('should test next real date', function () {
var ct = new cron.CronTime(new Date());
var nextDate = new Date();
nextDate.setMonth(nextDate.getMonth() + 1);
expect(nextDate).to.be.gt(ct.source._d);
var nextdt = ct._getNextDateFrom(nextDate);
expect(nextdt.isSame(nextDate)).to.be.true;
});
it('should test next month selection');
describe('should throw an exception because `L` not supported', function () {
it('(* * * L * *)', function () {
expect(function () {
new cron.CronTime('* * * L * *');
}).to.throw(Error);
});
it('(* * * * * L)', function () {
expect(function () {
new cron.CronTime('* * * * * L');
}).to.throw(Error);
});
});
it('should strip off millisecond', function () {
var cronTime = new cron.CronTime('0 */10 * * * *');
var x = cronTime._getNextDateFrom(new Date("2018-08-10T02:20:00.999Z"));
expect(x.toISOString()).to.equal('2018-08-10T02:30:00.000Z');
});
it('should strip off millisecond (2)', function () {
var cronTime = new cron.CronTime('0 */10 * * * *');
var x = cronTime._getNextDateFrom(new Date("2018-08-10T02:19:59.999Z"));
expect(x.toISOString()).to.equal('2018-08-10T02:20:00.000Z');
});
it('should generete the right next days when cron is set to every minute', function () {
var cronTime = new cron.CronTime('* * * * *');
var min=60000;
var previousDate=new Date(Date.UTC(2018,5,3,0,0));
for(var i=0;i<25;i++){
var nextDate = cronTime._getNextDateFrom(previousDate);
expect(nextDate.valueOf()).to.equal(previousDate.valueOf()+min)
previousDate=nextDate;
}
});
it('should generete the right next days when cron is set to every 15 min', function () {
var cronTime = new cron.CronTime('*/15 * * * *');
var min=60000*15;
var previousDate=new Date(Date.UTC(2016,6,3,0,0));
for(var i=0;i<25;i++){
var nextDate = cronTime._getNextDateFrom(previousDate);
expect(nextDate.valueOf()).to.equal(previousDate.valueOf()+min)
previousDate=nextDate;
}
});
it('should generete the right next day when cron is set to every 15 min in Feb', function () {
var cronTime = new cron.CronTime('*/15 * * FEB *');
var min=60000*15;
var previousDate=new Date(Date.UTC(2018,3,0,0,0));
var nextDate = cronTime._getNextDateFrom(previousDate,"UTC");
expect(nextDate.valueOf()).to.equal(new Date(Date.UTC(2019,1,1,0,0)).valueOf());
});
});