-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhistory.test.ts
More file actions
327 lines (294 loc) · 10.9 KB
/
history.test.ts
File metadata and controls
327 lines (294 loc) · 10.9 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import type { ArticlesViewedSettings } from '../../shared/types';
import {
getArticleViewCountByMultipleTagForWeeks,
getArticleViewCountForWeeks,
getArticleViewCounts,
getWeeksInWindow,
historyWithinArticlesViewedSettings,
} from './history';
describe('getArticleViewCountForWeeks', () => {
// Pass the current date into the tested function so the checks can be made
// against a fixed date.
const rightNow = new Date('2020-03-16T09:30:00');
it('should count views for one week properly', () => {
const history = [{ week: 18330, count: 45 }];
const numWeeks = 1;
const got = getArticleViewCountForWeeks(history, numWeeks, rightNow);
expect(got).toBe(45);
});
it('should count views for several weeks properly', () => {
const history = [
{ week: 18330, count: 15 },
{ week: 18323, count: 5 },
{ week: 18316, count: 5 },
];
const numWeeks = 3;
const got = getArticleViewCountForWeeks(history, numWeeks, rightNow);
expect(got).toBe(25);
});
it('should not count views for all weeks in the history object', () => {
const history = [
{ week: 18330, count: 15 },
{ week: 18323, count: 5 },
{ week: 18316, count: 5 },
{ week: 18309, count: 5 }, // not be be included as we only want 3 weeks
];
const numWeeks = 3;
const got = getArticleViewCountForWeeks(history, numWeeks, rightNow);
expect(got).toBe(25);
});
});
describe('getWeeksInWindow', () => {
// day 19835 = 2024-04-22
// calculate -> Date.UTC(2024, 3, 22) / 86400000;
it('includes the current week', () => {
const articleHistory = [
{ week: 19835, count: 2 },
{ week: 19828, count: 25 },
{ week: 19821, count: 20 },
{ week: 19814, count: 20 },
{ week: 19807, count: 20 },
{ week: 19800, count: 20 },
];
const result = getWeeksInWindow(articleHistory, 4, new Date('2024-04-23'));
expect(result.length).toBe(5);
expect(result[0].week).toBe(19835);
});
it('includes correct window when end date is in the past', () => {
const articleHistory = [
{ week: 19835, count: 2 },
{ week: 19828, count: 25 },
{ week: 19821, count: 20 },
{ week: 19814, count: 20 },
{ week: 19807, count: 20 },
{ week: 19800, count: 20 },
];
const result = getWeeksInWindow(articleHistory, 4, new Date('2024-04-16'));
expect(result.length).toBe(5);
expect(result[0].week).toBe(19828);
});
});
describe(' getArticleViewCountByMultipleTagForWeeks ', () => {
const rightNow = new Date('2020-03-16T09:30:00');
it('should count views for one week properly with multiple tags', () => {
const numWeeks = 1;
const articleHistoryWithOneWeekMultipleTags = [
{
week: 18330,
count: 53,
tags: {
'environment/environment': 15,
'environment/climate-crisis': 6,
'world/world': 5,
'business/business': 3,
'us-news/us-politics': 1,
'technology/technology': 1,
'science/science': 1,
'politics/politics': 3,
'books/books': 1,
'culture/culture': 1,
},
},
];
const got = getArticleViewCountForWeeks(
articleHistoryWithOneWeekMultipleTags,
numWeeks,
rightNow,
);
const acTag = getArticleViewCountByMultipleTagForWeeks(
['science/science'],
articleHistoryWithOneWeekMultipleTags,
numWeeks,
rightNow,
);
const acMultipleTag = getArticleViewCountByMultipleTagForWeeks(
['science/science', 'environment/environment', 'business/business'],
articleHistoryWithOneWeekMultipleTags,
numWeeks,
rightNow,
);
expect(got).toBe(53);
expect(acTag).toBe(1);
expect(acMultipleTag).toBe(19);
});
});
describe(' getArticleViewCounts ', () => {
const rightNow = new Date('2020-03-16T09:30:00');
it('should count views for one week properly with multiple tags', () => {
const numWeeks = 52;
const articleHistoryWithOneWeekMultipleTags = [
{
week: 18330,
count: 53,
tags: {
'environment/environment': 15,
'environment/climate-crisis': 6,
'world/world': 5,
'business/business': 3,
'us-news/us-politics': 1,
'technology/technology': 1,
'science/science': 1,
'politics/politics': 3,
'books/books': 1,
'culture/culture': 1,
},
},
];
const tagIds = ['science/science', 'environment/environment'];
const got = getArticleViewCounts(
articleHistoryWithOneWeekMultipleTags,
numWeeks,
tagIds,
rightNow,
);
expect(got.for52Weeks).toBe(53);
expect(got.forTargetedWeeks).toBe(16);
});
it('should count views for one week properly without tags ', () => {
const numWeeks = 52;
const articleHistoryWithOneWeekMultipleTags = [
{
week: 18330,
count: 53,
tags: {
'environment/environment': 15,
'environment/climate-crisis': 6,
'world/world': 5,
'business/business': 3,
'us-news/us-politics': 1,
'technology/technology': 1,
'science/science': 1,
'politics/politics': 3,
'books/books': 1,
'culture/culture': 1,
},
},
];
const tagIds: string[] = [];
const got = getArticleViewCounts(
articleHistoryWithOneWeekMultipleTags,
numWeeks,
tagIds,
rightNow,
);
expect(got.for52Weeks).toBe(53);
expect(got.forTargetedWeeks).toBe(53);
});
});
describe(' historyWithinArticlesViewedSettings ', () => {
const rightNow = new Date('2020-03-16T09:30:00');
it('should return the check for count to be between minViews and maxViews', () => {
const articleHistoryWithOneWeekMultipleTags = [
{
week: 18330,
count: 53,
tags: {
'environment/environment': 15,
'environment/climate-crisis': 6,
'world/world': 5,
'business/business': 3,
'us-news/us-politics': 1,
'technology/technology': 1,
'science/science': 1,
'politics/politics': 3,
'books/books': 1,
'culture/culture': 1,
},
},
];
const articlesViewedSettings: ArticlesViewedSettings = {
minViews: 5,
maxViews: 100,
periodInWeeks: 52,
tagIds: ['science/science', 'environment/environment'],
};
const got = historyWithinArticlesViewedSettings(
articlesViewedSettings,
articleHistoryWithOneWeekMultipleTags,
rightNow,
);
expect(got).toBe(true);
});
it('should count views for one week properly for a number of tags ', () => {
const rightNow = new Date('2020-03-16T09:30:00');
const articleHistoryWithOneWeekMultipleTags = [
{
week: 18330,
count: 53,
tags: {
'environment/environment': 15,
'environment/climate-crisis': 6,
'world/world': 5,
'business/business': 3,
'us-news/us-politics': 1,
'technology/technology': 1,
'science/science': 1,
'politics/politics': 3,
'books/books': 1,
'culture/culture': 1,
},
},
];
const articlesViewedSettings: ArticlesViewedSettings = {
minViews: 5,
maxViews: 100,
periodInWeeks: 52,
tagIds: ['science/science', 'environment/environment'],
};
const viewCountForWeeks =
articlesViewedSettings.tagIds?.length === 0
? getArticleViewCountForWeeks(
articleHistoryWithOneWeekMultipleTags,
articlesViewedSettings.periodInWeeks,
rightNow,
)
: getArticleViewCountByMultipleTagForWeeks(
articlesViewedSettings.tagIds,
articleHistoryWithOneWeekMultipleTags,
articlesViewedSettings.periodInWeeks,
rightNow,
);
expect(viewCountForWeeks).toBe(16);
});
it('should count views for one week properly for no tags ', () => {
const rightNow = new Date('2020-03-16T09:30:00');
const articleHistoryWithOneWeekMultipleTags = [
{
week: 18330,
count: 53,
tags: {
'environment/environment': 15,
'environment/climate-crisis': 6,
'world/world': 5,
'business/business': 3,
'us-news/us-politics': 1,
'technology/technology': 1,
'science/science': 1,
'politics/politics': 3,
'books/books': 1,
'culture/culture': 1,
},
},
];
const articlesViewedSettings: ArticlesViewedSettings = {
minViews: 5,
maxViews: 100,
periodInWeeks: 52,
tagIds: [],
};
const viewCountForWeeks =
articlesViewedSettings.tagIds?.length === 0
? getArticleViewCountForWeeks(
articleHistoryWithOneWeekMultipleTags,
articlesViewedSettings.periodInWeeks,
rightNow,
)
: getArticleViewCountByMultipleTagForWeeks(
articlesViewedSettings.tagIds,
articleHistoryWithOneWeekMultipleTags,
articlesViewedSettings.periodInWeeks,
rightNow,
);
expect(viewCountForWeeks).toBe(53);
});
});